App.vue 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <van-config-provider class="van-config" :theme-vars="themeVars" theme-vars-scope="global">
  3. <router-view v-slot="{ Component, route }" >
  4. <transition>
  5. <keep-alive>
  6. <component :is="Component"/>
  7. </keep-alive>
  8. </transition>
  9. </router-view>
  10. </van-config-provider>
  11. </template>
  12. <style scoped></style>
  13. <script setup>
  14. import {useTheme} from "@hooks/useTheme.js";
  15. import { useLifecycle } from "@hooks/useCommon.js";
  16. import {ref} from "vue";
  17. import requests from "@common/requests"
  18. let themeVars = ref(useTheme());
  19. function obtainEnterpriseWeChatParameters(data = {}) {
  20. const token = data.id
  21. // const curUrl = window.location.href.split('home')[0]
  22. const curUrl = window.location.href
  23. requests.post('/wxcorp/getCorpWXConfig', { url: curUrl, token }).then((res) => {
  24. wx.config({
  25. beta: true,
  26. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  27. appId: res.data.appid, // 必填,公众号的唯一标识
  28. timestamp: res.data.timestamp, // 必填,生成签名的时间戳
  29. nonceStr: res.data.noncestr, // 必填,生成签名的随机串
  30. signature: res.data.sign, // 必填,签名,见附录1
  31. jsApiList: ['chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'previewFile', 'getLocation', 'agentConfig']
  32. })
  33. wx.ready(function () {
  34. // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
  35. requests.post('/wxcorp/getCorpWXAgentConfig', { url: curUrl, token }).then((res) => {
  36. console.log(res, '<====== 返回的参数 /wxcorp/getCorpWXAgentConfig')
  37. wx.agentConfig({
  38. corpid: res.data.corpid, // 必填,企业微信的corpid,必须与当前登录的企业一致
  39. agentid: res.data.agentid, // 必填,企业微信的应用id (e.g. 1000247)
  40. timestamp: res.data.timestamp, // 必填,生成签名的时间戳
  41. nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
  42. signature: res.data.signature, // 必填,签名,见附录-JS-SDK使用权限签名算法
  43. jsApiList: ['selectExternalContact', 'openThirdAppServiceChat', 'openAppManage'], //必填,传入需要使用的接口名称
  44. success: function (result) {
  45. // wx.agentConfig成功回调后,WWOpenData 才会注入到 window 对象上面
  46. window.WWOpenData.bind(document.querySelector('ww-open-data'))
  47. },
  48. fail: function (res) {
  49. if (res.errMsg.indexOf('function not exist') > -1) {
  50. alert('版本过低请升级')
  51. }
  52. },
  53. })
  54. }).catch(err => {
  55. if (err.errMsg.indexOf('function not exist') > -1) {
  56. alert('版本过低请升级')
  57. }
  58. })
  59. })
  60. wx.error(function (res) {
  61. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  62. // alert('wxConfig发生异常:'+JSON.stringify(res));
  63. // 企业第一次授权安装进入后会报not in reliable domain的错误,刷新后正常
  64. // location.reload();
  65. });
  66. }).catch(err => {
  67. alert(err);
  68. })
  69. }
  70. useLifecycle({
  71. load: () => {
  72. },
  73. init: () => {
  74. const currentEnvironment = navigator.userAgent.toLowerCase();
  75. const isCorpWX = currentEnvironment.indexOf("wxwork") > 0 ? true : false
  76. const token = sessionStorage.getItem('token')
  77. if(isCorpWX && token) {
  78. console.log('企业微信转译执行')
  79. setTimeout(() => {
  80. const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
  81. console.log(userInfo, '<====== userInfo')
  82. obtainEnterpriseWeChatParameters(userInfo.userInfo)
  83. }, 500)
  84. }
  85. }
  86. });
  87. </script>