tools.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import wxJs from "jweixin-module"
  2. let tools = {}
  3. /**
  4. * 大小判断
  5. * @param a
  6. * @param b
  7. * @returns {number}
  8. */
  9. tools.sortNumber = function (a, b) {
  10. return a - b;
  11. }
  12. /**
  13. * 保留两位小数
  14. * @param num
  15. * @returns {string}
  16. */
  17. tools.twoFloating = function (num) {
  18. // 获取两位小数
  19. let price = "";
  20. price = num * 1;
  21. price = String(price).split(".")[1];
  22. if (price !== undefined && price.length === 1) {
  23. price = `.${price}0`;
  24. } else {
  25. price === undefined ? (price = ".00") : (price = `.${price}`);
  26. }
  27. return price;
  28. }
  29. tools.formatDecimal = function (num, decimal) {
  30. num = num.toString()
  31. let index = num.indexOf('.')
  32. if (index !== -1) {
  33. num = num.substring(0, decimal + index + 1)
  34. } else {
  35. num = num.substring(0)
  36. }
  37. return parseFloat(num).toFixed(decimal)
  38. }
  39. /**
  40. * 错误提示
  41. * @param msg
  42. */
  43. tools.error = function (msg) {
  44. uni.showToast({
  45. 'title': msg,
  46. 'icon': 'error',
  47. 'mask': true,
  48. 'duration': 1500
  49. })
  50. }
  51. /**
  52. * 成功提示
  53. * @param msg
  54. */
  55. tools.success = function (msg) {
  56. uni.showToast({
  57. 'title': msg,
  58. 'icon': 'success',
  59. 'mask': true,
  60. 'duration': 1500
  61. })
  62. }
  63. /**
  64. * 显示Loading
  65. */
  66. tools.showLoading = function () {
  67. uni.showLoading({
  68. title: '加载中...',
  69. mask: true
  70. });
  71. }
  72. /**
  73. * 关闭Loading
  74. */
  75. tools.hideLoading = function () {
  76. uni.hideLoading();
  77. }
  78. /**
  79. * 获取时间戳(毫秒)
  80. * @returns {number}
  81. */
  82. tools.getTime = function () {
  83. return new Date().getTime();
  84. }
  85. tools.updateVersion = function (sysVersion, appUrl) {
  86. let app_version = plus.runtime.version;
  87. console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
  88. console.log(app_version < sysVersion)
  89. if (app_version < sysVersion) {
  90. uni.showLoading({
  91. title: '更新中……'
  92. })
  93. uni.downloadFile({//执行下载
  94. url: appUrl, //下载地址
  95. success: (downloadResult) => {//下载成功
  96. uni.hideLoading();
  97. if (downloadResult.statusCode === 200) {
  98. uni.showModal({
  99. title: '',
  100. content: '更新成功,确定现在重启吗?',
  101. confirmText: '重启',
  102. confirmColor: '#EE8F57',
  103. success: function (res) {
  104. if (res.confirm === true) {
  105. plus.runtime.install(//安装
  106. downloadResult.tempFilePath, {
  107. force: true
  108. },
  109. function (res) {
  110. tools.success('更新成功,重启中')
  111. plus.runtime.restart();
  112. }
  113. );
  114. }
  115. }
  116. });
  117. }
  118. }
  119. });
  120. } else {
  121. // tools.success('你已是最新版本')
  122. }
  123. }
  124. /**
  125. * uniapp html图片显示控制
  126. * @param str
  127. * @returns {*}
  128. */
  129. tools.imgDeal = function (str) {
  130. console.log(str)
  131. if(str===null || str===undefined){
  132. return '';
  133. }else {
  134. return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
  135. }
  136. }
  137. /**
  138. * 获取平台类型 1:微信,2:支付宝
  139. * @returns {boolean|number}
  140. */
  141. tools.platformType = function () {
  142. let ua = window.navigator.userAgent.toLowerCase();
  143. if (ua.indexOf('micromessenger') != -1) {
  144. return 1;
  145. } else if (ua.indexOf('alipay') != -1) {
  146. return 2;
  147. } else {
  148. return 0;
  149. }
  150. }
  151. /**
  152. * 获取开发平台
  153. * @returns {string}
  154. */
  155. tools.getPlatform = function () {
  156. let platForm = undefined;
  157. // #ifdef H5
  158. platForm = 'H5';
  159. //#endif
  160. // #ifdef APP-PLUS
  161. platForm = 'APP';
  162. //#endif
  163. // #ifdef APP-PLUS-NVUE
  164. platForm = 'APP';
  165. //#endif
  166. // #ifdef APP-NVUE
  167. platForm = 'APP';
  168. //#endif
  169. // #ifdef MP-WEIXIN
  170. platForm = 'MP-WEIXIN'; // 小程序
  171. //#endif
  172. // #ifdef MP-ALIPAY
  173. platForm = 'MP-ALIPAY';
  174. //#endif
  175. // #ifdef MP-BAIDU
  176. platForm = 'MP-BAIDU';
  177. //#endif
  178. // #ifdef MP-TOUTIAO
  179. platForm = 'MP-TOUTIAO';
  180. //#endif
  181. // #ifdef MP-LARK
  182. platForm = 'MP-LARK';
  183. //#endif
  184. // #ifdef MP-QQ
  185. platForm = 'MP-QQ';
  186. //#endif
  187. // #ifdef MP-KUAISHOU
  188. platForm = 'MP-KUAISHOU';
  189. //#endif
  190. // #ifdef QUICKAPP-WEBVIEW
  191. platForm = 'QUICKAPP-WEBVIEW';
  192. //#endif
  193. return platForm;
  194. }
  195. /**
  196. * 记录用户登录信息
  197. * @param data
  198. * @param type
  199. */
  200. tools.setLoginData = function (data) {
  201. uni.setStorageSync('m_id', data.m_id)
  202. uni.setStorageSync('token', data.token)
  203. tools.success('登陆成功')
  204. setTimeout(() => {
  205. uni.reLaunch({
  206. url: '/pages/index/index'
  207. });
  208. }, 1500)
  209. }
  210. tools.jsConfig=function () {
  211. wxJs.config({
  212. beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
  213. debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  214. appId: '', // 必填,企业微信的corpID,必须是本企业的corpID,不允许跨企业使用
  215. timestamp: '', // 必填,生成签名的时间戳
  216. nonceStr: '', // 必填,生成签名的随机串
  217. signature: '',// 必填,签名,见 附录-JS-SDK使用权限签名算法
  218. jsApiList: [] // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
  219. });
  220. }
  221. tools.wxLogin = function () {
  222. // 微信浏览器
  223. let url = encodeURIComponent(window.location.href);
  224. window.location.href = `https://test-api.wealfavor.cn/api/weChat/wx?redirect_url=${Base64.encode(url)}`
  225. }
  226. export default tools