mirror of
https://github.com/zwpro/coupons.git
synced 2025-12-10 16:52:48 +00:00
commit
13206af4b5
4
App.vue
4
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',
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@ -1,3 +1,10 @@
|
||||
### 更新日志 12.26
|
||||
|
||||
新增订阅功能
|
||||
|
||||
修改 cloudfunctions-aliyun/common/utils/index.js 中的 appid和secret参数,上传代码到云服务器即可
|
||||
|
||||
|
||||
### 更新日志 11-30
|
||||
新增uniCloud云开发,小程序数据可通过api更改。
|
||||
(如只需要数据写死在前端,可切换到静态数据分支 [no-api](https://github.com/zwpro/coupons/tree/no-api))
|
||||
|
||||
@ -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:
|
||||
|
||||
}
|
||||
|
||||
21
cloudfunctions-aliyun/api/models/openid.js
Normal file
21
cloudfunctions-aliyun/api/models/openid.js
Normal file
@ -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;
|
||||
@ -10,6 +10,7 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"response": "file:../common/response"
|
||||
"response": "file:../common/response",
|
||||
"utils": "file:../common/utils"
|
||||
}
|
||||
}
|
||||
|
||||
2
cloudfunctions-aliyun/common/utils/index.js
Normal file
2
cloudfunctions-aliyun/common/utils/index.js
Normal file
@ -0,0 +1,2 @@
|
||||
exports.APPID = 'wx9472d5ad54e879ed'; //这里是我的appid,需要改成你自己的
|
||||
exports.SECREAT = '7fefd4********65778a'; //密钥也要改成你自己的
|
||||
12
cloudfunctions-aliyun/common/utils/package.json
Normal file
12
cloudfunctions-aliyun/common/utils/package.json
Normal file
@ -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"
|
||||
}
|
||||
4
cloudfunctions-aliyun/send/config.json
Normal file
4
cloudfunctions-aliyun/send/config.json
Normal file
@ -0,0 +1,4 @@
|
||||
// 阿里云
|
||||
[
|
||||
"cron:0 0 11 * * *"
|
||||
]
|
||||
89
cloudfunctions-aliyun/send/index.js
Normal file
89
cloudfunctions-aliyun/send/index.js
Normal file
@ -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,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
};
|
||||
11
cloudfunctions-aliyun/send/package-lock.json
generated
Normal file
11
cloudfunctions-aliyun/send/package-lock.json
generated
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "send",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"utils": {
|
||||
"version": "file:../common/utils"
|
||||
}
|
||||
}
|
||||
}
|
||||
14
cloudfunctions-aliyun/send/package.json
Normal file
14
cloudfunctions-aliyun/send/package.json
Normal file
@ -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"
|
||||
}
|
||||
}
|
||||
44
cloudfunctions-aliyun/subscribe/index.js
Normal file
44
cloudfunctions-aliyun/subscribe/index.js
Normal file
@ -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('未入传参数!');
|
||||
}
|
||||
};
|
||||
11
cloudfunctions-aliyun/subscribe/package-lock.json
generated
Normal file
11
cloudfunctions-aliyun/subscribe/package-lock.json
generated
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "subscribe",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"response": {
|
||||
"version": "file:../common/response"
|
||||
}
|
||||
}
|
||||
}
|
||||
14
cloudfunctions-aliyun/subscribe/package.json
Normal file
14
cloudfunctions-aliyun/subscribe/package.json
Normal file
@ -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"
|
||||
}
|
||||
}
|
||||
@ -44,7 +44,8 @@
|
||||
"mp-weixin" : {
|
||||
"appid" : "",
|
||||
"setting" : {
|
||||
"urlCheck" : true
|
||||
"urlCheck" : true,
|
||||
"minified" : true
|
||||
}
|
||||
},
|
||||
"h5" : {
|
||||
|
||||
@ -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 = []
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user