You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.8 KiB
74 lines
1.8 KiB
var express = require('express');
|
|
var router = express.Router();
|
|
|
|
// var UserModel = require('../bin/db/models/UserModel');
|
|
|
|
var sha1 = require('sha1');
|
|
var sign = require('../utils/sign')
|
|
|
|
/* GET home page. */
|
|
router.get('/', function(req, res, next) {
|
|
res.render('index', { title: 'Express' });
|
|
});
|
|
|
|
router.post('/reg', function (req, res, next) {
|
|
console.log('>>>>post', req.body)
|
|
|
|
// 接收 post 提交的数据
|
|
let { user, pwd } = req.body
|
|
|
|
// 使用 mongoose 提供的方法,将 数据存储到数据库
|
|
// new UserModel({
|
|
// user,
|
|
// pwd
|
|
// }).save().then(() => {
|
|
// res.send({code: 1, msg: '注册成功'})
|
|
// })
|
|
})
|
|
|
|
// 获取 jsapi config 信息
|
|
router.get('/jsapi', async function (req, res, next) {
|
|
let { url = '' } = req.query
|
|
url = decodeURIComponent(url || '')
|
|
|
|
let config = await sign(url);
|
|
console.log('>>>config', config)
|
|
|
|
res.send(config)
|
|
})
|
|
|
|
router.get('/weixinauth', function (req, res, next) {
|
|
|
|
// signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
|
|
// timestamp 时间戳
|
|
// nonce 随机数
|
|
// echostr 随机字符串
|
|
|
|
let { signature
|
|
, timestamp
|
|
, nonce
|
|
, echostr } = req.query
|
|
|
|
// 和微信端设置的 基本配置/填写服务器配置 token 一样,微信端用来验证签名
|
|
let token = 'huaxianliWeiXinToken'
|
|
|
|
let array = [token, timestamp, nonce]
|
|
array.sort() // 字段排序
|
|
|
|
console.log('>>>weixin-auth array', array)
|
|
|
|
let str = array.join('')
|
|
|
|
let resultStr = sha1(str) // 对字符串进行 sha1 加密
|
|
|
|
if (signature === resultStr) {
|
|
res.set('Content-type', 'text/plain')
|
|
res.send(echostr)
|
|
} else {
|
|
res.send('Error !!!')
|
|
}
|
|
|
|
// res.send('welcome to auth !!!')
|
|
})
|
|
|
|
module.exports = router;
|