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

查看协议后不会打开其他协议
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> <script>
import { wxLogin } from '@/api/auth/auth.js' 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 { getServiceTerms, getPrivacyPolicy } from '@/api/article/article.js'
import { commonEnum } from '../../enum/commonEnum' import { commonEnum } from '../../enum/commonEnum'
import { getNavBarHeight, getTitleBarHeight } from '../../utils/system' import { getNavBarHeight, getTitleBarHeight } from '../../utils/system'
@ -142,14 +142,7 @@ export default {
return match ? decodeURIComponent(match[1]) : null return match ? decodeURIComponent(match[1]) : null
}, },
toggleAgreement() { toggleAgreement() {
if (this.hasReadServiceTerms && this.hasReadPrivacyPolicy) { this.hasAgreed = !this.hasAgreed
this.hasAgreed = !this.hasAgreed
} else {
uni.showToast({
title: '请先阅读服务条款和隐私政策',
icon: 'none',
})
}
}, },
async showServiceTerms() { async showServiceTerms() {
@ -214,24 +207,12 @@ export default {
this.hasReadServiceTerms = true this.hasReadServiceTerms = true
this.closeServiceTermsPopup() this.closeServiceTermsPopup()
this.checkAgreementStatus() this.checkAgreementStatus()
if (!this.hasReadPrivacyPolicy) {
setTimeout(() => {
this.showPrivacyPolicy()
}, 500)
}
}, },
async agreePrivacyPolicy() { async agreePrivacyPolicy() {
this.hasReadPrivacyPolicy = true this.hasReadPrivacyPolicy = true
this.closePrivacyPolicyPopup() this.closePrivacyPolicyPopup()
this.checkAgreementStatus() this.checkAgreementStatus()
if (!this.hasReadServiceTerms) {
setTimeout(() => {
this.showServiceTerms()
}, 500)
}
}, },
checkAgreementStatus() { checkAgreementStatus() {
@ -240,94 +221,129 @@ export default {
} }
}, },
async showUnreadTerms() {
if (!this.hasReadServiceTerms) {
await this.showServiceTerms()
return
}
if (!this.hasReadPrivacyPolicy) {
await this.showPrivacyPolicy()
}
},
goToLogin() { goToLogin() {
this.$uv.throttle(this.Login, 3000) this.$uv.throttle(this.Login, 3000)
}, },
Login() { async Login() {
console.log('开始登录流程') 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('登录中...') this.pageLoading?.show('登录中...')
},
uni.showModal({ //
title: '确认登录', hideLoading() {
content: '我已阅读并同意服务协议和隐私政策', this.pageLoading?.hide()
confirmText: '确定登录', forceHideLoading()
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失败') 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) // code
console.log('携带的agentId:', this.agentId) getWxLoginCode() {
return new Promise((resolve, reject) => {
// 2. uni.login({
const loginData = { success: res => {
loginCode: loginRes.code, if (!res.code) {
agentId: this.agentId, reject(new Error('获取登录code失败'))
} return
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()
} }
} console.log('获取到登录code:', res.code)
}, resolve(res.code)
fail: err => { },
console.error('弹窗操作失败:', err) fail: err => {
this.pageLoading?.hide() 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> </script>