utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import storage from './storage';
  2. export function msg(content,time=3000){
  3. uni.showToast({
  4. icon:'none',
  5. title: content,
  6. duration: time
  7. });
  8. }
  9. export function showLoading(content="加载数据中...",mask=true){
  10. uni.showLoading({
  11. title: content,
  12. mask: mask
  13. });
  14. }
  15. export function hideLoading(timer=0){
  16. if(timer > 0){
  17. var t = setTimeout(function () {
  18. uni.hideLoading();
  19. clearTimeout(t);
  20. }, timer);
  21. }else{
  22. uni.hideLoading();
  23. }
  24. }
  25. export function in_array(search,array){
  26. let flag = false;
  27. for(let i in array){
  28. if(array[i]==search){
  29. flag = true;
  30. break;
  31. }
  32. }
  33. return flag;
  34. }
  35. export function isDataType(data,type){
  36. return Object.prototype.toString.call(data) === '[object '+type+']';
  37. }
  38. export function ltrim(str,char){
  39. let pos = str.indexOf(char);
  40. let sonstr = str.substr(pos+1);
  41. return sonstr;
  42. }
  43. export function rtrim(str,char){
  44. let pos = str.lastIndexOf(char);
  45. let sonstr = str.substr(0,pos);
  46. return sonstr;
  47. }
  48. /**
  49. * 保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面。
  50. */
  51. export function navigateTo(url,params){
  52. uni.navigateTo({
  53. url: parseUrl(url,params)
  54. })
  55. }
  56. /**
  57. * 关闭当前页面,跳转到应用内的某个页面。
  58. */
  59. export function redirectTo(url,params){
  60. uni.redirectTo({
  61. url: parseUrl(url,params)
  62. });
  63. }
  64. /**
  65. * 关闭所有页面,打开到应用内的某个页面。
  66. */
  67. export function reLaunch(url,params){
  68. uni.reLaunch({
  69. url: parseUrl(url,params)
  70. });
  71. }
  72. /**
  73. * 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
  74. */
  75. export function switchTab(url,params){
  76. uni.switchTab({
  77. url: parseUrl(url,params)
  78. });
  79. }
  80. /**
  81. * 关闭当前页面,返回上一页面或多级页面
  82. */
  83. export function navigateBack(delta){
  84. uni.navigateBack({
  85. delta: delta
  86. });
  87. }
  88. /**
  89. * 预加载页面,是一种性能优化技术。被预载的页面,在打开时速度更快。
  90. */
  91. export function preloadPage(){
  92. uni.preloadPage({
  93. url: parseUrl(url,params)
  94. });
  95. }
  96. export function prePage(){
  97. let pages = getCurrentPages();
  98. let prePage = pages[pages.length - 2];
  99. // #ifdef H5
  100. return prePage;
  101. // #endif
  102. return prePage.$vm;
  103. }
  104. /**
  105. * rpx转px
  106. * @param int|float num
  107. */
  108. export function rpx2px(num){
  109. // const info = uni.getSystemInfoSync()
  110. // let scale = 750 / info.windowWidth;
  111. // return (Number.isNaN(parseFloat(num)) ? 0 : parseFloat(num)) / scale;
  112. return uni.upx2px(num);
  113. }
  114. /**
  115. * @param int|float num
  116. */
  117. export function px2rpx(num){
  118. return num/(uni.upx2px(num)/num);
  119. }
  120. export function getSystemInfo(){
  121. const info = uni.getSystemInfoSync();
  122. return {
  123. w: info.windowWidth,
  124. h: info.windowHeight
  125. };
  126. }
  127. function parseUrl(url,params){
  128. let arr = [];
  129. let string = '';
  130. for(let i in params){
  131. arr.push(i + "=" + params[i]);
  132. }
  133. string = "/pages/" + url;
  134. if(arr.length > 0){
  135. string += "?" + arr.join("&");
  136. }
  137. return string;
  138. }