|
@@ -1,5 +1,5 @@
|
|
|
<script setup lang="ts">
|
|
<script setup lang="ts">
|
|
|
-import { onLoad } from '@dcloudio/uni-app';
|
|
|
|
|
|
|
+import { onLoad, onShareAppMessage } from '@dcloudio/uni-app';
|
|
|
import { ref } from 'vue';
|
|
import { ref } from 'vue';
|
|
|
import globalConfig from '@/config/global';
|
|
import globalConfig from '@/config/global';
|
|
|
import * as ww from '@wecom/jssdk';
|
|
import * as ww from '@wecom/jssdk';
|
|
@@ -12,26 +12,93 @@ import type { WecomVisitorProfile } from '@/api/wecom/types';
|
|
|
import { ensureBusinessAuthByVisitorProfile } from '@/utils/businessAuth/ensure'
|
|
import { ensureBusinessAuthByVisitorProfile } from '@/utils/businessAuth/ensure'
|
|
|
|
|
|
|
|
const AUTH_PAGE = '/pages/index/index'
|
|
const AUTH_PAGE = '/pages/index/index'
|
|
|
|
|
+const HOME_PAGE = '/pages/home/index'
|
|
|
|
|
+const OAUTH_QUERY_KEYS = ['code', 'state']
|
|
|
|
|
+
|
|
|
|
|
+const removeOAuthParams = (params: URLSearchParams) => {
|
|
|
|
|
+ let changed = false
|
|
|
|
|
+ OAUTH_QUERY_KEYS.forEach((key) => {
|
|
|
|
|
+ if (params.has(key)) {
|
|
|
|
|
+ params.delete(key)
|
|
|
|
|
+ changed = true
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ return changed
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const getHashSearchParams = () => {
|
|
|
|
|
+ if (typeof window === 'undefined') return null
|
|
|
|
|
+
|
|
|
|
|
+ const hash = window.location.hash.slice(1)
|
|
|
|
|
+ const queryStart = hash.indexOf('?')
|
|
|
|
|
+ if (queryStart === -1) return null
|
|
|
|
|
+
|
|
|
|
|
+ return new URLSearchParams(hash.slice(queryStart + 1))
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
const getCodeFromLocation = () => {
|
|
const getCodeFromLocation = () => {
|
|
|
if (typeof window === 'undefined') return ''
|
|
if (typeof window === 'undefined') return ''
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(window.location.search)
|
|
const searchParams = new URLSearchParams(window.location.search)
|
|
|
- return searchParams.get('code') || ''
|
|
|
|
|
|
|
+ return searchParams.get('code') || getHashSearchParams()?.get('code') || ''
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const stripOAuthCodeFromUrl = () => {
|
|
const stripOAuthCodeFromUrl = () => {
|
|
|
if (typeof window === 'undefined') return
|
|
if (typeof window === 'undefined') return
|
|
|
const url = new URL(window.location.href)
|
|
const url = new URL(window.location.href)
|
|
|
- const hadOAuthParams = url.searchParams.has('code') || url.searchParams.has('state')
|
|
|
|
|
- if (!hadOAuthParams) return
|
|
|
|
|
- url.searchParams.delete('code')
|
|
|
|
|
- url.searchParams.delete('state')
|
|
|
|
|
|
|
+ const changedSearch = removeOAuthParams(url.searchParams)
|
|
|
|
|
+
|
|
|
|
|
+ let changedHash = false
|
|
|
|
|
+ const rawHash = url.hash.slice(1)
|
|
|
|
|
+ const queryStart = rawHash.indexOf('?')
|
|
|
|
|
+ if (queryStart !== -1) {
|
|
|
|
|
+ const hashPath = rawHash.slice(0, queryStart)
|
|
|
|
|
+ const hashParams = new URLSearchParams(rawHash.slice(queryStart + 1))
|
|
|
|
|
+ changedHash = removeOAuthParams(hashParams)
|
|
|
|
|
+ if (changedHash) {
|
|
|
|
|
+ const nextHashSearch = hashParams.toString()
|
|
|
|
|
+ url.hash = `${hashPath}${nextHashSearch ? `?${nextHashSearch}` : ''}`
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!changedSearch && !changedHash) return
|
|
|
const nextSearch = url.searchParams.toString()
|
|
const nextSearch = url.searchParams.toString()
|
|
|
const next = `${url.pathname}${nextSearch ? `?${nextSearch}` : ''}${url.hash}`
|
|
const next = `${url.pathname}${nextSearch ? `?${nextSearch}` : ''}${url.hash}`
|
|
|
window.history.replaceState({}, '', next)
|
|
window.history.replaceState({}, '', next)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+const getCleanHomeUrl = () =>
|
|
|
|
|
+ typeof window === 'undefined'
|
|
|
|
|
+ ? HOME_PAGE
|
|
|
|
|
+ : `${window.location.origin}/#${HOME_PAGE}`
|
|
|
|
|
+
|
|
|
|
|
+const setupCleanShareUrl = () => {
|
|
|
|
|
+ if (typeof window === 'undefined') return
|
|
|
|
|
+
|
|
|
|
|
+ const shareData = {
|
|
|
|
|
+ title: '首页',
|
|
|
|
|
+ desc: '江北两度智治',
|
|
|
|
|
+ link: getCleanHomeUrl(),
|
|
|
|
|
+ imgUrl: '',
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ ww.register({
|
|
|
|
|
+ corpId: globalConfig.corpid,
|
|
|
|
|
+ agentId: globalConfig.agentId,
|
|
|
|
|
+ jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'],
|
|
|
|
|
+ })
|
|
|
|
|
+ void ww.updateAppMessageShareData(shareData).catch((err) => {
|
|
|
|
|
+ console.warn('分享给朋友配置失败:', err)
|
|
|
|
|
+ })
|
|
|
|
|
+ void ww.updateTimelineShareData(shareData).catch((err) => {
|
|
|
|
|
+ console.warn('分享到朋友圈配置失败:', err)
|
|
|
|
|
+ })
|
|
|
|
|
+ } catch (err) {
|
|
|
|
|
+ console.warn('分享链接配置失败:', err)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
const externalUserid = ref('');
|
|
const externalUserid = ref('');
|
|
|
const unionidValue = ref('');
|
|
const unionidValue = ref('');
|
|
|
const ownerId = ref('');
|
|
const ownerId = ref('');
|
|
@@ -105,6 +172,8 @@ const goReauthorize = () => {
|
|
|
|
|
|
|
|
onLoad(async (option) => {
|
|
onLoad(async (option) => {
|
|
|
const code = (option && option.code) || getCodeFromLocation()
|
|
const code = (option && option.code) || getCodeFromLocation()
|
|
|
|
|
+ stripOAuthCodeFromUrl()
|
|
|
|
|
+ setupCleanShareUrl()
|
|
|
|
|
|
|
|
if (code) {
|
|
if (code) {
|
|
|
try {
|
|
try {
|
|
@@ -120,8 +189,6 @@ onLoad(async (option) => {
|
|
|
} else {
|
|
} else {
|
|
|
goReauthorize()
|
|
goReauthorize()
|
|
|
}
|
|
}
|
|
|
- } finally {
|
|
|
|
|
- stripOAuthCodeFromUrl()
|
|
|
|
|
}
|
|
}
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -137,6 +204,11 @@ onLoad(async (option) => {
|
|
|
goReauthorize()
|
|
goReauthorize()
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+onShareAppMessage(() => ({
|
|
|
|
|
+ title: '首页',
|
|
|
|
|
+ path: HOME_PAGE,
|
|
|
|
|
+}))
|
|
|
|
|
+
|
|
|
const getQuestionPage = () => {
|
|
const getQuestionPage = () => {
|
|
|
const ownerQuery = ownerId.value ? `?owner=${encodeURIComponent(ownerId.value)}` : ''
|
|
const ownerQuery = ownerId.value ? `?owner=${encodeURIComponent(ownerId.value)}` : ''
|
|
|
uni.navigateTo({
|
|
uni.navigateTo({
|