// 加密插件
import CryptoJS from 'crypto-js'
import wx from 'weixin-js-sdk'
//是否是微信浏览器
const isWechat = () => {
  var ua = window.navigator.userAgent.toLowerCase()
  if (ua.match(/micromessenger/i) == 'micromessenger') {
    return true;
  } else {
    return false;
  }
}
//需要使用的JS接口列表
let jsApiList = [
  'checkJsApi',
  'getLocation',
  'openLocation',
  'wx-open-launch-app',
  'scanQRCode',
  'wx-open-launch-weapp'
]
// 生成随机字符
function randomString(len) {
  len = len || 32;
  var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  var maxPos = $chars.length;
  var pwd = '';
  for (let i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}
const baseModule = {
  namespaced: true,
  state: () => ({
    isWechat: isWechat()
  }),
  getters: {
    getB(state) {
      return `${state.b}99999`
    }
  },
  mutations: {},
  actions: {
    initJssdk(context, ticket){
      let url = window.location.href.split('#')[0]
      let timestamp = Math.round(new Date().getTime() / 1000).toString() // 时间戳
      let nonceStr = randomString()  // 随机字符
      let signature = CryptoJS.SHA1(`jsapi_ticket=${ticket}&noncestr=${nonceStr}&timestamp=${timestamp}&url=${url}`).toString()
      wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来
        appId: 'wx8398d947880ce37a', // 必填，公众号的唯一标识
        timestamp, // 必填，生成签名的时间戳
        nonceStr, // 必填，生成签名的随机串
        signature,// 必填，签名
        jsApiList, // 必填，需要使用的JS接口列表
        openTagList:['wx-open-launch-weapp']
      });
    }
  }
}

export default baseModule
