tools.js 5.7 KB

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