tools.js 4.6 KB

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