var Host = window.location.protocol + '//' + window.location.host + '/'; class CommonTool { static createSuccessRet(data, isMerge) { var data = data || {}; if (isMerge) { return Object.assign({result: 'success'}, data); } return { 'result': 'success', 'data': data, }; } static createFailRet(errMsg) { return { 'result': 'fail', 'reason': errMsg, }; } static getFailReason(ret) { return ret && ret.reason; } static isSuccessRet(ret) { return !this.isFailRet(ret); } static isFailRet(ret) { if (!ret || !ret.result) { return true; } if (ret.result !== 'success') { return true; } return false; } } class RubyTask { constructor() { this.hasLoginMallIds = []; this.urls = { getQueueStatMap: Host + 'ruby_dt/task/index/getQueueStatMap', clearDwsQueue: Host + 'ruby_dt/task/dws/clearDwsQueue', } } async updateHasLoginMallIds() { this.hasLoginMallIds = await RIC.desktopApi.getHasLoginMallIds(); console.log(this.hasLoginMallIds) } async getQueueStatMap(mallIds) { let url = this.urls.getQueueStatMap; return new Promise((resolve, reject) => { $.ajax({ url: url, method: 'post', dataType: 'json', data: { mallIds:mallIds } }).always(function () { }).done(function (data) { resolve(data) }).fail(function () { resolve(CommonTool.createFailRet('请求失败')) }); }) } async loopRun() { let _this = this; async function run() { console.log('run....') await _this.updateHasLoginMallIds(); let successCnt = await _this.processQueue(); if (successCnt === false) { return; } setTimeout(run, successCnt ? 5000 : 60000); } run(); } async processQueue() { let successCnt = 0; let queueStatMapRet = await this.getQueueStatMap(this.hasLoginMallIds); if (queueStatMapRet.stop == 1) { return false; } if (CommonTool.isFailRet(queueStatMapRet)) { return -1; } let queueStatMap = queueStatMapRet.queueStatMap || {}; for (let queueTimerName in queueStatMap) { let mallIds = queueStatMap[queueTimerName]; for (let mallId of mallIds) { if (!this.hasLoginMallIds.includes(mallId)) { continue; } let timer = eval(`new ${queueTimerName}(${mallId})`); let ret = await timer.run(); CommonTool.isSuccessRet(ret) && successCnt++; } } return successCnt; } } class RefreshAlimamaLogin { constructor() { this.lastLoginTime = 0; this.failTimes = 0; this.accountInfo = {} this.loopId = 0; } async loopRun(loopId, keepLogin = false) { this.loopId = loopId; let _this = this; let accountRet = await _this.getAlimamaAccount(); if (CommonTool.isFailRet(accountRet)) { console.log('get alimama account fail', accountRet) } _this.accountInfo = accountRet.allianceAccount || {}; console.log('alimama accountInfo', _this.accountInfo) if (_this.accountInfo.is_auto_login != 1 && keepLogin === false) { return; } async function run(loopId) { if (_this.loopId !== loopId) { return ; } if (_this.failTimes > 5) { console.log('max fail times stop ', _this.failTimes) return; } await _this.doLogin(); setTimeout(function () { run(loopId) }, 3000) } run(loopId); } async doLogin() { let _this = this; console.log('start login alimama', this.loopId) let now = Date.now(); //大概45分钟过期 if (now - _this.lastLoginTime < 40 * 60 * 1000) { return; } let ret = await RIC.desktopApi.autoLoginAccount('alimama', _this.accountInfo.login_username, _this.accountInfo.login_password) console.log('auto login alimama ret ', ret); if (CommonTool.isFailRet(ret) && ret.stop) { return; } let aliUserInfo = await RIC.desktopApi.superRequest('https://pub.alimama.com/common/getUnionPubContextInfo.json', {}, 'GET', 'alimama'); if (aliUserInfo.data && aliUserInfo.data.data && aliUserInfo.data.data.mmNick) { _this.lastLoginTime = Date.now(); _this.failTimes = 0; console.log('alimama login success ', aliUserInfo.data.data.mmNick); } else { _this.failTimes++; console.log('alimama login fail ', ret); } } async getAlimamaAccount() { let url = Host + 'ruby_dt/order/alliance/getAlimamaAccount' return new Promise((resolve, reject) => { $.ajax({ url: url, method: 'post', dataType: 'json', data: { } }).always(function () { }).done(function (ret) { resolve(ret) }).fail(function () { resolve(CommonTool.createFailRet('请求失败')) }); }) } } var task = new RubyTask(); var alimamaLogin = new RefreshAlimamaLogin(); $(function () { task.loopRun(); alimamaLogin.loopRun(Date.now()); document.addEventListener('restart-alimama-refresh-login', async function (event) { alimamaLogin.failTimes = 0; alimamaLogin.loopRun(Date.now(), true); }) })