tools.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import {
  2. Base64
  3. } from '@/js_sdk/js-base64/base64';
  4. let tools = {}
  5. /**
  6. * 大小判断
  7. * @param a
  8. * @param b
  9. * @returns {number}
  10. */
  11. tools.sortNumber = function (a, b) {
  12. return a - b;
  13. }
  14. /**
  15. * 保留两位小数
  16. * @param num
  17. * @returns {string}
  18. */
  19. tools.twoFloating = function (num) {
  20. // 获取两位小数
  21. let price = "";
  22. price = num * 1;
  23. price = String(price).split(".")[1];
  24. if (price !== undefined && price.length === 1) {
  25. price = `.${price}0`;
  26. } else {
  27. price === undefined ? (price = ".00") : (price = `.${price}`);
  28. }
  29. return price;
  30. }
  31. tools.formatDecimal = function (num, decimal) {
  32. num = num.toString()
  33. let index = num.indexOf('.')
  34. if (index !== -1) {
  35. num = num.substring(0, decimal + index + 1)
  36. } else {
  37. num = num.substring(0)
  38. }
  39. return parseFloat(num).toFixed(decimal)
  40. }
  41. /**
  42. * 错误提示
  43. * @param msg
  44. */
  45. tools.error = function (msg) {
  46. uni.showToast({
  47. 'title': msg,
  48. 'icon': 'error',
  49. 'mask': true,
  50. 'duration': 1500
  51. })
  52. }
  53. /**
  54. * 成功提示
  55. * @param msg
  56. */
  57. tools.success = function (msg) {
  58. uni.showToast({
  59. 'title': msg,
  60. 'icon': 'success',
  61. 'mask': true,
  62. 'duration': 1500
  63. })
  64. }
  65. /**
  66. * 显示Loading
  67. */
  68. tools.showLoading = function () {
  69. uni.showLoading({
  70. title: '加载中...',
  71. mask: true
  72. });
  73. }
  74. /**
  75. * 关闭Loading
  76. */
  77. tools.hideLoading = function () {
  78. uni.hideLoading();
  79. }
  80. /**
  81. * 获取时间戳(毫秒)
  82. * @returns {number}
  83. */
  84. tools.getTime = function () {
  85. return new Date().getTime();
  86. }
  87. tools.updateVersion = function (sysVersion, appUrl) {
  88. let app_version = plus.runtime.version;
  89. console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
  90. console.log(app_version < sysVersion)
  91. if (app_version < sysVersion) {
  92. uni.showLoading({
  93. title: '更新中……'
  94. })
  95. uni.downloadFile({//执行下载
  96. url: appUrl, //下载地址
  97. success: (downloadResult) => {//下载成功
  98. uni.hideLoading();
  99. if (downloadResult.statusCode === 200) {
  100. uni.showModal({
  101. title: '',
  102. content: '更新成功,确定现在重启吗?',
  103. confirmText: '重启',
  104. confirmColor: '#EE8F57',
  105. success: function (res) {
  106. if (res.confirm === true) {
  107. plus.runtime.install(//安装
  108. downloadResult.tempFilePath, {
  109. force: true
  110. },
  111. function (res) {
  112. tools.success('更新成功,重启中')
  113. plus.runtime.restart();
  114. }
  115. );
  116. }
  117. }
  118. });
  119. }
  120. }
  121. });
  122. } else {
  123. // tools.success('你已是最新版本')
  124. }
  125. }
  126. /**
  127. * uniapp html图片显示控制
  128. * @param str
  129. * @returns {*}
  130. */
  131. tools.imgDeal = function (str) {
  132. console.log(str)
  133. if(str===null || str===undefined){
  134. return '';
  135. }else {
  136. return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
  137. }
  138. }
  139. /**
  140. * 获取平台类型 1:微信,2:支付宝
  141. * @returns {boolean|number}
  142. */
  143. tools.platformType = function () {
  144. let ua = window.navigator.userAgent.toLowerCase();
  145. if (ua.indexOf('micromessenger') != -1) {
  146. return 1;
  147. } else if (ua.indexOf('alipay') != -1) {
  148. return 2;
  149. } else {
  150. return 0;
  151. }
  152. }
  153. /**
  154. * 获取开发平台
  155. * @returns {string}
  156. */
  157. tools.getPlatform = function () {
  158. let platForm = undefined;
  159. // #ifdef H5
  160. platForm = 'H5';
  161. //#endif
  162. // #ifdef APP-PLUS
  163. platForm = 'APP';
  164. //#endif
  165. // #ifdef APP-PLUS-NVUE
  166. platForm = 'APP';
  167. //#endif
  168. // #ifdef APP-NVUE
  169. platForm = 'APP';
  170. //#endif
  171. // #ifdef MP-WEIXIN
  172. platForm = 'MP-WEIXIN'; // 小程序
  173. //#endif
  174. // #ifdef MP-ALIPAY
  175. platForm = 'MP-ALIPAY';
  176. //#endif
  177. // #ifdef MP-BAIDU
  178. platForm = 'MP-BAIDU';
  179. //#endif
  180. // #ifdef MP-TOUTIAO
  181. platForm = 'MP-TOUTIAO';
  182. //#endif
  183. // #ifdef MP-LARK
  184. platForm = 'MP-LARK';
  185. //#endif
  186. // #ifdef MP-QQ
  187. platForm = 'MP-QQ';
  188. //#endif
  189. // #ifdef MP-KUAISHOU
  190. platForm = 'MP-KUAISHOU';
  191. //#endif
  192. // #ifdef QUICKAPP-WEBVIEW
  193. platForm = 'QUICKAPP-WEBVIEW';
  194. //#endif
  195. return platForm;
  196. }
  197. /**
  198. * 记录用户登录信息
  199. * @param data
  200. * @param type
  201. */
  202. tools.setLoginData = function (data) {
  203. uni.setStorageSync('m_id', data.m_id)
  204. uni.setStorageSync('token', data.token)
  205. tools.success('登陆成功')
  206. setTimeout(() => {
  207. uni.reLaunch({
  208. url: '/pages/index/index'
  209. });
  210. }, 1500)
  211. }
  212. export default tools