tplus-api
Version:
tplus api invoke
601 lines (573 loc) • 24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _assign = require("babel-runtime/core-js/object/assign");
var _assign2 = _interopRequireDefault(_assign);
var _iterator = require("babel-runtime/core-js/symbol/iterator");
var _iterator2 = _interopRequireDefault(_iterator);
var _symbol = require("babel-runtime/core-js/symbol");
var _symbol2 = _interopRequireDefault(_symbol);
var _promise = require("babel-runtime/core-js/promise");
var _promise2 = _interopRequireDefault(_promise);
var _LoginApi = require("../api/LoginApi");
var _LoginApi2 = _interopRequireDefault(_LoginApi);
var _mutantsMicrofx = require("mutants-microfx");
var _app = require("../const/app");
var _Channel = require("./Channel");
var _Channel2 = _interopRequireDefault(_Channel);
var _LogoutMW = require("../middleware/LogoutMW");
var _LogoutMW2 = _interopRequireDefault(_LogoutMW);
var _tools = require("../util/tools");
var _tools2 = _interopRequireDefault(_tools);
var _mutantsJsbridge = require("mutants-jsbridge");
var _mobx = require("mobx");
var _findIndex2 = require("lodash/findIndex");
var _findIndex3 = _interopRequireDefault(_findIndex2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var __awaiter = undefined && undefined.__awaiter || function (thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = _promise2.default))(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());
});
};
var __generator = undefined && undefined.__generator || function (thisArg, body) {
var _ = { label: 0, sent: function sent() {
if (t[0] & 1) throw t[1];return t[1];
}, trys: [], ops: [] },
f,
y,
t,
g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof _symbol2.default === "function" && (g[_iterator2.default] = function () {
return this;
}), g;
function verb(n) {
return function (v) {
return step([n, v]);
};
}
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) {
try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0:case 1:
t = op;break;
case 4:
_.label++;return { value: op[1], done: false };
case 5:
_.label++;y = op[1];op = [0];continue;
case 7:
op = _.ops.pop();_.trys.pop();continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
_ = 0;continue;
}
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
_.label = op[1];break;
}
if (op[0] === 6 && _.label < t[1]) {
_.label = t[1];t = op;break;
}
if (t && _.label < t[2]) {
_.label = t[2];_.ops.push(op);break;
}
if (t[2]) _.ops.pop();
_.trys.pop();continue;
}
op = body.call(thisArg, _);
} catch (e) {
op = [6, e];y = 0;
} finally {
f = t = 0;
}
}if (op[0] & 5) throw op[1];return { value: op[0] ? op[1] : void 0, done: true };
}
};
var constant = _mutantsMicrofx.env.constant,
platform = _mutantsMicrofx.env.platform,
browser = _mutantsMicrofx.env.browser,
os = _mutantsMicrofx.env.os;
var crypto = require('crypto');
var emptyAuth = {
userId: '',
orgId: '',
accessToken: '',
tplusToken: '',
authorization: '',
sid: '',
OrganID: '',
proxyserver: _app.prodProxyHost,
channel: {
targetURL: window.localStorage.targetURL
}
};
var User = /** @class */function () {
function User(obj) {
if (obj) {
(0, _assign2.default)(this, obj);
}
}
//认证用户名 密码
User.authentication = function (username, password) {
return __awaiter(this, void 0, void 0, function () {
var auth, authResult, authObj;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
password = User.pwdEncrypt(password);
auth = new User({ username: username, password: password });
return [4 /*yield*/, _LoginApi2.default.authentication(auth)];
case 1:
authResult = _a.sent();
if (authResult) {
authObj = auth.fromJS(authResult);
return [2 /*return*/, authObj];
}
return [2 /*return*/];
}
});
});
};
//认证用户名 密码 直接通过加密的密码进行登录的
User.authentication2 = function (username, password) {
return __awaiter(this, void 0, void 0, function () {
var auth, authResult, authObj;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
auth = new User({ username: username, password: password });
return [4 /*yield*/, _LoginApi2.default.authentication(auth)];
case 1:
authResult = _a.sent();
if (authResult) {
authObj = auth.fromJS(authResult);
return [2 /*return*/, authObj];
}
return [2 /*return*/];
}
});
});
};
//修改密码
User.updatePwd = function (oldPassword, newPassword) {
return __awaiter(this, void 0, void 0, function () {
var updateResult;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
oldPassword = User.pwdEncrypt(oldPassword);
newPassword = User.pwdEncrypt(newPassword);
return [4 /*yield*/, _LoginApi2.default.updatePwd(oldPassword, newPassword)];
case 1:
updateResult = _a.sent();
return [4 /*yield*/, (0, _mobx.runInAction)(function () {
_mutantsMicrofx.stores.user.options.set("password", newPassword);
})];
case 2:
_a.sent();
return [2 /*return*/, updateResult];
}
});
});
};
User.prototype.initChannel = function (orgId) {
return __awaiter(this, void 0, void 0, function () {
var checkOrg, _a, _b;
var _this = this;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (this.orgList) {
checkOrg = this.orgList.some(function (org) {
if (org.orgId === orgId) {
_this.orgName = org.orgFullName;
return true;
}
});
if (!checkOrg) {
throw new Error("orgId " + orgId + " is out of range user orgList");
}
}
this.orgId = orgId;
this.cache(); //初始化通道
_a = this;
return [4 /*yield*/, _Channel2.default.getChannel()];
case 1:
_a.channel = _c.sent();
//执行前端网关策略
this.executeFEGatewayStrategy();
//将会话信息缓存
_b = this;
return [4 /*yield*/, _Channel2.default.getChannel()];
case 2:
//将会话信息缓存
_b.channel = _c.sent();
this.cache();
return [2 /*return*/];
}
});
});
};
//执行代理
User.prototype.executeFEGatewayStrategy = function () {
return __awaiter(this, void 0, void 0, function () {
var data, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 4,, 5]);
return [4 /*yield*/, _LoginApi2.default.getApiConfig()];
case 1:
data = _a.sent().data;
//执行代理灰度策略
return [4 /*yield*/, this.executeProxyGrayStrategy(data)];
case 2:
//执行代理灰度策略
_a.sent();
//执行云代理灰度策略
return [4 /*yield*/, this.executeCloudproxyGrayStrategy(data)];
case 3:
//执行云代理灰度策略
_a.sent();
return [3 /*break*/, 5];
case 4:
err_1 = _a.sent();
console.error('err=======', err_1);
return [3 /*break*/, 5];
case 5:
return [2 /*return*/];
}
});
});
};
/**
* 执行代理灰度策略
* @param {*} data 配置信息
*/
User.prototype.executeProxyGrayStrategy = function (data) {
return __awaiter(this, void 0, void 0, function () {
var gray_filter, _a, gray_release, _b, gray_domain, _c, org_id, _d, user_id, _e, domain, isGray;
var _this = this;
return __generator(this, function (_f) {
gray_filter = data.gray_filter;
_a = gray_filter.gray_release, gray_release = _a === void 0 ? '' : _a, _b = gray_filter.gray_domain, gray_domain = _b === void 0 ? '' : _b, _c = gray_filter.org_id, org_id = _c === void 0 ? [] : _c, _d = gray_filter.user_id, user_id = _d === void 0 ? [] : _d, _e = gray_filter.domain, domain = _e === void 0 ? [] : _e;
if (!!gray_domain) {
switch (gray_release) {
//全部灰度
case 'all':
this.proxyserver = gray_domain;
break;
//部分灰度
case 'part':
isGray = (0, _findIndex3.default)(org_id, function (orgId) {
return _this.orgId == orgId;
}) > -1 || (0, _findIndex3.default)(user_id, function (userId) {
return _this.userId == userId;
}) > -1 || (0, _findIndex3.default)(domain, function (dom) {
return dom == _this.channel.targetURL;
}) > -1;
if (isGray) {
this.proxyserver = gray_domain;
}
break;
default:
break;
}
}
return [2 /*return*/];
});
});
};
/**
* 执行云代理灰度策略
* @param {*} data 配置信息
*/
User.prototype.executeCloudproxyGrayStrategy = function (data) {
return __awaiter(this, void 0, void 0, function () {
var cloudproxy_filter, _a, cloudproxy_domain, _b, org_id, _c, user_id, _d, domain, isProxyserver;
var _this = this;
return __generator(this, function (_e) {
cloudproxy_filter = data.cloudproxy_filter;
_a = cloudproxy_filter.cloudproxy_domain, cloudproxy_domain = _a === void 0 ? '' : _a, _b = cloudproxy_filter.org_id, org_id = _b === void 0 ? [] : _b, _c = cloudproxy_filter.user_id, user_id = _c === void 0 ? [] : _c, _d = cloudproxy_filter.domain, domain = _d === void 0 ? [] : _d;
if (!!cloudproxy_domain && !!this.channel.targetURL && this.channel.targetURL.indexOf('.chanjet.com') == -1) {
isProxyserver = (0, _findIndex3.default)(org_id, function (orgId) {
return _this.orgId == orgId;
}) > -1 || (0, _findIndex3.default)(user_id, function (userId) {
return _this.userId == userId;
}) > -1 || (0, _findIndex3.default)(domain, function (dom) {
return dom == _this.channel.targetURL;
}) > -1;
if (isProxyserver) {
this.channel.targetURL = _app.proxyServer;
}
}
return [2 /*return*/];
});
});
};
User.prototype.setChannel = function (targetURL) {
this.channel = _Channel2.default.setChannel(targetURL);
this.cache();
};
//登录T+
User.prototype.loginTplus = function (param) {
return __awaiter(this, void 0, void 0, function () {
var result, data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (_tools2.default.isApp() && !!window.localStorage.tplusToken) {
//移动端做共享token 基于性能优化考虑 复用token 其余登录绕过此过程
//从ls中获取登录token信息,绕过此步骤
this.tplusToken = window.localStorage.tplusToken;
this.authorization = "token " + this.tplusToken;
if (!!window.localStorage.sid) {
this.sid = window.window.localStorage.sid;
}
this.cache();
_mutantsMicrofx.stores.user.login(this);
return [2 /*return*/];
}
return [4 /*yield*/, _LoginApi2.default.getTplusToken(param)];
case 1:
result = _a.sent();
if (result.indexOf(';') > -1) {
data = result.split(';');
this.tplusToken = data[0];
this.sid = data[1];
} else {
this.tplusToken = result;
}
this.authorization = "token " + this.tplusToken;
this.cache();
_mutantsMicrofx.stores.user.login(this);
return [2 /*return*/];
}
});
});
};
//切换账套后更新User信息
User.prototype.changeUserInfo = function (param) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
if (param.tplusToken.indexOf(';') > -1) {
data = param.tplusToken.split(';');
this.tplusToken = data[0];
this.sid = data[1];
} else {
this.tplusToken = param.tplusToken;
}
this.authorization = "token " + this.tplusToken;
this.orgId = param.orgId;
this.orgName = param.orgName;
this.setChannel(param.targetURL);
if (!!window.localStorage.sid) {
this.sid = window.window.localStorage.sid;
}
this.cache();
_mutantsMicrofx.stores.user.login(this);
return [2 /*return*/];
});
});
};
// 二维码登录,只传入userid orgid
User.prototype.QRLogin = function (_a) {
var userId = _a.userId,
orgId = _a.orgId;
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
this.orgId = orgId;
this.userId = userId;
this.accessToken = '***';
this.cache();
return [4 /*yield*/, this.loginTplus({ clientName: '扫码登录', desc: '扫码登录' })];
case 1:
_b.sent();
return [2 /*return*/];
}
});
});
};
User.pwdEncrypt = function (password) {
var md5 = crypto.createHash('md5');
md5.update(password);
password = md5.digest('hex');
return password;
};
User.prototype.fromJS = function (result) {
//获取企业信息
var orgListInfo = result.orgListInfo;
var orgLength = orgListInfo.orgList.length;
if (orgLength > 0) {
this.userId = result.user_id;
this.accessToken = result.access_token;
this.orgList = orgListInfo.orgList;
}
return this;
};
//缓存数据
User.prototype.cache = function () {
var _this = this;
_mutantsMicrofx.localStore.set(_app.authKey, this);
_app.authKeys.forEach(function (authKey) {
if (!_tools2.default.isNull(_this[authKey])) {
_mutantsMicrofx.localStore.set(authKey, _this[authKey]);
}
});
};
//恢复数据
User.restore = function () {
var auth = _mutantsMicrofx.localStore.get(_app.authKey);
if (auth == null || auth == undefined) {
auth = emptyAuth;
}
return auth;
// const accessToken = localStore.get('accessToken');
// if(!tools.isNull(accessToken)){
// let cacheObj = {};
// authKeys.forEach((item)=>{
// cacheObj[item] = localStore.get(item);
// });
// return cacheObj;
// }
// return emptyAuth;
};
User.clear = function () {
if ((_mutantsMicrofx.stores.posLoginStore || {}).savePosCode) {
_mutantsMicrofx.stores.posLoginStore.savePosCode(false);
}
User.clearLocalSession();
};
User.clearLocalSession = function () {
_mutantsMicrofx.localStore.remove(_app.authKey);
_app.authKeys.forEach(function (item) {
_mutantsMicrofx.localStore.remove(item);
});
_Channel2.default.clear();
};
//注销对象
User.logout = function (fn, isdestroyApp) {
if (isdestroyApp === void 0) {
isdestroyApp = true;
}
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
return [4 /*yield*/, User.posLogout()];
case 1:
_a.sent(); //pos注销
_LogoutMW2.default.execute();
User.clear();
fn && fn();
// 用户退出不用调用history,门户可以通过用户状态判断显示登录页面
if (_mutantsMicrofx.stores.user) {
_mutantsMicrofx.stores.user.logout();
}
return [4 /*yield*/, User.mobileLogout(isdestroyApp)];
case 2:
_a.sent(); //移动端注销
return [2 /*return*/];
}
});
});
};
//移动端注销
User.mobileLogout = function (isdestroyApp) {
if (isdestroyApp === void 0) {
isdestroyApp = true;
}
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
//移动端的场景
//轻应用 与 微信
if (_tools2.default.isApp() || platform === constant.platform.weixin) {
//清空store中的用户信息
if (_mutantsMicrofx.stores.user) {
_mutantsMicrofx.stores.user.logout();
}
if (isdestroyApp) {
(0, _mutantsJsbridge.destroyApp)();
}
}
return [2 /*return*/];
});
});
};
//pos端注销
User.posLogout = function () {
return __awaiter(this, void 0, void 0, function () {
var posIsLogin, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
posIsLogin = (_mutantsMicrofx.stores.posLoginStore || {}).posIsLogin;
_a = posIsLogin;
if (!_a) return [3 /*break*/, 2];
return [4 /*yield*/, _LoginApi2.default.logout()];
case 1:
_a = _b.sent();
_b.label = 2;
case 2:
_a;
return [2 /*return*/];
}
});
});
};
//通过一次性码登录CIA
User.loginByAuthcode = function (authCode) {
return __awaiter(this, void 0, void 0, function () {
var authResult;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
return [4 /*yield*/, _LoginApi2.default.authenticationByCode(authCode)];
case 1:
authResult = _a.sent();
return [2 /*return*/, authResult];
}
});
});
};
//判断用户是否登录
User.isLogined = function () {
var user = User.restore();
if (_tools2.default.isNull(user.accessToken) || _tools2.default.isNull(user.userId) || _tools2.default.isNull(user.orgId)) {
return false;
}
return true;
};
return User;
}();
exports.default = User;
//# sourceMappingURL=User.js.map
//# sourceMappingURL=User.js.map