Skip to content

Commit

Permalink
Merge pull request zwpro#33 from SunJackson/subscribe
Browse files Browse the repository at this point in the history
添加订阅功能
  • Loading branch information
zwpro committed Dec 26, 2020
2 parents 8eec0c0 + d637cc6 commit 13206af
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 4 deletions.
4 changes: 3 additions & 1 deletion App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
};
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cloudfunctions-aliyun/api/index.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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:

}
Expand Down
21 changes: 21 additions & 0 deletions cloudfunctions-aliyun/api/models/openid.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion cloudfunctions-aliyun/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"response": "file:../common/response"
"response": "file:../common/response",
"utils": "file:../common/utils"
}
}
2 changes: 2 additions & 0 deletions cloudfunctions-aliyun/common/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.APPID = 'wx9472d5ad54e879ed'; //这里是我的appid,需要改成你自己的
exports.SECREAT = '7fefd4********65778a'; //密钥也要改成你自己的
12 changes: 12 additions & 0 deletions cloudfunctions-aliyun/common/utils/package.json
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions cloudfunctions-aliyun/send/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// 阿里云
[
"cron:0 0 11 * * *"
]
89 changes: 89 additions & 0 deletions cloudfunctions-aliyun/send/index.js
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions cloudfunctions-aliyun/send/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions cloudfunctions-aliyun/send/package.json
Original file line number Diff line number Diff line change
@@ -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 changes: 44 additions & 0 deletions cloudfunctions-aliyun/subscribe/index.js
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions cloudfunctions-aliyun/subscribe/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions cloudfunctions-aliyun/subscribe/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"mp-weixin" : {
"appid" : "",
"setting" : {
"urlCheck" : true
"urlCheck" : true,
"minified" : true
}
},
"h5" : {
Expand Down
Loading

0 comments on commit 13206af

Please sign in to comment.