cellar-js-sdk
Version:
晒啦第三方授权工具
309 lines • 11.8 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { CellarWindow } from "./iframe";
import { CellarEnv } from "./interface/cellar";
import pJson from "./../package.json";
export class Cellar {
constructor({ appId, env, walletList = false }) {
this.isLogin = false;
this.appId = appId;
this.walletList = walletList;
if (env) {
if (env === CellarEnv.DEV) {
env = CellarEnv.PRE;
}
}
else {
env = CellarEnv.PRO;
}
this.env = env;
let cellarOrigin = pJson.origins[this.env];
this.origin = cellarOrigin.origin;
this.url = this.origin + cellarOrigin.path + '?v=' + new Date().getTime() + '/#/?env=' + this.getEnv();
this.cellarWindow = new CellarWindow(this.appId, this.getEnv(), this.origin, this.url);
this.isLogin = this.getLocalStorage('isLogin') === '1';
if (this.isLogin) {
this.userCode = this.getLocalStorage('userCode');
this.userToken = this.getLocalStorage('userToken');
this.userWallet = this.getLocalStorage('userWallet');
this.authorityCode = this.getLocalStorage('authorityCode');
this.phone = this.getLocalStorage('phone');
}
}
log(...data) {
if (this.getEnv() === CellarEnv.PRE || this.getEnv() === CellarEnv.DEV) {
console.log(data);
}
}
localStorageKey(key) {
return this.getAppid() + '_' + key;
}
getLocalStorage(key) {
return localStorage.getItem(this.localStorageKey(key));
}
setLocalStorage(key, val) {
localStorage.setItem(this.localStorageKey(key), val);
}
getAppid() {
return this.appId;
}
getWalletList() {
return this.walletList;
}
getUserCode() {
return this.userCode;
}
getUserToken() {
return this.userToken;
}
getUserWallet() {
return this.userWallet;
}
getAuthorityCode() {
return this.authorityCode;
}
getLoginStatus() {
return this.isLogin;
}
getPhone() {
return this.phone;
}
getCallbackUserInfo() {
return {
userCode: this.getUserCode(),
userWallet: this.getUserWallet(),
authorityCode: this.getAuthorityCode(),
phone: this.getPhone(),
userToken: this.getUserToken()
};
}
getOrigin() {
return this.origin;
}
getWindow() {
return this.cellarWindow;
}
getEnv() {
return this.env;
}
setUserInfo(userCode, userToken, userWallet, authorityCode, phone) {
this.userCode = userCode;
this.userToken = userToken;
this.userWallet = userWallet;
this.authorityCode = authorityCode;
this.phone = phone;
if (this.userCode && this.userToken && this.userWallet && this.authorityCode && this.phone) {
this.isLogin = true;
this.setLocalStorage('userCode', this.userCode);
this.setLocalStorage('userToken', this.userToken);
this.setLocalStorage('userWallet', this.userWallet);
this.setLocalStorage('authorityCode', this.authorityCode);
this.setLocalStorage('phone', this.phone);
}
else {
this.isLogin = false;
}
this.setLocalStorage('isLogin', this.isLogin ? '1' : '');
}
clearUserInfo() {
this.isLogin = false;
this.userCode = '';
this.userToken = '';
this.userWallet = '';
this.authorityCode = '';
this.phone = '';
this.setLocalStorage('isLogin', '');
this.setLocalStorage('userCode', '');
this.setLocalStorage('userToken', '');
this.setLocalStorage('userWallet', '');
this.setLocalStorage('authorityCode', '');
this.setLocalStorage('phone', '');
}
//获取 conflux 账户授权
cfx_accounts() {
return __awaiter(this, void 0, void 0, function* () {
return yield new Promise((resolve, reject) => {
if (this.getLoginStatus()) {
resolve(this.getCallbackUserInfo());
return;
}
//iframe 只要加载完成就必须要发送的数据
let sendData = {
data: JSON.stringify({
method: 'initAppInfo',
data: {
appId: this.getAppid(),
env: this.getEnv(),
walletList: this.getWalletList(),
type: 'auth',
pathName: 'index',
},
}),
origin: this.getOrigin()
};
this.getWindow().showWindow(sendData, (iframe, info) => {
let data = info.data;
if (info.method === 'done') {
//TODO 登录授权成功
if (data) {
this.setUserInfo(data.userCode, data.userToken, data.userWallet, data.authorityCode, data.phone);
if (this.getLoginStatus()) {
resolve(this.getCallbackUserInfo());
}
else {
reject('登录失败');
}
}
else {
reject('登录失败');
}
}
else if (info.method === 'appInit') {
//TODO app页面已经初始化,显示页面 并发送 appId
iframe.postMessage(sendData.data, sendData.origin);
}
else if (info.method === 'error') {
if (data) {
reject(data.msg);
}
}
});
});
});
}
//发起 conflux 合约调用
cfx_sendTransaction(params) {
return __awaiter(this, void 0, void 0, function* () {
return yield new Promise((resolve, reject) => {
if (!this.getLoginStatus()) {
reject('无登录信息,请登录');
return;
}
let param;
if (params instanceof Array && params.length > 0) {
param = params[0];
}
else {
param = params;
}
if (!param) {
reject('参数错误');
return;
}
if (!param.data) {
reject('参数错误');
return;
}
if (!param.from) {
param.from = this.getUserWallet();
}
//iframe 只要加载完成就必须要发送的数据
let sendData = {
data: JSON.stringify({
method: 'initAppInfo',
data: {
appId: this.getAppid(),
userCode: this.getUserCode(),
userToken: this.getUserToken(),
authorityCode: this.getAuthorityCode(),
phone: this.getPhone(),
env: this.getEnv(),
walletList: this.getWalletList(),
type: 'call',
pathName: 'operAuth',
transactionParams: {
data: param.data,
from: param.from,
to: param.to
},
},
}),
origin: this.getOrigin()
};
this.getWindow().showWindow(sendData, (iframe, info) => {
let data = info.data;
if (info.method === 'done') {
//TODO 执行成功
resolve(data === null || data === void 0 ? void 0 : data.hash);
}
else if (info.method === 'appInit') {
//TODO app页面已经初始化,向其发送 appId userCode userToken
iframe.postMessage(sendData.data, sendData.origin);
}
else if (info.method === 'error') {
if (data) {
reject(data.msg);
}
}
});
});
});
}
//判断 cellar 用户是否登录
cellar_loginState(isLogin) {
return __awaiter(this, void 0, void 0, function* () {
return yield new Promise((resolve, reject) => {
if (isLogin) {
resolve(this.getCallbackUserInfo());
}
else {
reject('登录信息不存在,请重新授权登录');
}
});
});
}
//退出 cellar 用户登录
cellar_loginOut() {
return __awaiter(this, void 0, void 0, function* () {
return yield new Promise((resolve) => {
this.clearUserInfo();
resolve(undefined);
});
});
}
//获取 cellar js-sdk 版本
cellar_version() {
return __awaiter(this, void 0, void 0, function* () {
return yield new Promise((resolve) => {
resolve(pJson.version + ('.' + this.getEnv().toUpperCase()));
});
});
}
/**
* 请求晒啦sdk
* @param method 方法
* cellar_loginState 检查当前用户登录状态, 如果已登录会直接返回用户信息
* cfx_accounts 登录授权
* cellar_loginOut 退出当前登录用户
* cfx_sendTransaction 已登录用户, 调用线上合约
* cellar_version 获取当前晒啦js sdk 版本号
* @param params
*/
request({ method, params }) {
if (!this.getAppid()) {
throw new Error('初始化失败, 请检查参数是否正确');
}
switch (method) {
case 'cellar_loginState':
return this.cellar_loginState(this.getLoginStatus());
case 'cfx_accounts':
return this.cfx_accounts();
case 'cfx_sendTransaction':
return this.cfx_sendTransaction(params);
case 'cellar_version':
return this.cellar_version();
case 'cellar_loginOut':
return this.cellar_loginOut();
default:
throw new Error('无效的函数');
}
}
}
//# sourceMappingURL=cellar.js.map