支付宝兼容

This commit is contained in:
2026-03-09 09:07:58 +08:00
parent 069677957e
commit b3836b8bf2
31 changed files with 2382 additions and 307 deletions
+37 -8
View File
@@ -8,13 +8,27 @@
<view class="title">{{ $t('auth.loginTitle') }}</view>
<view class="subtitle">{{ $t('auth.loginDesc') }}</view>
<!-- 微信一键手机号快捷登录推荐 -->
<!-- 微信小程序一键手机号快捷登录 -->
<!-- #ifdef MP-WEIXIN -->
<button v-if="!isAgreed" class="btn primary" @click="handleLoginClick">
{{ $t('auth.getPhoneNumber') }}
</button>
<button v-else class="btn primary" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">
{{ $t('auth.getPhoneNumber') }}
</button>
<!-- #endif -->
<!-- 支付宝小程序授权码快捷登录不支持 open-type=getPhoneNumber -->
<!-- #ifdef MP-ALIPAY -->
<button v-if="!isAgreed" class="btn primary" @click="handleLoginClick">
{{ $t('auth.loginBtn') }}
</button>
<button v-else class="btn primary" @click="onAlipayLogin">
{{ $t('auth.loginBtn') }}
</button>
<!-- #endif -->
<!-- H5不显示小程序快捷登录按钮 -->
<!-- 手机号验证码登录 -->
<button class="btn outline" @click="goToPhoneLogin" v-if="isHTML5">
@@ -40,7 +54,7 @@
<script setup>
import { ref, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { wxLogin, getUserPhoneNumber, getUserInfo } from '@/util/index.js'
import { wxLogin, alipayLogin, getUserPhoneNumber, getUserInfo } from '@/util/index.js'
import { useI18n } from '@/utils/i18n.js'
const { t } = useI18n()
@@ -116,13 +130,12 @@
uni.reLaunch({ url: target })
}
// 微信快捷登录入口(备用,目前主流程使用一键手机号登录)
const onWeChatLogin = async () => {
try {
// 先检查是否同意协议
await checkAgreement()
await wxLogin()
uni.showToast({ title: t('auth.loginSuccess'), icon: 'success' })
await wxLogin()
uni.showToast({ title: t('auth.loginSuccess'), icon: 'success' })
await navigateAfterLogin()
} catch (error) {
if (error.message !== t('auth.pleaseAgreeToTerms')) {
@@ -131,6 +144,22 @@
}
}
// 支付宝快捷登录入口(支付宝小程序)
const onAlipayLogin = async () => {
try {
await checkAgreement()
await alipayLogin()
uni.showToast({ title: t('auth.loginSuccess'), icon: 'success' })
await navigateAfterLogin()
} catch (error) {
if (error.message !== t('auth.pleaseAgreeToTerms')) {
uni.showToast({ title: error.message || t('auth.loginFailed'), icon: 'none' })
}
}
}
// 注意:手机号一致性校验不在登录页做;扫码/租借前校验(支付宝 my.getPhoneNumber
const onGetPhoneNumber = async (e) => {
if (!e || e.detail.errMsg !== 'getPhoneNumber:ok') {
uni.showToast({ title: t('auth.phoneCancelled'), icon: 'none' })
@@ -138,9 +167,9 @@
}
try {
// 先微信登录,获取 token
// 先完成微信登录/app/user/quickLogin,后端建立/查询 WECHAT_MINI 用户)
await wxLogin()
// 再用微信返回的临时 code 换取手机号
// 再用微信返回的临时 code 换取手机号(后端按文档更新 phone 字段)
await getUserPhoneNumber(e.detail.code)
uni.showToast({ title: t('auth.loginSuccess'), icon: 'success' })
await navigateAfterLogin()