人民医院前端

helper.js 14KB

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