优化登录速度,减少点击繁琐文档,一键登录
This commit is contained in:
parent
d82cf5bc98
commit
b666713099
|
|
@ -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() {
|
||||
// 先检查是否同意协议
|
||||
|
||||
console.log('开始登录流程')
|
||||
|
||||
// 显示加载状态
|
||||
this.pageLoading?.show('登录中...')
|
||||
|
||||
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,
|
||||
})
|
||||
})
|
||||
|
||||
this.showUnreadTerms()
|
||||
return
|
||||
if (!loginRes.code) {
|
||||
throw new Error('获取登录code失败')
|
||||
}
|
||||
|
||||
// 使用箭头函数保持this指向
|
||||
uni.login({
|
||||
success: res => {
|
||||
if (res.code) {
|
||||
console.log('登录!', res)
|
||||
const data = {
|
||||
loginCode: res.code,
|
||||
agentId: this.agentId, // 携带agentId
|
||||
console.log('获取到登录code:', loginRes.code)
|
||||
console.log('携带的agentId:', this.agentId)
|
||||
|
||||
// 2. 调用登录接口
|
||||
const loginData = {
|
||||
loginCode: loginRes.code,
|
||||
agentId: this.agentId,
|
||||
}
|
||||
|
||||
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()
|
||||
const res = await wxLogin(loginData)
|
||||
|
||||
if (res.code === 200) {
|
||||
console.log(res, 'resres')
|
||||
console.log('登录成功:', res)
|
||||
uni.setStorageSync('token', res.token)
|
||||
|
||||
uni.showToast({
|
||||
title: '登录成功',
|
||||
icon: 'success',
|
||||
duration: 1500,
|
||||
})
|
||||
complete: () => {
|
||||
uni.switchTab({
|
||||
url: '/pages/index/index',
|
||||
})
|
||||
console.log('123')
|
||||
},
|
||||
})
|
||||
} else {
|
||||
throw new Error(res.msg || '登录失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('登录流程出错:', error)
|
||||
uni.showToast({
|
||||
title: res.msg || '登录失败',
|
||||
title: error.message || '登录失败',
|
||||
icon: 'none',
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
if (this.pageLoading) {
|
||||
this.pageLoading.hide()
|
||||
}
|
||||
} finally {
|
||||
this.pageLoading?.hide()
|
||||
forceHideLoading()
|
||||
|
||||
console.error('登录失败:', error)
|
||||
uni.showToast({
|
||||
title: '登录失败',
|
||||
icon: 'none',
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
console.error('微信登录失败:', err)
|
||||
uni.showToast({
|
||||
title: '微信登录失败',
|
||||
icon: 'none',
|
||||
})
|
||||
console.error('弹窗操作失败:', err)
|
||||
this.pageLoading?.hide()
|
||||
},
|
||||
})
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user