tools.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. import { getJsConfig } from '@/http/common.js'
  6. let tools = {}
  7. let wxInitPromise = null
  8. /**
  9. * 错误提示
  10. * @param msg
  11. */
  12. tools.error = function(msg) {
  13. uni.showToast({
  14. 'title': msg,
  15. 'icon': 'error',
  16. 'mask': true,
  17. 'duration': 1500
  18. })
  19. }
  20. /**
  21. * 成功提示
  22. * @param msg
  23. */
  24. tools.success = function(msg) {
  25. uni.showToast({
  26. 'title': msg,
  27. 'icon': 'success',
  28. 'mask': true,
  29. 'duration': 1500
  30. })
  31. }
  32. /**
  33. * 显示Loading
  34. */
  35. tools.showLoading = function() {
  36. uni.showLoading({
  37. title: '加载中...',
  38. mask: true
  39. });
  40. }
  41. /**
  42. * 关闭Loading
  43. */
  44. tools.hideLoading = function() {
  45. uni.hideLoading();
  46. }
  47. /**
  48. * 获取平台类型 1:微信,2:支付宝
  49. * @returns {boolean|number}
  50. */
  51. tools.platformType = function() {
  52. let ua = window.navigator.userAgent.toLowerCase();
  53. if (ua.indexOf('micromessenger') != -1) {
  54. return 1;
  55. } else if (ua.indexOf('alipay') != -1) {
  56. return 2;
  57. } else {
  58. return 0;
  59. }
  60. }
  61. /**
  62. * 获取开发平台
  63. * @returns {string}
  64. */
  65. tools.getPlatform = function() {
  66. let platForm = undefined;
  67. // #ifdef H5
  68. platForm = 'H5';
  69. //#endif
  70. // #ifdef APP-PLUS
  71. platForm = 'APP';
  72. //#endif
  73. // #ifdef APP-PLUS-NVUE
  74. platForm = 'APP';
  75. //#endif
  76. // #ifdef APP-NVUE
  77. platForm = 'APP';
  78. //#endif
  79. // #ifdef MP-WEIXIN
  80. platForm = 'MP-WEIXIN'; // 小程序
  81. //#endif
  82. // #ifdef MP-ALIPAY
  83. platForm = 'MP-ALIPAY';
  84. //#endif
  85. // #ifdef MP-BAIDU
  86. platForm = 'MP-BAIDU';
  87. //#endif
  88. // #ifdef MP-TOUTIAO
  89. platForm = 'MP-TOUTIAO';
  90. //#endif
  91. // #ifdef MP-LARK
  92. platForm = 'MP-LARK';
  93. //#endif
  94. // #ifdef MP-QQ
  95. platForm = 'MP-QQ';
  96. //#endif
  97. // #ifdef MP-KUAISHOU
  98. platForm = 'MP-KUAISHOU';
  99. //#endif
  100. // #ifdef QUICKAPP-WEBVIEW
  101. platForm = 'QUICKAPP-WEBVIEW';
  102. //#endif
  103. return platForm;
  104. }
  105. tools.wxInitialize = async function(jsApiList, debug) {
  106. // 如果已有成功的初始化缓存,直接返回
  107. if (wxInitPromise) {
  108. return wxInitPromise;
  109. }
  110. let url = window.location.href.replace('#', '');
  111. wxInitPromise = getJsConfig({
  112. 'url': url
  113. }).then((ret) => {
  114. if (ret.code === 1) {
  115. wxJs.config({
  116. debug: debug ? true : false,
  117. appId: ret.data.appid,
  118. timestamp: ret.data.timestamp * 1,
  119. nonceStr: ret.data.noncestr,
  120. signature: ret.data.signature,
  121. jsApiList: jsApiList
  122. });
  123. return new Promise((resolve) => {
  124. wxJs.ready(function() {
  125. resolve(true);
  126. });
  127. wxJs.error(function(res) {
  128. console.error('微信JSSDK初始化失败:', res);
  129. wxInitPromise = null; // 重置缓存,允许重试
  130. resolve(false);
  131. });
  132. });
  133. } else {
  134. console.error('获取微信配置失败:', ret);
  135. wxInitPromise = null;
  136. return false;
  137. }
  138. }).catch((err) => {
  139. console.error('微信JSSDK初始化异常:', err);
  140. wxInitPromise = null;
  141. return false;
  142. });
  143. return wxInitPromise;
  144. }
  145. tools.weiXinPay = function(data) {
  146. return new Promise((resolve, reject) => {
  147. wxJs.chooseWXPay({
  148. appId: data.appId, // 必填,公众号的唯一标识
  149. timestamp: data.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 === 'tepayh5ap.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