代码提交
This commit is contained in:
@ -16,12 +16,19 @@ const {loading, hideLoading} = useInterface()
|
||||
export const WechatProvider = 'wxpay'
|
||||
|
||||
// 支付类型(后端用)
|
||||
export const ServicePayType = {
|
||||
0: 'weixin_h5', // H5(微信内h5、微信外H5)
|
||||
1: 'weixin_applet', // 微信小程序
|
||||
2: 'weixin_app' // 微信APP
|
||||
export const ServiceFrom = {
|
||||
'h5': 'weixin_h5', // H5(微信内h5、微信外H5)
|
||||
'weixin': 'weixin_applet', // 微信小程序
|
||||
'app': 'weixin_app' // 微信APP
|
||||
}
|
||||
|
||||
export const ServicePayType = {
|
||||
'h5': 'weixin_h5', // H5(微信内h5、微信外H5)
|
||||
'weixin': 'weixin_applet', // 微信小程序
|
||||
'app': 'weixin_app' // 微信APP
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 支付类型(前端用)
|
||||
export const PayType = {
|
||||
@ -116,12 +123,15 @@ async function _chooseEnvToPayment(options) {
|
||||
*/
|
||||
function _appWechatPay(payInfo) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest({
|
||||
from: ServicePayType['2'],
|
||||
paytype: ServicePayType['2'],
|
||||
const payData = {
|
||||
from: ServiceFrom['app'],
|
||||
paytype: ServicePayType['app'],
|
||||
uni: payInfo.orderId
|
||||
})
|
||||
}
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest(payData)
|
||||
// 兼容性写法:防止电脑端用户支付后马上关闭支付弹窗导致失败
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(payData))
|
||||
const orderInfo = {
|
||||
appid: res.appId, // 微信开放平台审核通过的移动应用AppID 。
|
||||
prepayid: res.merchant_id, // 请填写商户号mchid对应的值。
|
||||
@ -148,13 +158,15 @@ function _appWechatPay(payInfo) {
|
||||
*/
|
||||
function _miniProgramPay(payInfo) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest({
|
||||
from: ServicePayType['1'],
|
||||
paytype: ServicePayType['1'],
|
||||
const payData = {
|
||||
from: ServiceFrom['weixin'],
|
||||
paytype: ServiceFrom['weixin'],
|
||||
uni: payInfo.orderId
|
||||
})
|
||||
|
||||
}
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest(payData)
|
||||
// 兼容性写法:防止电脑端用户支付后马上关闭支付弹窗导致失败
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(payData))
|
||||
uni.requestPayment({
|
||||
timeStamp: res.timeStamp,
|
||||
nonceStr: res.nonceStr,
|
||||
@ -177,12 +189,13 @@ function _miniProgramPay(payInfo) {
|
||||
*/
|
||||
async function _h5InWechatPay(payInfo) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest({
|
||||
from: 'h5',
|
||||
paytype: ServicePayType['0'],
|
||||
const payData = {
|
||||
from: ServiceFrom['h5'],
|
||||
paytype: ServicePayType['h5'],
|
||||
uni: payInfo.orderId
|
||||
})
|
||||
}
|
||||
// 请求线上获取 res:{ appId,timeStamp,nonceStr,paySign,package,signType,mwebUrl,codeUrl,merchant_id,out_trade_no }
|
||||
const res = await _doWechatPayRequest(payData)
|
||||
/** 注册JS SDK */
|
||||
jweixin.config({
|
||||
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
||||
@ -203,6 +216,7 @@ async function _h5InWechatPay(payInfo) {
|
||||
return reject(createMessage('微信版本过低,请升级微信版本', error))
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(payData))
|
||||
/** 去拉起微信支付 */
|
||||
jweixin.chooseWXPay({
|
||||
timestamp: res.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
|
||||
@ -212,16 +226,6 @@ async function _h5InWechatPay(payInfo) {
|
||||
paySign: res.paySign, // 支付签名
|
||||
success: (res) => {
|
||||
// 支付成功后的回调函数
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(
|
||||
{
|
||||
from: 'h5',
|
||||
paytype: ServicePayType['0'],
|
||||
uni: payInfo.orderId
|
||||
}
|
||||
))
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({url: '/pages/payStatus/index'})
|
||||
}, 3000)
|
||||
return resolve(createMessage('用户支付成功', res))
|
||||
},
|
||||
cancel: (r) => {
|
||||
@ -246,20 +250,15 @@ async function _h5InWechatPay(payInfo) {
|
||||
* @private
|
||||
*/
|
||||
async function _h5OutWechatPay(payInfo) {
|
||||
const res = await _doWechatPayRequest({
|
||||
from: 'h5',
|
||||
paytype: ServicePayType['0'],
|
||||
const payData = {
|
||||
from: ServiceFrom['h5'],
|
||||
paytype: ServicePayType['h5'],
|
||||
uni: payInfo.orderId
|
||||
})
|
||||
}
|
||||
const res = await _doWechatPayRequest(payData)
|
||||
if (res && res.mwebUrl) {
|
||||
// 缓存支付订单数据
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(
|
||||
{
|
||||
from: 'h5',
|
||||
paytype: ServicePayType['0'],
|
||||
uni: payInfo.orderId
|
||||
}
|
||||
))
|
||||
uni.setStorageSync(CacheKey.PAY_INFO, JSON.stringify(payData))
|
||||
location.replace(res.mwebUrl)
|
||||
return Promise.resolve(createMessage('用户支付成功', {type: 'h5'}))
|
||||
} else {
|
||||
|
@ -146,3 +146,12 @@ export function hasNetWork() {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取两数之间的随机数
|
||||
* @param min
|
||||
* @param max
|
||||
*/
|
||||
export function getRandom(min, max) {
|
||||
return (Math.random() * (max - min) + min).toFixed(2);
|
||||
}
|
||||
|
206
utils/wechatUtils.js
Normal file
206
utils/wechatUtils.js
Normal file
@ -0,0 +1,206 @@
|
||||
/**
|
||||
* @FileDescription: 微信工具类
|
||||
* @Author: kahu
|
||||
* @Date: 2023/6/29
|
||||
* @LastEditors: kahu
|
||||
* @LastEditTime: 2023/6/29
|
||||
*/
|
||||
|
||||
/**
|
||||
* 拍摄或从手机相册中选择图片或视频
|
||||
* @param options
|
||||
* @param options.count 数量 最大选取数量 最多可以选择的文件个数,基础库2.25.0前,最多可支持9个文件,2.25.0及以后最多可支持20个文件
|
||||
* @param options.mediaType 选取类型 image图片 video视频
|
||||
* @param options.sourceType 选取的方式 album相册 camera相机
|
||||
* @param options.maxDuration 录取视频的最大秒数,时间范围为 3s 至 60s 之间。不限制相册
|
||||
* @param options.sizeType 是否压缩所选内容,基础库2.25.0前仅对 mediaType 为 image 时有效,2.25.0及以后对全量 mediaType 有效
|
||||
* @param options.camera 拍摄时候的摄像头,仅在 sourceType 为 camera 时生效,使用前置或后置摄像头
|
||||
*/
|
||||
export function wxChooseMedia(options={}){
|
||||
return new Promise((resolve, reject) => {
|
||||
const mergeOptions = {
|
||||
count:9,
|
||||
mediaType:['image','video'],
|
||||
sourceType:['album','camera'],
|
||||
maxDuration:10,
|
||||
sizeType:['original','compressed'],
|
||||
camera:'back',
|
||||
...options,
|
||||
success:(res)=>{
|
||||
resolve(res)
|
||||
},
|
||||
fail:(err)=>{
|
||||
reject(err)
|
||||
}
|
||||
}
|
||||
wx.chooseMedia(mergeOptions)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用微信接口编辑图片
|
||||
* @param src 被编辑图片的临时路径
|
||||
*/
|
||||
export function wxEditImage(src){
|
||||
return new Promise((resolve, reject)=>{
|
||||
wx.editImage({
|
||||
src,
|
||||
success:(res)=>{
|
||||
resolve(res.tempFilePath)
|
||||
},
|
||||
fail:(err)=>{
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用微信接口裁剪图片
|
||||
* @param options
|
||||
* @param options.src 被裁剪图片的临时路径
|
||||
* @param options.cropScale 裁剪比例
|
||||
*/
|
||||
export function wxCropImage(options){
|
||||
return new Promise((resolve, reject) => {
|
||||
const mergeOptions = {
|
||||
cropScale:'1:1',
|
||||
...options,
|
||||
success:(res)=>{
|
||||
resolve(res.tempFilePath)
|
||||
},
|
||||
fail:(err)=>{
|
||||
reject(err)
|
||||
}
|
||||
}
|
||||
wx.cropImage(mergeOptions)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用微信接口处理视频
|
||||
* @param options
|
||||
* @param options.filePath 视频的本地路径
|
||||
* @param options.minDuration 视频的最小长度
|
||||
* @param options.maxDuration 视频的最大长度
|
||||
* @return { Promise<{duration:number,size:number,tempFilePath:string,tempThumbPath:string}> }
|
||||
*/
|
||||
export function wxEditVideo(options){
|
||||
return new Promise((resolve, reject)=>{
|
||||
if(options.minDuration>=options.maxDuration)return reject('MaxDuration Must Greater Than MinDuration')
|
||||
wx.openVideoEditor({
|
||||
...options,
|
||||
success:(res)=>{
|
||||
resolve(res)
|
||||
},
|
||||
fail:(err)=>{
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取视频的详情
|
||||
* @param src
|
||||
*/
|
||||
export function wxGetVideoInfo(src){
|
||||
return new Promise((resolve, reject)=>{
|
||||
wx.getVideoInfo({
|
||||
src,
|
||||
success:(res)=>{
|
||||
resolve(res)
|
||||
},
|
||||
fail:(err)=>{
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取照片的详情
|
||||
* @param src
|
||||
*/
|
||||
export function wxGetImageInfo(src){
|
||||
return new Promise((resolve, reject)=>{
|
||||
wx.getImageInfo({
|
||||
src,
|
||||
success:(res)=>{
|
||||
resolve(res)
|
||||
},
|
||||
fail:(err)=>{
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用微信login获取code
|
||||
*/
|
||||
export function wxLogin(){
|
||||
return new Promise((resolve, reject)=>{
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success: (res) => {
|
||||
resolve(res.code)
|
||||
},
|
||||
fail: (err) => {
|
||||
reject({
|
||||
message:'微信login方法出现错误',
|
||||
data:err
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用微信getUserProfile获取encryptedData、iv
|
||||
*/
|
||||
export function wxGetUserProfile(){
|
||||
return new Promise((resolve, reject)=> {
|
||||
uni.getUserProfile({
|
||||
desc: '用于完善用户信息',
|
||||
success: ({ encryptedData, iv }) => {
|
||||
resolve({encryptedData,iv})
|
||||
},
|
||||
fail:(err)=>{
|
||||
console.log(err)
|
||||
reject({
|
||||
message:'微信getUserProfile方法出现错误',
|
||||
data:err
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 调用地图获取位置信息
|
||||
* @param params
|
||||
*/
|
||||
export function wxChooseLocation(params){
|
||||
return new Promise((resolve, reject)=>{
|
||||
const obj = {
|
||||
success(e){
|
||||
resolve(e)
|
||||
},
|
||||
fail(err){
|
||||
console.log(err)
|
||||
reject({
|
||||
message:'微信ChooseLocation方法出现错误',
|
||||
data:err
|
||||
})
|
||||
}
|
||||
}
|
||||
Object.keys(params).forEach(key=>{
|
||||
// @ts-ignore
|
||||
obj[key] = params[key]
|
||||
})
|
||||
uni.chooseLocation(obj)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user