/** * https://github.com/jinke18/educoder_weapp * @licence GPL-3.0 * @author jinke18 */ import {Account, Course, Exercise, Question, School, Ui} 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) } }); }); } reset_password({login, code, new_password, new_password_confirmation, success, fail, complete}){ return new Promise((resolve, reject) => { Account.reset_password({ session: this.session, login: login, code: code, new_password: new_password, new_password_confirmation: new_password_confirmation, complete: complete, success: res=>{ 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 new Promise((resolve, reject) => { Account.get_verification_code({ session: this.session, login: login, type: 1, complete: complete, success: res => { if (typeof success == "function") { success(res); } resolve(res); }, fail: error => { if (typeof fail == "function") { fail(error); } reject(error); } }); }) } get_verification_code_for_reset_password({login, success, fail, complete}){ return new Promise((resolve, reject) => { Account.get_verification_code({ session: this.session, login: login, type: 2, complete: complete, success: res => { if (typeof success == "function") { success(res); } resolve(res); }, fail: error => { if (typeof fail == "function") { fail(error); } reject(error); } }); }) } upload_avatar({avatar_path, success, fail, complete}){ console.log("upload_avatar"); console.log(this.current_user); return new Promise((resolve, reject) => { Account.upload_avatar({ session: this.session, avatar_path: avatar_path, login: this.current_user.login||this.current_user.user_id, complete: complete, success: res => { if (typeof success == "function") { success(res); } resolve(res); }, fail: error => { if (typeof fail == "function") { fail(error); } reject(error); } }) }); } 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({category="",status="",sort_by="",sort_direction="",page=1, per_page=16, success, fail, complete}={}){ return new Promise((resolve, reject) => { Account.get_user_courses({ session: this.session, category: category, status: status, sort_by: sort_by, sort_direction: sort_direction, page: page, per_page: per_page, 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); } }); }); } create_course({data, success, fail, complete}){ return new Promise((resolve,reject)=>{ Course.create({ session: this.session, data: data, 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_question({question_id, exercise_choice_id,answer_text ,success, fail, complete}){ return new Promise((resolve, reject) =>{ Question.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); } }) } ); } create_question({exercise_id, data,success, fail, complete}){ return new Promise((resolve, reject) => { Question.create({ session: this.session, exercise_id, data, complete: complete, success: res=>{ if(typeof success == "function"){ success(res); } resolve(res); }, fail: error=>{ if(typeof fail == "function"){ fail(error); } reject(error); } }); }); } update_question({question_id, data, success, fail, complete}){ return new Promise((resolve, reject) => { Question.update({ session: this.session, question_id, data, complete: complete, success: res=>{ if(typeof success == "function"){ success(res); } resolve(res); }, fail: error=>{ if(typeof fail == "function"){ fail(error); } reject(error); } }); }); } create_exercise({course_id ,exercise_name, exercise_description, success, fail, complete}){ return new Promise((resolve, reject) => { Exercise.create({ session: this.session, course_id, exercise_name, exercise_description, complete: complete, success: res=>{ if(typeof success == "function"){ success(res); } resolve(res); }, fail: error=>{ if(typeof fail == "function"){ fail(error); } reject(error); } }); }); } update_exercise({exercise_id, exercise_name, exercise_description, success, fail, complete}){ return new Promise((resolve, reject) => { Exercise.update({ session: this.session, exercise_id, exercise_name, exercise_description, complete: complete, success: res=>{ if(typeof success == "function"){ success(res); } resolve(res); }, fail: error=>{ if(typeof fail == "function"){ fail(error); } reject(error); } }) }) } get_exercise_edit({exercise_id, success, fail, complete}){ return new Promise((resolve, reject) => { Exercise.get_edit({ session: this.session, exercise_id, complete: complete, success: res=>{ if(typeof success == "function"){ success(res); } resolve(res); }, fail: error=>{ if(typeof fail == "function"){ fail(error); } reject(error); } }) }); } publish_exercise({course_id, exercise_ids, end_time, success, fail, complete}){ return new Promise((resolve, reject) => { Exercise.publish({ session: this.session, course_id, exercise_ids, end_time, complete: complete, success: res=>{ if(typeof success == "function"){ success(res); } resolve(res); }, fail: error=>{ if(typeof fail == "function"){ fail(error); } reject(error); } }) }) } /** * @todo change limit default 10 */ get_exercise_result({ exercise_id, sort = "asc", page = 1, limit=50, success, fail, complete}){ return new Promise((resolve, reject)=>{ Exercise.get_result({ session: this.session, exercise_id, sort, page, limit, success: res => { if (typeof success == "function") { success(res); } resolve(res); }, fail: error => { if (typeof fail == "function") { fail(error); } reject(error); } }) }) } get_exercise_grade({exercise_id,order="end_at", search="", success, fail, complete}){ return new Promise((resolve, reject) => { Exercise.get_grade({ session: this.session, exercise_id, order, search, 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); } }) }); } search_schools({search = "", success, fail, complete} = {}){ return new Promise((resolve, reject) => { School.search({ session: this.session, 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_home_page({success, fail, complete}={}){ return new Promise((resolve, reject) => { Ui.get_home_page({ session: this.session, complete: complete, success: res=>{ if(typeof success == "function"){ success(res); }; resolve(res); }, fail: error=>{ if(typeof success == "function"){ fail(error); } reject(error); } }) }) } }