职贝云数AI新零售门户

标题: uni-app开发企微H5——推音讯给客户(安装包版) [打印本页]

作者: UK8Uf    时间: 2022-12-30 15:19
标题: uni-app开发企微H5——推音讯给客户(安装包版)
上一篇的企微推送音讯测试了之后,发现苹果手机不行,然后又要末尾重新看文档,搜处理方案。这个时分看到同事的一个企微客服的测试小项目,不看不知道,一看吓一跳。这不就是我苦苦寻觅的东西么?而且是经过实验的可用方案,奶思~
@wecom/jssdk - npm微信(企业微信)JSSDK,可用于替代 jweixin.js。. Latest version: 1.2.1, last published: 2 months ago. Start using @wecom/jssdk in your project by running `npm i @wecom/jssdk`. There are no other projects in the npm registry using @wecom/jssdk.
(, 下载次数: 17)

https://www.npmjs.com/package/@wecom/jssdk1、安装
npm i @wecom/jssdk
2、运用
假如只要一个页面需求运用到,就直接写在页面里,如下:
  1. <template>
  2.         <view class="content">
  3.                 <image class="logo" src="/static/logo.png"></image>
  4.                 <view class="text-area">
  5.                         <text class="title">{{title}}</text>
  6.                         <view>{{ version }}</view>
  7.                         <view>{{ content }}</view>
  8.                        
  9.                 </view>
  10.         </view>
  11. </template>
  12. <script>
  13.         import * as ww from '@wecom/jssdk'
  14.         export default {
  15.                 data() {
  16.                         return {
  17.                                 title: 'Hello',
  18.                                 version: "",
  19.                                 content: ""
  20.                         }
  21.                 },
  22.                 onLoad() {
  23.                         this.version = ww.VERSION
  24.                         let _this = this
  25.                        
  26.                         ww.register({
  27.                                 corpId: "请填入你的corpId",
  28.                                 agentId: 请填入你的agentId,
  29.                                 jsApiList: [
  30.                                         'checkJsApi',
  31.                                         'getContext',
  32.                                         'selectExternalContact',
  33.                                         'getCurExternalContact',
  34.                                         'getCurExternalChat',
  35.                                         'sendChatMessage',
  36.                                         'launchMiniprogram'
  37.                                 ],
  38.                                 async getConfigSignature(url) {
  39.                                         let [error, res] = await uni.request({
  40.                                                 url: '调用的接口url,js_sdkInit',
  41.                                                 data: {
  42.                                                         agentid: 请填入你的agentId,
  43.                                                         purl: url
  44.                                                 },
  45.                                         })
  46.                                         return {
  47.                                                 timestamp: res.data.timestamp,
  48.                                                 nonceStr: res.data.nonceStr,
  49.                                                 signature: res.data.signature
  50.                                         }
  51.                                 },
  52.                                 async getAgentConfigSignature(url) {
  53.                                         let [error, res] = await uni.request({
  54.                                                 url: '调用的接口url,Agentjs_sdkInit',
  55.                                                 data: {
  56.                                                         agentid: 1000060,
  57.                                                         purl: url
  58.                                                 },
  59.                                         })
  60.                                         console.log("getAgentConfigSignature:res",res)
  61.                                         return {
  62.                                                 timestamp: res.data.d.timestamp,
  63.                                                 nonceStr: res.data.d.nonceStr,
  64.                                                 signature: res.data.d.signature
  65.                                         }
  66.                                 },
  67.                                 onConfigSuccess(res) {
  68.                                         console.log("onConfigSuccess")
  69.                                         console.log(res)
  70.                                 },
  71.                                 onConfigFail(res) {
  72.                                         console.log("onConfigFail")
  73.                                         console.log(res)
  74.                                 },
  75.                                 onAgentConfigSuccess(res) {
  76.                                         console.log("onAgentConfigSuccess")
  77.                                         console.log(res)
  78.                                 },
  79.                                 onAgentConfigFail(res) {
  80.                                         console.log("onAgentConfigFail")
  81.                                         console.log(res)
  82.                                 }
  83.                         })
  84.                        
  85.                         //直接运用
  86.                         ww.getCurExternalContact({
  87.                                 success(res) {
  88.                                         _this.content = JSON.stringify(res)
  89.                                 }
  90.                         })
  91.                        
  92.                 },
  93.                 methods: {
  94.                 }
  95.         }
  96. </script>
  97. <style>
  98.         .content {
  99.                 display: flex;
  100.                 flex-direction: column;
  101.                 align-items: center;
  102.                 justify-content: center;
  103.         }
  104.         .logo {
  105.                 height: 200rpx;
  106.                 width: 200rpx;
  107.                 margin-top: 200rpx;
  108.                 margin-left: auto;
  109.                 margin-right: auto;
  110.                 margin-bottom: 50rpx;
  111.         }
  112.         .text-area {
  113.                 display: flex;
  114.                 justify-content: center;
  115.         }
  116.         .title {
  117.                 font-size: 36rpx;
  118.                 color: #8f8f94;
  119.         }
  120. </style>
复制代码
(1)、假如是多处运用,就写在js里,然后在main.js里注入。
  1. //qy_Config.js
  2. import * as ww from '@wecom/jssdk'
  3. ww.register({
  4.         corpId: "本人填",
  5.         agentId: 本人填,
  6.         jsApiList: [
  7.                 'checkJsApi',
  8.                 'getContext',
  9.                 'selectExternalContact',
  10.                 'getCurExternalContact',
  11.                 'getCurExternalChat',
  12.                 'sendChatMessage',
  13.                 'launchMiniprogram'
  14.         ],
  15.         async getConfigSignature(url) {
  16.                 let [error, res] = await uni.request({
  17.                         url: '本人填',
  18.                         data: {
  19.                                 agentid: 本人填,
  20.                                 purl: url
  21.                         },
  22.                 })
  23.                 return {
  24.                         timestamp: res.data.timestamp,
  25.                         nonceStr: res.data.nonceStr,
  26.                         signature: res.data.signature
  27.                 }
  28.         },
  29.         async getAgentConfigSignature(url) {
  30.                 let [error, res] = await uni.request({
  31.                         url: '本人填',
  32.                         data: {
  33.                                 agentid: 本人填,
  34.                                 purl: url
  35.                         },
  36.                 })
  37.                 console.log("getAgentConfigSignature:res", res)
  38.                 return {
  39.                         timestamp: res.data.d.timestamp,
  40.                         nonceStr: res.data.d.nonceStr,
  41.                         signature: res.data.d.signature
  42.                 }
  43.         },
  44.         onConfigSuccess(res) {
  45.                 console.log("onConfigSuccess")
  46.                 console.log(res)
  47.         },
  48.         onConfigFail(res) {
  49.                 console.log("onConfigFail")
  50.                 console.log(res)
  51.         },
  52.         onAgentConfigSuccess(res) {
  53.                 console.log("onAgentConfigSuccess")
  54.                 console.log(res)
  55.         },
  56.         onAgentConfigFail(res) {
  57.                 console.log("onAgentConfigFail")
  58.                 console.log(res)
  59.         }
  60. })
复制代码
(2)、在main.js里注入
  1. //main.js
  2. import qy_Config from '@/common/qy_Config.js'  //援用
复制代码
(3)、在页面中运用,完成推送功能。
  1. <template>
  2.         <view>       
  3.                 <u-button class="send-btn" iconColor="#fff" @click="pushText()" type="error" shape="circle" size='mini'>我要推送</u-button>
  4.         </view>
  5. </template>
  6. <script>
  7.         import * as ww from '@wecom/jssdk'
  8.        
  9.         export default {
  10.                 data() {
  11.                         return {
  12.                                 content:'我要推送的文案'
  13.                         };
  14.                 },
  15.                 methods:{
  16.                         // 推送音讯
  17.                         pushText(){
  18.                        
  19.                                 var that = this
  20.                                 var ua = window.navigator.userAgent.toLowerCase();
  21.                                 console.log(ua)
  22.                                 console.log(ua.match(/wxwork/i) == 'wxwork')
  23.                                 if (ua.match(/wxwork/i) == 'wxwork') {
  24.                                        
  25.                                         ww.invoke('sendChatMessage', {
  26.                                                 msgtype:"text", //音讯类型,必填
  27.                                                 enterChat: true, //为true时表示发送完成之后特地进入会话,仅移动端3.1.10及以上版本支持该字段
  28.                                                 text: {
  29.                                                         content: that.content, //文本内容
  30.                                                 }
  31.                                         }, function(res) {
  32.                                                
  33.                                                 if(res.res.err_msg == "sendChatMessage:fail_nosupport"){
  34.                                                         uni.showModal({
  35.                                                                 title: "无法推送",
  36.                                                                 content: "当前入口无法推送,请在对话栏打开此页面操作!"
  37.                                                         })
  38.                                                 }
  39.                                                 if (res.err_msg == 'sendChatMessage:ok') {
  40.                                                         //发送成功
  41.                                                 }
  42.                                         })
  43.                                 } else {
  44.                                         uni.showToast({
  45.                                                 title: "请在企业微信端操作",
  46.                                                 icon: "none"
  47.                                         })
  48.                                         return
  49.                                 }
  50.                                
  51.                         }
  52.                        
  53.                        
  54.                 }
  55.         }
  56. </script>
复制代码
uni-app开发企微H5——推送音讯给客户_Strawberry96的博客-CSDN博客在企微推送个音讯给客户怎样就这么难那???别以为有了开发文档你就可以勇往直前了!后面的路还需求你披荆斩棘那~
favicon32

https://blog.csdn.net/zhangying1996/article/details/123053312




欢迎光临 职贝云数AI新零售门户 (https://www.taojin168.com/cloud/) Powered by Discuz! X3.5