Brak opisu

helper.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* eslint-disable */
  2. import mRouter from '@/utils/router';
  3. import mStore from '@/store';
  4. import appShare from '@/utils/share';
  5. import { http } from '@/utils/request';
  6. export default {
  7. /**
  8. * toast提示
  9. */
  10. toast(title, duration = 3000, mask = false, icon = 'none') {
  11. if (Boolean(title) === false) {
  12. return;
  13. }
  14. uni.showToast({
  15. title,
  16. duration,
  17. mask,
  18. icon
  19. });
  20. },
  21. httpPost(url,params,index){
  22. http.post(url,params).then((res)=>{
  23. if(res.state.toLowerCase()==="success"){
  24. uni.hideLoading()
  25. uni.showToast({
  26. title:'操作成功',
  27. duration:500
  28. });
  29. setTimeout(() => {
  30. uni.$emit("updateList",{}); //列表刷新数据
  31. uni.navigateBack({ delta:index, })
  32. },500)
  33. }
  34. })
  35. },
  36. httpGetDetail(url,type,id,fn){
  37. let detailContentData
  38. const params = {
  39. type: type,
  40. workorderid:id,
  41. token: uni.getStorageSync("token"),
  42. }
  43. http.get(url, params).then((response) => {
  44. if (response.state.toLowerCase() === "success") {
  45. fn(response.data)
  46. }
  47. }).catch((e) => {
  48. console.log(e);
  49. })
  50. },
  51. // 详情返回页面 参数表示页面回退几级
  52. returnPage(index) {
  53. uni.navigateBack({
  54. delta:index,
  55. })
  56. },
  57. getImgString(string) {
  58. const imgIdListToString= string.toString()
  59. const imgid = (imgIdListToString.substring(imgIdListToString.length - 1) == ',') ?
  60. imgIdListToString.substring(0, imgIdListToString.length - 1) : imgIdListToString;
  61. return imgid
  62. },
  63. //获取当前时间
  64. CurentTime() {
  65. const now = new Date();
  66. const year = now.getFullYear(); //年
  67. const month = now.getMonth() + 1; //月 .toString().padstart(2,0)
  68. const day = now.getDate().toString().padStart(2,0); //日
  69. const hh = now.getHours().toString().padStart(2,0); //时
  70. const mm = now.getMinutes().toString().padStart(2,0); //分
  71. const ss = now.getSeconds().toString().padStart(2,0); //秒
  72. // return year + "-" + month.toString().padStart(2,0) + "-" + day + " " + hh + ":" + mm + ":" + ss ;
  73. return year + "-" + month.toString().padStart(2,0) + "-" + day + " " + hh + ":" + mm + ":" + ss ;
  74. },
  75. nowTime() {
  76. const now = new Date();
  77. const year = now.getFullYear(); //年
  78. const month = now.getMonth() + 1; //月 .toString().padstart(2,0)
  79. const day = now.getDate().toString().padStart(2,0); //日
  80. const hh = now.getHours().toString().padStart(2,0); //时
  81. const mm = now.getMinutes().toString().padStart(2,0); //分
  82. const ss = now.getSeconds().toString().padStart(2,0); //秒
  83. return hh + ":" + mm + ":" + ss ;
  84. },
  85. //获取当前时间年月份
  86. CurentTimeType() {
  87. const now = new Date();
  88. const year = now.getFullYear(); //年
  89. const month = now.getMonth() + 1; //月 .toString().padstart(2,0)
  90. const day = now.getDate().toString().padStart(2,0); //日
  91. const hh = now.getHours().toString().padStart(2,0); //时
  92. const mm = now.getMinutes().toString().padStart(2,0); //分
  93. const ss = now.getSeconds().toString().padStart(2,0); //秒
  94. return year + "年" + month.toString().padStart(2,0) + "月" + day + "日" + hh + "时" + mm + "分" + ss + "秒";
  95. },
  96. /**
  97. * 返回上一页携带参数
  98. */
  99. prePage(index) {
  100. let pages = getCurrentPages();
  101. let prePage = pages[pages.length - (index || 2)];
  102. // #ifdef H5
  103. return prePage;
  104. // #endif
  105. return prePage.$vm;
  106. },
  107. /**
  108. * 开发环境全局打印日志
  109. * @param {Object} title
  110. */
  111. log(title) {
  112. if (process.env.NODE_ENV === 'development' && Boolean(title) === true) {
  113. console.log(JSON.stringify(title));
  114. }
  115. },
  116. /**
  117. * 异步获取设备信息
  118. */
  119. getInfoAsync() {
  120. return new Promise((resolve, reject) => {
  121. plus.device.getInfo({
  122. success(e) {
  123. resolve(e);
  124. },
  125. fail(e) {
  126. reject(e.message);
  127. }
  128. });
  129. });
  130. },
  131. /**
  132. * 安卓10不支持IMEI,则获取OAID
  133. */
  134. getOaidAsync() {
  135. return new Promise((resolve, reject) => {
  136. plus.device.getOAID({
  137. success(e) {
  138. resolve(e);
  139. },
  140. fail(e) {
  141. reject(e.message);
  142. }
  143. });
  144. });
  145. },
  146. /**
  147. * 获取一个随机数
  148. * @param {Object} min
  149. * @param {Object} max
  150. */
  151. random(min, max) {
  152. switch (arguments.length) {
  153. case 1:
  154. return parseInt(Math.random() * min + 1, 10);
  155. break;
  156. case 2:
  157. return parseInt(Math.random() * (max - min + 1) + min, 10);
  158. break;
  159. default:
  160. return 0;
  161. break;
  162. }
  163. },
  164. /**
  165. * 获取ios的IDFA
  166. */
  167. getIdfa() {
  168. let idfa = '';
  169. try {
  170. if ('iOS' == plus.os.name) {
  171. let manager = plus.ios.invoke('ASIdentifierManager', 'sharedManager');
  172. if (plus.ios.invoke(manager, 'isAdvertisingTrackingEnabled')) {
  173. let identifier = plus.ios.invoke(manager, 'advertisingIdentifier');
  174. idfa = plus.ios.invoke(identifier, 'UUIDString');
  175. plus.ios.deleteObject(identifier);
  176. }
  177. plus.ios.deleteObject(manager);
  178. }
  179. } catch (e) {
  180. console.error('获取idfa失败');
  181. }
  182. return idfa;
  183. },
  184. /*
  185. * obj 转 params字符串参数
  186. * 例子:{a:1,b:2} => a=1&b=2
  187. */
  188. objParseParam(obj) {
  189. let paramsStr = '';
  190. if (obj instanceof Array) return paramsStr;
  191. if (!(obj instanceof Object)) return paramsStr;
  192. for (let key in obj) {
  193. paramsStr += `${key}=${obj[key]}&`;
  194. }
  195. return paramsStr.substring(0, paramsStr.length - 1);
  196. },
  197. /*
  198. * obj 转 路由地址带参数
  199. * 例子:{a:1,b:2} => /pages/index/index?a=1&b=2
  200. */
  201. objParseUrlAndParam(path, obj) {
  202. let url = path || '/';
  203. let paramsStr = '';
  204. if (obj instanceof Array) return url;
  205. if (!(obj instanceof Object)) return url;
  206. paramsStr = this.objParseParam(obj);
  207. paramsStr && (url += '?');
  208. url += paramsStr;
  209. return url;
  210. },
  211. /*
  212. * 获取url字符串参数
  213. */
  214. getRequestParameters(locationhref) {
  215. let href = locationhref || '';
  216. let theRequest = new Object();
  217. let str = href.split('?')[1];
  218. if (str != undefined) {
  219. let strs = str.split('&');
  220. for (let i = 0; i < strs.length; i++) {
  221. theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
  222. }
  223. }
  224. return theRequest;
  225. },
  226. /**
  227. * 加密字符串
  228. */
  229. strEncode(str) {
  230. const key = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  231. let l = key.length;
  232. let a = key.split('');
  233. let s = '',
  234. b,
  235. b1,
  236. b2,
  237. b3;
  238. for (let i = 0; i < str.length; i++) {
  239. b = str.charCodeAt(i);
  240. b1 = b % l;
  241. b = (b - b1) / l;
  242. b2 = b % l;
  243. b = (b - b2) / l;
  244. b3 = b % l;
  245. s += a[b3] + a[b2] + a[b1];
  246. }
  247. return s;
  248. },
  249. /**
  250. * 解密字符串
  251. */
  252. strDecode(str) {
  253. const key = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  254. let l = key.length;
  255. let b,
  256. b1,
  257. b2,
  258. b3,
  259. d = 0,
  260. s;
  261. s = new Array(Math.floor(str.length / 3));
  262. b = s.length;
  263. for (let i = 0; i < b; i++) {
  264. b1 = key.indexOf(str.charAt(d));
  265. d++;
  266. b2 = key.indexOf(str.charAt(d));
  267. d++;
  268. b3 = key.indexOf(str.charAt(d));
  269. d++;
  270. s[i] = b1 * l * l + b2 * l + b3;
  271. }
  272. b = eval('String.fromCharCode(' + s.join(',') + ')');
  273. return b;
  274. },
  275. /**
  276. * 比较版本号
  277. */
  278. compareVersion(reqV, curV) {
  279. if (curV && reqV) {
  280. let arr1 = curV.split('.'),
  281. arr2 = reqV.split('.');
  282. let minLength = Math.min(arr1.length, arr2.length),
  283. position = 0,
  284. diff = 0;
  285. while (
  286. position < minLength &&
  287. (diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0
  288. ) {
  289. position++;
  290. }
  291. diff = diff != 0 ? diff : arr1.length - arr2.length;
  292. if (diff > 0) {
  293. if (position == minLength - 1) {
  294. return 1;
  295. } else {
  296. return 2;
  297. }
  298. } else {
  299. return 0;
  300. }
  301. } else {
  302. return 0;
  303. }
  304. },
  305. /**
  306. * H5复制
  307. */
  308. h5Copy(content) {
  309. let textarea = document.createElement('textarea');
  310. textarea.value = content;
  311. textarea.readOnly = 'readOnly';
  312. document.body.appendChild(textarea);
  313. textarea.select(); // 选择对象
  314. textarea.setSelectionRange(0, content.length); //核心
  315. let result = document.execCommand('Copy'); // 执行浏览器复制命令
  316. textarea.remove();
  317. const msg = result ? '复制成功' : '复制失败';
  318. this.toast(msg);
  319. },
  320. /**
  321. * app分享
  322. */
  323. handleAppShare(shareUrl, shareTitle, shareContent, shareImg) {
  324. let shareData = {
  325. shareUrl,
  326. shareTitle,
  327. shareContent,
  328. shareImg
  329. };
  330. appShare(shareData, res => {});
  331. },
  332. // 去掉字符串中的空格
  333. trim(str){
  334. if (!str) {
  335. return '';
  336. }
  337. return str.replace(/\s*/g,'');
  338. },
  339. // 判断两个对象是否相同
  340. isObjectValueEqual(x, y) {
  341. // 指向同一内存时
  342. if (x === y) {
  343. return true;
  344. } else if (
  345. typeof x == 'object' &&
  346. x != null &&
  347. typeof y == 'object' && y != null
  348. ) {
  349. if (Object.keys(x).length != Object.keys(y).length) return false;
  350. for (var prop in x) {
  351. if (y.hasOwnProperty(prop)) {
  352. if (!this.isObjectValueEqual(x[prop], y[prop])) return false;
  353. } else return false;
  354. }
  355. return true;
  356. } else return false;
  357. },
  358. platformGroupFilter () {
  359. let platformGroup = 'wechat';
  360. // #ifdef H5
  361. platformGroup = 'h5';
  362. // #endif
  363. // #ifdef MP-QQ
  364. platformGroup = 'qqMp';
  365. // #endif
  366. // #ifdef MP-WEIXIN
  367. platformGroup = 'wechatMp';
  368. // #endif
  369. // #ifdef MP-ALIPAY
  370. platformGroup = 'aliMp';
  371. // #endif
  372. // #ifdef MP-QQ
  373. platformGroup = 'qqMp';
  374. // #endif
  375. // #ifdef MP-BAIDU
  376. platformGroup = 'baiduMp';
  377. // #endif
  378. // #ifdef APP-PLUS
  379. switch(uni.getSystemInfoSync().platform){
  380. case 'android':
  381. platformGroup = 'android';
  382. break;
  383. case 'ios':
  384. platformGroup = 'ios';
  385. break;
  386. }
  387. // #endif
  388. return platformGroup;
  389. }
  390. };