tools.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. tools.replenishZero = function (str, n) {
  39. console.log(str)
  40. if (str.indexOf('0x') === 0) {
  41. str = str.substring(2)
  42. }
  43. console.log(str)
  44. if (!n) {
  45. n = 64
  46. }
  47. return (Array(n).join(0) + str).slice(-n);
  48. }
  49. /**
  50. * 错误提示
  51. * @param msg
  52. */
  53. tools.error = function (msg) {
  54. uni.showToast({
  55. 'title': msg,
  56. 'icon': 'error',
  57. 'mask': true,
  58. 'duration': 1500
  59. })
  60. }
  61. /**
  62. * 成功提示
  63. * @param msg
  64. */
  65. tools.success = function (msg) {
  66. uni.showToast({
  67. 'title': msg,
  68. 'icon': 'success',
  69. 'mask': true,
  70. 'duration': 1500
  71. })
  72. }
  73. /**
  74. * 显示Loading
  75. */
  76. tools.showLoading = function () {
  77. uni.showLoading({
  78. title: '加载中...',
  79. mask: true
  80. });
  81. }
  82. /**
  83. * 关闭Loading
  84. */
  85. tools.hideLoading = function () {
  86. uni.hideLoading();
  87. }
  88. /**
  89. * 获取时间戳(毫秒)
  90. * @returns {number}
  91. */
  92. tools.getTime = function () {
  93. return new Date().getTime();
  94. }
  95. tools.updateVersion = function (sysVersion, appUrl) {
  96. let app_version = plus.runtime.version;
  97. console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
  98. console.log(app_version < sysVersion)
  99. if (app_version < sysVersion) {
  100. uni.showLoading({
  101. title: '更新中……'
  102. })
  103. uni.downloadFile({//执行下载
  104. url: appUrl, //下载地址
  105. success: (downloadResult) => {//下载成功
  106. uni.hideLoading();
  107. if (downloadResult.statusCode === 200) {
  108. uni.showModal({
  109. title: '',
  110. content: '更新成功,确定现在重启吗?',
  111. confirmText: '重启',
  112. confirmColor: '#EE8F57',
  113. success: function (res) {
  114. if (res.confirm === true) {
  115. plus.runtime.install(//安装
  116. downloadResult.tempFilePath, {
  117. force: true
  118. },
  119. function (res) {
  120. tools.success('更新成功,重启中')
  121. plus.runtime.restart();
  122. }
  123. );
  124. }
  125. }
  126. });
  127. }
  128. }
  129. });
  130. } else {
  131. // tools.success('你已是最新版本')
  132. }
  133. }
  134. /**
  135. * uniapp html图片显示控制
  136. * @param str
  137. * @returns {*}
  138. */
  139. tools.imgDeal = function (str) {
  140. console.log(str)
  141. if(str===null || str===undefined){
  142. return '';
  143. }else {
  144. return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
  145. }
  146. }
  147. /**
  148. * 获取平台类型 1:微信,2:支付宝
  149. * @returns {boolean|number}
  150. */
  151. tools.platformType = function () {
  152. let ua = window.navigator.userAgent.toLowerCase();
  153. if (ua.indexOf('micromessenger') != -1) {
  154. return 1;
  155. } else if (ua.indexOf('alipay') != -1) {
  156. return 2;
  157. } else {
  158. return 0;
  159. }
  160. }
  161. /**
  162. * 获取开发平台
  163. * @returns {string}
  164. */
  165. tools.getPlatform = function () {
  166. let platForm = undefined;
  167. // #ifdef H5
  168. platForm = 'H5';
  169. //#endif
  170. // #ifdef APP-PLUS
  171. platForm = 'APP';
  172. //#endif
  173. // #ifdef APP-PLUS-NVUE
  174. platForm = 'APP';
  175. //#endif
  176. // #ifdef APP-NVUE
  177. platForm = 'APP';
  178. //#endif
  179. // #ifdef MP-WEIXIN
  180. platForm = 'MP-WEIXIN'; // 小程序
  181. //#endif
  182. // #ifdef MP-ALIPAY
  183. platForm = 'MP-ALIPAY';
  184. //#endif
  185. // #ifdef MP-BAIDU
  186. platForm = 'MP-BAIDU';
  187. //#endif
  188. // #ifdef MP-TOUTIAO
  189. platForm = 'MP-TOUTIAO';
  190. //#endif
  191. // #ifdef MP-LARK
  192. platForm = 'MP-LARK';
  193. //#endif
  194. // #ifdef MP-QQ
  195. platForm = 'MP-QQ';
  196. //#endif
  197. // #ifdef MP-KUAISHOU
  198. platForm = 'MP-KUAISHOU';
  199. //#endif
  200. // #ifdef QUICKAPP-WEBVIEW
  201. platForm = 'QUICKAPP-WEBVIEW';
  202. //#endif
  203. return platForm;
  204. }
  205. tools.leftClick=function (){
  206. let pages=getCurrentPages();
  207. if(pages.length>1){
  208. uni.navigateBack({
  209. delta: 1
  210. })
  211. }else {
  212. uni.reLaunch({
  213. url: '/pages/index/index'
  214. });
  215. }
  216. }
  217. /**
  218. * 跳转到异常界面
  219. * @param errorType
  220. */
  221. tools.goToError=function (errorType){
  222. uni.reLaunch({
  223. 'url':'/pages/index/error?errorType='+errorType
  224. })
  225. }
  226. tools.toError=function (errorMsg){
  227. if(!errorMsg){
  228. errorMsg='出错啦!~~';
  229. }
  230. uni.reLaunch({
  231. url: '/pages/index/error?errorMsg='+errorMsg
  232. });
  233. }
  234. tools.setLoginInfo=function (data){
  235. uni.setStorageSync('token', data.token);
  236. }
  237. tools.getShowAddress=function (address){
  238. return (address.substr(0,8)+'...'+address.substr(address.length-4))
  239. }
  240. tools.getAddress=function (address){
  241. return address.substr(0,4)+'...'+address.substr(-4);
  242. }
  243. tools.isDevelopment=function () {
  244. if(process.env.NODE_ENV==='development'){
  245. return true
  246. }else {
  247. return false
  248. }
  249. }
  250. export default tools