企微助手 ,仓库名 短剧

extend.vue 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <!-- 推广链接h5 -->
  3. </template>
  4. <script lang="ts" setup>
  5. import { getQueryString } from '@/utils/common'
  6. import { onBeforeMount } from "vue";
  7. import { Toast } from 'vant';
  8. import $axios from '@/utils/axios'
  9. function goUrl(url) {
  10. let link = document.createElement("a");
  11. link.href = url;
  12. document.body.appendChild(link);
  13. link.click();
  14. document.body.removeChild(link);
  15. }
  16. const setOriginalLink = () => {
  17. try {
  18. const currentTime: any = new Date().getTime()
  19. const pageStartTime: any = localStorage.getItem('pageStartTime') || 0
  20. if ((currentTime - Number(pageStartTime)) > 3000) {
  21. localStorage.setItem('pageStartTime', currentTime)
  22. localStorage.setItem('_originalLink', window.location.href)
  23. }
  24. } catch (error) {
  25. console.log('error => ', error)
  26. }
  27. }
  28. // 异常上报 (step 异常步骤)
  29. // [ 1, '链接无企微id' ],
  30. // [ 2, '链接无code参数' ],
  31. // [ 3, '链接无state参数' ],
  32. // [ 4, '"api/oauth2/userInfo"接口没有返回值' ],
  33. // [ 5, '链接无jumpLink参数' ],
  34. // [ 6, '跳转企微授权' ],
  35. // [ 7, '跳转第三方链接' ],
  36. const handleWarnReport = async (step, errMsg?) => {
  37. try {
  38. const _originalLink = localStorage.getItem('_originalLink')
  39. const linkInfo = {
  40. url: window.location.href,
  41. originalLink: _originalLink,
  42. errMsg: errMsg,
  43. }
  44. const url = '/api/oauth2/warnReport'
  45. const params = {
  46. step,
  47. corpid: getQueryString('corpid') || '0',
  48. link: JSON.stringify(linkInfo),
  49. }
  50. await $axios.get(url, {...params})
  51. } catch (error) {
  52. console.log('error =>', error)
  53. }
  54. }
  55. onBeforeMount(() => {//组件挂载之前
  56. setOriginalLink()
  57. if (getQueryString('againJump')) {
  58. getUserInfo()
  59. } else {//获取用户信息
  60. // 链接无企微id => 异常上报
  61. if (!getQueryString('corpid')) {
  62. handleWarnReport(1)
  63. }
  64. try {
  65. let redirect_uri = encodeURIComponent(window.location.href + '&againJump=true')
  66. let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${getQueryString('corpid')}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=1&agentid=1000012#wechat_redirect`;
  67. goUrl(url)
  68. } catch (error) {
  69. // 企微授权跳转 => 异常
  70. handleWarnReport(6, error?.message)
  71. }
  72. }
  73. });
  74. const getUserInfo = () => {
  75. // 链接无code参数 => 异常上报
  76. if (!getQueryString('code')) {
  77. handleWarnReport(2)
  78. }
  79. // 链接无state参数 => 异常上报
  80. if (!getQueryString('state')) {
  81. handleWarnReport(3)
  82. }
  83. Toast.loading({
  84. duration: 0,
  85. message: '加载中...',
  86. forbidClick: true,
  87. });
  88. $axios.get('/api/oauth2/userInfo', {
  89. corpid: getQueryString('corpid'),
  90. code: getQueryString('code'),
  91. state: getQueryString('state')
  92. }, true).then((res) => {
  93. Toast.clear();
  94. if (res.rst) {
  95. if (getQueryString('jumpLink')) {
  96. try {
  97. // 注:url 参数拼接如果修改,playletGroupH5.vue 中 handleGetPlayletGroup() 处参数拼接也要同步修改
  98. let corpid = getQueryString('corpid')
  99. let jumpLink = decodeURIComponent(getQueryString('jumpLink'))
  100. if (jumpLink.indexOf('?') != -1) {
  101. let url = jumpLink + '&params=' + res.rst.external_userid +
  102. '&userId=' + res.rst.external_userid +
  103. '&qyOpenId=' + res.rst.external_userid +
  104. '&corpId=' + corpid +
  105. '&exparams=' + (corpid?corpid:'') + '|' + '' + '|' + '' + '|' + (res.rst.external_userid ? res.rst.external_userid : '') +
  106. '&msgId=' + (res.rst.msgId ? res.rst.msgId : '') +
  107. '&msgType=' + (res.rst.msgType ? res.rst.msgType : '') +
  108. '&qw_params=' + (res.rst.msgId ? res.rst.msgId : '') +
  109. '&attach=' + (res.rst.msgId ? res.rst.msgId : '');
  110. goUrl(url)
  111. } else {
  112. let url = jumpLink + '?params=' + res.rst.external_userid +
  113. '&userId=' + res.rst.external_userid +
  114. '&qyOpenId=' + res.rst.external_userid +
  115. '&corpId=' + corpid +
  116. '&exparams=' + (corpid?corpid:'') + '|' + '' + '|' + '' + '|' + (res.rst.external_userid ? res.rst.external_userid : '') +
  117. '&msgId=' + (res.rst.msgId ? res.rst.msgId : '') +
  118. '&msgType=' + (res.rst.msgType ? res.rst.msgType : '') +
  119. '&qw_params=' + (res.rst.msgId ? res.rst.msgId : '') +
  120. '&attach=' + (res.rst.msgId ? res.rst.msgId : '');
  121. goUrl(url)
  122. }
  123. } catch (error) {
  124. // 跳转第三方链接 => 异常上报
  125. handleWarnReport(7, error?.message)
  126. }
  127. } else {
  128. Toast('无回跳链接!')
  129. // 链接无jumpLink参数 => 异常上报
  130. handleWarnReport(5)
  131. }
  132. } else {
  133. // 'api/oauth2/userInfo' 接口没有返回值 => 异常上报
  134. handleWarnReport(4, JSON.stringify(res))
  135. }
  136. }).catch((err) => {
  137. Toast.clear();
  138. Toast(err.message)
  139. })
  140. }
  141. </script>