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.
pk8f3pmu2/data/client.js

182 lines
6.1 KiB

import {Account, Course} from "./eduapi"
import {Session} from "./requests";
import {Cookie} from "./cookie";
/**
* 客户端类其method实现客户端各种操作将接口Promise化从而使其调用更加方便简洁
* 要在此处实现网络数据及cookie的缓存
*/
export class Client{
constructor({session}={}){
if(!session) {
this.session = new Session();
}else {
this.session = session;
}
this.load_cached_cookies();
this.current_user = {};
console.log(this.session);
}
load_cached_cookies(){
try {
var value = wx.getStorageSync('_educoder_session');
if (value) {
this.session.set_cookie(new Cookie(value))
}
var value = wx.getStorageSync("autologin_trustie");
if (value) {
this.session.set_cookie(new Cookie(value));
}
console.log("read cookies");
console.log(this.session.cookies);
} catch (e) {
// Do something when catch error
console.log("there is not cachede cookie")
console.error(e);
}
}
save_cookies(){
for(var cookie of Object.values(this.session.cookies)){
console.log("client save cookie");
console.log(cookie);
cookie.save();
}
}
require_login(){
wx.redirectTo({
url: "../pages/login/login"
});
wx.showToast({
title:"请先登陆",
icon: "none",
duration: 4000
});
}
login({login, password, autologin, success, fail, complete}){
return new Promise((resolve, reject) => {
Account.login({
session: this.session, login: login, password: password, autologin: autologin,
fail: error => {
if(typeof fail == "function") {
fail(error);
}
reject(error);
},
complete: complete,
success: res => {
this.save_cookies();
this.current_user = {...this.current_user, ...res.data};
this.get_user_info();
if (typeof success == "function") {
success(res);
}
resolve(res);
}
});
});
}
logout({success, fail, complete}={}){
return new Promise((resolve, reject) => {
Account.logout({session: this.session,
success: res=>{
if(typeof success == "function") {
success(res);
}
this.get_user_info();
resolve(res);
}, fail: error=>{
if(typeof fail =="function"){
fail(error);
}
reject(error);
}, complete: complete});
})
}
get_user_info({success, fail, complete}={}){
return new Promise((resolve, reject) => {
Account.get_user_info({session: this.session, complete: complete,
success: res=>{
this.current_user = {...this.current_user, ...res.data};
if(typeof success == "function"){
success(res);
}
resolve(res);
},
fail: error=>{
if(typeof fail == "function"){
fail(error);
}
reject(error);
}
})
});
}
register({login, password, code, success, fail, complete}){
return new Promise((resolve, reject) => {
Account.register({session: this.session, login:login, password:password, code:code,
complete:complete,
success: res=>{
this.save_cookies();
if(typeof success == "function"){
success(res);
}
resolve(res);
},
fail:error=>{
if(typeof fail == "function") {
fail(error);
}
reject(error)
}
});
});
}
get_verification_code_for_register({login, success, fail, complete}){
return Account.get_verification_code({session: this.session, login: login, type: 1,
fail: fail, complete: complete, success: success});
}
get_verification_code_for_reset_password({}){}
search_courses({limit=20, page=1, order="all", search="", success, fail, complete}={}){
return new Promise((resolve, reject) => {
Course.search({session: this.session, page: page, order: order, search: search, complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
}
resolve(res);
},
fail: error=>{
if(typeof fail == "function"){
fail(error);
}
reject(error);
}
});
});
}
join_course({invite_code, professor=null,assistant_professor=null,student=null, success, fail, complete}){
return new Promise((resolve, reject)=>{
Course.join({session: this.session, invite_code: invite_code, professor: professor, assistant_professor: assistant_professor, student: student, complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
}
resolve(res);
},
fail: error=>{
if(typeof fail == "function"){
fail(error);
}
reject(error);
}
})
});
}
search_exercise(){}
start_answer_exercise(){}
answer_exercise_question(){}
}