Просмотр исходного кода

feat: onLoad 解析扫码参数,已登录时绑定渠道

Developer 1 день назад
Родитель
Сommit
e682f8d905
1 измененных файлов с 27 добавлено и 0 удалено
  1. 27 0
      pages/home.vue

+ 27 - 0
pages/home.vue

@@ -67,6 +67,33 @@
 	const { proxy } = getCurrentInstance()
 	import { useUserStore } from '@/common/stores/user';
 	const userStore = useUserStore();
+	import { onLoad } from '@dcloudio/uni-app'
+
+	const parseUrlParams = (url) => {
+		const result = {}
+		const queryStr = url.indexOf('?') !== -1 ? url.split('?')[1] : (url.indexOf('#') !== -1 ? url.split('#')[1] : '')
+		if (!queryStr) return result
+		queryStr.split('&').forEach(pair => {
+			const [key, val] = pair.split('=')
+			if (key) result[decodeURIComponent(key)] = decodeURIComponent(val || '')
+		})
+		return result
+	}
+
+	onLoad((options) => {
+		if (options && options.q) {
+			const url = decodeURIComponent(options.q)
+			const params = parseUrlParams(url)
+			const channelId = params.channel
+			if (!channelId) return
+			// 未登录不做任何操作
+			const token = uni.getStorageSync('token')
+			if (!token) return
+			proxy.$api.post(`/channel/bind?channelId=${channelId}`, {}).then(({ data: res }) => {
+				if (res.code !== 0) console.error('渠道绑定失败:', res.msg)
+			})
+		}
+	})
 	
 	const bannarList = ref([])
 	const bannarOrigin = ref([])