Files
CompanyProject/衣莎项目/源码/YiShaXiYi/model/payment.js
2024-09-27 01:32:49 +08:00

103 lines
4.1 KiB
JavaScript

const app = getApp();
const config = app.loadConfig()
const api = app.loadApi()
const regeneratorRuntime = app.loadAsync()
const { Http } = require("../http/ajax")
const http = new Http
const model = app.loadModel("index")
// 支付未完成等待看接口
export class Payment {
constructor() {
}
/**
* 封装小程序支付模块,方便对支付的控制
*/
async pay(url, data) {
let tel = await this.isTelnum()
if(tel == 1) return Promise.resolve(3);
return new Promise((resolve, reject) => {
console.log(12);
wx.showModal({
content: '确定支付',
cancelText: '取消',
confirmText: '确认',
async success(res) {
if (res.confirm) {
if (config.isPay) {
let payInfo = await http.request2({ url: `${url}`, method: "POST", data: data })
if (payInfo.data.data.type == 1 || payInfo.data.data.code == 200) {
wx.showToast({
title: '支付成功',
icon: 'none',
duration: 1000
})
resolve(1)
}
if (!!payInfo.data.data.nonceStr) {
wx.requestPayment({
timeStamp: payInfo.data.data.timeStamp,
nonceStr: payInfo.data.data.nonceStr,
package: payInfo.data.data.package,
signType: 'md5',
paySign: payInfo.data.data.paySign,
success(res) {
if (res.errMsg == "requestPayment:ok") {
wx.showToast({
title: '支付成功',
icon: 'none',
duration: 1000
})
resolve(1)
}
},
fail(res) {
wx.showToast({
title: '取消支付',
icon: 'none',
duration: 1000
})
resolve(2)
}
})
}
} else {
wx.showToast({
title: '支付功能已关闭',
icon: 'none',
duration: 2000
})
}
} else if (res.cancel) {
resolve(2)
}
},
})
})
}
isTelnum() {
return new Promise((resolve, reject) => {
let userInfo = app.globalData.userInfo
if (typeof userInfo['telnum'] == 'string' && (userInfo['telnum'] == "未授权手机号" || userInfo['telnum'] == '')) {
wx.showModal({
title: '未授权手机号不能办卡哦!!!',
content: '点击确定前往授权手机号',
success(res) {
if (res.confirm) {
resolve(1)
wx.navigateTo({
url: '/pages/login/login'
})
}else{
resolve(1)
}
}
})
}else{
resolve(2)
}
})
}
}