import config from "./config"; import { client } from "./js/client"; wx.cloud.init({ traceUser: true, env: "educoder", success:console.log, fail: console.error }); App({ globalData: { openid:wx.getStorageSync('openid'), isCrawl: false, //是否是爬虫 scene: -1 }, client, openapi(name){ return ({success, fail, complete, ...data}={})=>{ return this.callOpenapi({name, data, success,fail, complete}); } }, cloudapi(name){ return ({success, fail, complete, ...data}={})=>{ return this.callCloudfunction({ name:"cloudapi", data:{name, data}, success, fail, complete }); } }, callOpenapi({name, data, success, fail, complete}){ return this.callCloudfunction({name:"openapi", data:{name, data}, success, fail, complete}); }, cloudfunction(name){ return ({success, fail, complete, ...data}={})=>{ return this.callCloudfunction({name, data, success, fail, complete}); } }, callCloudfunction({name, data, success, fail, complete}){ return new Promise((resolve, reject)=>{ return wx.cloud.callFunction({name, data, success:res=>{ success&&success(res.result); resolve(res.result); complete&&complete(res.result); }, fail:e=>{ fail&&fail(e); reject(e); complete&&complete(e); } }); }); }, reportPageHistory:function({page, route, scene, status, options}={}){ let data = {} page = page||getCurrentPages()[getCurrentPages().length-1]; if(page){ Object.assign(data, { route: page.route, page: page.route, //@desprate scene: this.globalData.scene, status: 200, options: page.options }) } if(route){ data.page = data.route = route; } if(scene) data.scene = scene; if(status) data.status = status; if(options) data.options = options; this.cloudapi("reportPageHistory")(data).catch(e=>{ global.realTimeLog.error("report history error", e); wx.reportMonitor('2', 1); }) }, realTimeLog:global.realTimeLog, api(name, config) { return client.api(name, config) }, callApi(options) { return client.callApi(options) }, user() { return client.user }, syncUser(options) { return client.syncUser(options) }, updateUserInfo(info){return client.updateUserInfo(info)}, onLaunch: function (options) { this.globalData.scene = options.scene; if(options.scene==1129){ this.globalData.isCrawl = true; // this.cloudfunction("login")({mpcrawl: true}); this.api("accounts.login")(global.accountManager.testAccount) .then(res=>{ let account = { ...res, ...global.accountManager.testAccount}; global.accountManager.setCurrentAccount(account); }); /* 爬虫访问时运行这些没有用 wx.reportMonitor('1', 1); global.realTimeLog.info("爬虫访问", options); global.realTimeLog.setFilterMsg("mpcrawl"); */ }else if(!this.globalData.openid){ this.cloudfunction("login")() .then(res=>{ this.globalData.openid = res.openid; wx.setStorage({ data: res.openid, key: 'openid', }) }) } if (options.referrerInfo && options.referrerInfo.appId) { var db = wx.cloud.database(); let { appId, extraData } = options.referrerInfo; let { scene, path } = options; db.collection("referrer_info") .add({ data: { appId, extraData, scene, path, createdAt: db.serverDate() } }) } /** * 系统升级提醒 this.api("users.system_update")().then(res => { if (res.system_update) { let { subject = "升级服务通知", system_score} = res; if(!system_score){ let {end_time} = res; if(end_time){ const {getFormatDatetime} = require('./js/utils'); system_score = "为了给大家提供更优质的体验,平台正在对系统进行升级,期间系统响应会有一定延迟。系统将于"+getFormatDatetime(new Date(end_time))+"恢复正常。敬请谅解"; }else{ system_score = "为了给大家提供更优质的体验,平台正在对系统进行升级,期间系统响应会有一定延迟,敬请谅解"; } } wx.showModal({ title: subject, content: system_score, showCancel: false }) } }).catch(e=>{}); */ if(global.config.saveFlow){ wx.showToast({ title: '省流模式已开启,默认不加载图片',icon:"none",duration:2600 }) } }, onShow() { if (client.user_id && client.user_id != 2) client.getTidingInfo(); }, onPageNotFound(res) { this.realTimeLog.error("page not find!! redirect", res); this.reLaunch({ url: "{main}" }); }, showError(e, duration = 1500) { wx.showToast({ title: e.message, icon: "none", duration }) }, showMsg({message=""}, duration = 1500) { wx.showToast({ title: message, duration }) }, getPageUrl(url, root = "/") { return url.replace(/{(.*)}/, function (match, name) { for (var u of __wxConfig.pages) { if (u.endsWith("/" + name)) return root + u; } for (var u of config.pages) { if (u.endsWith("/" + name)) return root + u; } return match; }); }, reLaunch({ url, success, fail, complete }) { wx.reLaunch({ url: this.getPageUrl(url), success, fail, complete }) }, redirectTo({ url, success, fail, complete }) { wx.redirectTo({ url: this.getPageUrl(url), success, fail, complete }) }, navigateTo({ url, success, fail, complete }) { wx.navigateTo({ url: this.getPageUrl(url), success, fail, complete }) }, checkLogin({showModal=1, content="您需要登陆后才能使用该功能"}={}){ if(client.user.user_id==2){ if(showModal){ wx.showModal({ confirmText:"现在登录", title:"提示", content, success:res=>{ if(res.confirm) this.navigateTo({url:"{account}"}) } }) } return false; }else return true; }, addToFavorites({title, query, imageUrl}={}){ let db = wx.cloud.database(); let data = { type:"addToFavorites", title, imageUrl, query, scene: this.globalData.scene, version: config.version, time: db.serverDate() } db.collection("shareInfo").add({ data }); return { title, query, imageUrl } }, shareTimeline({title, query, imageUrl}={}){ let db = wx.cloud.database(); let data = { type:"shareTimeline", title, imageUrl, query, scene: this.globalData.scene, version: config.version, time: db.serverDate() } db.collection("shareInfo").add({ data }); return { title, query, imageUrl } }, shareApp({ imageUrl, path, title="EduCoder教学"}={}) { let db = wx.cloud.database(); let shareInfo = { title, imageUrl, path } if (config.envVersion == 'release'&&!this.globalData.isCrawl) { if (!path) { let pages = getCurrentPages(); let page = pages[pages.length - 1]; let options = page.options; let route = page.route; let query = Object.keys(options).map(i => `${i}=${options[i]}`).join("&"); path = "/" + route + (query ? ('?' + query) : ''); } let data = { type:"shareAppMessage", title, imageUrl, path, scene: this.globalData.scene, version: config.version, time: db.serverDate() } console.log("upload", data); db.collection("shareInfo").add({ data }); } return shareInfo; } }); // 兼容Promise.finally if(!Promise.prototype.finally){ Promise.prototype.finally = function (callback) { let P =this.constructor; return this.then( value => P.resolve(callback()).then(() => value), reason => P.resolve(callback()).then(() => {throw reason }) ); }; } /* function getCurrentPath() { let pages = getCurrentPages(); return pages[pages.length - 1].route; } function toAbsPath(current, path) { if (path.startsWith("/")) current = "."; else current = current + "/../"; path = current + path; while (path.indexOf("..") != -1) { path = path.replace(/[^\/]*\/\.\.\//g, "") } while (path.indexOf(".") != -1) { path = path.replace(/\.\//, ""); } return path; } */