实现部分页面前后端相通

This commit is contained in:
2026-07-16 17:29:27 +08:00
parent 7e45ecd144
commit a9ccdab421
16 changed files with 778 additions and 184 deletions
+51 -2
View File
@@ -48,20 +48,69 @@
</template>
<script>
const authApi = require('../../api/auth')
const store = require('../../store/index')
export default {
data() {
return {
statusBarHeight: 0,
agreed: false
agreed: false,
loading: false
}
},
onLoad() {
const systemInfo = uni.getSystemInfoSync()
this.statusBarHeight = systemInfo.statusBarHeight || 20
// 已登录则直接跳转首页
if (store.isLoggedIn) {
uni.switchTab({ url: '/pages/index/index' })
}
},
methods: {
toggleAgree() { this.agreed = !this.agreed },
handleLogin() { uni.switchTab({ url: '/pages/index/index' }) }
handleLogin() {
if (!this.agreed) {
uni.showToast({ title: '请先同意用户协议', icon: 'none' })
return
}
if (this.loading) return
this.loading = true
// 微信小程序登录
// #ifdef MP-WEIXIN
uni.login({
provider: 'weixin',
success: (loginRes) => {
authApi.miniappLogin(loginRes.code)
.then((res) => {
store.setLogin(res.accessToken, { memberId: res.memberId, isNewUser: res.isNewUser })
uni.switchTab({ url: '/pages/index/index' })
})
.catch((err) => {
console.error('登录失败:', err)
uni.showToast({ title: '登录失败,请重试', icon: 'none' })
})
.finally(() => { this.loading = false })
},
fail: (err) => {
console.error('wx.login 失败:', err)
uni.showToast({ title: '获取微信授权失败', icon: 'none' })
this.loading = false
}
})
// #endif
// #ifndef MP-WEIXIN
// H5 / App 环境:使用模拟登录(开发调试)
uni.showToast({ title: '非小程序环境,使用模拟登录', icon: 'none' })
setTimeout(() => {
store.setLogin('dev-token-' + Date.now(), { memberId: 1, isNewUser: false })
uni.switchTab({ url: '/pages/index/index' })
this.loading = false
}, 500)
// #endif
}
}
}
</script>