人民医院前端

helper.js 12KB

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