import 'jweixin-module' import './base64.js' const Base64 = (typeof window !== 'undefined' ? window.Base64 : globalThis.Base64) const wxJs = (typeof window !== 'undefined') ? (window.jWeixin || window.wx) : {} import { getJsConfig } from '@/http/common.js' let tools = {} let wxInitPromise = null /** * 错误提示 * @param msg */ tools.error = function(msg) { uni.showToast({ 'title': msg, 'icon': 'error', 'mask': true, 'duration': 1500 }) } /** * 成功提示 * @param msg */ tools.success = function(msg) { uni.showToast({ 'title': msg, 'icon': 'success', 'mask': true, 'duration': 1500 }) } /** * 显示Loading */ tools.showLoading = function() { uni.showLoading({ title: '加载中...', mask: true }); } /** * 关闭Loading */ tools.hideLoading = function() { uni.hideLoading(); } /** * 获取平台类型 1:微信,2:支付宝 * @returns {boolean|number} */ tools.platformType = function() { let ua = window.navigator.userAgent.toLowerCase(); if (ua.indexOf('micromessenger') != -1) { return 1; } else if (ua.indexOf('alipay') != -1) { return 2; } else { return 0; } } /** * 获取开发平台 * @returns {string} */ tools.getPlatform = function() { let platForm = undefined; // #ifdef H5 platForm = 'H5'; //#endif // #ifdef APP-PLUS platForm = 'APP'; //#endif // #ifdef APP-PLUS-NVUE platForm = 'APP'; //#endif // #ifdef APP-NVUE platForm = 'APP'; //#endif // #ifdef MP-WEIXIN platForm = 'MP-WEIXIN'; // 小程序 //#endif // #ifdef MP-ALIPAY platForm = 'MP-ALIPAY'; //#endif // #ifdef MP-BAIDU platForm = 'MP-BAIDU'; //#endif // #ifdef MP-TOUTIAO platForm = 'MP-TOUTIAO'; //#endif // #ifdef MP-LARK platForm = 'MP-LARK'; //#endif // #ifdef MP-QQ platForm = 'MP-QQ'; //#endif // #ifdef MP-KUAISHOU platForm = 'MP-KUAISHOU'; //#endif // #ifdef QUICKAPP-WEBVIEW platForm = 'QUICKAPP-WEBVIEW'; //#endif return platForm; } tools.wxInitialize = async function(jsApiList, debug) { // 如果已有成功的初始化缓存,直接返回 if (wxInitPromise) { return wxInitPromise; } let url = window.location.href.replace('#', ''); wxInitPromise = getJsConfig({ 'url': url }).then((ret) => { if (ret.code === 1) { wxJs.config({ debug: debug ? true : false, appId: ret.data.appid, timestamp: ret.data.timestamp * 1, nonceStr: ret.data.noncestr, signature: ret.data.signature, jsApiList: jsApiList }); return new Promise((resolve) => { wxJs.ready(function() { resolve(true); }); wxJs.error(function(res) { console.error('微信JSSDK初始化失败:', res); wxInitPromise = null; // 重置缓存,允许重试 resolve(false); }); }); } else { console.error('获取微信配置失败:', ret); wxInitPromise = null; return false; } }).catch((err) => { console.error('微信JSSDK初始化异常:', err); wxInitPromise = null; return false; }); return wxInitPromise; } tools.weiXinPay = function(data) { return new Promise((resolve, reject) => { wxJs.chooseWXPay({ appId: data.appId, // 必填,公众号的唯一标识 timestamp: data.timeStamp, // 支付签名时间戳,注意微信 jssdk 中的所有使用 timestamp 字段均为小写。但最新版的支付后台生成签名使用的 timeStamp 字段名需大写其中的 S 字符 nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位 package: data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*) signType: data.signType, // 微信支付V3的传入 RSA ,微信支付V2的传入格式与V2统一下单的签名格式保持一致 paySign: data.paySign, // 支付签名 success: function(res) { resolve(true) }, fail: function(res) { resolve(false) }, }); }) } // tools.wxSilentLogin = function(urlPath) { // // 微信浏览器 // let url = encodeURIComponent(window.location.origin + '/#' + urlPath); // let appid = 'wx27df73f402695133'; // let toUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + url + // "&response_type=code&scope=snsapi_base&state=123#wechat_redirect" // location.href = toUrl // } tools.wxLogin = function(urlPath) { // 微信浏览器 // let appid = 'wx5f2adeb213567b23';//测试 let appid = 'wx27df73f402695133'; //正式 let url = window.location.href; var type = ''; if(appid === 'wx27df73f402695133'){ type = 1 }else{ type = window.location.host === 'tepayh5ap.jhcs888.com' ? '3' : '1'; } url = url.replace('&code=', '&code_a=1') url = encodeURIComponent(url); window.location.href = `https://wapwx2.jhcs888.com/#/?redirect_url=${Base64.encode(url)}&type=${type}` } export default tools