/** * https://github.com/jinke18/educoder_weapp * @licence GPL-3.0 * @author jinke18 */ const api_base_url = "https://www.educoder.net/api"; /** * https://www.educoder.net的api接口,通过其实现用户与后端进行交互 */ export class Account{ 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 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 logout({session, success, fail, complete}){ session.request({ url: api_base_url + "/accounts/logout.json", method: "GET", 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 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 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_user_courses({session,login,category="",status="",sort_by="",sort_direction="",page=1, per_page=16, success, fail, complete}){ let data = {page: page, per_page: per_page, category:category, status: status, sort_by: sort_by, sort_direction: sort_direction}; return session.request({ url: api_base_url + "/users/"+login +"/courses.json", method: "GET", data: data, success: success, fail: fail, complete: complete }) } static reset_password({session, login, code, new_password, new_password_confirmation, success, fail, complete}){ let data ={ login: login, code: code, new_password: new_password, new_password_confirmation: new_password_confirmation }; return session.request({ url: api_base_url + "/accounts/reset_password.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 set_password({}){ } static upload_avatar({session, login, avatar_path, success, fail, complete}){ var avatar_data=null; wx.getFileSystemManager().readFile({ filePath: avatar_path, encoding: "base64", success:res=> { console.log("in success"); console.log(res); avatar_data ="data:image/jpeg;base64,"+res.data; console.log("upload_avatar"); console.info(avatar_data); session.request({ url: "https://www.educoder.net/api/users/accounts/"+login+"/avatar.json", method: "PUT", data: {image: avatar_data}, success: success, fail: fail, complete: complete }) }, fail: error=>{ console.error(error); } }); } } export class School{ static search({ session, search = "", success, fail, complete}){ let data = {search: search}; session.request({ url: api_base_url + "/schools/school_list.json", method: "GET", 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 }) } } export class Course{ static create({session, data, success, fail, complete}){ return session.request({ url: api_base_url + "/courses.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 update({session, course_id, data, success, fail, complete}){ return session.request({ }) } 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 create({ session, course_id ,exercise_name, exercise_description, success, fail, complete}){ let data = {exercise_name:exercise_name, exercise_description: exercise_description}; return session.request({ url: api_base_url +`/courses/${course_id}/exercises.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 409: // fail(new Error(res.data.message)); // return; // } } if (typeof success == "function") { success(res); } }, fail: fail, complete: complete }) } static update({session, exercise_id, exercise_name, exercise_description, success, fail, complete}){ let data = {session, exercise_id, exercise_name, exercise_description, success, fail, complete}; session.request({ url: api_base_url+`/exercises/${exercise_id}.json`, method: "PUT", 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 get_edit({session, exercise_id, success, fail, complete}){ session.request({ url: api_base_url + `/exercises/${exercise_id}/edit.json`, method: "GET", success(res) { if ("status" in res.data) { if (res.data.status < 0) { fail(new Error(res.data.message)); return; } } if (typeof success == "function") { success(res); } }, fail: fail, complete: complete }) } /**@todo limit change */ static get_result({session, exercise_id, sort="asc", page=1, limit=50,success, fail, complete}){ let data={sort, page, limit}; session.request({ url: api_base_url +`/exercises/${exercise_id}/exercise_result.json`, method: "GET", data, success(res) { if ("status" in res.data) { if (res.data.status < 0) { fail(new Error(res.data.message)); return; } } if (typeof success == "function") { success(res); } }, fail: fail, complete: complete }) } static publish({session,course_id, exercise_ids, end_time, success, fail, complete}){ let data = {check_ids: exercise_ids, end_time}; session.request({ url: api_base_url+ `/courses/${course_id}/exercises/publish.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 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 get_grade({session, exercise_id,order="end_at", search="", success, fail, complete}){ let data = {order, search}; session.request({ url: api_base_url + `/exercises/${exercise_id}/exercise_lists.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 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 Question { static create({session, exercise_id, data, success, fail, complete}){ let data_ = {...data, exercise_bank_id: exercise_id}; session.request({ url: api_base_url + `/exercises/${exercise_id}/exercise_questions.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 409: // fail(new Error(res.data.message)); // return; // } } if (typeof success == "function") { success(res); } }, fail: fail, complete: complete }) } static update({session, question_id, data, success, fail, complete}){ session.request({ url: api_base_url + `/exercise_questions/${question_id}.json`, method: "PUT", 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 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} console.log("eduapi.js: request data", data, " url", api_base_url + "/exercise_questions/" + question_id + "/exercise_answers.json"); 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{ static get_home_page({session, success, fail, complete}){ return session.request({ session: this.session, url: api_base_url + "/home/index.json", method: "GET", success: res=> { if ("status" in res.data) { if (res.data.status < 0){ fail(new Error(res.data.message)); return; } } if (typeof success == "function") { success(res); } }, fail:fail, complete: complete }) } }