jssdk.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * @Author: xiebaomin 1771403033@qq.com
  3. * @Date: 2023-03-29 14:01:47
  4. * @LastEditors: xiebaomin 1771403033@qq.com
  5. * @LastEditTime: 2023-10-13 09:33:53
  6. * @FilePath: \caimei-authentic-www\plugins\jssdk.js
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. import Vue from 'vue'
  10. import { appId, jsApiList } from '@/configs/jssdk'
  11. const dev = process.env.EVN
  12. Vue.prototype.$wxReady = async function(callback, apiList = []) {
  13. try {
  14. const url = window.location.href.split('#')[0]
  15. // console.log('当前页面地址:', url)
  16. const res = await Vue.prototype.$http.api.initWxConfig({ url, appId })
  17. const wx = window.wx
  18. if (!wx) return
  19. wx.config({
  20. debug: true && dev === 'development', // 开启调试模式,调用的所有 api 的返回值会在客户端 alert 出来,若要查看传入的参数,可以在 pc 端打开,参数信息会通过 log 打出,仅在 pc 端时才会打印。
  21. appId: appId, // 必填,公众号的唯一标识
  22. timestamp: res.data.timestamp, // 必填,生成签名的时间戳
  23. nonceStr: res.data.noncestr, // 必填,生成签名的随机串
  24. signature: res.data.signature, // 必填,签名
  25. jsApiList: [...jsApiList, ...apiList], // 必填,需要使用的 JS 接口列表
  26. })
  27. wx.ready(callback(wx))
  28. wx.error(function(error) {
  29. throw error
  30. })
  31. } catch (error) {
  32. if (dev === 'development') {
  33. console.warn('jssdk init error:' + error.msg || error.message)
  34. } else {
  35. console.warn('jssdk init error')
  36. }
  37. }
  38. }