tools.js 5.4 KB

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