123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <template>
- <!-- 推广链接h5 -->
- </template>
- <script lang="ts" setup>
- import { getQueryString } from '@/utils/common'
- import { onBeforeMount } from "vue";
- import { Toast } from 'vant';
- import $axios from '@/utils/axios'
- function goUrl(url) {
- let link = document.createElement("a");
- link.href = url;
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- }
- const setOriginalLink = () => {
- try {
- const currentTime: any = new Date().getTime()
- const pageStartTime: any = localStorage.getItem('pageStartTime') || 0
- if ((currentTime - Number(pageStartTime)) > 3000) {
- localStorage.setItem('pageStartTime', currentTime)
- localStorage.setItem('_originalLink', window.location.href)
- }
- } catch (error) {
- console.log('error => ', error)
- }
- }
- // 异常上报 (step 异常步骤)
- // [ 1, '链接无企微id' ],
- // [ 2, '链接无code参数' ],
- // [ 3, '链接无state参数' ],
- // [ 4, '"api/oauth2/userInfo"接口没有返回值' ],
- // [ 5, '链接无jumpLink参数' ],
- // [ 6, '跳转企微授权' ],
- // [ 7, '跳转第三方链接' ],
- const handleWarnReport = async (step, errMsg?) => {
- try {
- const _originalLink = localStorage.getItem('_originalLink')
- const linkInfo = {
- url: window.location.href,
- originalLink: _originalLink,
- errMsg: errMsg,
- }
- const url = '/api/oauth2/warnReport'
- const params = {
- step,
- corpid: getQueryString('corpid') || '0',
- link: JSON.stringify(linkInfo),
- }
- await $axios.get(url, {...params})
- } catch (error) {
- console.log('error =>', error)
- }
- }
- onBeforeMount(() => {//组件挂载之前
- setOriginalLink()
- if (getQueryString('againJump')) {
- getUserInfo()
- } else {//获取用户信息
- // 链接无企微id => 异常上报
- if (!getQueryString('corpid')) {
- handleWarnReport(1)
- }
- try {
- let redirect_uri = encodeURIComponent(window.location.href + '&againJump=true')
- 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`;
- goUrl(url)
- } catch (error) {
- // 企微授权跳转 => 异常
- handleWarnReport(6, error?.message)
- }
- }
- });
- const getUserInfo = () => {
- // 链接无code参数 => 异常上报
- if (!getQueryString('code')) {
- handleWarnReport(2)
- }
- // 链接无state参数 => 异常上报
- if (!getQueryString('state')) {
- handleWarnReport(3)
- }
- Toast.loading({
- duration: 0,
- message: '加载中...',
- forbidClick: true,
- });
- $axios.get('/api/oauth2/userInfo', {
- corpid: getQueryString('corpid'),
- code: getQueryString('code'),
- state: getQueryString('state')
- }, true).then((res) => {
- Toast.clear();
- if (res.rst) {
- if (getQueryString('jumpLink')) {
- try {
- // 注:url 参数拼接如果修改,playletGroupH5.vue 中 handleGetPlayletGroup() 处参数拼接也要同步修改
- let corpid = getQueryString('corpid')
- let jumpLink = decodeURIComponent(getQueryString('jumpLink'))
- if (jumpLink.indexOf('?') != -1) {
- let url = jumpLink + '¶ms=' + res.rst.external_userid +
- '&userId=' + res.rst.external_userid +
- '&qyOpenId=' + res.rst.external_userid +
- '&corpId=' + corpid +
- '&exparams=' + (corpid?corpid:'') + '|' + '' + '|' + '' + '|' + (res.rst.external_userid ? res.rst.external_userid : '') +
- '&msgId=' + (res.rst.msgId ? res.rst.msgId : '') +
- '&msgType=' + (res.rst.msgType ? res.rst.msgType : '') +
- '&qw_params=' + (res.rst.msgId ? res.rst.msgId : '') +
- '&attach=' + (res.rst.msgId ? res.rst.msgId : '');
- goUrl(url)
- } else {
- let url = jumpLink + '?params=' + res.rst.external_userid +
- '&userId=' + res.rst.external_userid +
- '&qyOpenId=' + res.rst.external_userid +
- '&corpId=' + corpid +
- '&exparams=' + (corpid?corpid:'') + '|' + '' + '|' + '' + '|' + (res.rst.external_userid ? res.rst.external_userid : '') +
- '&msgId=' + (res.rst.msgId ? res.rst.msgId : '') +
- '&msgType=' + (res.rst.msgType ? res.rst.msgType : '') +
- '&qw_params=' + (res.rst.msgId ? res.rst.msgId : '') +
- '&attach=' + (res.rst.msgId ? res.rst.msgId : '');
- goUrl(url)
- }
- } catch (error) {
- // 跳转第三方链接 => 异常上报
- handleWarnReport(7, error?.message)
- }
- } else {
- Toast('无回跳链接!')
- // 链接无jumpLink参数 => 异常上报
- handleWarnReport(5)
- }
- } else {
- // 'api/oauth2/userInfo' 接口没有返回值 => 异常上报
- handleWarnReport(4, JSON.stringify(res))
- }
- }).catch((err) => {
- Toast.clear();
- Toast(err.message)
- })
- }
- </script>
|