fastman3-dfyjapp-jsbridge
Version:
a jsbridge module for dfyj app
466 lines • 13.9 kB
JavaScript
var __extends = this && this.__extends || function () {
var _extendStatics = function extendStatics(d, b) {
_extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function (d, b) {
d.__proto__ = b;
} || function (d, b) {
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
};
return _extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
_extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
}();
/*
* @Author: shenzhiwei
* @Date: 2021-04-11 09:32:25
* @Company: orientsec.com.cn
* @Description: 用户基础信息核心类(app),存储了各链路过程中产生的用户数据
*/
import { getStorageSync, removeStorageSync, setStorageSync } from "fastman3-dfyjapp-syncstorage"; // 必须这么导入,不要使用全局 Taro 导入
import Authorize from "./authorize";
import WeixinAuthorize from "./weixinAuthorize";
import { aes, utf8 } from "fastman3-common-crypto";
import { isEmpty } from "../util.web";
import { IF115000 } from "../IF115000";
import { isFromApp, extend } from "fastman3-common-helper";
/**
* V2版本中请不要使用该对象,由于oauth被修改为按需调用,因此在没有调用oauth的情况下该对象并没有数据
*/
var AppAuthorize = /** @class */function (_super) {
__extends(AppAuthorize, _super);
function AppAuthorize() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* 单例模式
* @returns {AppAuthorize}
*/
AppAuthorize.getInstance = function () {
if (!AppAuthorize.instance) {
AppAuthorize.instance = new AppAuthorize();
if (!Authorize.isEmptyObject(this.userDefault())) {
/** 接档操作,用于前一次的本地存储数据恢复,只针对非APP端做次操作 **/
if (!isFromApp()) {
try {
// AES解密
var _userDefaultStringify = aes.decrypt(this.userDefault(), AppAuthorize.AESKey).toString(utf8);
// 对象反序列
var _lastUserDefault = JSON.parse(_userDefaultStringify);
// 浅复制
extend(AppAuthorize.instance, _lastUserDefault);
} catch (e) {
// 容错处理,万一出现store中数据反序列化错误则直接要求重复登录(可能被篡改)
this.removeUserDefault();
// location.href = createHref('../account-login2/index.html', location.href)
}
}
}
}
return AppAuthorize.instance;
};
Object.defineProperty(AppAuthorize.prototype, "vtDeviceId", {
get: function get() {
return this._vtDeviceId;
},
// APP虚拟设备号(非APP端该字段为空) //
set: function set(vtDeviceId) {
this._vtDeviceId = vtDeviceId;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "loginUser", {
get: function get() {
return this._loginUser;
},
// 登录用户名 //
set: function set(loginUser) {
this._loginUser = loginUser;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "clientType", {
get: function get() {
return this._clientType;
},
// 客户类型 0-个人 1-机构 //
set: function set(clientType) {
this._clientType = clientType;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "fundAccount", {
get: function get() {
return this._fundAccount;
},
// 当前所切换的资金账户(只有资金账户登录时适用该字段) //
set: function set(fundAccount) {
this._fundAccount = fundAccount;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "regAccount", {
get: function get() {
return this._regAccount;
},
// 当前注册账号 //
set: function set(regAccount) {
this._regAccount = regAccount;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "cifAccount", {
get: function get() {
return this._cifAccount;
},
// 财富账号 //
set: function set(cifAccount) {
this._cifAccount = cifAccount;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "clientId", {
get: function get() {
return this._clientId;
},
// 客户号 //
set: function set(clientId) {
this._clientId = clientId;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "isPersonalUser", {
/**
* 是否个人用户
* 返回类型: boolean
*/
get: function get() {
return this.clientType == "0";
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "isOrganUser", {
/**
* 是否机构用户
* 返回类型: boolean
*/
get: function get() {
return this.clientType == "1";
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "defaultAccount", {
// 根据最新的timestamp返回最近的账户作为默认账户
get: function get() {
try {
this._defaultAccount = this._defaultAccount || [];
if (this._defaultAccount.length <= 0) {
// @ts-ignore
return null;
} else {
this._defaultAccount = this._defaultAccount.sort(function (a, b) {
return b.value.t - a.value.t;
});
// @ts-ignore
return this._defaultAccount[0];
}
} catch (e) {
// @ts-ignore
return null;
}
},
// 默认账户(由二级登录功能派生出来的定义),数据结构设计如下:
// [
// {
// key: fundAccount, // 在原来的设计上简化了该字段的加密算法,去掉了md5算法
// value: {
// t: timestamp,
// },
// }
// ]
set: function set(da) {
this._defaultAccount = this._defaultAccount || [];
var defaultAccountObj = this._defaultAccount.find(function (n) {
return n.key == da;
});
if (defaultAccountObj) {
// 获取该引用类型进行对应属性的赋值
defaultAccountObj.value.t = new Date().valueOf();
} else {
var __defaultAccount = {
key: da,
value: {
t: new Date().valueOf()
}
};
this._defaultAccount.push(__defaultAccount);
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "bizSysId", {
get: function get() {
if (!isFromApp()) {
if (this.defaultAccount == null) {
return Authorize.BIZ_SYS_UNKNOWED;
}
// @ts-ignore
if (this.defaultAccount.key.length == 8) {
// @ts-ignore
if (this.defaultAccount.key.indexOf("988") == 0) {
return Authorize.BIZ_SYS_CREDIT;
} else {
return Authorize.BIZ_SYS_TRADE;
}
}
// @ts-ignore
if (this.defaultAccount.key.length == 10) {
return Authorize.BIZ_SYS_OPTIONS;
}
return Authorize.BIZ_SYS_UNKNOWED;
}
return this._bizSysId;
},
// 系统编号(只有资金账户登录时使用该字段) //
set: function set(bizSysId) {
// @ts-ignore
this._bizSysId = bizSysId;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "isTradeAccount", {
/**
* 是否交易资金账号
* 返回类型: boolean
*/
get: function get() {
return this.bizSysId == Authorize.BIZ_SYS_TRADE;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "isCreditAccount", {
/**
* 是否信用资金账号
* 返回类型: boolean
*/
get: function get() {
return this.bizSysId == Authorize.BIZ_SYS_CREDIT;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "isOptionsAccount", {
/**
* 是否期权资金账号
* 返回类型: boolean
*/
get: function get() {
return this.bizSysId == Authorize.BIZ_SYS_OPTIONS;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "isOldAccount", {
/**
* 是否养老金账号
* 返回类型: boolean
*/
get: function get() {
return this.bizSysId == Authorize.OLD_SYS_OPTIONS;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "isShuBiAccount", {
/**
* 是否数币账号
* 返回类型: boolean
*/
get: function get() {
return this.bizSysId == Authorize.SHUBI_SYS_OPTIONS;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "clientName", {
get: function get() {
return this._clientName;
},
// 客户名称,可能为空 //
set: function set(clientName) {
this._clientName = clientName;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "storeFlag", {
get: function get() {
return this._storeFlag;
},
// 是否恢复标识 //
set: function set(storeFlag) {
this._storeFlag = storeFlag;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "authSign", {
get: function get() {
return this._authSign;
},
// refreshToken 方案前的 auth header,4.12.0开始由 newAuthSign 替代 //
set: function set(authSign) {
this._authSign = authSign;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "refreshToken", {
get: function get() {
return isEmpty(this._refreshToken) ? "" : this._refreshToken;
},
set: function set(refreshToken) {
this._refreshToken = refreshToken;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "accessToken", {
get: function get() {
return isEmpty(this._accessToken) ? "" : this._accessToken;
},
set: function set(accessToken) {
this._accessToken = accessToken;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "logon", {
get: function get() {
return this._logon;
},
/**
* 登录状态
* true-已登录;false-未登录
*/
set: function set(logon) {
this._logon = logon;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "newAuthSign", {
// 生成auth header //
// <4.12.0: 返回 this._authSign //
// >=4.12.0: 返回 {loginUser}:4:{refreshToken} //
get: function get() {
// <4.12.0
if (isEmpty(this.refreshToken)) {
return this.authSign;
}
// >=4.12.0
else {
return "".concat(this.loginUser, ":4:").concat(this.refreshToken);
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(AppAuthorize.prototype, "ticket", {
get: function get() {
return this._ticket;
},
set: function set(v) {
this._ticket = v;
},
enumerable: false,
configurable: true
});
/**
* 是否保持着一级登录的会话态
* true-已登录;false-未登录或已失效;
*/
AppAuthorize.prototype.isLongLogin = function () {
try {
if (typeof this.logon === "boolean") {
return Promise.resolve(this.logon);
} else {
// const resp = getSystemTimeService()
// if(resp.$baseApiHeader) {
// return resp.$baseApiHeader.state
// }
// else {
// return true;
// }
return new Promise(function (resolve, reject) {
IF115000().then(function (resp) {
return resolve(resp.header.state);
}).catch(function (err) {
return reject(err);
});
});
}
} catch (err) {
return Promise.resolve(true);
}
};
// 静态函数 //
/**
* 获取用户信息的本地存储
*/
AppAuthorize.userDefault = function () {
var val = getStorageSync(AppAuthorize._ArchiveKey_);
return !val ? {} : val;
};
/**
* 清除用户信息的本地存储
*/
AppAuthorize.removeUserDefault = function () {
removeStorageSync(AppAuthorize._ArchiveKey_);
};
// 公共函数 打开了app归档的操作权限,为了解决跳转OTC开通的问题 //
/**
* 归档(APP也需要执行归档操作)
*/
AppAuthorize.prototype.archive = function () {
var _aes = aes.encrypt(JSON.stringify(this), AppAuthorize.AESKey).toString();
setStorageSync(AppAuthorize._ArchiveKey_, _aes);
};
/**
* 接档
*/
AppAuthorize.prototype.unarchive = function () {
AppAuthorize.removeUserDefault();
};
/**
* 是否需要执行数据恢复(登录页不需要恢复)
*/
AppAuthorize.prototype.needStore = function () {
return !this._clientType && !this._storeFlag;
};
// userDefault用户基础信息本地化存储的Key值
AppAuthorize._ArchiveKey_ = "__DF_u__";
// AES算法所使用到的密钥
AppAuthorize.AESKey = "DF-kf6";
return AppAuthorize;
}(WeixinAuthorize);
export default AppAuthorize.getInstance();