人民医院前端

helper.js 11KB

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