From 0320a78d18daab80ad3cc4c79284aef6366bc2f1 Mon Sep 17 00:00:00 2001 From: educoder_weapp Date: Thu, 14 May 2020 18:04:44 +0800 Subject: [PATCH] =?UTF-8?q?A=20=E9=A1=B5=E9=9D=A2=E8=B7=9F=E8=B8=AA?= =?UTF-8?q?=E5=B9=B6=E6=8F=90=E4=BA=A4=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog.md | 5 ++ cloudfunctions/cloudapi/index.js | 21 ++++- cloudfunctions/login/index.js | 2 +- cloudfunctions/openapi/index.js | 8 +- cloudfunctions/trigger/config.json | 15 ++++ cloudfunctions/trigger/index.js | 84 +++++++++++++++++++ cloudfunctions/trigger/package.json | 14 ++++ miniprogram/app.js | 45 +++++++--- .../components/require-login/require-login.js | 5 +- miniprogram/config.js | 18 ++-- miniprogram/js/utils.js | 3 +- miniprogram/markdown/path/path/path.js | 19 ++++- miniprogram/markdown/shixun/shixun/shixun.js | 18 +++- miniprogram/sitemap.json | 4 + project.config.json | 10 ++- 15 files changed, 229 insertions(+), 42 deletions(-) create mode 100644 cloudfunctions/trigger/config.json create mode 100644 cloudfunctions/trigger/index.js create mode 100644 cloudfunctions/trigger/package.json diff --git a/changelog.md b/changelog.md index a5d922b..10bbf29 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,8 @@ +## v0.16.6 + * A 支持选用微信头像 + * F 兼容Promise.finally + * F 爬虫访问处理 + ## v0.16.5 * A 头像安全检查 * U 信息安全检查速度 diff --git a/cloudfunctions/cloudapi/index.js b/cloudfunctions/cloudapi/index.js index 128b7ca..0d4fa9f 100644 --- a/cloudfunctions/cloudapi/index.js +++ b/cloudfunctions/cloudapi/index.js @@ -5,11 +5,30 @@ cloud.init() exports.main = async (event, context) => { let {name, data} = event; - const wxContext = cloud.getWXContext() + const wxContext = cloud.getWXContext(); + let db = cloud.database(); switch (name) { case 'getOpenData': { return cloud.getOpenData(data); } + case "login":{ + return { + openid: wxContext.OPENID, + appid: wxContext.APPID, + unionid: wxContext.UNIONID, + env: wxContext.ENV, + } + } + case "reportPageHistory":{ + db.collection("pageHistory").add({ + data:{ + ...data, + time: new Date(), + timestamp: Date.now(), + openid: wxContext.OPENID + } + }); + } default: { return } diff --git a/cloudfunctions/login/index.js b/cloudfunctions/login/index.js index 6278729..43f1594 100644 --- a/cloudfunctions/login/index.js +++ b/cloudfunctions/login/index.js @@ -12,7 +12,7 @@ exports.main = (event, context) => { const wxContext = cloud.getWXContext() - + console.log("call api login", event); return { openid: wxContext.OPENID, appid: wxContext.APPID, diff --git a/cloudfunctions/openapi/index.js b/cloudfunctions/openapi/index.js index 5d7d1d6..113fdd8 100644 --- a/cloudfunctions/openapi/index.js +++ b/cloudfunctions/openapi/index.js @@ -7,7 +7,7 @@ cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV}) exports.main = async (event, context) => { console.log("调用云函数openapi, 参数event:", event); let {name, data} = event; - switch (event.name||event.action) { + switch (name||event.action) { case "wxacode.getUnlimited": case "getWXACodeUnlimited":{ return getWXACodeUnlimited(data) @@ -48,12 +48,12 @@ exports.main = async (event, context) => { return e; } } - case 'getOpenData': { - return cloud.getOpenData(data); - } case "search.siteSearch":{ return cloud.openapi.search.siteSearch(data); } + case "search.submitPages":{ + return cloud.openapi.search.submitPages(data); + } default: { return } diff --git a/cloudfunctions/trigger/config.json b/cloudfunctions/trigger/config.json new file mode 100644 index 0000000..bcfb085 --- /dev/null +++ b/cloudfunctions/trigger/config.json @@ -0,0 +1,15 @@ +{ + "permissions": { + "openapi": [ + "search.submitPages" + ] + }, + "triggers": [ + { + + "name": "submitPages", + "type": "timer", + "config": "0 0 2 * * * *" + } + ] +} \ No newline at end of file diff --git a/cloudfunctions/trigger/index.js b/cloudfunctions/trigger/index.js new file mode 100644 index 0000000..75cd3e6 --- /dev/null +++ b/cloudfunctions/trigger/index.js @@ -0,0 +1,84 @@ + +const cloud = require('wx-server-sdk') + +cloud.init() + + +exports.main = async (event, context) => { + const wxContext = cloud.getWXContext() + console.log(event); + let {TriggerName, Time} = event; + switch(TriggerName){ + case "submitPages":{ + return submitPages({Time}); + } + } +} + +async function submitPages({Time}){ + console.log("start submitPages"); + const db = cloud.database(); + const _ = db.command; + const pageHistoryCollection = db.collection("pageHistory"); + + let shixunPath = "markdown/shixun/shixun/shixun"; + let pathPath = "markdown/path/path/path"; + let timeEnd = new Date(Time); + let timeStart = new Date(timeEnd.getTime()-24*60*60*1000); + + let shixunResult = await pageHistoryCollection.where({ + page: shixunPath, + status: 200, + time: _.lt(timeEnd).and(_.gte(timeStart)) + }).get(); + + let pathResult = await pageHistoryCollection.where({ + page: pathPath, + status:200, + time:_.lt(timeEnd).and(_.gte(timeStart)) + }).get(); + + let shixunQueries = shixunResult.data.reduce(function(arr, v){ + var query = Object.keys(v.options).map(k=>`${k}=${v.options[k]}`).join("&"); + if(arr.indexOf(query)==-1) + arr.push(query); + return arr + },[]); + + let pathQueries = pathResult.data.reduce(function(arr, v){ + var query = Object.keys(v.options).map(k=>`${k}=${v.options[k]}`).join("&"); + if(arr.indexOf(query)==-1) + arr.push(query); + return arr + },[]); + + console.info("shixunQueries", shixunQueries.length, shixunQueries); + console.info("pathQueries", pathQueries.length, pathQueries); + + if(shixunQueries.length>0){ + var pages = shixunQueries.map(i=>{ + return { + path: shixunPath, + query: i + } + }); + var res = await cloud.openapi.search.submitPages({pages}); + console.log("submit shixun pages",pages.length, res); + console.info("arguments", pages); + }else{ + console.warn("no shixun history"); + } + if(pathQueries.length>0){ + var pages = pathQueries.map(i=>{ + return { + path: pathPath, + query: i + } + }); + var res = await cloud.openapi.search.submitPages({pages}); + console.log("submit path pages success", pages.length, res); + console.info("arguments", pages); + }else{ + console.warn("no path history"); + } +} \ No newline at end of file diff --git a/cloudfunctions/trigger/package.json b/cloudfunctions/trigger/package.json new file mode 100644 index 0000000..ab42214 --- /dev/null +++ b/cloudfunctions/trigger/package.json @@ -0,0 +1,14 @@ +{ + "name": "trigger", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "wx-server-sdk": "~2.0.2" + } +} \ No newline at end of file diff --git a/miniprogram/app.js b/miniprogram/app.js index 6b21abd..b7a6d7a 100644 --- a/miniprogram/app.js +++ b/miniprogram/app.js @@ -10,7 +10,9 @@ App({ globalData: { versionCode: config.versionCode, debug: config.debug, - openid:wx.getStorageSync('openid') + openid:wx.getStorageSync('openid'), + isCrawl: false, //是否是爬虫 + scene: -1 }, client, openapi(name){ @@ -19,7 +21,7 @@ App({ } }, cloudapi(name){ - return ({success, fail, complete, ...data})=>{ + return ({success, fail, complete, ...data}={})=>{ return this.callCloudfunction({ name:"cloudapi", data:{name, data}, success, fail, complete }); @@ -55,15 +57,29 @@ App({ syncUser(options) { return client.syncUser(options) }, updateUserInfo(info){return client.updateUserInfo(info)}, onLaunch: function (options) { - if(options.scene==1129||options["user-agent"]=="mpcrawler"){ + 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(); @@ -96,16 +112,7 @@ App({ }) } }).catch(e=>{}); - if(!this.globalData.openid){ - this.cloudfunction("login")() - .then(res=>{ - this.globalData.openid = res.openid; - wx.setStorage({ - data: res.openid, - key: 'openid', - }) - }) - } + }, onShow() { @@ -159,6 +166,18 @@ App({ } }); + +// 兼容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(); diff --git a/miniprogram/components/require-login/require-login.js b/miniprogram/components/require-login/require-login.js index bf79128..ea9b1a8 100644 --- a/miniprogram/components/require-login/require-login.js +++ b/miniprogram/components/require-login/require-login.js @@ -1,4 +1,4 @@ -import { deprecate} from "../../js/utils"; + const app = getApp(); Component({ properties: { @@ -29,7 +29,8 @@ Component({ }, methods:{ refresh(){ - return app.syncUser().then(res=>{ + return app.syncUser() + .then(res=>{ if(this.data.user_id!=res.user.user_id){ if(this.data.user_id!=-1) this.triggerEvent("change",{}); diff --git a/miniprogram/config.js b/miniprogram/config.js index b0dec21..9a93971 100644 --- a/miniprogram/config.js +++ b/miniprogram/config.js @@ -1,35 +1,37 @@ const cloudDir = "cloud://educoder.6564-educoder-1300855313/"; -let { miniProgram:{ envVersion, version}={}} = wx.getAccountInfoSync(); +let _version = "0.16.6"; +/** + */ +let { miniProgram:{ envVersion="release", version=_version}={}} = wx.getAccountInfoSync(); +version = version||_version; const developUrl = "https://test-newweb.educoder.net"; const trialUrl = "https://pre-newweb.educoder.net"; const releaseUrl = "https://www.educoder.net"; -let _version = "0.16.5"; var eduUrl = releaseUrl; -/** - */ + export function switchEnv(env) { config.env = env; if (env == "develop") { eduUrl = developUrl; - config.version = "开发环境 " + _version +"(点击切换)"; + config.version = "开发环境 " + version +"(点击切换)"; } else if (env == "trial") { eduUrl = trialUrl; - config.version = "体验版 " + _version; + config.version = "体验版 " + version; } else { eduUrl = releaseUrl; - config.version = version||_version; + config.version = version; } } const config = global.config = { - version:version||_version, + version, envVersion, env:envVersion, get apiRoot(){return eduUrl + "/api/"}, diff --git a/miniprogram/js/utils.js b/miniprogram/js/utils.js index 0827c01..8482ace 100644 --- a/miniprogram/js/utils.js +++ b/miniprogram/js/utils.js @@ -1,4 +1,3 @@ - export function deprecate(name=""){ try{ throw new Error(name + "这个方法或界面被弃用了"); @@ -272,7 +271,7 @@ export function throttle(func, wait, options) { }; export function RealTimeLogManager(){ - const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null; + const log = wx.getRealtimeLogManager&&global.config.envVersion=='release'? wx.getRealtimeLogManager() : null; return { debug(...args) { console.debug.apply(null, arguments); diff --git a/miniprogram/markdown/path/path/path.js b/miniprogram/markdown/path/path/path.js index 22c652c..939bd93 100644 --- a/miniprogram/markdown/path/path/path.js +++ b/miniprogram/markdown/path/path/path.js @@ -74,21 +74,32 @@ Page({ res=>{ if(showLoading) wx.hideLoading() - }); + } + ); + wx.setNavigationBarTitle({ + title:"实践课程-"+res[0].name + }) + app.cloudapi("reportPageHistory")({ + page: this.route, + options: this.options, + status: 200, + scene: app.globalData.scene, + isCrawl: app.globalData.isCrawl + }) + return res; }).catch(e=>{ if(e.code==403) e.message = "您没有权限访问" wx.hideLoading(); app.showError(e); + return e; }) }, - onShareAppMessage: function () { - let {subject} = this.data; return app.shareApp({ - title: subject.name, + title: "[实践课程]"+subject.name, imageUrl: global.config.eduUrl + "/" + subject.cover }) } diff --git a/miniprogram/markdown/shixun/shixun/shixun.js b/miniprogram/markdown/shixun/shixun/shixun.js index 6bbaefa..fd4b724 100644 --- a/miniprogram/markdown/shixun/shixun/shixun.js +++ b/miniprogram/markdown/shixun/shixun/shixun.js @@ -22,7 +22,6 @@ Page({ let api_name = is_collect?"collections.cancel":"collections"; app.api(api_name)({container_type:"Shixun", container_id:id}) .then(res=>{ - ; this.pullShixun({showLoading: 0}); if(is_collect){ res.message = "已取消收藏"; @@ -81,22 +80,35 @@ Page({ if(showLoading) wx.hideLoading() }); + wx.setNavigationBarTitle({ + title: "实训项目-" + shixun.name + }); + return shixun; }, onLoad: function (options) { + console.log(options); let {identifier,cate_type} = options; let current = cateTypes[cate_type]; this.setData({identifier, current}); this.pullChallenges().catch(e=>{ }); - this.pullShixun().catch(e=>{ + this.pullShixun().then(res=>{ + app.cloudapi("reportPageHistory")({ + page: this.route, + options: this.options, + status: 200, + scene: app.globalData.scene, + isCrawl: app.globalData.isCrawl + }) + }).catch(e=>{ app.showError(e); }); }, onShareAppMessage: function () { return app.shareApp({ - title:this.data.shixun.name, + title:"[实训项目]"+this.data.shixun.name, imageUrl: global.config.eduImgDir+"avatars/Shixun/"+this.data.shixun.id }) } diff --git a/miniprogram/sitemap.json b/miniprogram/sitemap.json index a6f5f2c..c47b7f4 100644 --- a/miniprogram/sitemap.json +++ b/miniprogram/sitemap.json @@ -29,6 +29,10 @@ "action":"disallow", "page":"account/pages/accounts/accounts" }, + { + "action":"disallow", + "page":"account/pages/profile/profile" + }, { "action":"disallow", "page":"course/pages/course/course" diff --git a/project.config.json b/project.config.json index 6620a45..04d403d 100644 --- a/project.config.json +++ b/project.config.json @@ -42,6 +42,7 @@ "isGameTourist": false, "simulatorType": "wechat", "simulatorPluginLibVersion": {}, + "cloudfunctionTemplateRoot": "cloudfunctionTemplate", "condition": { "search": { "current": -1, @@ -130,11 +131,11 @@ "scene": 1129 }, { - "id": 16, - "name": "shixun/pages/shixun/shixun", - "pathName": "shixun/pages/shixun/shixun", + "id": 9, + "name": "markdown/shixun/shixun/shixun", + "pathName": "markdown/shixun/shixun/shixun", "query": "identifier=8bu9zmjy", - "scene": null + "scene": 1000 }, { "id": 17, @@ -217,6 +218,7 @@ "id": -1, "name": "setting/pages/image_crop/image_crop", "pathName": "setting/pages/image_crop/image_crop", + "query": "", "scene": null } ]