utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. }
  139. export function platformAgent() {
  140. let obj = {
  141. type: "",
  142. name: "",
  143. isWechat: false,
  144. isMini: false
  145. };
  146. // #ifdef APP-PLUS
  147. let platform = uni.getSystemInfoSync().platform;
  148. obj.type = "app";
  149. obj.name = platform;
  150. // #endif
  151. // #ifdef H5
  152. obj.type = "h5";
  153. obj.name = window.navigator.userAgent;
  154. obj.isWechat = window.navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1; // 检查是否在微信公众号打开
  155. // #endif
  156. // #ifdef MP
  157. obj.isMini = true;
  158. // #endif
  159. // #ifdef MP-WEIXIN
  160. obj.type = "mp-weixin";
  161. obj.name = "微信小程序";
  162. // #endif
  163. // #ifdef MP-ALIPAY
  164. obj.type = "mp-alipay";
  165. obj.name = "支付宝小程序";
  166. // #endif
  167. // #ifdef MP-BAIDU
  168. obj.type = "mp-baidu";
  169. obj.name = "百度小程序";
  170. // #endif
  171. // #ifdef MP-TOUTIAO
  172. obj.type = "mp-toutiao";
  173. obj.name = "字节跳动小程序";
  174. // #endif
  175. // #ifdef MP-QQ
  176. obj.type = "mp-qq";
  177. obj.name = "QQ小程序";
  178. // #endif
  179. // #ifdef MP-360
  180. obj.type = "mp-360";
  181. obj.name = "360小程序";
  182. // #endif
  183. return obj;
  184. }
  185. export function isIOS() {
  186. return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  187. }
  188. export function getShareUrl(url) {
  189. let users = storage.getJson("users");
  190. // #ifdef H5 || APP-PLUS
  191. if (users != null) {
  192. url += '#u=' + users.id;
  193. }
  194. // #endif
  195. return url;
  196. }
  197. export function iCopy(content) {
  198. return new Promise((resolve, reject) => {
  199. if (!content) {
  200. reject('复制的内容不能为空 !');
  201. } else {
  202. // H5端的复制逻辑
  203. // #ifdef H5
  204. if (!document.queryCommandSupported('copy')) {
  205. reject('浏览器不支持');
  206. } else {
  207. let textarea = document.createElement("textarea")
  208. textarea.value = content
  209. textarea.readOnly = "readOnly"
  210. document.body.appendChild(textarea)
  211. textarea.select()
  212. textarea.setSelectionRange(0, content.length)
  213. let result = document.execCommand("copy")
  214. if (result) {
  215. resolve("内容复制成功")
  216. } else {
  217. reject("复制失败,请检查h5中调用该方法的方式,是不是用户点击的方式调用的,如果不是请改为用户点击的方式触发该方法,因为h5中安全性,不能js直接调用!")
  218. }
  219. textarea.remove()
  220. }
  221. // #endif
  222. // 小程序端 和 app端的复制功能
  223. //#ifndef H5
  224. uni.setClipboardData({
  225. data: content,
  226. success: function() {
  227. resolve("复制成功")
  228. },
  229. fail: function() {
  230. reject("复制失败")
  231. }
  232. });
  233. //#endif
  234. }
  235. });
  236. }