Files
houyi-uniapp/com/jssdk.js
PC-202306242200\Administrator 85b89ccea7 first commit
2026-03-28 23:27:25 +08:00

140 lines
4.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import wx from 'weixin-js-sdk';
import request from "./http.js";
export default {
/* 判断是否在微信中 */
isWechat: function() {
var ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/micromessenger/i) == 'micromessenger') {
//console.log('是微信客户端')
return true;
} else {
//console.log('不是微信客户端')
//以下是我项目中所需要的操作其他,可以自定义
uni.showModal({
title: '提示',
content: '请在微信浏览器中打开',
showCancel: false,
confirmColor: '#00875a',
success: function(res) {
if (res.confirm) {
// console.log('用户点击确定');
} else if (res.cancel) {
// console.log('用户点击取消');
}
}
});
return false;
}
},
/* 获取sdk初始化配置 */
initJssdk: async function(callback) {
//获取当前url然后传递给后台获取授权和签名信息
var url = encodeURIComponent(window.location.href.split('#')[0]); //当前网页的URL不包含#及其后面部分
console.log(window.location.href.split('#')[0]);
//这里调用的是后端的接口后端去获取签名以及config里面所需的信息
const {
data
} = await request.get('/com/wechat/jssdk', {
url: ''
})
//返回需要的参数appId,timestamp,noncestr,signature等
//注入config权限配置
const {
appId,
timestamp,
nonceStr,
signature,
debug,
jsApiList
} = data;
wx.config({
debug: debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来若要查看传入的参数可以在pc端打开参数信息会通过log打出仅在pc端时才会打印。
// beta: true, // 文档没有这个参数这个参数需设为true才能调用那些微信还没有正式开放的新接口比如wx.invoke
appId: appId, // 必填,公众号的唯一标识
timestamp: timestamp, // 必填,生成签名的时间戳
nonceStr: nonceStr, // 必填,生成签名的随机串
signature: signature, // 必填,签名
jsApiList: jsApiList
});
// 本地环境测试使用里面信息是测试号的appid和签名
// jWeixin.config({
// debug: true,
// appId: 'wx451eff21c6c0d938',
// timestamp: 1659065946,
// nonceStr: 'dzklsf',
// signature: 'd2ada1c92409e14c9e720ed58056dcd3800ab0a7',
// jsApiList: ['scanQRCode']
// })
// 本地环境测试结束
if (callback) {
callback(data);
}
},
//选择图片
chooseImage: function(callback) {
if (!this.isWechat()) {
//console.log('不是微信客户端')
return;
}
//console.log(data);
this.initJssdk(function(res) {
wx.ready(function() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album'],
success: function(rs) {
callback(rs)
}
})
});
});
},
//微信支付
wxpay: function(data, callback) {
if (!this.isWechat()) {
//console.log('不是微信客户端')
return;
}
this.initJssdk(function(res) {
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
appId: 'wxcf948c7b5a0863b1',
timeStamp: data.timeStamp, // 支付签名时间戳注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
package: data.package, // 统一支付接口返回的prepay_id参数值提交格式如prepay_id=\*\*\*
signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: data.paySign, // 支付签名
},
function(res) {
callback(res)
}
);
// wx.ready(function() {
// wx.chooseWXPay({
// timeStamp: data
// .timeStamp, // 支付签名时间戳注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
// nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
// package: data
// .package, // 统一支付接口返回的prepay_id参数值提交格式如prepay_id=\*\*\*
// signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
// paySign: data.paySign, // 支付签名
// success: function(res) {
// // console.log(res);
// callback(res)
// },
// fail: function(res) {
// callback(res)
// },
// });
// });
});
},
}