fix(mp): handle mini-program launch params and pre-create share code for user page

App.vue: add handleLaunchParams() to read token/shareCode from onLaunch/onShow query in MP-WEIXIN context. user.vue: pre-create share record on page load, use dynamic path with shareCode in onShareAppMessage.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
yuzhiran
2026-06-22 14:23:27 +08:00
parent d74fc74f28
commit 1a45822a58
2 changed files with 51 additions and 3 deletions
+23 -2
View File
@@ -1,15 +1,24 @@
<script setup lang="ts">
import { onLaunch } from '@dcloudio/uni-app'
import { onLaunch, onShow } from '@dcloudio/uni-app'
onLaunch(() => {
onLaunch((options) => {
// #ifdef MP-WEIXIN
initPrivacy()
handleLaunchParams(options?.query)
// #endif
// #ifdef H5
handleH5UrlParams()
// #endif
})
// #ifdef MP-WEIXIN
onShow((options) => {
if (options?.query) {
handleLaunchParams(options.query)
}
})
// #endif
// #ifdef H5
function handleH5UrlParams() {
const params = new URLSearchParams(window.location.search)
@@ -28,6 +37,18 @@ function handleH5UrlParams() {
// #endif
// #ifdef MP-WEIXIN
function handleLaunchParams(query?: Record<string, string>) {
if (!query) return
const token = query.token
if (token) {
uni.setStorageSync('token', token)
}
const shareCode = query.share || query.shareCode
if (shareCode) {
uni.setStorageSync('shareCode', shareCode)
}
}
function initPrivacy() {
if (wx.onNeedPrivacyAuthorization) {
wx.onNeedPrivacyAuthorization((resolve) => {
+28 -1
View File
@@ -175,7 +175,13 @@ import { onShow } from '@dcloudio/uni-app'
import { api } from '../../config'
// #ifdef MP-WEIXIN
onShareAppMessage(() => ({ title: '职引 - 个人中心 | AI模拟面试', path: '/pages/user/user' }))
const shareCodeRef = ref('')
onShareAppMessage(() => {
const path = shareCodeRef.value
? `/pages/user/user?share=${shareCodeRef.value}`
: '/pages/user/user'
return { title: '职引 - 个人中心 | AI模拟面试', path }
})
onShareTimeline(() => ({ title: '职引 - 个人中心 | AI模拟面试' }))
// #endif
@@ -199,8 +205,29 @@ const refreshState = () => {
loadMemberStatus()
checkAdmin()
fetchUserInfo()
// #ifdef MP-WEIXIN
preCreateShare()
// #endif
}
// #ifdef MP-WEIXIN
async function preCreateShare() {
if (shareCodeRef.value) return // already have one
try {
const res = await uni.request({
url: api('/share/create'), method: 'POST',
data: { type: 'app', title: '我在AI磁场·职引练习面试', description: 'AI模拟面试+简历优化,快来一起提升吧' },
header: { Authorization: `Bearer ${token.value}` },
})
if (res.statusCode < 200 || res.statusCode >= 300) return
const data = res.data?.data || res.data
if (data.shareCode) {
shareCodeRef.value = data.shareCode
}
} catch(e) { /* silent */ }
}
// #endif
const fetchUserInfo = async () => {
try {
const res = await uni.request({ url: api('/user/info'), method: 'GET', header: { 'Authorization': `Bearer ${token.value}` } })