fix+feat: 小程序登录按钮不渲染修复(isMiniApp编译期判定);静默登录ensureLogin——收藏/游客问答额度升级一键无感登录;版本v1.4.6

This commit is contained in:
Hermes Agent
2026-09-19 21:16:00 +08:00
parent 65c6802995
commit df52c039d8
6 changed files with 64 additions and 21 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "宇之然AI维度",
"appid": "wxdad62baf4ccd09e3",
"versionName": "1.4.5",
"versionName": "1.4.6",
"versionCode": "100",
"description": "AI 知识探索社区 — 从5个维度系统理解人工智能",
"uni-app": {
+8 -1
View File
@@ -208,8 +208,15 @@ function goLogin() {
uni.navigateTo({ url: '/pages/login/login' })
}
function onQuotaTap() {
async function onQuotaTap() {
if (isGuest.value) {
// 静默登录一键升级(微信 code 换 token 无感),失败再显式引导
const ok = await userStore.ensureLogin()
if (ok) {
uni.showToast({ title: '已升级为登录用户,每天 5 次', icon: 'success' })
loadQuota()
return
}
uni.showModal({
title: '游客体验',
content: '每天可免费提问 3 次;登录后每天 5 次,答对检测题还能点亮知识星球',
+15 -11
View File
@@ -229,18 +229,22 @@ function retryQuiz() {
async function toggleCollect() {
if (!userStore.isLoggedIn) {
uni.showModal({
title: '登录后收藏',
content: '登录后即可收藏喜欢的 AI 知识',
confirmText: '去登录',
cancelText: '暂不',
success: (res) => {
if (res.confirm) {
uni.navigateTo({ url: '/pages/login/login' })
// 静默登录(微信 code 换 token,用户无感);失败再显式引导
const ok = await userStore.ensureLogin()
if (!ok) {
uni.showModal({
title: '登录后收藏',
content: '登录后即可收藏喜欢的 AI 知识',
confirmText: '去登录',
cancelText: '暂不',
success: (res) => {
if (res.confirm) {
uni.navigateTo({ url: '/pages/login/login' })
}
}
}
})
return
})
return
}
}
try {
if (isCollected.value) {
+5 -7
View File
@@ -135,14 +135,12 @@ import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { useUserStore } from '@/stores/index'
const isMiniApp = ref(false)
// 编译期判定:MP-WEIXIN 构建即小程序(运行时系统信息与按钮显示无关,避免旧API字段缺失导致按钮不渲染)
// #ifdef MP-WEIXIN
try {
const sysInfo = uni.getSystemInfoSync()
if (sysInfo && sysInfo.version && sysInfo.platform) {
isMiniApp.value = true
}
} catch (e) {}
const isMiniApp = ref(true)
// #endif
// #ifndef MP-WEIXIN
const isMiniApp = ref(false)
// #endif
const activeTab = ref('login')
+13 -1
View File
@@ -173,7 +173,19 @@ function goBack() {
uni.navigateBack()
}
function onFavorite() {
async function onFavorite() {
// 静默登录后提示(收藏态由详情页统一管理)
const ok = await userStore.ensureLogin()
if (!ok) {
uni.showModal({
title: '登录后收藏',
content: '登录后即可收藏资讯',
confirmText: '去登录',
cancelText: '暂不',
success: (res) => { if (res.confirm) uni.navigateTo({ url: '/pages/login/login' }) }
})
return
}
isCollected.value = !isCollected.value
uni.showToast({ title: isCollected.value ? '已收藏' : '已取消收藏', icon: 'none' })
}
+22
View File
@@ -28,6 +28,27 @@ export const useUserStore = defineStore('user', () => {
return data
}
/**
* 静默登录(微信小程序):code 换 token,用户无感知。
* 已登录直接返回 true;失败返回 false(调用方再引导显式登录)。
*/
async function ensureLogin() {
if (token.value) return true
// #ifdef MP-WEIXIN
try {
const loginRes = await new Promise((resolve, reject) => {
uni.login({ success: resolve, fail: reject })
})
if (!loginRes.code) return false
await login(loginRes.code, {})
return true
} catch (e) { return false }
// #endif
// #ifndef MP-WEIXIN
return false
// #endif
}
async function h5Login(loginData) {
const data = await authApi.h5Login(loginData)
token.value = data.token
@@ -110,6 +131,7 @@ export const useUserStore = defineStore('user', () => {
isSubscribed,
subscriptionLevel,
login,
ensureLogin,
h5Login,
h5Register,
fetchMe,