node服务器接入微信与企业微信js-sdk 第二篇

结合第一篇使用

home.js

import axios from "axios";
// 微信
async getStoreWXConfig() {
  try {
    const { data } = await axios.get("/apiWx/wechat-config", {
      params: {
        operate: "getJsapiConfig",
        url: window.location.href,
      },
    });
    // console.log("result", data);
    return data;
  } catch (error) {
    console.log("getStoreWXConfig", error);
    return false;
  }
}
// 企业微信
async getStoreQYWXConfig() {
  try {
    const { data } = await axios.get("/apiWx/qywechat-config", {
      params: {
        operate: "getJsapiConfig",
        url: window.location.href,
      },
    });
    // console.log("result", data);
    return data;
  } catch (error) {
    console.log("getStoreWXConfig", error);
    return false;
  }
}

main.js

import Vue from "vue";
Vue.prototype.$wx = window.wx;

调用

// 微信
async getAppWXConfig() {
  const result = await this.getStoreWXConfig();

  if (result.success) {
    // console.log("result", result);
    const { appId, timestamp, nonceStr, signature, jsApiList } =
      result.data;
    this.$wx.config({
      debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
      appId,
      timestamp: parseInt(timestamp),
      nonceStr,
      signature,
      jsApiList,
    });

    this.$wx.ready(() => {
      console.log("getAppWXConfig ready");
    });
  }
}
// 企业微信
async getAppQYWXConfig() {
  const result = await this.getStoreQYWXConfig();

  if (result.success) {
    // console.log("result", result);
    const { corpid, agentid, timestamp, nonceStr, signature, jsApiList } =
      result.data;
    this.$wx.config({
      beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
      debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
      appId: corpid,
      timestamp: parseInt(timestamp),
      nonceStr,
      signature,
      jsApiList,
    });

    this.$wx.ready(() => {
      console.log("getAppQYWXConfig ready");
    });
    this.$wx.checkJsApi({
      jsApiList, // 需要检测的JS接口列表,所有JS接口列表见附录2,
      success: function (res) {
        console.log("checkJsApi", res);
        // 以键值对的形式返回,可用的api值true,不可用为false
        // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
      },
      fail: function (res) {
        console.log("checkJsApi fail", res);
      },
    });
    this.$wx.agentConfig({
      corpid, // 必填,企业微信的corpid,必须与当前登录的企业一致
      agentid, // 必填,企业微信的应用id
      timestamp: parseInt(timestamp), // 必填,生成签名的时间戳
      nonceStr, // 必填,生成签名的随机串
      signature, // 必填,签名,见附录1
      jsApiList, //必填
      success: function (res) {
        // 回调
        console.log("agentConfig", res);
      },
      fail: function (res) {
        console.log("agentConfig fail", res);
        if (res.errMsg.indexOf("function not exist") > -1) {
          alert("版本过低请升级");
        }
      },
    });
  }
}

作者:白马不是马

posted @ 2023-02-10 09:28  中亿丰数字科技  阅读(123)  评论(0)    收藏  举报