You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.2 KiB

import {triggerApi, Session} from "./api";
import md5 from "./md5";
import { accountManager} from "./utils";
class Client{
constructor({session} = {}) {
this.session = session || new Session();
this.user = {};
this.synch = 0;
this.load_cookies();
this.load_user();
this.randomcode=0;
this.tidingGet = 0;
this.cb={
before:{},
success:{},
fail:{}
};
this.initCallback();
}
on(cd,name,cb){
this.cb[cd][name]=cb;
}
trigger(cd,name, data){
if(typeof this.cb[cd][name]=="function")
return this.cb[cd][name](data);
else
return this.cb[cd][name];
}
initCallback(){
var getSms=({login})=>({smscode:md5(login)});
this.on("before", "accounts.get_verification_code", getSms);
this.on("before", "weapps.verification_code",getSms);
var getLogin = () => ({ login: this.user.login });
var getId = ()=>({user_id: this.user.user_id});
this.on("before", "users.accounts", getId);
this.on("before", "users.accounts.password", getLogin);
this.on("before","users.courses", getLogin);
this.on("before","users.subjects", getLogin);
this.on("before","users.homepage_info", getLogin);
this.on("before","users.accounts.avatar",getLogin);
this.on("before","users.unread_message_info", getLogin);
this.on("before","users.shixuns",getLogin);
this.on("success", "accounts.logout", res=>{
this.synch = 0;
this.session.cookies="";
accountManager.deactivateCurrentAccount();
this.user={};
this.save_user();
});
this.on("success","users.get_user_info", res=>{
this.updateUserInfo(res);
this.synch = 1;
this.save_user();
this.save_cookies();
});
this.on("success","accounts.login", res=>{
this.synch=0;
this.user = {};
this.save_cookies();
});
this.on("success", "accounts.register", res => {
this.synch = 0;
this.save_cookies();
});
this.on("success","first_stamp", res=>{
this.randomcode=res.message;
this.client_key = key(this.randomcode);
})
}
getTidingInfo({login}={}){
let index = 2;
this.tidingGet = 1;
const handler = {
fail: res=>{this.tidingGet=0}
};
return this.api("users.unread_message_info")({login}).then(res => {
if (res.unread_message_count)
wx.setTabBarBadge({ index, text: res.unread_message_count.toString(),...handler});
else if (res.unread_tiding_count)
wx.showTabBarRedDot({index,...handler});
else{
wx.removeTabBarBadge({index});
wx.hideTabBarRedDot({index});
}
}).catch(e => {
});
}
syncUser({refresh=0}={}){
if(!this.synching||!this.tmp_promise||!this.synch||refresh){
this.synching = 1;
this.tmp_promise = this._syncUser({refresh});
}
return this.tmp_promise;
}
async _syncUser({ refresh = 0 }) {
let old_id = this.user.user_id;
if(!this.synch||refresh){
let info = await this.callApi({name:"users.get_user_info"});
let account = accountManager.getCurrentAccount();
if(info.user_id==2&&account&&account.active){
await this.callApi({name:"accounts.login", data:account})
.catch(e=>{
accountManager.deactivateCurrentAccount();
});
await this.callApi({ name: "users.get_user_info" });
}
}
let changed = old_id != this.user.user_id;
if (this.user.login && this.user.login!=2){
if(changed)
this.tidingGet = 0;
if(!this.tidingGet){
this.getTidingInfo({ login: this.user.login });
}
}
this.synching = 0;
return {synch:this.synch,changed, user:this.user};
}
updateUserInfo(info){
this.user = {...this.user, ...info};
}
refresh_key(){
let newCode = Date.parse(Date()) / 1e3;
if(newCode-this.randomcode>10){
this.randomcode = newCode;
this.client_key = md5(this.randomcode);
}
return {randomcode:this.randomcode,client_key:this.client_key};
}
api(name,config={},data={}){
global.realTimeLog.error("call desprated method!!!");
return ({success, fail, complete,..._data}={})=>{
return this.callApi({name,config, data:{...data,..._data}, success, fail, complete});
}
}
callApi({ name, data={},config={}, success, fail, complete}) {
let session = this.session;
let _data = this.trigger("before",name,data)||{};
data = {..._data, ...data};
return triggerApi({
session, name, config, data: {...this.refresh_key(),...data},
success:res=>{
this.trigger("success",name,res);
success&&success(res);
}, fail:(e,res)=>{
this.trigger("fail", name, e);
fail&&fail(e, res);
// for trace error
global.realTimeLog.warn(`api [${name}] called fail: `, e.toString(), res, data);
},complete
});
}
load_user() {
let value = wx.getStorageSync('user') || {};
this.user = value;
//this.synch = 1;
}
save_user() {
wx.setStorage({ key: "user", data: this.user });
}
load_cookies() {
this.session.cookies = wx.getStorageSync("cookies");
}
save_cookies() {
wx.setStorage({
key: 'cookies',
data: this.session.cookies,
})
}
}
const client = global.client = new Client();
client.syncUser({refresh:1});
module.exports = {client};