mirror of
https://github.com/zwpro/coupons.git
synced 2025-12-10 16:52:48 +00:00
订阅功能最新测试通过,添加首页排序功能,优化缓存openid,优化订阅取消按钮
This commit is contained in:
parent
d637cc65a2
commit
ac15613d82
1
.gitignore
vendored
1
.gitignore
vendored
@ -13,3 +13,4 @@ yarn-error.log*
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
manifest.json
|
||||
5
App.vue
5
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: '',
|
||||
},
|
||||
|
||||
|
||||
|
||||
};
|
||||
</script>
|
||||
|
||||
@ -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
|
||||
},
|
||||
}
|
||||
|
||||
@ -35,16 +35,21 @@ 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 => {
|
||||
// 发送订阅消息
|
||||
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,
|
||||
@ -63,6 +68,7 @@ exports.main = async (event, context) => {
|
||||
template_id: message.templateId,
|
||||
};
|
||||
console.log(send_data);
|
||||
|
||||
// uniCloud.httpclient 发起请求
|
||||
const sendres = await uniCloud.httpclient.request(sendUrl,
|
||||
{
|
||||
@ -70,20 +76,24 @@ exports.main = async (event, context) => {
|
||||
method: 'POST',
|
||||
contentType: 'json',
|
||||
dataType:"json",
|
||||
},
|
||||
function(error, response, body) {
|
||||
if (!error && response.statusCode == 200) {
|
||||
console.log(response) // 请求成功的处理逻辑
|
||||
});
|
||||
console.log('发送成功');
|
||||
console.log(message);
|
||||
// 发送成功后将消息的状态改为已发送
|
||||
db.collection('messages')
|
||||
return await db.collection('messages')
|
||||
.doc(message._id)
|
||||
.update({
|
||||
data: {
|
||||
send: true,
|
||||
},
|
||||
sendDate: now_date,
|
||||
}
|
||||
)
|
||||
}
|
||||
}catch(e){
|
||||
//TODO handle the exception
|
||||
console.log('发送失败');
|
||||
console.log(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
return Promise.all(sendPromises)
|
||||
|
||||
};
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name" : "外卖专享优惠",
|
||||
"appid" : "",
|
||||
"name" : "惠吃会喝",
|
||||
"appid" : "__UNI__B7F4003",
|
||||
"description" : "",
|
||||
"versionName" : "1.0.0",
|
||||
"versionCode" : "100",
|
||||
@ -42,7 +42,7 @@
|
||||
},
|
||||
"quickapp" : {},
|
||||
"mp-weixin" : {
|
||||
"appid" : "",
|
||||
"appid" : "wx9472d5ad54e879ed",
|
||||
"setting" : {
|
||||
"urlCheck" : true,
|
||||
"minified" : true
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
}],
|
||||
"globalStyle": {
|
||||
"navigationBarTextStyle": "white",
|
||||
"navigationBarTitleText": "外卖专享优惠",
|
||||
"navigationBarTitleText": "惠吃会喝",
|
||||
"navigationBarBackgroundColor": "#007AFF",
|
||||
"backgroundColor": "#FFFFFF",
|
||||
"titleView": false,
|
||||
|
||||
@ -21,6 +21,8 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@ -38,7 +40,7 @@ export default {
|
||||
//#endif
|
||||
//#ifdef MP-WEIXIN
|
||||
let tabId = e.tabId ? parseInt(e.tabId) : 0
|
||||
this.onSubscribe();
|
||||
this.onSubscribe()
|
||||
//#endif
|
||||
for(let i in this.tabs){
|
||||
if(tabId == this.tabs[i].tabId){
|
||||
@ -80,22 +82,11 @@ export default {
|
||||
},
|
||||
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;
|
||||
try {
|
||||
const openid = uni.getStorageSync('openid');
|
||||
if (openid) {
|
||||
console.log('获取缓存openid');
|
||||
console.log(openid);
|
||||
uni.showModal({
|
||||
title: '订阅提示',
|
||||
content: '点击一下订阅,避免错过一个亿!',
|
||||
@ -119,7 +110,93 @@ export default {
|
||||
});
|
||||
setTimeout(function () {
|
||||
uni.hideLoading();
|
||||
}, 15000);
|
||||
}, 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: '订阅完成',
|
||||
image: '../../static/error.png',
|
||||
duration: 2000,
|
||||
});
|
||||
},
|
||||
fail(res) {
|
||||
console.log(res)
|
||||
wx.showToast({
|
||||
title: '订阅失败',
|
||||
icon: 'error',
|
||||
duration: 2000,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
});
|
||||
} else if (res.cancel) {
|
||||
console.log('用户点击取消');
|
||||
wx.showToast({
|
||||
title: '订阅失败',
|
||||
image: '../../static/error.png',
|
||||
duration: 2000,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}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 可在小程序管理后台申请
|
||||
@ -141,7 +218,7 @@ export default {
|
||||
console.log(res)
|
||||
wx.showToast({
|
||||
title: '订阅失败',
|
||||
icon: 'error',
|
||||
image: '../../static/error.png',
|
||||
duration: 2000,
|
||||
});
|
||||
}
|
||||
@ -152,6 +229,11 @@ export default {
|
||||
});
|
||||
} else if (res.cancel) {
|
||||
console.log('用户点击取消');
|
||||
wx.showToast({
|
||||
title: '订阅失败',
|
||||
image: '../../static/error.png',
|
||||
duration: 2000,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -162,6 +244,11 @@ export default {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('获取openid失败');
|
||||
}
|
||||
|
||||
},
|
||||
changeTab(index) {
|
||||
console.log('当前选中的项:' + index);
|
||||
|
||||
BIN
static/error.png
Normal file
BIN
static/error.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.0 KiB |
Loading…
x
Reference in New Issue
Block a user