勾选同意后,一键登录无弹窗,

查看协议后不会打开其他协议
This commit is contained in:
WindowBird 2025-09-04 14:07:03 +08:00
parent 1d565c9531
commit 9b60a142da

View File

@ -79,7 +79,7 @@
<script>
import { wxLogin } from '@/api/auth/auth.js'
import { forceHideLoading, AutoLoadingManager } from '@/utils/request.js'
import { forceHideLoading } from '@/utils/request.js'
import { getServiceTerms, getPrivacyPolicy } from '@/api/article/article.js'
import { commonEnum } from '../../enum/commonEnum'
import { getNavBarHeight, getTitleBarHeight } from '../../utils/system'
@ -142,14 +142,7 @@ export default {
return match ? decodeURIComponent(match[1]) : null
},
toggleAgreement() {
if (this.hasReadServiceTerms && this.hasReadPrivacyPolicy) {
this.hasAgreed = !this.hasAgreed
} else {
uni.showToast({
title: '请先阅读服务条款和隐私政策',
icon: 'none',
})
}
this.hasAgreed = !this.hasAgreed
},
async showServiceTerms() {
@ -214,24 +207,12 @@ export default {
this.hasReadServiceTerms = true
this.closeServiceTermsPopup()
this.checkAgreementStatus()
if (!this.hasReadPrivacyPolicy) {
setTimeout(() => {
this.showPrivacyPolicy()
}, 500)
}
},
async agreePrivacyPolicy() {
this.hasReadPrivacyPolicy = true
this.closePrivacyPolicyPopup()
this.checkAgreementStatus()
if (!this.hasReadServiceTerms) {
setTimeout(() => {
this.showServiceTerms()
}, 500)
}
},
checkAgreementStatus() {
@ -240,94 +221,129 @@ export default {
}
},
async showUnreadTerms() {
if (!this.hasReadServiceTerms) {
await this.showServiceTerms()
return
}
if (!this.hasReadPrivacyPolicy) {
await this.showPrivacyPolicy()
}
},
goToLogin() {
this.$uv.throttle(this.Login, 3000)
},
Login() {
async Login() {
console.log('开始登录流程')
this.showLoading()
//
try {
// 1.
if (!this.hasAgreed) {
const confirmed = await this.showLoginConfirmation()
if (!confirmed) return
}
// 2. code
const wxCode = await this.getWxLoginCode()
// 3.
const loginResult = await this.executeLogin(wxCode)
// 4.
this.handleLoginSuccess(loginResult)
} catch (error) {
this.handleLoginError(error)
} finally {
this.hideLoading()
}
},
//
showLoading() {
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,
})
})
//
hideLoading() {
this.pageLoading?.hide()
forceHideLoading()
},
if (!loginRes.code) {
throw new Error('获取登录code失败')
}
//
showLoginConfirmation() {
return new Promise(resolve => {
uni.showModal({
title: '确认登录',
content: '我已阅读并同意服务协议和隐私政策',
confirmText: '确定登录',
cancelText: '取消',
success: res => {
this.hasAgreed = res.confirm
resolve(res.confirm)
},
fail: err => {
console.error('弹窗操作失败:', err)
resolve(false)
},
})
})
},
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()
// code
getWxLoginCode() {
return new Promise((resolve, reject) => {
uni.login({
success: res => {
if (!res.code) {
reject(new Error('获取登录code失败'))
return
}
}
},
fail: err => {
console.error('弹窗操作失败:', err)
this.pageLoading?.hide()
console.log('获取到登录code:', res.code)
resolve(res.code)
},
fail: err => {
reject(new Error(`微信登录失败: ${err.errMsg}`))
},
})
})
},
//
async executeLogin(wxCode) {
console.log('携带的agentId:', this.agentId)
const loginData = {
loginCode: wxCode,
agentId: this.agentId,
}
const res = await wxLogin(loginData)
if (res.code !== 200) {
throw new Error(res.msg || '登录失败')
}
return res
},
//
handleLoginSuccess(loginResult) {
console.log('登录成功:', loginResult)
uni.setStorageSync('token', loginResult.token)
uni.showToast({
title: '登录成功',
icon: 'success',
duration: 1500,
complete: () => {
uni.switchTab({
url: '/pages/index/index',
})
},
})
},
//
handleLoginError(error) {
console.error('登录流程出错:', error)
uni.showToast({
title: error.message || '登录失败',
icon: 'none',
})
},
},
}
</script>