Skip to content

Commit

Permalink
Merge pull request #6 from TencentCloudBase/master
Browse files Browse the repository at this point in the history
新增云函数
  • Loading branch information
snail1 authored Feb 18, 2020
2 parents 975a496 + 96f9646 commit 45f89a7
Show file tree
Hide file tree
Showing 15 changed files with 620 additions and 0 deletions.
78 changes: 78 additions & 0 deletions cloudfunctions/checkPassCode/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
const sm2 = require('miniprogram-sm-crypto').sm2;
const uuid = require('node-uuid');
const Redis = require('ioredis');

const redis = new Redis({
port: 6379, // Redis port
host: '10.0.0.10',
family: 4,// 4 (IPv4) or 6 (IPv6)
password: 'tcb12345678',
db: 2,
});
cloud.init({ env: 'cloud-tcb' });

// 云函数入口函数
exports.main = async (event, context) => {

const wxContext = cloud.getWXContext();
const db = cloud.database();

var cacheKey = 'tcbst:admin:token:' + event.token;
var value = await redis.get(cacheKey);
value = JSON.parse(value);

if (value.openid == wxContext.OPENID) {

cacheKey = 'tcbst:admin:priKey:' + event.serial.replace(/-/g, '');
var privateKey = await redis.get(cacheKey);
var cipherMode = 1;
var info = JSON.parse(sm2.doDecrypt(event.passData, privateKey, cipherMode));

var result = await db.collection('tcbst_visit_code').where({
serial: event.serial,
openid: wxContext.OPENID,
code_data: info.codeData,
valid: true
}).get();

if (result.data.length > 0) {

let data = result.data[0];
let timestamp = Math.round(new Date().getTime() / 1000);

/*************增加用户通行记录 */
let body = await db.collection('tcbst_pass_info').add({
data: {
passNo: uuid.v1(),
serial: event.serial,
openid: info.openid,
area_id: data.area_id,
area_area: data.area_area,
org_mame: data.org_name,
unionid: wxContext.UNIONID,
env: info.env,
source: info.source,
info: info.info,
area_place: data.area_place,
valid: true,
create_time: db.serverDate()
}
});

return {
error: null,
requestId: event.requestId,
timestamp: Math.round(new Date().getTime() / 1000)
};

} else {
return { error: 'data get fail' };
};

}else{
return { error: 'token data is error' };
};

}
17 changes: 17 additions & 0 deletions cloudfunctions/checkPassCode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "checkPassCode",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest",
"node-uuid": "latest",
"ioredis": "latest",
"miniprogram-sm-crypto": "latest"
}
}
57 changes: 57 additions & 0 deletions cloudfunctions/getAreaInfo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
const sm2 = require('miniprogram-sm-crypto').sm2;
const Redis = require('ioredis');

const redis = new Redis({
port: 6379, // Redis port
host: '10.0.0.10',
family: 4,// 4 (IPv4) or 6 (IPv6)
password: 'tcb12345678',
db: 2,
});
cloud.init({ env: 'cloud-tcb' });

// 云函数入口函数
exports.main = async (event, context) => {

const wxContext = cloud.getWXContext();
const db = cloud.database();

var cacheKey = 'tcbst:admin:pubKey:' + event.serial.replace(/-/g, '');
var publicKey = await redis.get(cacheKey);

var msg = 'serial=' + event.serial + '&timestamp=' + event.codeTimestamp;
if (event.mateCode != null) {
msg = msg + event.mateCode;
};

if (sm2.doVerifySignature(msg, event.signData, publicKey)){

let result = await db.collection('tcbst_visit_code').where({
serial: event.serial
}).get();

if (result.data.length > 0){

let data = result.data[0];

return {
error: null,
requestId: event.requestId,
codeData: data.code_data,
areaPlace: data.area_place,
areaId: data.area_id,
orgName: data.org_name,
timestamp: Math.round(new Date().getTime() / 1000)
};

}else{
return { error: 'code data is null' };
};

}else{
return {error: 'verifySignature is fail'};
};

}
16 changes: 16 additions & 0 deletions cloudfunctions/getAreaInfo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "getAreaInfo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest",
"ioredis": "latest",
"miniprogram-sm-crypto": "latest"
}
}
43 changes: 43 additions & 0 deletions cloudfunctions/getPassCode/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
const sm2 = require('miniprogram-sm-crypto').sm2;
const Redis = require('ioredis');

const redis = new Redis({
port: 6379, // Redis port
host: '10.0.0.10',
family: 4,// 4 (IPv4) or 6 (IPv6)
password: 'tcb12345678',
db: 2,
});
cloud.init({ env: 'cloud-tcb' });

// 云函数入口函数
exports.main = async (event, context) => {

const wxContext = cloud.getWXContext()

var cacheKey = 'tcbst:admin:pubKey:' + event.serial.replace(/-/g, '');
var publicKey = await redis.get(cacheKey);
var timestamp = Math.round(new Date().getTime() / 1000);

var msg = JSON.stringify({
serial: event.serial,
unionid: wxContext.UNIONID,
env: wxContext.ENV,
source: wxContext.SOURCE,
openid: wxContext.OPENID,
info: event.info,
timestamp: timestamp
});
var cipherMode = 1;
var encryptData = sm2.doEncrypt(msg, publicKey, cipherMode); //加密

return {
error: null,
requestId: event.requestId,
openid: wxContext.OPENID,
passData: encryptData,
serial: event.serial
}
}
16 changes: 16 additions & 0 deletions cloudfunctions/getPassCode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "getPassCode",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest",
"ioredis": "latest",
"miniprogram-sm-crypto": "latest"
}
}
98 changes: 98 additions & 0 deletions cloudfunctions/getVisitCode/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 云函数入口文件
const cloud = require('wx-server-sdk');
const sm2 = require('miniprogram-sm-crypto').sm2;
const Redis = require('ioredis');
const uuid = require('node-uuid');

const redis = new Redis({
port: 6379, // Redis port
host: '10.0.0.10',
family: 4,// 4 (IPv4) or 6 (IPv6)
password: 'tcb12345678',
db: 2,
});
cloud.init({ env: 'cloud-tcb' });

// 云函数入口函数
exports.main = async (event, context) => {

const wxContext = cloud.getWXContext();
const db = cloud.database();

var cacheKey = 'tcbst:admin:token:' + event.token;
var value = await redis.get(cacheKey);
value = JSON.parse(value);

if (value.openid == wxContext.OPENID){

// 查询数据
var result = await db.collection('tcbst_admin').where({
name: 'testOne', valid: true
}).get();

if (result.data.length > 0) {

let data = result.data[0];

let serial = uuid.v1();
let codeData = (uuid.v4() + uuid.v4()).replace(/-/g, '').toUpperCase();

let nowDate = new Date();
let timestamp = Math.round(new Date().getTime() / 1000);

if (event.expiresTime == null || event.expiresTime < 3600) {
/***** 有效时间最低1小时, 默认24小时 */
event.expiresTime = 3600 * 24;
};
/*************增加扫码记录 */
let body = await db.collection('tcbst_visit_code').add({
data: {
areaId: event.areaId,
serial: serial,
code_data: codeData,
openid: data.openid,
area_id: data.area_id,
area_area: data.area_area,
org_mame: data.org_name,
type: 'visit',
valid: true,
area_place: event.place,
mate_code: event.mateCode,
create_time: nowDate,
expires_time: event.expiresTime,
update_time: nowDate
}
});

let keypair = sm2.generateKeyPairHex();
let publicKey = keypair.publicKey; // 公钥
let privateKey = keypair.privateKey; // 私钥

cacheKey = 'tcbst:admin:pubKey:' + serial.replace(/-/g, '');
redis.set(cacheKey, publicKey, 'EX', event.expiresTime);
cacheKey = 'tcbst:admin:priKey:' + serial.replace(/-/g, '');
redis.set(cacheKey, privateKey, 'EX', event.expiresTime);

let msg = 'serial=' + serial + '&timestamp=' + timestamp;
if (event.mateCode != null){
msg = msg + event.mateCode;
};
let sigData = sm2.doSignature(msg, privateKey);

return {
error: null,
requestId: event.requestId,
serial: serial,
sigData: sigData,
timestamp: timestamp
};

} else {
return { error: 'admin data is error' };
};

}else{
return { error: 'token data is error' };
};
};

17 changes: 17 additions & 0 deletions cloudfunctions/getVisitCode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "getVisitCode",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest",
"ioredis": "latest",
"miniprogram-sm-crypto": "latest",
"node-uuid": "latest"
}
}
5 changes: 5 additions & 0 deletions cloudfunctions/login/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"permissions": {
"openapi": []
}
}
Loading

0 comments on commit 45f89a7

Please sign in to comment.