uniapp【优化】: 支付宝登录功能

This commit is contained in:
15093570141
2024-10-25 10:46:21 +08:00
parent 45904fbf91
commit c79e481aa7
5 changed files with 70 additions and 8 deletions

View File

@@ -53,3 +53,21 @@
}
}
}
.alipay-btn {
color: #fff;
border: none;
background: none;
width: 100%;
font-size: 14px;
position: absolute;
left: 0px;
top: 0px;
z-index: 99;
height: 100%;
display: flex;
.btn-tit{
display: block;
margin: auto;
}
}

View File

@@ -32,8 +32,19 @@
text="暂不授权"></uv-button>
</view>
<view class="btn">
<!-- #ifdef MP-WEIXIN -->
<uv-button v-if="state.agreement" :customStyle="{ 'width': '100%' }" openType="getPhoneNumber"
@getphonenumber="getPhoneNumber" type="success" size="normal" text="确定授权"></uv-button>
<!-- #endif -->
<!-- #ifdef MP-ALIPAY -->
<uv-button v-if="state.agreement" :customStyle="{ 'width': '100%' ,'position':'relative'}"
type="success" size="normal">
<button class="alipay-btn" type="success" size="mini" open-type="getAuthorize"
scope="phoneNumber" @getAuthorize="getAliPhoneNumber" v-if="state.agreement">
<view class="btn-tit">确定授权</view>
</button>
</uv-button>
<!-- #endif -->
<uv-button v-else @click="handleClickOk" :customStyle="{ 'width': '100%' }" type="success"
size="normal" text="确定授权"></uv-button>
</view>
@@ -46,7 +57,7 @@
import { useShopConfigStore, useUserInfoStore } from '@/core/store';
import type { Response } from '@/core/models';
import { Invitecode, UserToken } from '@/core/consts';
import { queryLoginPhoneNumber, queryLogin, queryAliPayAppAuthTokenBYCode } from '@/core/api';
import { queryLoginPhoneNumber, queryLogin, queryAliPayAppAuthTokenBYCode ,queryDecryptPhoneNumberByAli} from '@/core/api';
import { loginGetCode, handleShowToast, handleRouteNavigateTo } from '@/core/utils';
/** 获取项目配置 */
@@ -97,7 +108,7 @@
// #endif
// #ifdef MP-ALIPAY
const login : Response<any> = await queryAliPayAppAuthTokenBYCode({id:code});
const login : Response<any> = await queryAliPayAppAuthTokenBYCode({ id: code });
// #endif
if (login.status) {
@@ -137,7 +148,7 @@
state.agreement = false;
}
/** 获取授权手机号 */
/** 微信获取授权手机号 */
const getPhoneNumber = (e : any) => {
emits('handleChangePopup', false);
if (e.errMsg.includes('ok')) {
@@ -156,9 +167,37 @@
}
}
/** 支付宝获取授权手机号 */
const getAliPhoneNumber = () => {
emits('handleChangePopup', false);
my.getPhoneNumber({
success: (res : any) => {
let encryptedData = JSON.parse(res.response);
let data = {
sessionAuthId: state.sessionAuthIdTool,
sign: encryptedData.sign,
encryptedData: encryptedData.response,
}
/** 有推荐码的话,带上 */
if (uni.getStorageSync(Invitecode)) {
data['invitecode'] = uni.getStorageSync(Invitecode);
}
handleLogin(data);
}
})
}
/** 去登录获取数据 */
const handleLogin = async (data : any) => {
// #ifdef MP-WEIXIN
const phoneNumber : Response<any> = await queryLoginPhoneNumber(data);
// #endif
// #ifdef MP-ALIPAY
const phoneNumber : Response<any> = await queryDecryptPhoneNumberByAli(data);
// #endif
if (phoneNumber.status && phoneNumber.data.token) {
uni.setStorageSync(UserToken, phoneNumber.data.token);
if (phoneNumber.otherData) {

View File

@@ -1,13 +1,20 @@
import { post } from '@/core/utils/http';
import type { Response } from '@/core/models';
/** 获取商品分类配置 */
/** 小程序手机授权(拉取手机号码) */
export const queryLoginPhoneNumber = (data : any) : Promise<Response<any>> => {
return post('Api/User/DecryptPhoneNumber', {
data,
});
}
/** 支付宝小程序手机授权(拉取手机号码) */
export const queryDecryptPhoneNumberByAli = (data : any) : Promise<Response<any>> => {
return post('Api/User/DecryptPhoneNumberByAli', {
data,
});
}
/** 小程序解析code */
export const queryLogin = (data : any) : Promise<Response<any>> => {
return post('Api/User/OnLogin', {

View File

@@ -1,7 +1,5 @@
<template>
<!-- #ifndef MP-ALIPAY -->
<coreshop-navbar :isBack="false" mode="left" title="购物车"> </coreshop-navbar>
<!-- #endif -->
<coreshop-navbar :isBack="false" mode="left" title="购物车"> </coreshop-navbar>
<view class="content-box p-25 page-bg">
<view class="cart-box" v-if="state.cartList.length > 0">
<view class="all-select" :style="{ 'top': `${statusBarHeight}px` }">

View File

@@ -1,6 +1,6 @@
<template>
<coreshop-page :title="state.isScrollToTop ? '' :userInfoStore.userInfo?.nickName" :placeholder="false"
titleColor="#fff" mode="left" bgColor="#d33123" :isShowStatusBarHeight="false" :isBack="false"
titleColor="#fff" mode="left" :bgColor="state.isScrollToTop ? 'rgba(0,0,0,0)':'#d33123'" :isShowStatusBarHeight="false" :isBack="false"
showLoginModalDom>
<view class="layout-member-page">
<view class="member-head">