import apiConfig from "./apiConfig"; import edu from "./edu"; import md5 from "./md5"; import Session from "./requests"; import { getResConstruction, accountManager} from "./utils"; export default 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.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}={}){ 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: 1, text: res.unread_message_count.toString(),...handler}); else if (res.unread_tiding_count) wx.showTabBarRedDot({index: 1,...handler}); else{ wx.removeTabBarBadge({index:1}); wx.hideTabBarRedDot({index:1}); } }).catch(e => { console.error("getTidingInfo", e); }); } 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 }); } } 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={}){ 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 edu({ session,name, config, data: {...this.refresh_key(),...data}, success:res=>{ this.trigger("success",name,res); console.debug(`EduCoder api ${name} construction:`, getResConstruction(res)); if(success) success(res) }, fail:e=>{ this.trigger("fail",name,e); if(fail) fail(e); }, 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};