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

406 lines
14 KiB

import {Account, Course, Exercise, ExerciseQuestion} 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 = {};
this.callback={require_login: ()=>{}};
this.load_current_user();
}
load_current_user(){
var value = wx.getStorageSync('current_user');
if(value) {
this.current_user = value;
console.log("read current_user from storage");
console.log(this.current_user);
}
}
save_current_user(){
console.log("save current user to storage");
console.log(this.current_user);
wx.setStorageSync("current_user", this.current_user);
}
clear_current_user(){
wx.setStorageSync("current_user", {});
}
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 from wx.Storage:");
console.log(this.session.cookies);
} catch (e) {
// Do something when catch error
console.error("there is not cached cookie")
console.error(e);
}
}
save_cookies(){
for(var cookie of Object.values(this.session.cookies)){
console.log("client save new cookie");
console.log(cookie);
cookie.save();
}
}
onRequireLogin(cd){
this.callback.require_login = cd;
}
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};
console.log("current_user info update");
console.log(this.current_user);
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});
})
}
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_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};
console.log("current_user info update");
console.log(this.current_user);
if(typeof success == "function"){
success(res);
}
resolve(res);
this.save_current_user();
},
fail: error=>{
this.callback.require_login();
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);
}
});
});
}
get_course_info({course_id, success, fail, complete}){
return new Promise((resolve, reject) => {
Course.get_course_info({
session: this.session,
course_id: course_id,
complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
}
resolve(res);
},
fail: error=>{
if(typeof fail == "function"){
fail(error);
}
reject(error);
}
})
});
}
get_user_courses({page=1, per_page=16, success, fail, complete}={}){
return new Promise((resolve, reject) => {
Account.get_user_courses({
session: this.session,
page: page,
per_page: per_page,
user: this.current_user.login,
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);
}
})
});
}
get_course_files({course_id, page_size=15, page=1,search="",sort=0, sort_type="created_on", success, fail, complete}){
return new Promise((resolve, reject) => {
Course.get_files({
session: this.session,course_id:course_id, page_size: page_size, page:page, search: search, sort: sort, sort_type: sort_type, complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
};
resolve(res);
},
fail: error=>{
if(typeof success == "function"){
fail(error);
}
reject(error);
}
});
});
}
get_course_students({course_id, order=1,sort="asc",page=1, limit=20, course_group_id="", success, fail, complete}){
return new Promise((resolve, reject) => {
Course.get_students({
session: this.session,
course_id: course_id,
order: order,
sort: sort,
page: page,
limit: limit,
course_group_id: course_group_id,
complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
};
resolve(res);
},
fail: error=>{
if(typeof success == "function"){
fail(error);
}
reject(error);
}
})
})
}
search_exercises({course_id, type="", page=1, limit=15, success, fail, complete}){
return new Promise((resolve, reject) => {
Exercise.search({
session: this.session,
course_id: course_id,
type: type,
page: page,
limit: limit,
complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
};
resolve(res);
},
fail: error=>{
if(typeof fail == "function"){
fail(error);
}
reject(error);
}
})
});
}
start_answer_exercise({exercise_id, success, fail, complete}){
return new Promise((resolve, reject) => {
Exercise.start_answer({
session: this.session,
exercise_id: exercise_id,
login: this.current_user.login,
complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
};
resolve(res);
},
fail: error=>{
if(typeof fail == "function"){
fail(error);
}
reject(error);
}
})
});
}
answer_exercise_question({question_id, exercise_choice_id,answer_text ,success, fail, complete}){
return new Promise((resolve, reject) =>{
ExerciseQuestion.answer({
session: this.session,
question_id: question_id,
answer_text: answer_text,
exercise_choice_id: exercise_choice_id,
complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
};
resolve(res);
},
fail: error=>{
if(typeof fail == "function"){
fail(error);
}
reject(error);
}
})
} );
}
save_exercise({exercise_id, success, fail, complete}){
return new Promise((resolve, reject) => {
Exercise.save({
session: this.session,
exercise_id: exercise_id,
complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
};
resolve(res);
},
fail: error=>{
if(typeof success == "function"){
fail(error);
}
reject(error);
}
})
});
}
commit_exercise({exercise_id, commit_method=1, success, fail , complete}){
return new Promise((resolve, reject) => {
Exercise.commit({
session: this.session,
exercise_id: exercise_id,
commit_method: commit_method,
complete: complete,
success: res=>{
if(typeof success == "function"){
success(res);
};
resolve(res);
},
fail: error=>{
if(typeof success == "function"){
fail(error);
}
reject(error);
}
})
});
}
}