企业微信API

本文档介绍了如何在Vue项目中引入并使用企业微信API,包括在HTML中引入外部文件,判断企业微信版本以及调用相关API的方法。同时,提供了检查企业微信版本是否大于3.0.24或小于2.5.0的函数,并展示了如何在页面中注入和调用API的示例代码,如`sendApi`函数的实现和`isQunCome`方法的调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue项目引用企业微信相关api 相关问题

api地址(开发前须知 - 接口文档 - 企业微信开发者中心

1,引入文件---调企业微信api需要用的外部文件--(index.html文件)

<script src="https://res.wx.qq.com/wwopen/js/jsapi/jweixin-1.0.0.js"></script> 

<script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"></script>

此文件为 debug 调试工具 可以查看相关报错以及api调用 接口调用问题

<script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>

  <script>

          var vConsole = new window.VConsole();

 </script>

2.判断企业微信版本以及是否为企微相关方法

// 判断是否企业微信并且浏览器版本是否大于3.0.24
export const wxworkGE3_0_24 = function() {
  const ua = navigator.userAgent;
  const result = ua.match(/wxwork\/([^\s]*)/i);
  if (!Array.isArray(result)) {
    return { version: 0, isWxwork: false, ge3_0_24: false }; // 不是企业微信
  }
  const version = result[1].split(".");

  if (
    version[0] > 3 ||
    (version[0] == 3 && version[1] > 0) ||
    (version[0] == 3 && version[1] == 0 && version[2] >= 24)
  ) {
    return { version: result[1], isWxwork: true, ge3_0_24: true }; // 是企业微信,版本大于等于3.0.24;
  }
  return { version: result[1], isWxwork: true, ge3_0_24: false }; // 是企业微信,版本小于3.0.24;
};

// 判断是否企业微信并且浏览器版本小于2.5.0
export const wxworkLT2_5_0 = function() {
  const ua = navigator.userAgent;
  const result = ua.match(/wxwork\/([^\s]*)/i);
  if (!Array.isArray(result)) {
    return { version: 0, isWxwork: false, lt2_5_0: false }; // 不是企业微信
  }
  const version = result[1].split(".");
  if (
    version[0] < 2 ||
    (version[0] == 2 && version[1] < 5) ||
    (version[0] == 2 && version[1] == 5 && version[2] < 0)
  ) {
    return { version: result[1], isWxwork: true, lt2_5_0: true }; // 是企业微信,版本小于2.5.0;
  }

  return { version: result[1], isWxwork: true, lt2_5_0: false }; // 是企业微信,版本大于2.5.0;
};

3.在所需要的页面中调用注入相关api

引入方法

// 获取weChat SDK Config 配置参数

export const getWchatSdkConfig = params => xhr.get("/v1/wechat/get-wechat-jssdk-config", { params });

-----------------------------------------------------------------------------------------------------------------------------

import { getWchatSdkConfig } from "@/api/commonRequest";

import { wxworkLT2_5_0, wxworkGE3_0_24 } from "@/utils/wxworkVersion";

4.注入所需要的api

    // 注入 发送api
    async sendApi() {
      const ua = navigator.userAgent;
      const wxwork = ua.match(/wxwork/i); // 匹配企微;
      try {
        // 判断企微
        let ge3_0_24Res = wxworkGE3_0_24();
        let lt2_5_0Res = wxworkLT2_5_0();
        if (lt2_5_0Res.isWxwork && lt2_5_0Res.lt2_5_0Res) {
          // this.$message({
          //   message: "请升级企业微信版本",
          //   type: "warning",
          // });
          this.tipsdisplay("请升级企业微信版本", "warning");
          return false;
        }
        // 如果是企微并且大于3.0.24, 直接使用 agentConfig
        // if (ge3_0_24Res.isWxwork && ge3_0_24Res.ge3_0_24) {
        // 如果是企微并且大于2.5, 直接使用 agentConfig
        if (lt2_5_0Res.isWxwork && !lt2_5_0Res.lt2_5_0Res) {
          let { code, data, msg } = await getWchatSdkConfig({
            type: 2, // 1 企微, 2 企微侧边栏, 3 绿维
            agent: "sider",
            url: encodeURIComponent(location.href.split("#")[0]),
          });
          if (code != 0) {
            return false;
          }
          let jsApiList = [
            "sendChatMessage",
            "getCurExternalChat",
            "getCurExternalContact",
          ];
          // if (wxwork && wxwork[0] == "wxwork") {
          //   agentConfig.beta = true; // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
          // }
          let that = this;
          wx.agentConfig({
            corpid: data.appId, // 必填,企业微信的corpid,必须与当前登录的企业一致
            agentid: data.agentId, // 必填,企业微信的应用id (e.g. 1000247)
            timestamp: data.timestamp, // 必填,生成签名的时间戳
            nonceStr: data.nonceStr, // 必填,生成签名的随机串
            signature: data.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
            jsApiList: [...jsApiList, "checkJsApi"], //必填,传入需要使用的接口名称
            success: function (res) {
              console.log(res, "成功ag");
            },
            fail: function (err) {
              console.log(err, "失败ag");
            },
          });
          wx.ready(function () {
            wx.checkJsApi({ jsApiList });
          });
          wx.error(function (err) {
            console.log(err, "hhhhh");
          });
          return false;
        }
      } catch (error) {
        console.log(error);
      }
    },

5,调用相关api 样例

 isQunCome() {
      let wc = window.wx;
      let that = this;
      wc.invoke("getCurExternalChat", {}, function (res) {
        if (res.err_msg == "getCurExternalChat:ok") {
          // chatId = res.chatId; //返回当前外部群的群聊ID
          console.log(res, "获取群聊id----success");
        } else {
          //错误处理
          console.log(res, "获取群聊id-----error");
        }
      });
},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值