人民医院前端

helper.js 11KB

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