From b5a58583bdf36a2d27a40814f5359a8a4e72b954 Mon Sep 17 00:00:00 2001 From: wuzilong <704541015@qq.com> Date: Fri, 18 Sep 2026 11:15:47 +0800 Subject: [PATCH] feat(miniprogram): add phone quick login button on login page. Co-authored-by: Cursor --- miniprogram/pages/login/login.vue | 142 +++++++++++++++++++++++++++++- miniprogram/stores/user.js | 4 +- 2 files changed, 142 insertions(+), 4 deletions(-) diff --git a/miniprogram/pages/login/login.vue b/miniprogram/pages/login/login.vue index a6a38ba86..4d23a282f 100644 --- a/miniprogram/pages/login/login.vue +++ b/miniprogram/pages/login/login.vue @@ -15,6 +15,7 @@ const isLoading = ref(false) const agreeProtocol = ref(false) const loginDisabled = computed(() => isLoading.value) const protocolDialogVisible = ref(false) +const protocolDialogForWechat = ref(false) let protocolDialogResolver = null onShow(() => { @@ -28,6 +29,7 @@ const loginPageStyle = computed(() => ({ '--theme-color': '#0a9f6d' })) const handleSubmit = async () => { if (!agreeProtocol.value) { + protocolDialogForWechat.value = false const shouldContinue = await new Promise((resolve) => { protocolDialogResolver = resolve protocolDialogVisible.value = true @@ -72,11 +74,61 @@ const toggleProtocolAgreement = () => { const resolveProtocolDialog = (confirmed) => { protocolDialogVisible.value = false + protocolDialogForWechat.value = false if (protocolDialogResolver) { protocolDialogResolver(confirmed) protocolDialogResolver = null } } + +const onWechatGate = () => { + if (isLoading.value) return + protocolDialogForWechat.value = true + protocolDialogVisible.value = true +} + +const onAgreeGetPhoneNumber = (e) => { + agreeProtocol.value = true + protocolDialogVisible.value = false + protocolDialogForWechat.value = false + onGetPhoneNumber(e) +} + +const onGetPhoneNumber = async (e) => { + if (isLoading.value) return + if (!agreeProtocol.value) { + onWechatGate() + return + } + const detail = (e && e.detail) || {} + const errMsg = detail.errMsg || '' + if (errMsg && errMsg.indexOf('ok') === -1) { + uni.showToast({ title: '需要授权手机号,或请使用账号密码登录', icon: 'none' }) + return + } + const phoneCode = detail.code + if (!phoneCode) { + uni.showToast({ title: '需要授权手机号,或请使用账号密码登录', icon: 'none' }) + return + } + isLoading.value = true + try { + const loginRes = await new Promise((resolve, reject) => { + uni.login({ provider: 'weixin', success: resolve, fail: reject }) + }) + const ok = await userStore.wechatLogin({ + loginCode: loginRes.code, + phoneCode + }) + if (ok) { + userStore.navigateToHome() + } + } catch (err) { + uni.showToast({ title: (err && err.message) || '登录失败', icon: 'none' }) + } finally { + isLoading.value = false + } +}