优化登录速度,减少点击繁琐文档,一键登录

This commit is contained in:
WindowBird 2025-09-02 16:47:08 +08:00
parent d82cf5bc98
commit b666713099

View File

@ -9,7 +9,7 @@
</view>
<view class="main-content">
<!-- 登录按钮 -->
<button :disabled="loginLoading" class="login-btn" @click="getPhoneNumber">
<button :disabled="loginLoading" class="login-btn" @click="goToLogin">
<text class="btn-text">{{ loginLoading ? '登录中...' : '微信用户一键登录' }}</text>
</button>
</view>
@ -247,81 +247,78 @@ export default {
}
},
getPhoneNumber() {
if (!this.hasReadServiceTerms || !this.hasReadPrivacyPolicy) {
uni.showToast({
title: '请先阅读服务条款和隐私政策',
icon: 'none',
duration: 2000,
})
goToLogin() {
//
this.showUnreadTerms()
return
}
console.log('开始登录流程')
// 使this
uni.login({
success: res => {
if (res.code) {
console.log('登录!', res)
const data = {
loginCode: res.code,
agentId: this.agentId, // agentId
}
//
this.pageLoading?.show('登录中...')
console.log('登录请求携带的agentId:', this.agentId)
console.log('登录请求数据:', data)
if (this.pageLoading) {
this.pageLoading.show('登录中...')
}
wxLogin(data)
.then(res => {
if (this.pageLoading) {
this.pageLoading.hide()
}
forceHideLoading()
if (res.code === 200) {
console.log(res, 'resres')
uni.setStorageSync('token', res.token)
uni.showToast({
title: '登录成功',
icon: 'success',
duration: 1500,
})
uni.switchTab({
url: '/pages/index/index',
})
console.log('123')
} else {
uni.showToast({
title: res.msg || '登录失败',
icon: 'none',
})
}
})
.catch(error => {
if (this.pageLoading) {
this.pageLoading.hide()
}
forceHideLoading()
console.error('登录失败:', error)
uni.showToast({
title: '登录失败',
icon: 'none',
uni.showModal({
title: '确认登录',
content: '我已阅读并同意服务协议和隐私政策',
confirmText: '确定登录',
cancelText: '取消',
success: async modalRes => {
if (modalRes.confirm) {
this.hasAgreed = true
try {
// 1. code
const loginRes = await new Promise((resolve, reject) => {
uni.login({
success: resolve,
fail: reject,
})
})
if (!loginRes.code) {
throw new Error('获取登录code失败')
}
console.log('获取到登录code:', loginRes.code)
console.log('携带的agentId:', this.agentId)
// 2.
const loginData = {
loginCode: loginRes.code,
agentId: this.agentId,
}
const res = await wxLogin(loginData)
if (res.code === 200) {
console.log('登录成功:', res)
uni.setStorageSync('token', res.token)
uni.showToast({
title: '登录成功',
icon: 'success',
duration: 1500,
complete: () => {
uni.switchTab({
url: '/pages/index/index',
})
},
})
} else {
throw new Error(res.msg || '登录失败')
}
} catch (error) {
console.error('登录流程出错:', error)
uni.showToast({
title: error.message || '登录失败',
icon: 'none',
})
} finally {
this.pageLoading?.hide()
forceHideLoading()
}
}
},
fail: err => {
console.error('微信登录失败:', err)
uni.showToast({
title: '微信登录失败',
icon: 'none',
})
console.error('弹窗操作失败:', err)
this.pageLoading?.hide()
},
})
},