人民医院前端

helper.js 14KB

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