const app = getApp(); import {triggerApi, Session} from "../../../js/api" const session = new Session("testAccount"); // to trigger api using a new session function api(name){ return function({success, fail, complete, ...data}={}){ return triggerApi({ session, name, data, success, fail, complete }) } } Page({ data: { github_url:"https://github.com/educoder-weapp/educoder_weapp.git", gitee_url:"https://gitee.com/educoder_weapp/educoder_weapp.git", identifier:"mkgya7vu", file_content:"", routes:[], base:"https://forge.educoder.net/projects/6295/repository/revisions/master/raw", eduImgDir: global.config.eduImgDir }, onShareAppMessage(){ return app.shareApp({ title:"EduCoder教学-版本库" }) }, api(name){ return ({success, fail, complete, ...data}={})=>{ return api(name)({success, fail, complete, identifier:this.data.identifier, ...data}) } }, copyGitUrl(){ wx.setClipboardData({ data: this.data.gitee_url }) }, navTo({name, type}){ console.log({name, type}) let routes = JSON.parse(JSON.stringify(this.data.routes)); routes.push({name, type}); let api_name = type=='tree'?"repository":"file_content"; let path = this.getCurrentPath(routes); if(type=='blob'){ let match = name.match(/.*(\.[^\.]*$)/) if(match&&[".jpg", ".jpeg", ".png", ".bmp", ".gif", ".svg"].indexOf(match[1])!=-1){ let content = `![${name}](${this.data.base}/${path})`; this.setData({content, routes}) return; } } this.api("shixuns."+api_name)({path}) .then(res=>{ if(type!='tree'){ if(!name.endsWith(".md")){ res.content = "```\n"+res.content+"\n```" } wx.showLoading({ title: '加载中' }) } console.log(res); this.setData({...res, routes},wx.hideLoading); }).catch(e=>{ app.showError(e); }) }, getCurrentPath(routes){ routes = routes?routes:this.data.routes; return routes.map(i=>i.name).join("/") }, navBack({level, delta}){ if(delta) level = this.data.routes.length - delta; let routes = this.data.routes.slice(0, level); if(level>0&&routes[routes.length-1].type!="tree")//点击的是当前文件的路径,不执行 return; let api_name = "repository"; let path = this.getCurrentPath(routes); this.api("shixuns."+api_name)({path}) .then(res=>{ this.setData({...res, routes, content:""}); }).catch(e=>{ app.showError(e); }) }, onTapNavBack(){ this.navBack({delta:1}) }, onTapFile(e){ console.log(e); let {currentTarget:{dataset:{name, type}}} = e; this.navTo({name, type}); }, onTapRoute(e){ let {currentTarget:{dataset:{level}}} = e; this.navBack({level}); }, onLoad(){ wx.showLoading({title:"加载中"}); app.cloudapi("reportPageHistory")({ page: this.route, options: this.options, status: 200, scene: app.globalData.scene }) let testAccount = global.accountManager.testAccount; api("accounts.login")(testAccount).then(res=>{ this.api("shixuns.repository")().then(res=>{ console.log(res); this.setData(res); }).finally(()=>{ wx.hideLoading(); }) }).catch(e=>{ wx.showToast({ title: '获取失败',icon:"none" }); wx.hideLoading(); }) }, })