人民医院前端

helper.js 11KB

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