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