tools.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import 'jweixin-module'
  2. import './base64.js'
  3. const Base64 = (typeof window !== 'undefined' ? window.Base64 : globalThis.Base64)
  4. const wxJs = (typeof window !== 'undefined') ? (window.jWeixin || window.wx) : {}
  5. let tools = {}
  6. let wxInitPromise = null
  7. /**
  8. * 错误提示
  9. * @param msg
  10. */
  11. tools.error = function(msg) {
  12. uni.showToast({
  13. 'title': msg,
  14. 'icon': 'error',
  15. 'mask': true,
  16. 'duration': 1500
  17. })
  18. }
  19. /**
  20. * 成功提示
  21. * @param msg
  22. */
  23. tools.success = function(msg) {
  24. uni.showToast({
  25. 'title': msg,
  26. 'icon': 'success',
  27. 'mask': true,
  28. 'duration': 1500
  29. })
  30. }
  31. /**
  32. * 显示Loading
  33. */
  34. tools.showLoading = function() {
  35. uni.showLoading({
  36. title: '加载中...',
  37. mask: true
  38. });
  39. }
  40. /**
  41. * 关闭Loading
  42. */
  43. tools.hideLoading = function() {
  44. uni.hideLoading();
  45. }
  46. /**
  47. * 获取平台类型 1:微信,2:支付宝
  48. * @returns {boolean|number}
  49. */
  50. tools.platformType = function() {
  51. let ua = window.navigator.userAgent.toLowerCase();
  52. if (ua.indexOf('micromessenger') != -1) {
  53. return 1;
  54. } else if (ua.indexOf('alipay') != -1) {
  55. return 2;
  56. } else {
  57. return 0;
  58. }
  59. }
  60. /**
  61. * 获取开发平台
  62. * @returns {string}
  63. */
  64. tools.getPlatform = function() {
  65. let platForm = undefined;
  66. // #ifdef H5
  67. platForm = 'H5';
  68. //#endif
  69. // #ifdef APP-PLUS
  70. platForm = 'APP';
  71. //#endif
  72. // #ifdef APP-PLUS-NVUE
  73. platForm = 'APP';
  74. //#endif
  75. // #ifdef APP-NVUE
  76. platForm = 'APP';
  77. //#endif
  78. // #ifdef MP-WEIXIN
  79. platForm = 'MP-WEIXIN'; // 小程序
  80. //#endif
  81. // #ifdef MP-ALIPAY
  82. platForm = 'MP-ALIPAY';
  83. //#endif
  84. // #ifdef MP-BAIDU
  85. platForm = 'MP-BAIDU';
  86. //#endif
  87. // #ifdef MP-TOUTIAO
  88. platForm = 'MP-TOUTIAO';
  89. //#endif
  90. // #ifdef MP-LARK
  91. platForm = 'MP-LARK';
  92. //#endif
  93. // #ifdef MP-QQ
  94. platForm = 'MP-QQ';
  95. //#endif
  96. // #ifdef MP-KUAISHOU
  97. platForm = 'MP-KUAISHOU';
  98. //#endif
  99. // #ifdef QUICKAPP-WEBVIEW
  100. platForm = 'QUICKAPP-WEBVIEW';
  101. //#endif
  102. return platForm;
  103. }
  104. tools.wxInitialize = async function(jsApiList, debug) {
  105. // 如果已有成功的初始化缓存,直接返回
  106. if (wxInitPromise) {
  107. return wxInitPromise;
  108. }
  109. let url = window.location.href.replace('#', '');
  110. wxInitPromise = getJsConfig({
  111. 'url': url
  112. }).then((ret) => {
  113. if (ret.code === 1) {
  114. wxJs.config({
  115. debug: debug ? true : false,
  116. appId: ret.data.appid,
  117. timestamp: ret.data.timestamp * 1,
  118. nonceStr: ret.data.noncestr,
  119. signature: ret.data.signature,
  120. jsApiList: jsApiList
  121. });
  122. return new Promise((resolve) => {
  123. wxJs.ready(function() {
  124. resolve(true);
  125. });
  126. wxJs.error(function(res) {
  127. console.error('微信JSSDK初始化失败:', res);
  128. wxInitPromise = null; // 重置缓存,允许重试
  129. resolve(false);
  130. });
  131. });
  132. } else {
  133. console.error('获取微信配置失败:', ret);
  134. wxInitPromise = null;
  135. return false;
  136. }
  137. }).catch((err) => {
  138. console.error('微信JSSDK初始化异常:', err);
  139. wxInitPromise = null;
  140. return false;
  141. });
  142. return wxInitPromise;
  143. }
  144. tools.weiXinPay = function(data) {
  145. return new Promise((resolve, reject) => {
  146. wxJs.chooseWXPay({
  147. appId: data.appId, // 必填,公众号的唯一标识
  148. timestamp: data
  149. .timeStamp, // 支付签名时间戳,注意微信 jssdk 中的所有使用 timestamp 字段均为小写。但最新版的支付后台生成签名使用的 timeStamp 字段名需大写其中的 S 字符
  150. nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
  151. package: data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  152. signType: data.signType, // 微信支付V3的传入 RSA ,微信支付V2的传入格式与V2统一下单的签名格式保持一致
  153. paySign: data.paySign, // 支付签名
  154. success: function(res) {
  155. resolve(true)
  156. },
  157. fail: function(res) {
  158. resolve(false)
  159. },
  160. });
  161. })
  162. }
  163. // tools.wxSilentLogin = function(urlPath) {
  164. // // 微信浏览器
  165. // let url = encodeURIComponent(window.location.origin + '/#' + urlPath);
  166. // let appid = 'wx27df73f402695133';
  167. // let toUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + url +
  168. // "&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
  169. // location.href = toUrl
  170. // }
  171. tools.wxLogin = function(urlPath) {
  172. // 微信浏览器
  173. // let appid = 'wx5f2adeb213567b23';//测试
  174. let appid = 'wx27df73f402695133'; //正式
  175. let url = window.location.href;
  176. var type = '';
  177. if(appid = 'wx27df73f402695133'){
  178. type = 1
  179. }else{
  180. type = window.location.host === 'testwap.jhcs888.com' ? '3' : '1';
  181. }
  182. url = url.replace('&code=', '&code_a=1')
  183. url = encodeURIComponent(url);
  184. window.location.href = `https://wapwx2.jhcs888.com/#/?redirect_url=${Base64.encode(url)}&type=${type}`
  185. }
  186. export default tools