123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import jweixin, { ready } from 'weixin-js-sdk';
- import $helper from '@/sheep/helper';
- import AuthUtil from '@/sheep/api/member/auth';
- let configSuccess = false;
- export default {
-
- isWechat() {
- const ua = window.navigator.userAgent.toLowerCase();
-
- return ua.match(/micromessenger/i) == 'micromessenger';
- },
- isReady(api) {
- jweixin.ready(api);
- },
-
- async init(callback) {
- if (!this.isWechat()) {
- $helper.toast('请使用微信网页浏览器打开');
- return;
- }
-
- const url = location.href.split('#')[0];
- const { code, data } = await AuthUtil.createWeixinMpJsapiSignature(url);
- if (code === 0) {
- jweixin.config({
- debug: false,
- appId: data.appId,
- timestamp: data.timestamp,
- nonceStr: data.nonceStr,
- signature: data.signature,
- jsApiList: ['chooseWXPay'],
- openTagList: data.openTagList
- });
- }
-
- configSuccess = true;
- jweixin.error((err) => {
- configSuccess = false;
- console.error('微信 JSSDK 初始化失败', err);
-
- });
- jweixin.ready(() => {
- if (configSuccess) {
- console.log('微信 JSSDK 初始化成功');
- }
- })
-
- if (callback) {
- callback(data);
- }
- },
-
- getLocation(callback) {
- this.isReady(() => {
- jweixin.getLocation({
- type: 'gcj02',
- success: function (res) {
- callback(res);
- },
- fail: function (res) {
- console.log('%c微信H5sdk,getLocation失败:', 'color:green;background:yellow');
- },
- });
- });
- },
-
- openAddress(callback) {
- this.isReady(() => {
- jweixin.openAddress({
- success: function (res) {
- callback.success && callback.success(res);
- },
- fail: function (err) {
- callback.error && callback.error(err);
- console.log('%c微信H5sdk,openAddress失败:', 'color:green;background:yellow');
- },
- complete: function (res) {},
- });
- });
- },
-
- scanQRCode(callback) {
- this.isReady(() => {
- jweixin.scanQRCode({
- needResult: 1,
- scanType: ['qrCode', 'barCode'],
- success: function (res) {
- callback(res);
- },
- fail: function (res) {
- console.log('%c微信H5sdk,scanQRCode失败:', 'color:green;background:yellow');
- },
- });
- });
- },
-
- updateShareInfo(data, callback = null) {
- this.isReady(() => {
- const shareData = {
- title: data.title,
- desc: data.desc,
- link: data.link,
- imgUrl: data.image,
- success: function (res) {
- if (callback) {
- callback(res);
- }
-
- },
- cancel: function (res) {},
- };
-
- jweixin.updateAppMessageShareData(shareData);
-
- jweixin.updateTimelineShareData(shareData);
- });
- },
-
- openLocation(data, callback) {
- this.isReady(() => {
- jweixin.openLocation({
-
- latitude: data.latitude,
- longitude: data.longitude,
- });
- });
- },
-
- chooseImage(callback) {
- this.isReady(() => {
- jweixin.chooseImage({
- count: 1,
- sizeType: ['compressed'],
- sourceType: ['album'],
- success: function (rs) {
- callback(rs);
- },
- });
- });
- },
-
- wxpay(data, callback) {
- this.isReady(() => {
- jweixin.chooseWXPay({
- timestamp: data.timeStamp,
- nonceStr: data.nonceStr,
- package: data.packageValue,
- signType: data.signType,
- paySign: data.paySign,
- success: function (res) {
- callback.success && callback.success(res);
- },
- fail: function (err) {
- callback.fail && callback.fail(err);
- },
- cancel: function (err) {
- callback.cancel && callback.cancel(err);
- },
- });
- });
- },
- };
|