const api_base_url = "https://www.educoder.net/api"; /** * https://www.educoder.net的api接口,通过其实现用户与后端进行交互 */ export class Account{ static login({session, login, password, autologin, success, fail, complete}) { console.log("login"); let data = {login: login, password: password, autologin: autologin}; session.request({ url: api_base_url + "/accounts/login.json", method: "POST", data: data, header: {"content-type": "application/json"}, success: (res)=>{ if("status" in res.data && res.data.status<0){ fail(new Error(res.data.message)); return; } console.log("type of success:"+ typeof success); if(typeof success == "function") { success(res); } }, fail: fail, complete: complete }); } static get_user_info({session, success, fail, complete}){ let data = {school: 1} session.request({ url: api_base_url + "/users/get_user_info.json", method: "GET", data: data, success: success, fail: fail, complete: complete }) } static get_verification_code({session, login, type, success, fail, complete}){ let data = {login: login, type: type}; //@notice: type: 验证码用途 type=1=> 注册 type=2 => 重置密码 session.request({ url: api_base_url + "/accounts/get_verification_code.json", method: "GET", data: data, success: success, fail: fail, complete: complete }) } static register({session, login, password, code, success, fail, complete}){ let data = {login: login, password: password, code: code}; session.request({ url: api_base_url + "/accounts/register.json", method: "POST", data:data, success(res) { if("status" in res.data && res.data.status<0){ fail(new Error(res.data.message)); return; } success(res); }, fail: fail, complete: complete }) } static attendance({session, success, fail, complete}){ session.request({ url: api_base_url + "/users/attendance.json", success(res) { if("status" in res.data && res.data.status<0){ fail(new Error(res.data.status)); return; } if(typeof success == "function"){ success(res); } }, fail: fail, complete: complete }) } static logout({session, success, fail, complete}){ session.request({ url: api_base_url + "/accounts/logout.json", method: "GET", success: success, fail: fail, complete: complete }) } static get_user_courses({session,user,page=1, per_page=16, success, fail, complete}){ let data = {page: page, per_page: per_page}; return session.request({ url: api_base_url + "/users/"+user +"/courses.json", method: "GET", data: data, success: success, fail: fail, complete: complete }) } } export class Course{ static create({session, data, success, fail, complete}){ } static search({session, limit=20, page=1, order="all", search="", success, fail, complete}){ let data = {limit: limit, page: page, order: order, search: search}; return session.request({ url: api_base_url + "/courses.json", method: "GET", data: data, success: success, fail: fail, complete: complete }) } static join({session, invite_code, professor=null,assistant_professor=null,student=null, success, fail, complete}){ let data = { invite_code: invite_code, professor: professor, assistant_professor: assistant_professor, student: student }; return session.request({ url: api_base_url + "/courses/apply_to_join_course.json", method:"POST", data: data, success: res=>{ if("status" in res.data && res.data.status<0){ fail(new Error(res.data.message)); return; } if(typeof success == "function"){ success(res); } }, fail: fail, complete: complete }) } static get_user_info({session, course_id, success, fail, complete}){ let data = {course_id: course_id, school: 1} return session.request({ url: api_base_url + "/users/get_user_info.json", method: "GET", data: data, success: success, fail: fail, complete: complete }) } static get_files({session, course_id, page_size=15, page=1,search="",sort=0, sort_type="created_on", success, fail, complete}){ let data = {course_id: course_id, page_size: page_size, page: page, search: search, sort: sort, sort_type: sort_type}; return session.request({ url: api_base_url + "/files.json", method: "GET", data: data, success: success, fail: fail, complete: complete }) } static get_students({session, course_id, order=1,sort="asc",page=1, limit=20, course_group_id="", success, fail, complete}){ let data = { order: order, sort: sort, page: page, limit: limit, course_group_id: course_group_id }; return session.request({ url: api_base_url + "/courses/" + course_id + "/students.json", method: "GET", data: data, success(res){ if("status" in res.data && "message" in res.data){ fail(new Error(res.data.message)); return; } success(res); }, fail: fail, complete: complete }) } static get_course_info({session, course_id, success, fail, complete}){ session.request({ url: api_base_url + "/courses/"+course_id+"/top_banner.json", method : "GET", success: success, fail: fail, complete: complete }) } } export class Exercise{ static search({session,course_id, page=1, limit=15, success, fail, complete}){ let data = {page:page, limit:limit}; return session.request({ url: api_base_url + "/courses/"+course_id+"/exercises.json", method: "GET", data: data, success(res) { if ("status" in res.data ) { if (res.data.status < 0){ fail(new Error(res.data.message)); return; } switch(res.data.status){ case 409: fail(new Error(res.data.message)); return; } } if (typeof success == "function") { success(res); } }, fail:fail, complete: complete }) } static start_answer({session, exercise_id, login, success, fail, complete}){ let data = {login: login}; return session.request({ url: api_base_url + "/exercises/" + exercise_id + "/start_answer.json", method: "GET", data: data, success(res) { if ("status" in res.data ) { if (res.data.status < 0){ fail(new Error(res.data.message)); return; } switch(res.data.status){ case 409: fail(new Error(res.data.message)); return; } } if (typeof success == "function") { success(res); } }, fail: fail, complete: complete }) } static save({session, exercise_id, success, fail, complete}){ return session.request({ url: api_base_url + "/exercises/"+exercise_id+"/begin_commit.json", method: "GET", success: success, fail: fail, complete: complete }) } static commit({session, exercise_id, commit_method=1, success, fail, complete}){ let data = {commit_method: commit_method}; return session.request({ url: api_base_url+ "/exercises/"+exercise_id+"/commit_exercise.json", method: "POST", data: data, success: success, fail: fail, complete: complete }) } } export class ExerciseQuestion { static answer({session, question_id, exercise_choice_id, answer_text, success, fail, complete}){ let data = {}; if(exercise_choice_id){data["exercise_choice_id"] = exercise_choice_id;} if(answer_text){data["answer_text"] = answer_text} return session.request({ url: api_base_url + "/exercise_questions/" + question_id +"/exercise_answers.json", method: "POST", data: data, success: res=>{ if("status" in res.data){ if(res.data.status<0){ fail(new Error(res.data.message)); return; } /*switch (res.data.status) { case -1: fail(new Error("???")); return; }*/ } if(typeof success == "function") { success(res); } }, fail: error=>{ // console.error("wx.request error:"); // console.log(error); // console.log(error.toString()); // console.log(error.message); // console.log(error.errMsg); let e = error; if(!("message" in error)){ if("errMsg" in error){ e = new Error(error.errMsg); } } if(typeof fail == "function") { fail(e); } }, complete: complete }) } } export class Ui{ }