企业微信内嵌H5项目接入聊天功能

产品需求是,在列表中把符合条件的列表接入聊天功能,以下是详细步骤:

1.引入企业微信

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

 2.获取wx签名(必须要)

/**
 * 获取wx签名
 **/
export function getWxJsApi(data) {
    return request({
        url: '/js-sdk',
        method: 'post',
        data
    })
}

3.封装组件(两个)

wxConfig.js

import {getWxJsApi} from "@/api/arrives";

export const wxConfig = async (url, jsApiList, jsApiListChat) => {
  try {
    let res = await getWxJsApi({url});
    if (res && res.status === 200) {
      const { corpId, nonceStr, signature, timeStamp } = res.data
      wx &&
        wx.config({
          beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
          appId: corpId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
          timestamp: timeStamp, // 必填,生成签名的时间戳
          nonceStr, // 必填,生成签名的随机串
          signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
          jsApiList,
        });
  
      wx &&
        wx.ready(() => {
          if (jsApiListChat) {
            getWxJsApi ({url}).then(result => {
              if (res.status === 200) {
                wx.agentConfig({
                  beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
                  debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                  appId: result.data.corpId, // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
                  timestamp: result.data.timeStamp, // 必填,生成签名的时间戳
                  nonceStr: result.data.nonceStr, // 必填,生成签名的随机串
                  signature: result.data.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
                  jsApiList:jsApiListChat
                });
              }
            })
          }
          wx.error(function() {
            console.log('error');
          });
        });
    }
  } catch(error) {

  } 
}

mixin.js

import {wxConfig} from '@/utils/wxConfig'
const chatMixin = {
  mounted() {
    let url = location.href
    wxConfig(url, ['openEnterpriseChat','checkJsApi'], ['selectExternalContact', 'getCurExternalContact'])
  },
  methods: {
    wxEnterpriseChat (obj) {
      wx &&
        wx.checkJsApi({
          jsApiList: ['openEnterpriseChat'],
          success: function (res) {
            wx.openEnterpriseChat({
              ...obj,
              groupName: '',
              chatId: "",
              success: function(res) {
              var chatId = res.chatId;
                  // 回调
              },
              fail: function(res) {
                  if(res.errMsg.indexOf('function not exist') > -1){
                      alert('版本过低请升级')
                  }
              }
            })
          },
          fail: function(error) {
            console.log(error)
          }
        })
    }
  }
}

export {chatMixin}

4.页面上使用

<1>引入

import {chatMixin} from "@/utils/mixin";
export default {
   mixins: [chatMixin],
}

html

<div class="right-customer-detail flex" v-if="enterpriseWeChatUserId == item.userAccountId && item.externalUserId">
        <img
          src="https://static-resource.dossen.com/H5/marketing/img/wechat.png"
          class="wechat-img"
          @click.stop="() => testChat(item.externalUserId)"
        />
 </div>

methods:

 testChat(externalUserIds) {
      let obj = {externalUserIds}
      this.wxEnterpriseChat(obj)
    },

已结束

备注: 

这里的this.wxEnterpriseChat就是上面mixin.js封装的方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在H5网页中接入微信登录功能,可以通过微信开放平台提供的接口来实现。 首先,需要在微信开放平台上注册开发者账号,并创建一个移动应用。在创建应用时,需要填写应用的基本信息,并获取到AppID和AppSecret。 接下来,在H5网页中引入微信的JS-SDK,通过调用微信提供的API来实现登录功能。首先,在网页的HTML代码中引入微信JS-SDK的SDK文件: ```html <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> ``` 接着,在网页的JS代码中初始化微信JS-SDK,并进行登录功能的实现: ```javascript wx.config({ debug: false, appId: '<your_app_id>', timestamp: '<current_timestamp>', nonceStr: '<random_string>', signature: '<generated_signature>', jsApiList: ['checkJsApi', 'login', 'getUserInfo'] // 需要使用的JS接口 }); wx.ready(function() { // 微信JS-SDK初始化完成后的操作 wx.checkJsApi({ jsApiList: ['login', 'getUserInfo'], // 需要检测的JS接口 success: function(res) { // 可以使用登录功能了 wx.login({ success: function(res) { var code = res.code; // 获取到用户登录凭证 code // 通过 code 获取用户的唯一标识 openid // 可以将 openid 保存在后台,以便后续的登录验证操作 } }); } }); }); ``` 在上述代码中,`<your_app_id>` 需要替换为你的微信开放平台的AppID。`<current_timestamp>` 和 `<random_string>` 可以根据实际情况自行生成。`<generated_signature>` 需要根据当前页面的URL、当前时间戳、当前随机字符串和AppSecret生成。 在用户登录成功之后,可以通过获取到的code来调用微信开放平台提供的接口,获取到用户的唯一标识openid。可以将该openid保存在后台,以便后续的登录验证和用户信息的获取。 需要注意的是,为了确保账号安全,还需要对服务器端的接口进行相应的安全验证和防护措施,以防止恶意请求和非法访问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值