提交支付宝支付

This commit is contained in:
zpc 2025-05-03 03:58:13 +08:00
parent e291af7fb9
commit 66fed95370
11 changed files with 271 additions and 37 deletions

View File

@ -32,6 +32,8 @@ const testing = {
wxAppId: 'wx0e33d80d35e4a3b1'
};
// 根据环境变量选择对应配置
let currentEnv = testing;

View File

@ -0,0 +1,29 @@
import BasePlatform from './BasePlatform';
class AppPlatform extends BasePlatform {
constructor() {
super();
this.code = 'APP_ANDROID';
this.env = 'app';
}
getPayData(url,data) {
return data;
}
pay({ data }) {
return new Promise((resolve) => {
console.log(`App支付${data.amount}分,订单号:${data.orderId}`);
// 模拟调用H5支付SDK
// window.H5PaySDK?.pay(data.amount, data.orderId, resolve);
});
}
share({ title, desc, image, url }) {
console.log(`H5分享${title} - ${desc}`);
// 调用浏览器原生分享(如果可用)
if (navigator.share) {
return navigator.share({ title, text: desc, url });
}
// 降级方案
alert(`请手动分享:${url}`);
}
}
export default AppPlatform;

View File

@ -0,0 +1,46 @@
/**
* 多端平台抽象基类父类
* 定义所有端必须实现的方法
*/
class BasePlatform {
constructor() {
if (new.target === BasePlatform) {
throw new Error('BasePlatform 是抽象类,不能直接实例化');
}
this.code = ''; // 平台代码WEB/MP/APP
this.env = ''; // 运行环境标识
}
getPayData(url,data) {
throw new Error('子类必须实现 getPayData 方法');
}
/**
* 支付方法子类必须实现
* @param {number} amount - 支付金额
* @param {string} orderId - 订单号
*/
pay({ data }) {
throw new Error('子类必须实现 pay 方法');
}
/**
* 分享方法子类必须实现
* @param {object} params - 分享参数 { title, desc, image, url }
*/
share({ title, desc, link, image }) {
throw new Error('子类必须实现 share 方法');
}
/**
* 通用方法所有端共用
*/
getPlatformInfo() {
return {
code: this.code,
env: this.env,
ua: navigator?.userAgent || ''
};
}
}
export default BasePlatform;

View File

@ -0,0 +1,48 @@
import BasePlatform from './BasePlatform';
class H5Platform extends BasePlatform {
constructor() {
super();
this.code = 'WEB_H5';
this.env = 'h5';
}
getPayData(url, data) {
data['quitUrl']=escape(window.location.href);
data['returnUrl']=escape(window.location.href);
console.log('处理数据',data);
return data;
}
pay({ data }) {
console.log('支付1111');
return new Promise((resolve) => {
console.log(data);
// 创建一个临时div元素
const tempDiv = document.createElement('div');
// 将data作为HTML插入到div中假设data是表单的HTML字符串
tempDiv.innerHTML = data;
// 将表单添加到body中
document.body.appendChild(tempDiv.firstChild);
// 获取表单并提交
const form = document.getElementById('alipaysubmit');
if (form) {
form.submit();
resolve({ success: true });
} else {
resolve({ success: false, message: '表单创建失败' });
}
});
}
share({ title, desc, image, url }) {
console.log(`H5分享${title} - ${desc}`);
// 调用浏览器原生分享(如果可用)
if (navigator.share) {
return navigator.share({ title, text: desc, url });
}
// 降级方案
alert(`请手动分享:${url}`);
}
}
export default H5Platform;

View File

@ -0,0 +1,64 @@
import BasePlatform from './BasePlatform';
/**
* 小程序平台
*/
class MiniProgramPlatform extends BasePlatform {
constructor() {
super();
this.code = 'MP-WEIXIN';
this.env = 'miniProgram';
}
getPayData(url, data) {
return data;
}
pay({ data }) {
return new Promise((resolve, reject) => {
let provider = "weixin";
uni.requestPayment({
provider,
...data,
success: res => {
// console.log(res)
},
fail: err => {
// console.log('common.wxMpPay-error', err)
},
complete: res => {
console.log('complete (res)', res)
if (res.errMsg == 'requestPayment:fail cancel') {
this.toast({
title: '取消支付',
icon: 'none',
duration: 500,
success: () => {
/* 取消订单 */
}
})
resolve('cancel')
}
if (res.errMsg == 'requestPayment:ok') {
this.toast({
title: '支付成功',
icon: 'success',
duration: 500,
success: () => {
resolve('success')
}
})
}
}
})
})
}
share({ title, desc, image, url }) {
wx.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline']
});
// 监听用户点击分享按钮
wx.onShareAppMessage(() => ({ title, imageUrl: image }));
}
}
export default MiniProgramPlatform;

View File

@ -0,0 +1,29 @@
import MiniProgramPlatform from './MiniProgramPlatform';
import AppPlatform from './AppPlatform';
import H5Platform from './H5Platform';
class PlatformFactory {
static create() {
// 判断小程序环境
//#ifdef MP-WEIXIN
return new MiniProgramPlatform();
//#endif
// 判断 App 环境
//#ifdef APP-ANDROID
return new AppPlatform();
//#endif
// 判断 H5 环境
//#ifdef WEB
return new H5Platform();
//#endif
// 默认返回 小程序
return new MiniProgramPlatform();
}
}
// 使用示例
const platform = PlatformFactory.create();
console.log(platform.env);
export { platform };

View File

@ -8,6 +8,7 @@ import md5 from 'js-md5'
import ConfigManager from '@/common/config.js'
import { apiWhiteList } from '@/common/config.js'
import RouterManager from '@/common/router.js'
import { platform } from '@/common/platform/PlatformFactory'
class RequestManager {
/**
* 判断URL是否在白名单中
@ -69,18 +70,14 @@ class RequestManager {
}
}
})
const url = param.url || ''
const method = param.method || 'POST'
const data = param.data || {}
const Loading = param.Loading || false
const token = uni.getStorageSync('token')
let client = ""
// #ifdef H5
client = "h5"
// #endif
let client = platform.code
// 获取API基础URL
const apiBaseUrl = EnvConfig.apiBaseUrl

View File

@ -24,6 +24,13 @@
<text style="font-size: 20rpx; color: #999999;">查看更多 ></text>
</view>
</view>
<view>
<view v-if="localBossCardData && localBossCardData.king_user" class="lingzhu-img relative"
style="position: absolute;right: 12px;top: 180rpx;height: 120rpx;height: 120rpx;">
<image :src="localBossCardData.king_user.headimg" mode="aspectFill"></image>
<image :src="$img1('index/king.png')"></image>
</view>
</view>
<!-- 领主弹窗 - 显示领主详细信息和挑战人数/领主记录 -->
<uni-popup ref="bossPop" type="bottom" mask-background-color="rgba(0,0,0,0.8)">
@ -324,7 +331,7 @@ export default {
width: 75rpx;
height: 76rpx;
position: absolute;
bottom: 4rpx;
bottom: 8rpx;
left: 50%;
transform: translateX(-50%);
z-index: 1;

15
main.js
View File

@ -7,6 +7,7 @@ import router from '@/common/router.js'
import ConfigManager from '@/common/config.js'
import RequestManager from '@/common/request.js'
import EnvConfig from '@/common/env.js'
import { platform } from '@/common/platform/PlatformFactory'
// 全局注册uni-popup组件
import uniPopup from '@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue'
@ -22,7 +23,7 @@ Vue.prototype.$img1 = url => EnvConfig.iconBaseUrl + url
Vue.prototype.$sys = () => uni.getSystemInfoSync()
Vue.prototype.$loginPage = EnvConfig.loginPage
Vue.prototype.$wxloginPage = EnvConfig.wxLoginUrl
Vue.prototype.$platform = platform;
// 公共方法
Vue.prototype.gotoPage = gotopage
// 注册路由方法到Vue实例改用自定义属性名
@ -49,11 +50,11 @@ loadScript('https://res.wx.qq.com/open/js/jweixin-1.6.0.js')
// #ifdef MP-WEIXIN
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function(res) {
updateManager.onCheckForUpdate(function (res) {
console.log(res.hasUpdate)
})
updateManager.onUpdateReady(function() {
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
@ -65,7 +66,7 @@ updateManager.onUpdateReady(function() {
})
})
updateManager.onUpdateFailed(function() {
updateManager.onUpdateFailed(function () {
// 新版本下载失败
})
// #endif
@ -80,14 +81,14 @@ const app = new Vue({
// 创建全局数据对象
app.globalData = {
siteBaseUrl: EnvConfig.apiBaseUrl
siteBaseUrl: EnvConfig.apiBaseUrl
}
// 应用启动时加载全局配置
ConfigManager.init().then(config => {
console.log('全局配置加载完成')
console.log('全局配置加载完成')
}).catch(err => {
console.error('全局配置加载失败', err)
console.error('全局配置加载失败', err)
})
app.$mount()

View File

@ -14,7 +14,7 @@
<image class="ml20" :src="$img1('index/suoxinag.png')" mode="aspectFit"></image>
<view class="ml30">倒计时{{ secNum }}S</view>
</view>
<view class="header-title center"
<view class="header-title center"
:style="{ background: 'url(' + $img1('common/chouTitle.png') + ') no-repeat 0 0 / 100% 100%' }">
<text>
<template></template>
@ -346,6 +346,7 @@ export default {
onLoad(options) {
this.optData = options;
console.log(options);
this.getData();
uni.$on('chooseCoupon', data => {
// console.log('data', data)
@ -360,6 +361,10 @@ export default {
this.sendRuleData = res.data
}
})
if (options != null && options['order_no'] != null) {
let order_num = options['order_no'];
this.getPrize(order_num);
}
},
onShow() {
@ -368,7 +373,7 @@ export default {
onReady() {
// this.tabChange(this.tabCur)
this.getData();
// this.getData();
},
onHide() {
@ -552,15 +557,6 @@ export default {
};
let url = 'infinite_ordermoney'
console.log(type);
if (type == 1) {
url = 'infinite_orderbuy'
if (this.$refs.buyNotice.getIsShow() && !flag) {
this.$refs.buyNotice.getRule(6)
return
}
}
this.buyNum = num;
@ -571,18 +567,33 @@ export default {
this.couponData = null;
coupon_id = '';
}
console.log(type);
let postData = {
goods_id: this.pageData.goods.id,
prize_num: this.buyNum,
use_money_is: this.useMoney ? 1 : 2,
use_integral_is: this.useIntegral ? 1 : 2,
use_money2_is: this.useMoney2 ? 1 : 2,
coupon_id: coupon_id,
};
if (type == 1) {
url = 'infinite_orderbuy'
if (this.$refs.buyNotice.getIsShow() && !flag) {
this.$refs.buyNotice.getRule(6)
return
}
postData = this.$platform.getPayData(url, postData);
// postData['quitUrl']=window.location.href;
// postData['returnUrl']=window.location.href;
}
this.req({
url,
data: {
goods_id: this.pageData.goods.id,
prize_num: this.buyNum,
use_money_is: this.useMoney ? 1 : 2,
use_integral_is: this.useIntegral ? 1 : 2,
use_money2_is: this.useMoney2 ? 1 : 2,
coupon_id: coupon_id,
// ad_id: uni.getStorageSync('_ad_id')
},
data: postData,
success: async res => {
if (res.status == 1) {
if (type == 0) {
@ -605,7 +616,7 @@ export default {
this.close('buyPop')
if (res.data.status == 1) {
const status = await this.$c.wxMpPay({
const status = await this.$platform.pay({
data: res.data.res
})

Binary file not shown.