import jweixin from '@/uni_modules/jweixin-module' import api from '@/common/request/index' export default { //判断是否在微信中 isWechat: function() { var ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/micromessenger/i) == 'micromessenger') { return true; } else { return false; } }, initJssdk: function(callback) { let url = ''; if (uni.getStorageSync('platform') === 'wxOfficialAccount' && uni.getSystemInfoSync().platform === 'ios') { // url = encodeURIComponent(window.location.href.split('#')[0]) url = window.location.href.split('#')[0]; } else { // url = encodeURIComponent(window.location.href.split('#')[0]); //获取当前url然后传递给后台获取授权和签名信息 url = window.location.href.split('#')[0]; //获取当前url然后传递给后台获取授权和签名信息 } const jsApiList = [ 'getLocation', 'updateAppMessageShareData', 'updateTimelineShareData', ] api('h5.config',{ url: url }).then(res => { // console.log({ // debug: true, // appId: res.data.appId, // timestamp: res.data.timestamp, // nonceStr: res.data.nonceStr, // signature: res.data.signature, // jsApiList: jsApiList, // url: res.data.url, //自己添加的,debug为true的时候可以网页打印出对应的URL是否正确 // }) jweixin.config({ debug: false, appId: res.data.appId, timestamp: res.data.timestamp, nonceStr: res.data.nonceStr, signature: res.data.signature, jsApiList: jsApiList, url: res.data.url, //自己添加的,debug为true的时候可以网页打印出对应的URL是否正确 }); jweixin.error(function(res){ console.log(res) }); if (callback) { callback(res.data); } }) }, //在需要定位页面调用 getlocation: function(callback) { if (!this.isWechat()) { return; } this.initJssdk(function(res) { jweixin.ready(function() { jweixin.getLocation({ type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02' success: function(res) { callback(res) }, fail: function(res) {}, }); }); jweixin.error(function(res){ console.log(res) }); }); }, //获取微信收货地址 openAddress: function(callback) { if (!this.isWechat()) { return; } this.initJssdk(function(res) { jweixin.ready(function() { jweixin.openAddress({ success: function(res) { callback(res) }, fail: function(res) {}, }); }); jweixin.error(function(res){ console.log(res) }); }); }, // 微信扫码 scanQRCode: function(callback) { if (!this.isWechat()) { return; } this.initJssdk(function(res) { jweixin.ready(function() { jweixin.scanQRCode({ needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果, scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有 success: function(res) { callback(res) }, fail: function(res) {}, }); }); jweixin.error(function(res){ console.log(res) }); }); }, // 微信分享 share: function(data, callback) { if (!this.isWechat()) { return; } this.initJssdk(function(res) { jweixin.ready(function() { let protocol = window.location.protocol + '//'; var shareData = { title: data.title, desc: data.desc, link: data.path.split(protocol)[1], imgUrl: data.imgUrl, }; //分享到朋友 jweixin.updateAppMessageShareData(shareData); //分享到朋友圈接口 jweixin.updateTimelineShareData(shareData); }); jweixin.error(function(res){ console.log(res) }); }); }, // 微信定位 openLocation: function(data, callback) { //打开位置 if (!this.isWechat()) { return; } this.initJssdk(function(res) { jweixin.ready(function() { jweixin.openLocation({ //根据传入的坐标打开地图 latitude: data.latitude, longitude: data.longitude }); }); jweixin.error(function(res){ console.log(res) }); }); }, // 选择图片 chooseImage: function(callback) { //选择图片 if (!this.isWechat()) { return; } this.initJssdk(function(res) { jweixin.ready(function() { jweixin.chooseImage({ count: 1, sizeType: ['compressed'], sourceType: ['album'], success: function(rs) { callback(rs) } }) }); jweixin.error(function(res){ console.log(res) }); }); }, //微信支付 wxpay: function(data, callback) { let that = this; if (!this.isWechat()) { return; } this.initJssdk(function(res) { jweixin.ready(function() { jweixin.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) { callback(res) }, fail: function(res) { callback(res) }, cancel: function(res) { // that.tools.toast('取消支付') }, }); }); jweixin.error(function(res){ console.log(res) }); }); } }