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.
147 lines
4.5 KiB
147 lines
4.5 KiB
import apiConfig from "./apiConfig";
|
|
import edu from "./edu";
|
|
import md5 from "./md5";
|
|
import Session from "./requests";
|
|
import { getResConstruction} 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.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 })
|
|
this.on("before", "users.accounts", getLogin);
|
|
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;
|
|
wx.setStorageSync("autologin", 0);
|
|
this.user={};
|
|
this.save_user();
|
|
});
|
|
this.on("success","users.get_user_info", res=>{
|
|
this.user = res;
|
|
this.synch = 1;
|
|
this.save_user();
|
|
this.save_cookies();
|
|
});
|
|
this.on("success","accounts.login", res=>{
|
|
this.synch=0;
|
|
this.save_cookies();
|
|
this.getTidingInfo({login:res.login});
|
|
});
|
|
this.on("success","first_stamp", res=>{
|
|
this.randomcode=res.message;
|
|
this.client_key = key(this.randomcode);
|
|
})
|
|
}
|
|
getTidingInfo({login}={}){
|
|
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() });
|
|
else if (res.unread_tiding_count)
|
|
wx.showTabBarRedDot({ 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 autologin = wx.getStorageSync("autologin");
|
|
let login = wx.getStorageSync("login");
|
|
let password = wx.getStorageSync("_password");
|
|
if(info.user_id==2&&autologin&&password&&login){
|
|
await this.callApi({name:"accounts.login", data:{login, password}})
|
|
.catch(e=>{wx.setStorage({
|
|
key: 'autologin',
|
|
data: 0,
|
|
})});
|
|
await this.callApi({ name: "users.get_user_info" });
|
|
}
|
|
}
|
|
return {synch:this.synch,change:old_id!=this.user.user_id,user:this.user};
|
|
}
|
|
refresh_key(){
|
|
let newCode = Date.parse(Date()) / 1e3;
|
|
if(newCode-this.randomcode>10){
|
|
//this.callApi({name:"main.first_stamp"});
|
|
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};
|