diff --git a/.gitignore b/.gitignore index 4a3296c..f31b6e4 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ yarn-error.log* *.ntvs* *.njsproj *.sln +manifest.json \ No newline at end of file diff --git a/App.vue b/App.vue index 2c467e2..a446578 100644 --- a/App.vue +++ b/App.vue @@ -15,7 +15,10 @@ export default { openid: 'https://88d58ce0-c72d-4ad5-9ad5-5196f032ef71.bspapp.com/http/api/openid', }, subscribe: 'https://88d58ce0-c72d-4ad5-9ad5-5196f032ef71.bspapp.com/http/subscribe', - } + openid: '', + }, + + }; diff --git a/cloudfunctions-aliyun/api/models/home.js b/cloudfunctions-aliyun/api/models/home.js index 4736505..b3c7dd9 100644 --- a/cloudfunctions-aliyun/api/models/home.js +++ b/cloudfunctions-aliyun/api/models/home.js @@ -6,7 +6,7 @@ var home = { return tabs }, coupons: () => { - let coupons = db.collection('coupon').get(); + let coupons = db.collection('coupon').orderBy("sort", "desc").get(); return coupons }, } diff --git a/cloudfunctions-aliyun/db_init.json b/cloudfunctions-aliyun/db_init.json index 49557d3..db71261 100644 --- a/cloudfunctions-aliyun/db_init.json +++ b/cloudfunctions-aliyun/db_init.json @@ -1,88 +1,102 @@ -// 在本文件中可配置云数据库初始化,数据格式见:https://uniapp.dcloud.io/uniCloud/cf-database?id=db_init - -// 编写完毕后对本文件点右键,可按配置规则创建表和添加数据 - { - "coupon": { // 集合(表名) - "data": [ // 数据 - { - "name": "饿了么红包", - "icon": "/static/coupon/ele.png", - "bannerPic": "/static/coupon/ele_banner.png", - "url": "https://s.click.ele.me/frZOjvu", - "type": 1, - "tabId": 1, - "minapp": { - "appid": "wxece3a9a4c82f58c9", - "path": "pages/sharePid/web/index?scene=https://s.click.ele.me/wR9ecuu" - } - }, - { - "name": "美团外卖红包", - "icon": "/static/coupon/meituan.png", - "bannerPic": "/static/coupon/meituan_banner.png", - "url": "https://runion.meituan.com/url?key=cd23768d09c339d1641b2738df39aa67&url=https%3A%2F%2Fi.meituan.com%2Fawp%2Fhfe%2Fblock%2Fa945391288b790d558b7%2F78716%2Findex.html%3Fappkey%3Dcd23768d09c339d1641b2738df39aa67%3Ajuhe&sid=juhe", - "type": 1, - "tabId": 2, - "minapp": { - "appid": "wxde8ac0a21135c07d", - "path": "/index/pages/h5/h5?weburl=https%3A%2F%2Frunion.meituan.com%2Furl%3Fkey%3D591ec05930c57331c1212b936e6785c1%26url%3Dhttps%253A%252F%252Fi.meituan.com%252Fawp%252Fhfe%252Fblock%252Fa13b87919a9ace9cfab4%252F89400%252Findex.html%253Fappkey%253D591ec05930c57331c1212b936e6785c1%253A000001%26sid%3D000001&lch=cps:waimai:5:591ec05930c57331c1212b936e6785c1:000001&f_token=1&f_userId=1" - } - }, - { - "name": "三只松鼠大礼包", - "icon": "/static/coupon/jd.png", - "bannerPic": "/static/coupon/sanzhisongshu.png", - "url":"https://u.jd.com/tFDejq", - "type": 0, - "tabId": 3, - "minapp": { - "appid": "wx91d27dbf599dff74", - "path": "pages/proxy/union/union" - } - } - ], - "schema": { - "bsonType": "object", - "permission": { - ".read": true, - ".create": false, - ".update": false, - ".delete": false - } - } - }, - "tab": { // 集合(表名) - "data": [ // 数据 - { - "icon": "/static/all.png", - "text": "全部", - "tabId": 0 - }, - { - "icon": "/static/ele.png", - "text": "饿了么", - "tabId": 1 - }, - { - "icon": "/static/meituan.png", - "text": "美团", - "tabId": 2 - }, - { - "icon": "/static/jd.png", - "text": "京东", - "tabId": 3 - } - ], - "schema": { - "bsonType": "object", - "permission": { - ".read": true, - ".create": false, - ".update": false, - ".delete": false - } - } - } -} + "coupon": { + "data": [ + { + "_id": { + "$oid": "5fe62b3eee98e30001cb59d0" + }, + "bannerPic": "/static/coupon/meituan_banner.png", + "icon": "/static/coupon/meituan.png", + "minapp": { + "appid": "wxde8ac0a21135c07d", + "path": "/index/pages/h5/h5?weburl=https%3A%2F%2Frunion.meituan.com%2Furl%3Fkey%3D591ec05930c57331c1212b936e6785c1%26url%3Dhttps%253A%252F%252Fi.meituan.com%252Fawp%252Fhfe%252Fblock%252Fa13b87919a9ace9cfab4%252F89400%252Findex.html%253Fappkey%253D591ec05930c57331c1212b936e6785c1%253A000001%26sid%3D000001&lch=cps:waimai:5:591ec05930c57331c1212b936e6785c1:000001&f_token=1&f_userId=1" + }, + "name": "美团外卖红包", + "tabId": 2, + "type": 1, + "url": "https://runion.meituan.com/url?key=cd23768d09c339d1641b2738df39aa67&url=https%3A%2F%2Fi.meituan.com%2Fawp%2Fhfe%2Fblock%2Fa945391288b790d558b7%2F78716%2Findex.html%3Fappkey%3Dcd23768d09c339d1641b2738df39aa67%3Ajuhe&sid=juhe", + "sort": 100 + }, + { + "_id": { + "$oid": "5fe62b3eee98e30001cb59cf" + }, + "bannerPic": "/static/coupon/ele_banner.png", + "icon": "/static/coupon/ele.png", + "minapp": { + "appid": "wxece3a9a4c82f58c9", + "path": "pages/sharePid/web/index?scene=https://s.click.ele.me/wR9ecuu" + }, + "name": "饿了么红包", + "tabId": 1, + "type": 1, + "url": "https://s.click.ele.me/frZOjvu", + "sort": 99 + }, + { + "_id": { + "$oid": "5fe62b3eee98e30001cb59d1" + }, + "bannerPic": "/static/coupon/sanzhisongshu.png", + "icon": "/static/coupon/jd.png", + "minapp": { + "appid": "wx91d27dbf599dff74", + "path": "pages/proxy/union/union" + }, + "name": "三只松鼠大礼包", + "tabId": 3, + "type": 0, + "url": "https://u.jd.com/tFDejq", + "sort": 110 + } + ], + "schema": { + "bsonType": "object", + "permission": { + ".create": false, + ".delete": false, + ".read": true, + ".update": false + } + } + }, + "messages": { + "data": [ + { + "_id": { + "$oid": "5fe63c00646351000153d6a8" + }, + "touser": "o8Pqo5Ac9_ntJ7cOdhYVcF5QaAVg", + "page": "pages/index/index", + "data": "外卖领券提醒, 快来领优惠券啦!", + "templateId": "hLV31-w38lq0yq8p6GEQUtBU7brtMrCFmaCCyxbU4xI", + "subscribeDate": "2020-12-26", + "sendDate": "2020-12-27", + "send": true + }, + { + "_id": { + "$oid": "5fe8a6c9fb0f85000120f7ee" + }, + "touser": "o8Pqo5B3v9GtB3lCx0EujzSUs-v4", + "page": "pages/index/index", + "data": "外卖领券提醒, 快来领优惠券啦!", + "templateId": "hLV31-w38lq0yq8p6GEQUtBU7brtMrCFmaCCyxbU4xI", + "subscribeDate": "2020-12-28", + "sendDate": "2020-12-27", + "send": true + } + ] + }, + "tab": { + "schema": { + "bsonType": "object", + "permission": { + ".create": false, + ".delete": false, + ".read": true, + ".update": false + } + } + } +} \ No newline at end of file diff --git a/cloudfunctions-aliyun/send/index.js b/cloudfunctions-aliyun/send/index.js index e4991dd..c1d7aae 100644 --- a/cloudfunctions-aliyun/send/index.js +++ b/cloudfunctions-aliyun/send/index.js @@ -35,55 +35,65 @@ exports.main = async (event, context) => { // 从云开发数据库中查询等待发送的消息列表 const messages = await db .collection('messages') - // 查询条件这里做了简化,只查找了状态为未发送的消息 - // 在真正的生产环境,可以根据开课日期等条件筛选应该发送哪些消息 .where({ send: false, }) .get(); const now_date = getFormatDate(0); + let sendArr = []; // 循环消息列表 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: "记得领红包哦!", + try{ + if (sendArr.includes(message.touser)){ + console.log('该用户已发送!') + }else + { + sendArr.push(message.touser); + 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, + } }, - 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, - }, - }); - } + 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", + }); + console.log('发送成功'); + console.log(message); + // 发送成功后将消息的状态改为已发送 + return await db.collection('messages') + .doc(message._id) + .update({ + send: true, + sendDate: now_date, + } + ) + } + }catch(e){ + //TODO handle the exception + console.log('发送失败'); + console.log(message); } - ); -}) + }); + return Promise.all(sendPromises) + }; \ No newline at end of file diff --git a/pages/index/index.vue b/pages/index/index.vue index 99f4f60..fd15a97 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -21,6 +21,8 @@