175 lines
7.0 KiB
JavaScript
175 lines
7.0 KiB
JavaScript
import BasePlatform from './BasePlatform';
|
||
|
||
|
||
function parseQueryString(queryString) {
|
||
// 如果以 ? 开头,先去掉 ?
|
||
if (queryString.startsWith('?')) {
|
||
queryString = queryString.substring(1);
|
||
}
|
||
|
||
const params = {};
|
||
const pairs = queryString.split('&');
|
||
|
||
for (const pair of pairs) {
|
||
const [key, value] = pair.split('=');
|
||
// 解码 URI 组件
|
||
params[key] = decodeURIComponent(value);
|
||
}
|
||
|
||
return params;
|
||
}
|
||
|
||
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}`);
|
||
}
|
||
|
||
/**
|
||
* 选择地址
|
||
* @returns
|
||
*/
|
||
chooseAddress() {
|
||
return new Promise((resolve, reject) => {
|
||
uni.navigateTo({
|
||
url: '/pages/other/choose_address',
|
||
events: {
|
||
// 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
|
||
selectAddress: function (data) {
|
||
console.log(data, { userName: data.address.receiver_name, telNumber: data.address.receiver_phone, detailed_address: data.address.detailed_address })
|
||
resolve({ userName: data.address.receiver_name, telNumber: data.address.receiver_phone, detailed_address: data.address.detailed_address });
|
||
},
|
||
someEvent: function (data) {
|
||
console.log(data)
|
||
}
|
||
|
||
},
|
||
success: function (res) {
|
||
// 通过eventChannel向被打开页面传送数据
|
||
res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'data from starter page' })
|
||
}
|
||
})
|
||
});
|
||
}
|
||
downloadFile(url) {
|
||
return new Promise((resolve, reject) => {
|
||
try {
|
||
// 创建一个隐藏的a标签
|
||
const a = document.createElement('a');
|
||
a.href = url;
|
||
a.download = url.split('/').pop() || 'download'; // 使用URL中的文件名或默认名称
|
||
a.style.display = 'none';
|
||
document.body.appendChild(a);
|
||
a.click(); // 触发下载
|
||
document.body.removeChild(a); // 清理DOM
|
||
resolve({ success: true });
|
||
} catch (error) {
|
||
console.error('下载失败:', error);
|
||
// 降级方案,直接打开
|
||
window.location.href = url;
|
||
resolve({ success: false, error: error.message });
|
||
}
|
||
});
|
||
}
|
||
AppLaunch(options) {
|
||
console.log("AppLaunch", options);
|
||
if (options.query) {
|
||
let query = options.query;
|
||
if (query['method'] != null && query['method'] == "alipay.trade.wap.pay.return" && query['out_trade_no'] != null) {
|
||
//支付宝跳转过来的
|
||
//重定向url
|
||
let url = window.location.origin + window.location.pathname;
|
||
let out_trade_no = query['out_trade_no'];
|
||
uni.setStorageSync('pay_order_num', out_trade_no);
|
||
// alert('存入订单编号' + out_trade_no)
|
||
const delKey = ['charset', 'out_trade_no', 'method', 'total_amount', 'sign', 'trade_no', 'auth_app_id', 'version', 'app_id', 'sign_type', 'seller_id', 'timestamp'];
|
||
for (const key of delKey) {
|
||
delete query[key];
|
||
}
|
||
let str = Object.entries(query)
|
||
.map(([key, value]) => `${key}=${value}`)
|
||
.join('&')
|
||
|
||
url += "?" + str;
|
||
console.log('支付成功,重定向页面。',url);
|
||
window.location.href = url;
|
||
}
|
||
|
||
// query['']
|
||
// {
|
||
// "goods_id": "1081",
|
||
// "type_text": "%E6%97%A0%E9%99%90%E8%B5%8F",
|
||
// "order_no": "MH_ZFAH5ZFB2025050449989950960",
|
||
// "charset": "UTF-8",
|
||
// "out_trade_no": "MH_ZFAH5ZFB2025050449989950960",
|
||
// "method": "alipay.trade.wap.pay.return",
|
||
// "total_amount": "0.01",
|
||
// "sign": "JD2PDNDh2qTi/y4wiqtsyJSc5uPy1OgO5W7NaYRdc/ctHT85yVJP9fDVpJAIXRDj/xd8oWpT9a2BSrNfqIbZ0sT0Fa8ha15unfi9RX+jYEkKAa2Grz0rkcv3YOlMni9vThCRdmNPCzh0fKLCKFRBZiuxPIFW7qgoRxFkWvziR8dUi0GccleOSUV6Z3W+HG6b+Ru3fgZSRMm+8+d0UXrH5wi7KIgJja+ZbAnVYkh+4R7BjxS1zpUPMXNzej9fAFHO6UD8RBobYi1r6D3gYpKJaChVUJXaXkRqU/6ybgUbX7FsvviCYoEGUkukiq9HCaKZ/bfrD3TvoAicC4aXo6p7Dw==",
|
||
// "trade_no": "2025050422001402751446415564",
|
||
// "auth_app_id": "2021005141643914",
|
||
// "version": "1.0",
|
||
// "app_id": "2021005141643914",
|
||
// "sign_type": "RSA2",
|
||
// "seller_id": "2088170936712914",
|
||
// "timestamp": "2025-05-04 01:41:48"
|
||
// }
|
||
}
|
||
}
|
||
getOrderNo() {
|
||
return new Promise((resolve, reject) => {
|
||
//window.location.search
|
||
let query = parseQueryString(window.location.search);
|
||
if (query['method'] != null && query['method'] == "alipay.trade.wap.pay.return" && query['out_trade_no'] != null) {
|
||
//表示还在支付请求中,还未开始重定向
|
||
console.log('window.location.search',window.location.search);
|
||
|
||
resolve(null);
|
||
return;
|
||
}
|
||
let pay_order_num = uni.getStorageSync('pay_order_num');
|
||
// alert("获取订单编号"+pay_order_num)
|
||
setTimeout(() => { uni.removeStorageSync('pay_order_num'); }, 2000)
|
||
resolve(pay_order_num);
|
||
});
|
||
}
|
||
}
|
||
export default H5Platform; |