236 lines
7.2 KiB
JavaScript
236 lines
7.2 KiB
JavaScript
/**
|
|
* 该文件为整个项目的方法配置文件
|
|
* 以下方法使用前必须在/static/js/config.js中配置好属性
|
|
*/
|
|
|
|
import store from '../../store/index.js';
|
|
// 获取配置文件
|
|
import config from '@/static/js/config.js';
|
|
import { WXpayAPI, WXpayDYPAPI, WXpayDIYAPI, createOrderNewAPI, agOrderPayAPI, cancelOrderAPI} from '@/request/restaurant/index.js';
|
|
import {
|
|
WXpayH5API
|
|
} from '@/request/recharge/index.js';
|
|
|
|
// 项目通用方法类型
|
|
class project{
|
|
constructor(){}
|
|
// 获取当前环境
|
|
get environment(){
|
|
return getApp()['globalData']['parameters']?.isWechat;
|
|
}
|
|
|
|
// 获取当前品牌
|
|
get Brand(){
|
|
return store['getters']['shopping/BrandInfor']['brand_id'];
|
|
}
|
|
|
|
}
|
|
|
|
|
|
// 订单功能模块的方法类
|
|
class OrderConfig extends project{
|
|
// 获取订单状态图片
|
|
OrderStateImg(status = 0){
|
|
// 当前品牌
|
|
// 状态图标名称
|
|
console.log(config[`Brand_${this['Brand']}`],'StatusImg');
|
|
const StatusImg = config[`Brand_${this['Brand']}`]['customStyle']['OrderStatus'][`OrderStatus${status}`];
|
|
// 存放与https://img.agrimedia.cn/chwl域名下的哪个文件夹
|
|
const file = config[`Brand_${this['Brand']}`]['customStyle']['OrderStatus']['http'];
|
|
return `https://img.agrimedia.cn/chwl/${file}/${StatusImg}`;
|
|
}
|
|
|
|
// 取消订单
|
|
// OrderCancel(order_sn) {
|
|
// // 待付款 0
|
|
// // 待发货 1
|
|
// // 已发货 2
|
|
// // 已取消 3
|
|
// // 已完成 5
|
|
// // 已退款 6
|
|
// return new Promise((resolve,reject)=>{
|
|
// cancelOrderAPI({
|
|
// order_sn
|
|
// }).then(data => {
|
|
// resolve(3)
|
|
// }).catch(res=>{
|
|
// reject()
|
|
// })
|
|
// })
|
|
// }
|
|
|
|
}
|
|
|
|
// 支付功能模块方法类
|
|
class PayConfig extends project{
|
|
/**
|
|
* 支付方法参数说明
|
|
* @param {String} order_sn订单号
|
|
* @param {Number} type订单类型
|
|
* @param {String} redirect_url回调地址(查询支付状态完成之后的回调地址,并非请求支付接口时的回调地址)
|
|
*
|
|
* 使用方法
|
|
* 每一个@payment都为一步
|
|
* 注意:请求接口H5环境请求接口时redirect_url为拉取支付之后的回跳地址(该地址并不会在用户支付完成或者失败之后跳转,而是在用户拉起支付之后4-5秒之后自动跳转)
|
|
*
|
|
* @payment 调用支付方法时先判断是否是微信环境,还是H5环境
|
|
*
|
|
* @payment 微信环境则需要调用后台微信的支付接口拿取微信支付参数,然后使用wx.miniProgram.navigateTo跳转至微信的支付页面调起支付
|
|
* 其中在拿取到微信支付参数之后miniPayRequest对象中除了微信支付参数以外,必要参数分别是
|
|
* order_sn:订单号
|
|
* return_url:回调地址(用来在支付页面支付完成之后跳转订单查询页面查询订单状态完毕之后要前往哪个页面)
|
|
* type:当前支付的类型(后台返回)
|
|
*
|
|
* @payment H5环境直接拿取后台返回的地址使用location.href跳转即可
|
|
*/
|
|
payment(data) {
|
|
uni.showLoading({
|
|
title: '支付中...'
|
|
});
|
|
let {
|
|
type = 1,
|
|
order_sn = '',
|
|
redirect_url = '',
|
|
} = data;
|
|
const isWechat =
|
|
console.log(data, '新支付方法参数');
|
|
console.log(this['environment'],'是否微信环境');
|
|
if (this['environment']) {
|
|
this._H5WXPAY(data).then(res => {
|
|
uni.hideLoading();
|
|
console.log(res['data'], '微信支付参数');
|
|
const miniPayRequest = {
|
|
timeStamp: encodeURIComponent(res['data']['result']['timestamp']),
|
|
nonceStr: encodeURIComponent(res['data']['result']['nonceStr']),
|
|
package: encodeURIComponent(res['data']['result']['package']),
|
|
signType: encodeURIComponent(res['data']['result']['signType']),
|
|
paySign: encodeURIComponent(res['data']['result']['paySign']),
|
|
order_sn: res['data']['out_trade_no'],
|
|
type,
|
|
business: 'restaurant',
|
|
// 微信结算付款页面需要return_url字段
|
|
return_url: redirect_url
|
|
};
|
|
console.log(encodeURIComponent(JSON.stringify(miniPayRequest)), '编码');
|
|
// return
|
|
wx.miniProgram.navigateTo({
|
|
url: `/pages/wxpay/wxpay?data=${encodeURIComponent(JSON.stringify(miniPayRequest))}&isencodeURL=true`
|
|
});
|
|
})
|
|
|
|
} else {
|
|
// 测试
|
|
// location.href = `/pages/pay/index?order_sn=${order_sn}&type=${type}&redirect_url=${redirect_url}`;
|
|
// return
|
|
WXpayH5API({
|
|
order_sn,
|
|
type,
|
|
redirect_url: `${getApp()['globalData']['urlfileName']}/pages/pay/index?order_sn=${order_sn}&type=${type}&redirect_url=${redirect_url}`
|
|
}).then(res => {
|
|
uni.hideLoading();
|
|
location.href = res['data']['redirect_url'];
|
|
})
|
|
}
|
|
}
|
|
|
|
// H5在小程序环境下支付时每个接口不同所以封装为统一方法
|
|
_H5WXPAY(PayPara) {
|
|
|
|
// 点餐微信支付接口
|
|
if (PayPara['type'] == 3) {
|
|
let { order_sn: out_order_num, money } = PayPara;
|
|
return WXpayAPI({ out_order_num, money });
|
|
};
|
|
|
|
// 电影票微信支付接口
|
|
if (PayPara['type'] == 4) {
|
|
let { order_sn: orderNo } = PayPara;
|
|
return WXpayDYPAPI({ orderNo });
|
|
}
|
|
|
|
// DIY微信支付接口
|
|
if (PayPara['type'] == 5) {
|
|
let { order_sn, type} = PayPara;
|
|
return WXpayDIYAPI({order_sn, type});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @支付回调统一管理方法
|
|
* @查询订单状态完成之后调用该方法来跳转指定页面
|
|
*/
|
|
payRedirectUrl(parameters, redirect_url) {
|
|
// console.log(order_sn,'pay_statepage方法参数');
|
|
let url = '';
|
|
url += redirect_url;
|
|
// 拼接参数
|
|
console.log('拼接参数')
|
|
if (typeof parameters !== 'undefined') {
|
|
for (let key in parameters) {
|
|
if (url.indexOf('?') == -1) {
|
|
url += `?${key}=${parameters[key]}`;
|
|
continue;
|
|
};
|
|
url += `&${key}=${parameters[key]}`
|
|
}
|
|
};
|
|
if (redirect_url) {
|
|
uni.redirectTo({
|
|
url
|
|
})
|
|
}
|
|
}
|
|
|
|
// 再次支付方法
|
|
// AgainPayment(order_sn,type,money = 0) {
|
|
// console.log('再次支付');
|
|
// let redirect_url;
|
|
// // 肯德基/麦当劳再次支付回跳地址
|
|
// if (this['Brand'] == 1 || this['Brand'] == 5) {
|
|
// redirect_url = `/pages/restaurant/order/order-status/order-status`;
|
|
// } else if (this['Brand'] == 10 || this['Brand'] == 13) {
|
|
// // 瑞幸/星巴克再次支付回跳地址
|
|
// redirect_url = `/pages/restaurant/order/order-details/details`
|
|
// }else if(this['Brand'] == 30){
|
|
// // DIY定制再次支付回跳地址
|
|
// redirect_url = `/pages/diy/orders/order-infor/order-infor`
|
|
// };
|
|
|
|
// this.payment({
|
|
// order_sn,
|
|
// type,
|
|
// redirect_url,
|
|
// money,
|
|
// });
|
|
// // 小程序再次支付
|
|
// if(this['isWechat']){
|
|
// this.payment({
|
|
// order_sn,
|
|
// type,
|
|
// redirect_url,
|
|
// money,
|
|
// });
|
|
// }else{
|
|
// // H5再次支付
|
|
// agOrderPayAPI({
|
|
// order_sn,
|
|
// type,
|
|
// redirect_url: `${getApp()['globalData']['urlfileName']}/pages/pay/index?order_sn=${order_sn}&type=${type}&redirect_url=${redirect_url}`
|
|
// }).then(data => {
|
|
// location.href = data['data']['redirect_url'];
|
|
// })
|
|
// }
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
const install = (Vue) => {
|
|
Vue.prototype.$project = new project();
|
|
Vue.prototype.$Order = new OrderConfig();
|
|
Vue.prototype.$PayConfig = new PayConfig();
|
|
}
|
|
export default {
|
|
install
|
|
};
|