const app = getApp(); Component({ properties: { show:{ type:Boolean, value: false, observer: "onShowChange" }, show_code:{ type:Number, value:1 }, invite_code: String, auto_navigate:{ type:Number, value:1 }, opentype:{ type:String, value:"navigateTo" } }, data: { identities:[], showDialog: false, buttons:[ {text:"取消"}, {text:"提交"} ] }, methods: { onShowChange(value){ if(value){ if(!app.checkLogin({content:"您需要登录后才能加入课堂"})||!this.checkProfile()){ value = false; this.setData({show: false}); } wx.getClipboardData({ success:res=>{ if(!res.data||res.data==this.clipboardData) return; this.clipboardData = res.data; var match = res.data.match(/(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Z]{5,6}/); if(match) this.setData({invite_code: match[0]}); } }) this.setData({showDialog: value}); }else{ this.setData({showDialog: value}); } }, checkProfile(){ if(app.user().profile_completed) return true; else{ wx.showModal({ title:"提示", content:"您需要完善您的个人资料,才能加入课堂", confirmText:"立即完善", success:res=>{ if(res.confirm){ app.navigateTo({ url:"{profile}" }); } } }) return false; } }, scan(){ wx.scanCode({ success:res=>{ var fail = false; if(res.scanType=="QR_CODE") this.setData({invite_code: res.result}); else if(res.scanType=="WX_CODE"&&res.path){ var match = res.path.match(/course_invite\?(.*)$/) if(match){ var options = {} match[1].split("&").map(i=>{ var index = i.indexOf("="); var k = i.slice(0, index); var v = i.slice(index + 1); options[k]=v}) if(options.scene){ var scene = {}; for (var i of decodeURIComponent(options.scene).split("&")) { var index = i.indexOf("="); var k = i.slice(0,index); var v = i.slice(index+1); scene[k] = v; } if(scene.course_id){ var {course_id} = scene; app.api("weapps.courses.basic_info")({ course_id }) .then(({ course:{invite_code=""}}) => { this.setData({invite_code}); }).catch(app.showError); }else fail = true; }else fail = true; }else fail= true; }else fail = true; if(fail) wx.showToast({ title:"识别失败",icon:"none" }) } }) }, onCancel() { this.setData({ show: false, invite_code:'', professor:"", student:"", assistant_professor:"" }); }, onTapButton({detail:{index}}){ if(index==0) this.onCancel(); else if(index==1) this.join_course(); }, update_invite_code: function ({ detail: { value } }) { this.setData({ invite_code: value }); }, update_identities: function ({ detail: { value } }) { var data = {assistant_professor:"", professor:"",student:""} for(var identity of value) data[identity] = 1 this.setData(data) }, join_course () { if(this.disabled) return; let { invite_code="", assistant_professor="", professor,student=""} = this.data; this.disabled = true; app.api("courses.apply_to_join_course")({ invite_code, assistant_professor, professor, student,complete: res=>{this.disabled=false}}) .then(res => { app.showMsg(res,800); wx.reportAnalytics('join_course', {}); if (res.course_id&&this.data.auto_navigate&&res.message.indexOf("助教申请")==-1){ setTimeout(()=>{ app[this.data.opentype]({ url: `{course}?course_id=${res.course_id}` }); },640); } this.triggerEvent("success"); this.onCancel(); }) .catch(e=>{ if(e.code==402){ //一般是信息没完善 if(this.checkProfile()){ //信息已完善还是出错 wx.showToast({ title: '发生未知错误',icon:"none"}) global.realTimeLog.error("join course", e); } } else app.showError(e); }) } } })