一、云函数可本地调试,无需每次上传了再看云端的console.log('xxx')
二、如果使用真机调试则本地调试无效,必须将云函数上传上去才有效果!
三、本地调试的时候,env的值是undefined,如果你要指定某个环境,建议用如下方式:
// 两个env的id
const test_env = 'l4-test-20191121';
const prod_env = 'l4-production-ts477';
// 强制指定哪个环境
cloud.init({
env: prod_env
})
四、云函数不支持es6的格式(小程序端可以哦),只能这样:
const chalBiz = require('./chal-biz');
const demo_date = require('./demo_data');
如果这样,就会出错:
import chalBiz from './chal-biz';
五、云函数拆分方法,根据event.action来做分拆:
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext();
switch (event.action) {
case 'getWXACode': return sysUtil.getWXACode(event)
case 'getAccessToken': {
const app_id = event.appid;
return sysUtil.getToken(app_id);
}
case 'msgSecCheck': return sysUtil.msgSecCheck(event)
case 'sendMsg':{
const openid = event.openid;
const templateId = event.templateId;
const msgData = event.msgData;
const pagePath = event.pagePath;
return msger.sendMsg(openid, templateId, msgData, pagePath);
}
case "getCredit": {
const encryptedData = event.encryptedData;
const iv = event.iv;
const js_code = event.js_code;
const appid = wxContext.APPID;
const openid = wxContext.OPENID;
return await sysUtil.getUserCredit(js_code, appid, encryptedData, iv, openid);
}
default:
{
return {
event,
openid: wxContext.OPENID,
appid: wxContext.APPID,
env: wxContext.ENV,
};
}
}
}
六、要用解密方法获取unionID,必须先注册开放平台、公众号、小程序,然后把公众号和小程序都绑定到开放平台,再在开放平台通过开发者资质认证(费用300)。
七、数据库运维,
- 在开发者平台中创建一个系统数据集合,例如sys_info,将小程序的appId和appsecret保存到这个集合中
- 创建集合:`https://api.weixin.qq.com/tcb/databasecollectionadd?access_token=${token}`,方法如下:
-
const token = await getAccessToken(appId); console.log("ENV:", env); const url = `https://api.weixin.qq.com/tcb/databasecollectionadd?access_token=${token}`; col_names.map(col_name => { console.log("create collection==>" + col_name); const contents = JSON.stringify({ env: env, collection_name: col_name }); REQ.post(url, { headers: { "Content-Type": "application/json;charset=utf8", "Content-Length": contents.length }, body: contents, callback: (err, res, body) => { console.log("ERR", err); console.log("RES", res); console.log("BODY", JSON.parse(body)); const result = JSON.parse(body); if (result.errcode != 0) { if (result.errcode == -501000) { } else { return `create collection ${col_name} err:${err}`; } } } }) });
- 导入数据:云存储中创建一个文件夹initData,将数据的json文件(ex:database_user_type.json)上传到云存储,然后这样导入:
-
const REQ = require('request'); const col_for_import = ['player_sign']; // 要导入的集合名称 const token = await getAccessToken(appId); const url = `https://api.weixin.qq.com/tcb/databasemigrateimport?access_token=${token}`; col_for_import.map(col_name => { const file_path = `init_data/database_${col_name}.json`; const contents = JSON.stringify({ env: env, collection_name: col_name, file_path: file_path, file_type: 1, conflict_mode: 2, //1:insert 2:upsert stop_on_error: true, }); REQ.post(url, { headers: { "Content-Type": "application/json;charset=utf8", "Content-Length": contents.length }, body: contents, callback: (err, res, body) => { console.log("ERR", err); console.log("RES", res); console.log("BODY", JSON.parse(body)); const result = JSON.parse(body); return result; } }); });
八、最好把默认环境(第一个环境)设置为测试环境或开发环境,另外创建的环境命名为xx-prod生产环境。