From af8c684037037bc545f811a41cbb814a9b0c3ad9 Mon Sep 17 00:00:00 2001 From: "jiajia.sun" Date: Sat, 26 Dec 2020 04:16:29 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 4 +- README.md | 7 ++ cloudfunctions-aliyun/api/index.js | 6 ++ cloudfunctions-aliyun/api/models/openid.js | 21 +++++ cloudfunctions-aliyun/api/package.json | 3 +- cloudfunctions-aliyun/common/utils/index.js | 2 + .../common/utils/package.json | 12 +++ cloudfunctions-aliyun/send/config.json | 4 + cloudfunctions-aliyun/send/index.js | 89 +++++++++++++++++++ cloudfunctions-aliyun/send/package-lock.json | 11 +++ cloudfunctions-aliyun/send/package.json | 14 +++ cloudfunctions-aliyun/subscribe/index.js | 44 +++++++++ .../subscribe/package-lock.json | 11 +++ cloudfunctions-aliyun/subscribe/package.json | 14 +++ manifest.json | 7 +- pages/index/index.vue | 88 +++++++++++++++++- 16 files changed, 331 insertions(+), 6 deletions(-) create mode 100644 cloudfunctions-aliyun/api/models/openid.js create mode 100644 cloudfunctions-aliyun/common/utils/index.js create mode 100644 cloudfunctions-aliyun/common/utils/package.json create mode 100644 cloudfunctions-aliyun/send/config.json create mode 100644 cloudfunctions-aliyun/send/index.js create mode 100644 cloudfunctions-aliyun/send/package-lock.json create mode 100644 cloudfunctions-aliyun/send/package.json create mode 100644 cloudfunctions-aliyun/subscribe/index.js create mode 100644 cloudfunctions-aliyun/subscribe/package-lock.json create mode 100644 cloudfunctions-aliyun/subscribe/package.json diff --git a/App.vue b/App.vue index e8a9f2d..2c467e2 100644 --- a/App.vue +++ b/App.vue @@ -11,8 +11,10 @@ export default { }, globalData: { api: { - home: 'https://055cfd20-bfe4-4b9a-be9d-f7c2cac59a57.bspapp.com/http/api/home', + home: 'https://88d58ce0-c72d-4ad5-9ad5-5196f032ef71.bspapp.com/http/api/home', + openid: 'https://88d58ce0-c72d-4ad5-9ad5-5196f032ef71.bspapp.com/http/api/openid', }, + subscribe: 'https://88d58ce0-c72d-4ad5-9ad5-5196f032ef71.bspapp.com/http/subscribe', } }; diff --git a/README.md b/README.md index 0b07b8c..9acf945 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ +### 更新日志 12.26 + +新增订阅功能,位于分支subscribe + +修改 cloudfunctions-aliyun/common/utils/index.js 中的 appid和secret参数,上传代码到云服务器即可 + + ### 更新日志 11-30 新增uniCloud云开发,小程序数据可通过api更改。 (如只需要数据写死在前端,可切换到静态数据分支 [no-api](https://github.com/zwpro/coupons/tree/no-api)) diff --git a/cloudfunctions-aliyun/api/index.js b/cloudfunctions-aliyun/api/index.js index f64353b..2703699 100644 --- a/cloudfunctions-aliyun/api/index.js +++ b/cloudfunctions-aliyun/api/index.js @@ -1,6 +1,7 @@ 'use strict'; const response = require('response') const homeModel = require('./models/home') +const getOpenId = require('./models/openid') exports.main = async (event, context) => { //event为客户端上传的参数 console.log('event : ', event) @@ -15,6 +16,11 @@ exports.main = async (event, context) => { resp.coupons = homeModelCoupons.data return response.success(resp) break; + case '/openid': + var openid = await getOpenId(event.queryStringParameters.jsCode) + resp.openid = openid + return response.success(resp) + break; default: } diff --git a/cloudfunctions-aliyun/api/models/openid.js b/cloudfunctions-aliyun/api/models/openid.js new file mode 100644 index 0000000..db56cad --- /dev/null +++ b/cloudfunctions-aliyun/api/models/openid.js @@ -0,0 +1,21 @@ +'use strict'; +// 后台获取openid +const utils = require('utils') +async function getOpenId(jsCode){ + const appid = utils.APPID; + const secret = utils.SECREAT; + const url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + jsCode + '&grant_type=authorization_code'; + console.log(url) + const sendres = await uniCloud.httpclient.request(url, + { + data: {}, + method: 'GET', + contentType: 'json', + dataType:"json", + } + ); + console.log(sendres.data.openid); + return sendres.data.openid; +}; + +module.exports = getOpenId; \ No newline at end of file diff --git a/cloudfunctions-aliyun/api/package.json b/cloudfunctions-aliyun/api/package.json index 2688678..a5ea3c0 100644 --- a/cloudfunctions-aliyun/api/package.json +++ b/cloudfunctions-aliyun/api/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { - "response": "file:../common/response" + "response": "file:../common/response", + "utils": "file:../common/utils" } } diff --git a/cloudfunctions-aliyun/common/utils/index.js b/cloudfunctions-aliyun/common/utils/index.js new file mode 100644 index 0000000..80fd06b --- /dev/null +++ b/cloudfunctions-aliyun/common/utils/index.js @@ -0,0 +1,2 @@ +exports.APPID = 'wx9472d5ad54e879ed'; //这里是我的appid,需要改成你自己的 +exports.SECREAT = '7fefd4********65778a'; //密钥也要改成你自己的 diff --git a/cloudfunctions-aliyun/common/utils/package.json b/cloudfunctions-aliyun/common/utils/package.json new file mode 100644 index 0000000..61ee279 --- /dev/null +++ b/cloudfunctions-aliyun/common/utils/package.json @@ -0,0 +1,12 @@ +{ + "name": "utils", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/cloudfunctions-aliyun/send/config.json b/cloudfunctions-aliyun/send/config.json new file mode 100644 index 0000000..6ced425 --- /dev/null +++ b/cloudfunctions-aliyun/send/config.json @@ -0,0 +1,4 @@ +// 阿里云 +[ + "cron:0 0 11 * * *" +] \ No newline at end of file diff --git a/cloudfunctions-aliyun/send/index.js b/cloudfunctions-aliyun/send/index.js new file mode 100644 index 0000000..e4991dd --- /dev/null +++ b/cloudfunctions-aliyun/send/index.js @@ -0,0 +1,89 @@ +'use strict'; +const utils = require('utils'); + +function getFormatDate(ms) { + let date = new Date(); + date.setTime(date.getTime() + ms); + const year = date.getFullYear(); + const month = date.getMonth() + 1; + const strDate = date.getDate(); + if (month >= 1 && month <= 9) { + month = '0' + month; + } + if (strDate >= 0 && strDate <= 9) { + strDate = '0' + strDate; + } + const currentdate = year + '-' + month + '-' + strDate; + return currentdate; +}; + + +exports.main = async (event, context) => { + const appid = utils.APPID; + const secret = utils.SECREAT; + const tokenUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appid + '&secret=' + secret; + // uniCloud.httpclient 发起请求 + const res = await uniCloud.httpclient.request(tokenUrl, + { + method: 'GET', + dataType:"json" + }); + //返回数据给客户端 + const access_token = res.data.access_token; + const db = uniCloud.database(); + console.log('access_token:' + access_token); + // 从云开发数据库中查询等待发送的消息列表 + const messages = await db + .collection('messages') + // 查询条件这里做了简化,只查找了状态为未发送的消息 + // 在真正的生产环境,可以根据开课日期等条件筛选应该发送哪些消息 + .where({ + send: false, + }) + .get(); + const now_date = getFormatDate(0); + // 循环消息列表 + const sendPromises = messages.data.map(async message => { + // 发送订阅消息 + const sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' + access_token; + let send_data = { + touser: message.touser, + page: "pages/index/index", + data: { + thing1: { + value: "记得领红包哦!", + }, + thing4: { + value: message.data, + }, + thing5: { + value: now_date, + } + }, + template_id: message.templateId, + }; + console.log(send_data); + // uniCloud.httpclient 发起请求 + const sendres = await uniCloud.httpclient.request(sendUrl, + { + data: send_data, + method: 'POST', + contentType: 'json', + dataType:"json", + }, + function(error, response, body) { + if (!error && response.statusCode == 200) { + console.log(response) // 请求成功的处理逻辑 + // 发送成功后将消息的状态改为已发送 + db.collection('messages') + .doc(message._id) + .update({ + data: { + send: true, + }, + }); + } + } + ); +}) +}; \ No newline at end of file diff --git a/cloudfunctions-aliyun/send/package-lock.json b/cloudfunctions-aliyun/send/package-lock.json new file mode 100644 index 0000000..6f98b7b --- /dev/null +++ b/cloudfunctions-aliyun/send/package-lock.json @@ -0,0 +1,11 @@ +{ + "name": "send", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "utils": { + "version": "file:../common/utils" + } + } +} diff --git a/cloudfunctions-aliyun/send/package.json b/cloudfunctions-aliyun/send/package.json new file mode 100644 index 0000000..8b24970 --- /dev/null +++ b/cloudfunctions-aliyun/send/package.json @@ -0,0 +1,14 @@ +{ + "name": "send", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "utils": "file:../common/utils" + } +} diff --git a/cloudfunctions-aliyun/subscribe/index.js b/cloudfunctions-aliyun/subscribe/index.js new file mode 100644 index 0000000..d5e80b2 --- /dev/null +++ b/cloudfunctions-aliyun/subscribe/index.js @@ -0,0 +1,44 @@ +'use strict'; +const db = uniCloud.database(); +const response = require('response') + +function getFormatDate(ms) { + var date = new Date(); + date.setTime(date.getTime() + ms); + var year = date.getFullYear(); + var month = date.getMonth() + 1; + var strDate = date.getDate(); + if (month >= 1 && month <= 9) { + month = '0' + month; + } + if (strDate >= 0 && strDate <= 9) { + strDate = '0' + strDate; + } + var currentdate = year + '-' + month + '-' + strDate; + return currentdate; +} + +exports.main = async (event, context) => { + + console.log(event); + if (event.queryStringParameters) { + try { + const result = await db.collection('messages').add({ + touser: event.queryStringParameters.openid, // 订阅者的openid + page: 'pages/index/index', // 订阅消息卡片点击后会打开小程序的哪个页面 + data: event.queryStringParameters.data, // 订阅消息的数据 + templateId: event.queryStringParameters.templateId, // 订阅消息模板ID + subscribeDate: getFormatDate(24*60*60*1000), // 创建时间 + sendDate: '', //发送时间 + send: false + }); + return result; + } catch (err) { + console.log(err); + return response.error('订阅失败!'); + } + + }else{ + return response.error('未入传参数!'); + } +}; \ No newline at end of file diff --git a/cloudfunctions-aliyun/subscribe/package-lock.json b/cloudfunctions-aliyun/subscribe/package-lock.json new file mode 100644 index 0000000..ee616a2 --- /dev/null +++ b/cloudfunctions-aliyun/subscribe/package-lock.json @@ -0,0 +1,11 @@ +{ + "name": "subscribe", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "response": { + "version": "file:../common/response" + } + } +} diff --git a/cloudfunctions-aliyun/subscribe/package.json b/cloudfunctions-aliyun/subscribe/package.json new file mode 100644 index 0000000..04014e1 --- /dev/null +++ b/cloudfunctions-aliyun/subscribe/package.json @@ -0,0 +1,14 @@ +{ + "name": "subscribe", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "response": "file:../common/response" + } +} diff --git a/manifest.json b/manifest.json index 5d2e83d..606c3ed 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name" : "外卖专享优惠", - "appid" : "", + "appid" : "__UNI__B7F4003", "description" : "", "versionName" : "1.0.0", "versionCode" : "100", @@ -42,9 +42,10 @@ }, "quickapp" : {}, "mp-weixin" : { - "appid" : "", + "appid" : "wx9472d5ad54e879ed", "setting" : { - "urlCheck" : true + "urlCheck" : true, + "minified" : true } }, "h5" : { diff --git a/pages/index/index.vue b/pages/index/index.vue index 293ae6a..99f4f60 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -27,7 +27,8 @@ export default { current: 0, tabs: [], couponList: [], - coupons: [] + coupons: [], + openid: '' }; }, onLoad(e) { @@ -37,6 +38,7 @@ export default { //#endif //#ifdef MP-WEIXIN let tabId = e.tabId ? parseInt(e.tabId) : 0 + this.onSubscribe(); //#endif for(let i in this.tabs){ if(tabId == this.tabs[i].tabId){ @@ -77,6 +79,90 @@ export default { return messages[Math.floor(Math.random()*messages.length)]; }, methods: { + onSubscribe() { + uni.login({ + success: function(res) { + if (res.code) { + uni.getUserInfo({ + success: function(res) { + console.log('存在code'); + } + }); + uni.request({ + url: getApp().globalData.api.openid, + data:{ + jsCode: res.code + }, + success: (res) => { + console.log(res.data); + const openid = res.data.data.openid; + uni.showModal({ + title: '订阅提示', + content: '点击一下订阅,避免错过一个亿!', + cancelText: '不差钱', + cancelColor: '#BEBEBE', + confirmText: '订阅', + confirmColor: '#007AFF', + success: function (res) { + if (res.confirm) { + console.log('订阅开始') + // 活动开始提醒 模板 + const lessonTmplId = 'hLV31-w38lq0yq8p6GEQUtBU7brtMrCFmaCCyxbU4xI'; + const data = { + data: '外卖领券提醒, 快来领优惠券啦!', + templateId: lessonTmplId, + openid: openid, + }; + console.log(data) + uni.showLoading({ + title: '订阅中...', + }); + setTimeout(function () { + uni.hideLoading(); + }, 15000); + // 调用微信 API 申请发送订阅消息 + wx.requestSubscribeMessage({ + // 传入订阅消息的模板id,模板 id 可在小程序管理后台申请 + tmplIds: [lessonTmplId], + success(res) { + // 申请订阅成功 + if (res.errMsg === 'requestSubscribeMessage:ok') { + uni.request({ + url: getApp().globalData.subscribe, + data: data, + success: (res) => { + wx.showToast({ + title: '订阅完成', + icon: 'success', + duration: 2000, + }); + }, + fail(res) { + console.log(res) + wx.showToast({ + title: '订阅失败', + icon: 'error', + duration: 2000, + }); + } + }); + } + }, + + }); + } else if (res.cancel) { + console.log('用户点击取消'); + } + } + }); + } + }); + } else { + console.log('获取用户登录态失败!' + res.errMsg); + } + } + }); + }, changeTab(index) { console.log('当前选中的项:' + index); this.couponList = [] From b2dd09169493037b212f2ff515b552c95c310467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=96=AF=E5=AD=90?= <593446899@qq.com> Date: Sat, 26 Dec 2020 13:58:52 +0800 Subject: [PATCH 2/3] Update manifest.json --- manifest.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manifest.json b/manifest.json index 606c3ed..4714a6c 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name" : "外卖专享优惠", - "appid" : "__UNI__B7F4003", + "appid" : "", "description" : "", "versionName" : "1.0.0", "versionCode" : "100", @@ -42,7 +42,7 @@ }, "quickapp" : {}, "mp-weixin" : { - "appid" : "wx9472d5ad54e879ed", + "appid" : "", "setting" : { "urlCheck" : true, "minified" : true From d637cc65a23573d9e6b487fd273820a679bd07f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=96=AF=E5=AD=90?= <593446899@qq.com> Date: Sat, 26 Dec 2020 14:00:58 +0800 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9acf945..7766db5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ### 更新日志 12.26 -新增订阅功能,位于分支subscribe +新增订阅功能 修改 cloudfunctions-aliyun/common/utils/index.js 中的 appid和secret参数,上传代码到云服务器即可