微信小程序云开发踩坑记

一、云函数可本地调试,无需每次上传了再看云端的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)。

七、数据库运维,

  1. 在开发者平台中创建一个系统数据集合,例如sys_info,将小程序的appId和appsecret保存到这个集合中
  2. 创建集合:`https://api.weixin.qq.com/tcb/databasecollectionadd?access_token=${token}`,方法如下:
  3.  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}`;
              }
    
            }
          }
        })
      });

     

  4. 导入数据:云存储中创建一个文件夹initData,将数据的json文件(ex:database_user_type.json)上传到云存储,然后这样导入:
  5. 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生产环境。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值