feat: 微信支付宝登录对接

This commit is contained in:
2025-09-23 23:19:16 +08:00
parent 0f033c1d8d
commit a22b987ed9
7 changed files with 463 additions and 492 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
"version" : "1.0", "version" : "1.0",
"configurations" : [ "configurations" : [
{ {
"playground" : "standard", "playground" : "custom",
"type" : "uni-app:app-android" "type" : "uni-app:app-android"
} }
] ]
+6 -3
View File
@@ -149,16 +149,19 @@ export function bindInvitationCode(params) {
// 微信一键登录 // 微信一键登录
export function wechatLogin(params) { export function wechatLogin(params) {
return request({ return request({
url: `/auth/loginByWechat?wechatCode=${params.wechatCode}&isValid=${params.isValid}`, url: `/auth/loginByWechat`,
method: "post" method: "post",
data: params,
cha:1
}); });
} }
// 支付宝一键登录 // 支付宝一键登录
export function alipayLogin(params) { export function alipayLogin(params) {
return request({ return request({
url: "/auth/loginByAlipay", url: "/auth/loginByAlipayNew",
method: "post", method: "post",
data: params, data: params,
cha:1
}); });
} }
+2 -1
View File
@@ -82,7 +82,8 @@
"weixin" : { "weixin" : {
"appid" : "wx7a5902cd556d9396", "appid" : "wx7a5902cd556d9396",
"UniversalLinks" : "https://app.tbmall.xin/uni-universallinks/__UNI__4910728/" "UniversalLinks" : "https://app.tbmall.xin/uni-universallinks/__UNI__4910728/"
} },
"apple" : {}
} }
}, },
"icons" : { "icons" : {
+15 -19
View File
@@ -62,7 +62,7 @@
<script> <script>
import { SMS_TYPE } from "@/utils/enumUtils.js"; import { SMS_TYPE } from "@/utils/enumUtils.js";
import { getUserList } from "@/api/common.js"; import { getUserList } from "@/api/common.js";
import { wechatLogin } from "@/api/auth.js"; import { wechatLogin,alipayLogin } from "@/api/auth.js";
import { import {
setStorageFun, setStorageFun,
TOKEN_NAME, TOKEN_NAME,
@@ -77,7 +77,7 @@ export default {
form: { form: {
mobile: null, mobile: null,
smsCode: null, // 验证码 smsCode: null, // 验证码
wechatCode: null, token: null,
}, },
inputCss: { inputCss: {
height: "80rpx", height: "80rpx",
@@ -89,17 +89,14 @@ export default {
countdown: false, // 清空倒计时 countdown: false, // 清空倒计时
butLoading: false, butLoading: false,
disabled: false, disabled: false,
type: 1, //1.微信 2.支付宝
}; };
}, },
onLoad() {
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const id = currentPage.options?.key;
if (id) {
this.disabled = true;
}
this.form.wechatCode = id; onLoad(option) {
this.form.token = option.token;
this.type = option.type;
console.log("-----1", option, this.form.token, this.type );
}, },
methods: { methods: {
jumpMine() { jumpMine() {
@@ -130,13 +127,12 @@ export default {
mobile: form.mobile, mobile: form.mobile,
smsCode: form.smsCode, smsCode: form.smsCode,
isValid: true, isValid: true,
wechatCode: form.id, token: form.token,
}; };
const response = this.type == 1 ? await wechatLogin(params) : await alipayLogin(params);
const response = await wechatLogin(params); console.log("微信登录接口返回:", params, response);
console.log("微信登录接口返回:", response); if (response && response.bizcode === 100) {
if (resp && resp.bizcode === 100) { const data = response.data;
const data = resp.data;
setStorageFun(TOKEN_NAME, data.token); setStorageFun(TOKEN_NAME, data.token);
// 用于无感登录 // 用于无感登录
setStorageFun(USER_KEY, ""); setStorageFun(USER_KEY, "");
@@ -153,7 +149,7 @@ export default {
setStorageFun(USER_LOGIN_TIME, currentTimeStamp); setStorageFun(USER_LOGIN_TIME, currentTimeStamp);
this.$refs.uToastRef.show({ this.$refs.uToastRef.show({
type: "success", type: "success",
message: resp.msg, message: response.msg,
}); });
setTimeout(() => { setTimeout(() => {
uni.switchTab({ uni.switchTab({
@@ -180,7 +176,7 @@ export default {
const params = { const params = {
mobile: form.mobile, mobile: form.mobile,
type: SMS_TYPE.WECHAT, type:this.type == 1 ? SMS_TYPE.WECHAT : SMS_TYPE.ALIPAY,
}; };
const response = await getUserList(params); const response = await getUserList(params);
if (response && response.bizcode === 100) { if (response && response.bizcode === 100) {
@@ -200,7 +196,7 @@ export default {
.header_ { .header_ {
display: flex; display: flex;
align-items: center; align-items: center;
padding: 0 30rpx; padding: calc(24rpx + env(safe-area-inset-bottom)) 30rpx 0 30rpx;
.title_ { .title_ {
font-weight: bold; font-weight: bold;
font-size: 36rpx; font-size: 36rpx;
+70 -101
View File
@@ -2,13 +2,7 @@
<view class="login_warp"> <view class="login_warp">
<view class="login_logo_warp"> <view class="login_logo_warp">
<view style="text-align: center"> <view style="text-align: center">
<up-image <up-image src="/static/logo.png" width="90" height="118" bgColor="#f1f6ff00" radius="10"></up-image>
src="/static/logo.png"
width="90"
height="118"
bgColor="#f1f6ff00"
radius="10"
></up-image>
</view> </view>
<!-- <view style="margin-top: 20rpx;"> <!-- <view style="margin-top: 20rpx;">
<up-image src="/static/logo_z.png" width="100" height="40" bgColor="#f1f6ff00"></up-image> <up-image src="/static/logo_z.png" width="100" height="40" bgColor="#f1f6ff00"></up-image>
@@ -16,59 +10,27 @@
</view> </view>
<view class="login_info"> <view class="login_info">
<view class="login_info_item"> <view class="login_info_item">
<up-input <up-input placeholder="请输入手机号/账户名称" border="surround" v-model="form.name" :customStyle="inputCss"
placeholder="请输入手机号/账户名称" @change="checkNameParam"></up-input>
border="surround"
v-model="form.name"
:customStyle="inputCss"
@change="checkNameParam"
></up-input>
</view> </view>
<!-- 密码登录 --> <!-- 密码登录 -->
<view class="login_info_item" v-if="loginWay === 'password'"> <view class="login_info_item" v-if="loginWay === 'password'">
<up-input <up-input placeholder="请输入密码" border="surround" v-model="form.password" :customStyle="inputCss"
placeholder="请输入密码" :type="isShow ? 'text' : 'password'" @change="checkParam">
border="surround"
v-model="form.password"
:customStyle="inputCss"
:type="isShow ? 'text' : 'password'"
@change="checkParam"
>
<template #suffix style="margin-right: 10rpx"> <template #suffix style="margin-right: 10rpx">
<up-image <up-image v-if="isShow" src="/static/login/show.png" width="20" height="20" bgColor="#f1f6ff00"
v-if="isShow" @click="isShow = false"></up-image>
src="/static/login/show.png" <up-image v-else src="/static/login/hidden.png" width="20" height="20" bgColor="#f1f6ff00"
width="20" @click="isShow = true"></up-image>
height="20"
bgColor="#f1f6ff00"
@click="isShow = false"
></up-image>
<up-image
v-else
src="/static/login/hidden.png"
width="20"
height="20"
bgColor="#f1f6ff00"
@click="isShow = true"
></up-image>
</template> </template>
</up-input> </up-input>
</view> </view>
<!-- 验证码登录 --> <!-- 验证码登录 -->
<view class="login_info_item" v-else> <view class="login_info_item" v-else>
<up-input <up-input placeholder="请输入验证码" border="surround" v-model="form.code" :customStyle="inputCss">
placeholder="请输入验证码"
border="surround"
v-model="form.code"
:customStyle="inputCss"
>
<template #suffix style="margin-right: 10rpx"> <template #suffix style="margin-right: 10rpx">
<view v-if="countdown" class="code-warp"> <view v-if="countdown" class="code-warp">
<up-count-down <up-count-down :time="60 * 1000" format="ss" @finish="countdown = false"></up-count-down>
:time="60 * 1000"
format="ss"
@finish="countdown = false"
></up-count-down>
<text style="margin-left: 10rpx">s</text> <text style="margin-left: 10rpx">s</text>
</view> </view>
<view v-else @click="getMobileCode" class="code_msg"> <view v-else @click="getMobileCode" class="code_msg">
@@ -82,62 +44,35 @@
<text style="margin-left: 10rpx">登录</text> <text style="margin-left: 10rpx">登录</text>
</view> </view>
<view class="cut_way_warp"> <view class="cut_way_warp">
<view v-if="loginWay === 'password'" @click="loginWay = 'code'" <view v-if="loginWay === 'password'" @click="loginWay = 'code'">验证码登录</view>
>验证码登录</view <view v-if="loginWay === 'code'" @click="loginWay = 'password'">密码登录登录</view>
>
<view v-if="loginWay === 'code'" @click="loginWay = 'password'"
>密码登录登录</view
>
<view @click="jumpForgot">忘记密码</view> <view @click="jumpForgot">忘记密码</view>
<view @click="jumpRegister">注册</view> <view @click="jumpRegister">注册</view>
</view> </view>
</view> </view>
<view> <view v-if="!isIOS">
<view class="other_login">其他登录方式</view> <view class="other_login">其他登录方式</view>
<view class="login_other_icon"> <view class="login_other_icon">
<view class="login_other_item" @click="wechatOneLogin"> <view class="login_other_item" @click="wechatOneLogin">
<up-image <up-image src="/static/login/WXICON.png" width="44" height="44" bgColor="#f1f6ff00"></up-image>
src="/static/login/WXICON.png"
width="44"
height="44"
bgColor="#f1f6ff00"
></up-image>
微信登录 微信登录
</view> </view>
<!-- <view class="login_other_item" @click="alipayOneLogin"> <view class="login_other_item" @click="alipayOneLogin">
<up-image <up-image src="/static/login/ZFB.png" width="44" height="44" bgColor="#f1f6ff00"></up-image>
src="/static/login/ZFB.png"
width="44"
height="44"
bgColor="#f1f6ff00"
></up-image>
支付宝登录 支付宝登录
</view> --> </view>
</view> </view>
</view> </view>
<view class="policy_warp"> <view class="policy_warp">
<up-checkbox <up-checkbox name="isCheck" usedAlone v-model:checked="isPolicy" shape="circle" activeColor="#7934F6"
name="isCheck" size="19" iconSize="18">
usedAlone
v-model:checked="isPolicy"
shape="circle"
activeColor="#7934F6"
size="19"
iconSize="18"
>
<template #label> <template #label>
<text>已阅读并同意</text> <text>已阅读并同意</text>
<text class="policy_a_warp" @click="jumpUserAgreement" <text class="policy_a_warp" @click="jumpUserAgreement">《服务协议》</text>
>《服务协议》</text
>
<text>、</text> <text>、</text>
<text class="policy_a_warp" @click="jumpPrivacyAgreement" <text class="policy_a_warp" @click="jumpPrivacyAgreement">《隐私政策》</text>
>《隐私政策》</text
>
<text>、</text> <text>、</text>
<text class="policy_a_warp" @click="jumpProductAgreement" <text class="policy_a_warp" @click="jumpProductAgreement">《售后保障协议》</text>
>《售后保障协议》</text
>
</template> </template>
</up-checkbox> </up-checkbox>
</view> </view>
@@ -147,9 +82,18 @@
<script> <script>
import InputCodeComm from "@/components/code-comm/code-comm.vue"; import InputCodeComm from "@/components/code-comm/code-comm.vue";
import { SMS_TYPE } from "@/utils/enumUtils.js"; import {
import { getUserList } from "@/api/common.js"; SMS_TYPE
import { loginbysms, loginbypw, wechatLogin, alipayLogin } from "@/api/auth.js"; } from "@/utils/enumUtils.js";
import {
getUserList
} from "@/api/common.js";
import {
loginbysms,
loginbypw,
wechatLogin,
alipayLogin
} from "@/api/auth.js";
import { import {
setStorageFun, setStorageFun,
TOKEN_NAME, TOKEN_NAME,
@@ -331,27 +275,33 @@ export default {
// 1. 调用微信登录API // 1. 调用微信登录API
uni.login({ uni.login({
provider: "weixin", provider: "weixin",
// onlyAuthorize: true, onlyAuthorize: true,
success: async (loginRes) => { success: async (loginRes) => {
console.log("微信登录成功:", loginRes); console.log("微信登录成功:", loginRes);
// 2. 发送code到后端换取token // 2. 发送code到后端换取token
const params = { const params = {
isValid: false, isValid: false,
wechatCode: loginRes.code, token: loginRes.code,
// 可以根据需要添加其他参数 // 可以根据需要添加其他参数
}; };
console.log("微信登录接口返回-1:", params);
const response = await wechatLogin(params); const response = await wechatLogin(params);
console.log("微信登录接口返回:", response); console.log("微信登录接口返回-2:", response, response.data, response.data.token);
if (response && response.bizcode === 100) { if (response && response.bizcode === 100) {
if (response.data.userId !== null) {
// 登录
this.loginWay = "wechat"; this.loginWay = "wechat";
this.loginFun(response); this.loginFun(response);
} else if (response.bizcode === 601) { } else {
// 去微信注册
uni.navigateTo({ uni.navigateTo({
url: `/pages/login_package/bind-phone/bind-phone?id=${loginRes.code}`, url: `/pages/login_package/bind-phone/bind-phone?token=${response.data.token}&type=1`,
}); });
}
} else { } else {
this.$refs.uToastRef.show({ this.$refs.uToastRef.show({
type: "error", type: "error",
@@ -394,21 +344,29 @@ export default {
return; return;
} }
hyyAlipay.openAuthScheme( hyyAlipay.openAuthScheme({
{
appId: "2021005183625053", // 支付宝分配给开发者的应用 ID appId: "2021005183625053", // 支付宝分配给开发者的应用 ID
scheme: "trustbridgeapp", // manifest.json -> "App常用其他设置" -> "UrlSchemes"里设置 scheme: "trustbridgeapp", // manifest.json -> "App常用其他设置" -> "UrlSchemes"里设置
}, },
(res) => { (res) => {
console.log("支付宝回调", res); console.log("支付宝回调", res);
alipayLogin({ alipayLogin({
certifyId: res.auth_code, isValid: false,
token: res.auth_code,
}).then((response) => { }).then((response) => {
console.log("支付宝登录接口返回:", response); console.log("支付宝登录接口返回:", response,response.data.userId !== null);
if (response && response.bizcode === 100) { if (response && response.bizcode === 100) {
if (response.data.userId !== null) {
// 登录
this.loginWay = "alipay"; this.loginWay = "alipay";
this.loginFun(response); this.loginFun(response);
} else {
// 去微信注册
uni.navigateTo({
url: `/pages/login_package/bind-phone/bind-phone?token=${response.data.token}&type=2`,
});
}
} else { } else {
this.$refs.uToastRef.show({ this.$refs.uToastRef.show({
type: "error", type: "error",
@@ -420,6 +378,14 @@ export default {
); );
}, },
}, },
computed: {
isIOS() {
// #ifdef APP-PLUS
return uni.getSystemInfoSync().platform === "ios";
// #endif
return false;
},
}
}; };
</script> </script>
@@ -473,6 +439,7 @@ export default {
color: #999999; color: #999999;
margin-top: 20rpx; margin-top: 20rpx;
} }
.other_login { .other_login {
font-size: 28rpx; font-size: 28rpx;
text-align: center; text-align: center;
@@ -480,10 +447,12 @@ export default {
margin-bottom: 32rpx; margin-bottom: 32rpx;
color: #999999; color: #999999;
} }
.login_other_icon { .login_other_icon {
display: flex; display: flex;
padding: 0 100rpx; padding: 0 100rpx;
justify-content: center; justify-content: center;
.login_other_item { .login_other_item {
display: flex; display: flex;
flex: 50%; flex: 50%;
+2 -1
View File
@@ -23,7 +23,8 @@ export const SMS_TYPE = {
"PASSWD": "passwd", "PASSWD": "passwd",
"LOGIN": "login", "LOGIN": "login",
"REGISTER": "register", "REGISTER": "register",
"WECHAT": "wechatValid" "WECHAT": "wechatValid",
"ALIPAY": "alipayValid"
} }
// 图片类型 图片分类:COMMON-公共,CATEGORY-商品类目图标,BRAND-品牌LOGO,SHOP-店铺LOGO,GOODS-商品图片,SPECS-商品规格图片 // 图片类型 图片分类:COMMON-公共,CATEGORY-商品类目图标,BRAND-品牌LOGO,SHOP-店铺LOGO,GOODS-商品图片,SPECS-商品规格图片
+2 -1
View File
@@ -46,6 +46,7 @@ function request(options) {
...headers, // 使用展开运算符合并默认头和自定义头 ...headers, // 使用展开运算符合并默认头和自定义头
}, },
success: (res) => { success: (res) => {
console.log('--success-',res)
if (isShowLoading) { if (isShowLoading) {
// 请求之前做一些事 // 请求之前做一些事
uni.hideToast(); uni.hideToast();
@@ -65,7 +66,7 @@ function request(options) {
return; return;
} }
// 601 微信登录没注册 // 601 微信登录没注册
if (data.bizcode === 100 || data.bizcode === 601) { if (data.bizcode === 100 ) {
resolve(data); // 成功时返回数据 resolve(data); // 成功时返回数据
} else if (data.bizcode === 201) { } else if (data.bizcode === 201) {
if ([201, 200].includes(data.errcode)) { if ([201, 200].includes(data.errcode)) {