跳至主要內容

自生成签名

XinYang's Blog大约 3 分钟小程序

wx.config({
  debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  appId: '', // 必填,公众号的唯一标识
  timestamp: , // 必填,生成签名的时间戳
  nonceStr: '', // 必填,生成签名的随机串
  signature: '',// 必填,签名
  jsApiList: [] // 必填,需要使用的JS接口列表
});

wx.confg中的对象,有个必传值就是signature,可以从官方工具open in new window进行在线生成。但是最终我们是要通过代码来实现的,这里介绍由JS实现自生成签名。

这里需要用到jsshaGitHub仓库open in new window,安装命令:

npm install jssha
查看示例代码
config.js
const _charStr = "abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";

/**
 * 随机生成索引
 * @param min 最小值
 * @param max 最大值
 * @param i 当前获取位置
 */
const RandomIndex = (min, max, i) => {
  let index = Math.floor(Math.random() * (max - min + 1) + min),
    numStart = _charStr.length - 10;
  if (i == 0 && index >= numStart) {
    index = RandomIndex(min, max, i);
  }
  return index;
};

/**
 * 随机生成字符串
 * @param len 指定生成字符串长度
 */
const getRandomString = len => {
  let min = 0,
    max = _charStr.length - 1,
    _str = "";
  len = len || 15;
  for (let i = 0, index; i < len; i++) {
    index = RandomIndex(min, max, i);
    _str += _charStr[index];
  }
  return _str;
};

const appID = "";//公众号的appID ,如果有正式公众号在登录:https://mp.weixin.qq.com/,没有就在测试公众号平台登录:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

const appsecret = "";//公众号的appsecret

const timestamp = new Date().getTime();

//随机字符串
const noncestr = getRandomString(16);

//我根据生产环境和开发环境获取当前页面的url,这里仅支持hash模式路由,若想支持history模式路由可以继续完善,但要注意记得在公众号后台将此域名加在安全域名列表里面
const url = process.env.NODE_ENV === "development" ? "http://195.2.199.160/scwz_wxgh/" : location.href.split("#")[0];

export { appID, appsecret, timestamp, noncestr, url };

补充:

  1. JS-SDK使用教程open in new window
  2. 自生成签名过程open in new window