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 + } +}