forked from zwpro/coupons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zwpro#33 from SunJackson/subscribe
添加订阅功能
- Loading branch information
Showing
16 changed files
with
329 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exports.APPID = 'wx9472d5ad54e879ed'; //这里是我的appid,需要改成你自己的 | ||
exports.SECREAT = '7fefd4********65778a'; //密钥也要改成你自己的 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// 阿里云 | ||
[ | ||
"cron:0 0 11 * * *" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
} | ||
} | ||
); | ||
}) | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('未入传参数!'); | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.