Merge pull request #35 from SunJackson/subscribe

修复不能给多人发送订阅BUG,添加首页排序功能,优化缓存openid,优化取消订阅显示
This commit is contained in:
小疯子 2020-12-27 23:57:44 +08:00 committed by GitHub
commit 0517d645e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 326 additions and 211 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@ yarn-error.log*
*.ntvs* *.ntvs*
*.njsproj *.njsproj
*.sln *.sln
manifest.json

View File

@ -15,7 +15,10 @@ export default {
openid: 'https://88d58ce0-c72d-4ad5-9ad5-5196f032ef71.bspapp.com/http/api/openid', openid: 'https://88d58ce0-c72d-4ad5-9ad5-5196f032ef71.bspapp.com/http/api/openid',
}, },
subscribe: 'https://88d58ce0-c72d-4ad5-9ad5-5196f032ef71.bspapp.com/http/subscribe', subscribe: 'https://88d58ce0-c72d-4ad5-9ad5-5196f032ef71.bspapp.com/http/subscribe',
} openid: '',
},
}; };
</script> </script>

View File

@ -6,7 +6,7 @@ var home = {
return tabs return tabs
}, },
coupons: () => { coupons: () => {
let coupons = db.collection('coupon').get(); let coupons = db.collection('coupon').orderBy("sort", "desc").get();
return coupons return coupons
}, },
} }

View File

@ -1,88 +1,102 @@
// https://uniapp.dcloud.io/uniCloud/cf-database?id=db_init
//
{ {
"coupon": { // "coupon": {
"data": [ // "data": [
{ {
"name": "饿了么红包", "_id": {
"icon": "/static/coupon/ele.png", "$oid": "5fe62b3eee98e30001cb59d0"
"bannerPic": "/static/coupon/ele_banner.png", },
"url": "https://s.click.ele.me/frZOjvu", "bannerPic": "/static/coupon/meituan_banner.png",
"type": 1, "icon": "/static/coupon/meituan.png",
"tabId": 1, "minapp": {
"minapp": { "appid": "wxde8ac0a21135c07d",
"appid": "wxece3a9a4c82f58c9", "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"
"path": "pages/sharePid/web/index?scene=https://s.click.ele.me/wR9ecuu" },
} "name": "美团外卖红包",
}, "tabId": 2,
{ "type": 1,
"name": "美团外卖红包", "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",
"icon": "/static/coupon/meituan.png", "sort": 100
"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, "_id": {
"tabId": 2, "$oid": "5fe62b3eee98e30001cb59cf"
"minapp": { },
"appid": "wxde8ac0a21135c07d", "bannerPic": "/static/coupon/ele_banner.png",
"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" "icon": "/static/coupon/ele.png",
} "minapp": {
}, "appid": "wxece3a9a4c82f58c9",
{ "path": "pages/sharePid/web/index?scene=https://s.click.ele.me/wR9ecuu"
"name": "三只松鼠大礼包", },
"icon": "/static/coupon/jd.png", "name": "饿了么红包",
"bannerPic": "/static/coupon/sanzhisongshu.png", "tabId": 1,
"url":"https://u.jd.com/tFDejq", "type": 1,
"type": 0, "url": "https://s.click.ele.me/frZOjvu",
"tabId": 3, "sort": 99
"minapp": { },
"appid": "wx91d27dbf599dff74", {
"path": "pages/proxy/union/union" "_id": {
} "$oid": "5fe62b3eee98e30001cb59d1"
} },
], "bannerPic": "/static/coupon/sanzhisongshu.png",
"schema": { "icon": "/static/coupon/jd.png",
"bsonType": "object", "minapp": {
"permission": { "appid": "wx91d27dbf599dff74",
".read": true, "path": "pages/proxy/union/union"
".create": false, },
".update": false, "name": "三只松鼠大礼包",
".delete": false "tabId": 3,
} "type": 0,
} "url": "https://u.jd.com/tFDejq",
}, "sort": 110
"tab": { // }
"data": [ // ],
{ "schema": {
"icon": "/static/all.png", "bsonType": "object",
"text": "全部", "permission": {
"tabId": 0 ".create": false,
}, ".delete": false,
{ ".read": true,
"icon": "/static/ele.png", ".update": false
"text": "饿了么", }
"tabId": 1 }
}, },
{ "messages": {
"icon": "/static/meituan.png", "data": [
"text": "美团", {
"tabId": 2 "_id": {
}, "$oid": "5fe63c00646351000153d6a8"
{ },
"icon": "/static/jd.png", "touser": "o8Pqo5Ac9_ntJ7cOdhYVcF5QaAVg",
"text": "京东", "page": "pages/index/index",
"tabId": 3 "data": "外卖领券提醒, 快来领优惠券啦!",
} "templateId": "hLV31-w38lq0yq8p6GEQUtBU7brtMrCFmaCCyxbU4xI",
], "subscribeDate": "2020-12-26",
"schema": { "sendDate": "2020-12-27",
"bsonType": "object", "send": true
"permission": { },
".read": true, {
".create": false, "_id": {
".update": false, "$oid": "5fe8a6c9fb0f85000120f7ee"
".delete": false },
} "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
}
}
}
}

View File

@ -35,55 +35,65 @@ exports.main = async (event, context) => {
// 从云开发数据库中查询等待发送的消息列表 // 从云开发数据库中查询等待发送的消息列表
const messages = await db const messages = await db
.collection('messages') .collection('messages')
// 查询条件这里做了简化,只查找了状态为未发送的消息
// 在真正的生产环境,可以根据开课日期等条件筛选应该发送哪些消息
.where({ .where({
send: false, send: false,
}) })
.get(); .get();
const now_date = getFormatDate(0); const now_date = getFormatDate(0);
let sendArr = [];
// 循环消息列表 // 循环消息列表
const sendPromises = messages.data.map(async message => { const sendPromises = messages.data.map(async message => {
// 发送订阅消息 // 发送订阅消息
const sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' + access_token; try{
let send_data = { if (sendArr.includes(message.touser)){
touser: message.touser, console.log('该用户已发送!')
page: "pages/index/index", }else
data: { {
thing1: { sendArr.push(message.touser);
value: "记得领红包哦!", 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: { template_id: message.templateId,
value: message.data, };
}, console.log(send_data);
thing5: {
value: now_date, // uniCloud.httpclient 发起请求
} const sendres = await uniCloud.httpclient.request(sendUrl,
}, {
template_id: message.templateId, data: send_data,
}; method: 'POST',
console.log(send_data); contentType: 'json',
// uniCloud.httpclient 发起请求 dataType:"json",
const sendres = await uniCloud.httpclient.request(sendUrl, });
{ console.log('发送成功');
data: send_data, console.log(message);
method: 'POST', // 发送成功后将消息的状态改为已发送
contentType: 'json', return await db.collection('messages')
dataType:"json", .doc(message._id)
}, .update({
function(error, response, body) { send: true,
if (!error && response.statusCode == 200) { sendDate: now_date,
console.log(response) // 请求成功的处理逻辑 }
// 发送成功后将消息的状态改为已发送 )
db.collection('messages') }
.doc(message._id) }catch(e){
.update({ //TODO handle the exception
data: { console.log('发送失败');
send: true, console.log(message);
},
});
}
} }
); });
}) return Promise.all(sendPromises)
}; };

View File

@ -21,6 +21,8 @@
</template> </template>
<script> <script>
export default { export default {
data() { data() {
return { return {
@ -38,7 +40,7 @@ export default {
//#endif //#endif
//#ifdef MP-WEIXIN //#ifdef MP-WEIXIN
let tabId = e.tabId ? parseInt(e.tabId) : 0 let tabId = e.tabId ? parseInt(e.tabId) : 0
this.onSubscribe(); this.onSubscribe()
//#endif //#endif
for(let i in this.tabs){ for(let i in this.tabs){
if(tabId == this.tabs[i].tabId){ if(tabId == this.tabs[i].tabId){
@ -80,88 +82,173 @@ export default {
}, },
methods: { methods: {
onSubscribe() { onSubscribe() {
uni.login({ try {
success: function(res) { const openid = uni.getStorageSync('openid');
if (res.code) { if (openid) {
uni.getUserInfo({ console.log('获取缓存openid');
success: function(res) { console.log(openid);
console.log('存在code'); uni.showModal({
} title: '订阅提示',
}); content: '点击一下订阅,避免错过一个亿!',
uni.request({ cancelText: '不差钱',
url: getApp().globalData.api.openid, cancelColor: '#BEBEBE',
data:{ confirmText: '订阅',
jsCode: res.code confirmColor: '#007AFF',
}, success: function (res) {
success: (res) => { if (res.confirm) {
console.log(res.data); console.log('订阅开始')
const openid = res.data.data.openid; //
uni.showModal({ const lessonTmplId = 'hLV31-w38lq0yq8p6GEQUtBU7brtMrCFmaCCyxbU4xI';
title: '订阅提示', const data = {
content: '点击一下订阅,避免错过一个亿!', data: '外卖领券提醒, 快来领优惠券啦!',
cancelText: '不差钱', templateId: lessonTmplId,
cancelColor: '#BEBEBE', openid: openid,
confirmText: '订阅', };
confirmColor: '#007AFF', console.log(data)
success: function (res) { uni.showLoading({
if (res.confirm) { title: '订阅中...',
console.log('订阅开始') });
// setTimeout(function () {
const lessonTmplId = 'hLV31-w38lq0yq8p6GEQUtBU7brtMrCFmaCCyxbU4xI'; uni.hideLoading();
const data = { }, 5000);
data: '外卖领券提醒, 快来领优惠券啦!', // API
templateId: lessonTmplId, wx.requestSubscribeMessage({
openid: openid, // id id
}; tmplIds: [lessonTmplId],
console.log(data) success(res) {
uni.showLoading({ //
title: '订阅中...', if (res.errMsg === 'requestSubscribeMessage:ok') {
}); uni.request({
setTimeout(function () { url: getApp().globalData.subscribe,
uni.hideLoading(); data: data,
}, 15000); success: (res) => {
// API wx.showToast({
wx.requestSubscribeMessage({ title: '订阅完成',
// id id image: '../../static/error.png',
tmplIds: [lessonTmplId], duration: 2000,
success(res) { });
// },
if (res.errMsg === 'requestSubscribeMessage:ok') { fail(res) {
uni.request({ console.log(res)
url: getApp().globalData.subscribe, wx.showToast({
data: data, title: '订阅失败',
success: (res) => { icon: 'error',
wx.showToast({ duration: 2000,
title: '订阅完成', });
icon: 'success', }
duration: 2000, });
}); }
}, },
fail(res) {
console.log(res) });
wx.showToast({ } else if (res.cancel) {
title: '订阅失败', console.log('用户点击取消');
icon: 'error', wx.showToast({
duration: 2000, title: '订阅失败',
}); image: '../../static/error.png',
} duration: 2000,
});
}
},
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
}); });
} }
}); }
} else { });
console.log('获取用户登录态失败!' + res.errMsg); }else{
} console.log('本地未获取到openid');
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.setStorage({
key:"openid",
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();
}, 5000);
// 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: '订阅失败',
image: '../../static/error.png',
duration: 2000,
});
}
});
}
},
});
} else if (res.cancel) {
console.log('用户点击取消');
wx.showToast({
title: '订阅失败',
image: '../../static/error.png',
duration: 2000,
});
}
}
});
}
});
} else {
console.log('获取用户登录态失败!' + res.errMsg);
}
}
});
} }
}); } catch (e) {
console.log('获取openid失败');
}
}, },
changeTab(index) { changeTab(index) {
console.log('当前选中的项:' + index); console.log('当前选中的项:' + index);

BIN
static/error.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB