utils.js 5.5 KB

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