import config from '../../config.js'; import request from '../request.js'; import common from '../common.js'; let jweixin = require('jweixin-module') /* * 获取jssdk配置参数 * 2020-08-14 */ jweixin.setConfig = function() { if (config.isTest) { return false } console.log({ debug: config.weixin_jssdk_debug ? 1 : 0, url: window.location.href.split('#')[0] }) request.getData('base/wechat/api/Jssdk/buildConfig', { debug: config.weixin_jssdk_debug ? 1 : 0, url: window.location.href.split('#')[0] }).then(res => { console.log(res) let data = res.data jweixin.config(data) }) } /* * 微信网页授权 * 2020-11-23 */ jweixin.getOpenid = function() { let openid = uni.getStorageSync('openid') let user_id = uni.getStorageSync('user_id') return new Promise((resolve, reject) => { if (config.isTest) { if (!openid) { let uid = uni.getStorageSync('uid') request.getData('base/wechat/api/Test/getTokenByOpenid', { uid: uid, openid: config.testOpenid }).then(res => { if (res.code === 0) { uni.setStorageSync('openid', res.data.openid) uni.setStorageSync('user_id', res.data.user_id) uni.setStorageSync('access_token', res.data.access_token) uni.setStorageSync('access_token_expire_time', res.data .access_token_expire_time) resolve({ openid: uni.getStorageSync('openid'), user_id: uni.getStorageSync('user_id'), access_token: uni.getStorageSync('access_token'), }) console.log('测试环境刚获得 缓存:openid = ' + uni.getStorageSync('openid') + ' user_id = ' + uni.getStorageSync( 'user_id')) } else { console.log(res) reject() } }) } else { resolve({ openid: uni.getStorageSync('openid'), user_id: uni.getStorageSync('user_id'), }) console.log('测试环境 缓存:openid = ' + uni.getStorageSync('openid') + ' user_id = ' + uni .getStorageSync('user_id')) } } else { if (!openid) { const code = common.getUrlParam('code') if (code === null || code === '') { request.getData('base/wechat/api/Auth/oauth', { url: window.location.href }).then(res => { window.location.href = res.data.redirect_url }) } else { request.getData('base/wechat/api/Auth/user', { code: code }).then(res => { if (res.code === 0) { uni.setStorageSync('openid', res.data.openid) uni.setStorageSync('user_id', res.data.user_id) uni.setStorageSync('access_token', res.data.access_token) uni.setStorageSync('access_token_expire_time', res.data .access_token_expire_time) resolve({ openid: uni.getStorageSync('openid'), user_id: uni.getStorageSync('user_id'), access_token: uni.getStorageSync('access_token'), }) console.log('生产环境刚获得 缓存:openid = ' + uni.getStorageSync('openid') + ' user_id = ' + uni.getStorageSync( 'user_id')) } else { console.log(res) reject() } }) } } else { resolve({ openid: uni.getStorageSync('openid'), user_id: uni.getStorageSync('user_id'), }) console.log('生产环境 缓存:openid = ' + uni.getStorageSync('openid') + ' user_id = ' + uni .getStorageSync('user_id')) } } }) } /* * 微信支付 * 2020-08-14 */ jweixin.wxPay = function(obj) { return new Promise((resolve, reject) => { jweixin.ready((res) => { jweixin.chooseWXPay({ timestamp: obj.timestamp, nonceStr: obj.nonceStr, package: obj.package, signType: obj.signType, paySign: obj.paySign, success: (res) => { // 支付成功后的回调函数 resolve(res) }, cancel: (res) => { reject(res) } }) }) }) } /* * 分享给朋友及分享到QQ、分享到朋友圈及分享到QQ空间 * 2020-08-14 */ jweixin.share = function(obj) { jweixin.ready(() => { //分享给朋友 分享到QQ jweixin.updateAppMessageShareData({ title: obj.title, // 分享标题 desc: obj.desc, // 分享描述 link: obj.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl: obj.imgUrl, // 分享图标 success: (res) => { // 设置成功 console.log(res) } }) //分享到朋友圈 分享到QQ空间 jweixin.updateTimelineShareData({ title: obj.title, // 分享标题 link: obj.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl: obj.imgUrl, // 分享图标 success: () => { // 设置成功 } }) }) } export default jweixin;