AI销管
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

util.js 17 KiB

il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. let WebIM = uni.WebIM = require("./WebIM.js")["default"];
  2. import * as CryptoJS from '../components/crypto-js'
  3. function formatLongTime(mss) {
  4. var days = parseInt(mss / (1000 * 60 * 60 * 24));
  5. var hours = parseInt(mss % (1000 * 60 * 60 * 24) / (1000 * 60 * 60));
  6. var minutes = parseInt(mss % (1000 * 60 * 60) / (1000 * 60));
  7. var seconds = mss % (1000 * 60) / 1000;
  8. seconds = parseInt(seconds);
  9. if (minutes < 9) {
  10. minutes = "0" + minutes;
  11. }
  12. if (seconds < 9) {
  13. seconds = "0" + seconds;
  14. }
  15. return minutes + ":" + seconds + "";
  16. }
  17. /**
  18. * 格式化时间
  19. * @param {String} date 原始时间格式
  20. * 格式后的时间:yyyy/mm/dd hh:mm:ss
  21. **/
  22. /**
  23. * 格式化时间
  24. * @param {String} date 原始时间格式
  25. * 格式后的时间:yyyy/mm/dd hh:mm:ss
  26. **/
  27. const formatSecond = seconds=> {
  28. var h = Math.floor(seconds / 3600) < 10 ? '0'+Math.floor(seconds / 3600) : Math.floor(seconds / 3600);
  29. var m = Math.floor((seconds / 60 % 60)) < 10 ? '0' + Math.floor((seconds / 60 % 60)) : Math.floor((seconds / 60 % 60));
  30. var s = Math.floor((seconds % 60)) < 10 ? '0' + Math.floor((seconds % 60)) : Math.floor((seconds % 60));
  31. return h + ":" + m + ":" + s;
  32. }
  33. /**
  34. *加密处理
  35. */
  36. const encryption = (params) => {
  37. let {
  38. data,
  39. type,
  40. param,
  41. key
  42. } = params
  43. const result = JSON.parse(JSON.stringify(data))
  44. if (type === 'Base64') {
  45. param.forEach(ele => {
  46. result[ele] = btoa(result[ele])
  47. })
  48. } else {
  49. param.forEach(ele => {
  50. var data = result[ele]
  51. key = CryptoJS.enc.Latin1.parse(key)
  52. var iv = key
  53. // 加密
  54. var encrypted = CryptoJS.AES.encrypt(
  55. data,
  56. key, {
  57. iv: iv,
  58. mode: CryptoJS.mode.CBC,
  59. padding: CryptoJS.pad.ZeroPadding
  60. })
  61. result[ele] = encrypted.toString()
  62. })
  63. }
  64. return result
  65. }
  66. const formatTime = date => {
  67. const year = date.getFullYear();
  68. const month = date.getMonth() + 1;
  69. const day = date.getDate();
  70. const hour = date.getHours();
  71. const minute = date.getMinutes();
  72. const second = date.getSeconds();
  73. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
  74. };
  75. /**
  76. * 格式化时间
  77. * @param {String} date 原始时间格式
  78. * 格式后的时间:hh:mm
  79. **/
  80. const formatDateTime = date => {
  81. const hour = date.getHours();
  82. const minute = date.getMinutes();
  83. return [hour, minute, second].map(formatNumber).join(':');
  84. };
  85. /**
  86. * 格式化时间
  87. * @param {String} date 原始时间格式
  88. * 格式后的时间:yyyy-mm-dd
  89. **/
  90. const formatDate = date => {
  91. const year = date.getFullYear();
  92. const month = date.getMonth() + 1;
  93. const day = date.getDate();
  94. return [year, month, day].map(formatNumber).join('-');
  95. };
  96. const formatNumber = n => {
  97. n = n.toString();
  98. return n[1] ? n : '0' + n;
  99. };
  100. /**
  101. * 获取指定日期前后N天的日期、前N个月日期、后N个月日期
  102. * @param {String} sdate 当前日期
  103. * @param {Number} interval 间隔天数
  104. * @param {String} caret 间隔符号
  105. * @example 获取当天日期 getNowFormatDate("",0,"-"); 结果为"2018-09-18";
  106. 获取前一天日期 getNowFormatDate("2018-03-01",-1,"-"); 结果为"2018-02-18";
  107. 获取后一天日期 getNowFormatDate("2018-02-28",1,"-"); 结果为"2018-03-01";
  108. **/
  109. function getNowFormatDate(sdate, interval, caret) {
  110. var patt1 = /^\d{4}-([0-1]?[0-9])-([0-3]?[0-9])$/; //判断输入的日期是否符合格式正则表达式
  111. if (!(sdate && typeof sdate == "string" && patt1.test(sdate))) {
  112. sdate = new Date(); //不满足日期的则使用当前年月日
  113. }
  114. interval = isNaN(parseInt(interval)) ? 0 : parseInt(interval); //若没有输入间隔,则使用当前日
  115. caret = caret && typeof caret == "string" ? caret : "";
  116. var gdate = new Date(sdate).getTime(); //获取指定年月日
  117. gdate = gdate + 1000 * 60 * 60 * 24 * interval; //加减相差毫秒数
  118. var speDate = new Date(gdate); //获取指定好毫秒数时间
  119. var preYear = speDate.getFullYear();
  120. var preMonth = speDate.getMonth() + 1;
  121. var preDay = speDate.getDate();
  122. preMonth = preMonth < 10 ? "0" + preMonth : preMonth;
  123. preDay = preDay < 10 ? "0" + preDay : preDay;
  124. var preDate = preYear + caret + preMonth + caret + preDay;
  125. return preDate;
  126. } // 获取某年某月的有多少周
  127. String.prototype.weekInMonthCount = function () {
  128. var date = new Date(new Date(this.replace(/-/g, "/")) || new Date());
  129. var firstWeekDate = 1; // 默认第一周是本月1号 为了模拟本月1号是否为本月第1周的判断
  130. if (date.getDay() === 1) {
  131. // 判断1号是周一
  132. firstWeekDate = 1;
  133. } else if (date.getDay() === 0) {
  134. // 判断1号是周日
  135. firstWeekDate = 8 - 7 + 1;
  136. } else {
  137. // 判断1号是周二至周六之间
  138. firstWeekDate = 8 - date.getDay() + 1;
  139. }
  140. date.setMonth(date.getMonth() + 1);
  141. date.setDate(0);
  142. var monthHasDays = date.getDate(); // 本月天数
  143. monthHasDays = date.getDate() - firstWeekDate + 1;
  144. var hasWeek = Math.ceil(monthHasDays / 7); // 计算本月有几周
  145. return hasWeek;
  146. }; // 获取今天是第几周 注:ios不支持2018-01-14 需转化为2018/01/14
  147. String.prototype.weekIndexInMonth = function () {
  148. var date_trim = (this.trim() != "" ? this : new Date()).replace(/-/g, "/");
  149. var date = new Date(date_trim);
  150. var dateStart_trim = new Date((this.trim() != "" ? this : new Date()).replace(/-/g, "/")).setDate(1);
  151. var dateStart = new Date(dateStart_trim); // 本月初
  152. var firstWeek = 1;
  153. if (dateStart.getDay() === 1) {
  154. firstWeek = 1;
  155. } else if (dateStart.getDay() === 0) {
  156. firstWeek = 8 - 7 + 1;
  157. } else {
  158. firstWeek = 8 - dateStart.getDay() + 1;
  159. }
  160. var weekIndex = 1;
  161. var c = date.getDate();
  162. if (date.getDay() === 1 && date.getDate() < 7) {
  163. weekIndex = 1;
  164. } else if (c < firstWeek) {
  165. weekIndex = -1;
  166. } else {
  167. if (c < 7) {
  168. weekIndex = Math.ceil(c / 7);
  169. } else {
  170. c = c - firstWeek + 1;
  171. if (c % 7 === 0) {
  172. if (dateStart.getDay() !== 6) {
  173. weekIndex = c / 7;
  174. } else {
  175. weekIndex = c / 7 + 1;
  176. }
  177. } else {
  178. weekIndex = Math.ceil(c / 7);
  179. }
  180. }
  181. }
  182. return weekIndex;
  183. };
  184. /**
  185. * 验证车牌号是否正确
  186. * @param number carNumber
  187. **/
  188. function isLicensePlate(carNumber) {
  189. if (carNumber == '') {
  190. return false;
  191. }
  192. if (/^[A-Za-z]+$/.test(carNumber.slice(1))) {
  193. //全为字母
  194. return false;
  195. }
  196. return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(carNumber);
  197. }
  198. /**
  199. * 验证手机号
  200. */
  201. function checkPhone(phone) {
  202. let that = this;
  203. if (phone == '') {
  204. return false;
  205. }
  206. if (!/^1[3456789]\d{9}$/.test(phone)) {
  207. return false;
  208. }
  209. return true;
  210. }
  211. /**
  212. * 隐藏字符串
  213. * @param string str 字符串
  214. * @param int frontLen 前面需要保留几位
  215. * @param int endLen 后面需要保留几位
  216. */
  217. function hiddenString(str, frontLen, endLen) {
  218. var len = str.length - frontLen - endLen;
  219. var xing = '';
  220. for (var i = 0; i < len; i++) {
  221. xing += '*';
  222. }
  223. return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
  224. }
  225. /**
  226. * 从一个数组中随机取出若干个元素组成数组
  227. * @param {Array} arr 原数组
  228. * @param {Number} count 需要随机取得个数
  229. **/
  230. const getRandomArray = (arr, count) => {
  231. var shuffled = arr.slice(0),
  232. i = arr.length,
  233. min = i - count,
  234. temp,
  235. index;
  236. while (i-- > min) {
  237. index = Math.floor((i + 1) * Math.random());
  238. temp = shuffled[index];
  239. shuffled[index] = shuffled[i];
  240. shuffled[i] = temp;
  241. }
  242. return shuffled.slice(min);
  243. };
  244. /**
  245. * 从一个数组中随机取出一个元素
  246. * @param {Array} arr 原数组
  247. **/
  248. const getRandomArrayElement = arr => {
  249. return arr[Math.floor(Math.random() * arr.length)];
  250. };
  251. /**
  252. * 去除数组中重复的值
  253. * @param {Array} arr 原数组
  254. **/
  255. const getUnique = array => {
  256. var n = {},
  257. r = [],
  258. len = array.length,
  259. val,
  260. type;
  261. for (var i = 0; i < array.length; i++) {
  262. val = array[i];
  263. type = typeof val;
  264. if (!n[val]) {
  265. n[val] = [type];
  266. r.push(val);
  267. } else if (n[val].indexOf(type) < 0) {
  268. n[val].push(type);
  269. r.push(val);
  270. }
  271. }
  272. return r;
  273. };
  274. /**
  275. * 读取xml字符串
  276. * @param {String} xmlString 数据
  277. **/
  278. function loadXMLStr(xmlString) {
  279. var danmulist = [];
  280. if (xmlString.indexOf("<d p")) {
  281. var str = xmlString.substring(xmlString.indexOf("<d p"), xmlString.length);
  282. var reg = /<d p/g;
  283. var arr = str.match(reg);
  284. if (arr) {
  285. console.log(str);
  286. console.log(arr.length);
  287. for (var i = 0; i < arr.length; ++i) {
  288. var getstr = str.substring(0, str.indexOf("</d>") + "</d>".length);
  289. str = str.substring(str.indexOf("</d>") + "</d>".length, str.length);
  290. var danmu = {
  291. text: getstr.substring(getstr.indexOf(">") + ">".length, getstr.indexOf("</d>")),
  292. color: '#ff00ff',
  293. time: Math.floor(getstr.substring(getstr.indexOf("<d p=\"") + "<d p=\"".length, getstr.indexOf(",")))
  294. };
  295. danmulist.push(danmu);
  296. }
  297. }
  298. }
  299. return danmulist;
  300. } // 显示警告提示(7个汉字长度)
  301. var showWarn = text => uni.showToast({
  302. title: text,
  303. image: 'https://qufang.oss-cn-beijing.aliyuncs.com/upload/icon/xcx/jjycrm/warn.png'
  304. }); // 显示错误提示(7个汉字长度)
  305. var showError = text => uni.showToast({
  306. title: text,
  307. image: 'https://qufang.oss-cn-beijing.aliyuncs.com/upload/icon/xcx/jjycrm/error.png'
  308. }); // 显示繁忙提示(7个汉字长度)
  309. var showBusy = text => uni.showToast({
  310. title: text,
  311. icon: 'loading',
  312. duration: 1500
  313. }); // 显示成功提示(7个汉字长度)
  314. var showSuccess = text => uni.showToast({
  315. title: text,
  316. icon: 'success'
  317. }); // 显示无图标提示(两行)
  318. var showNone = text => uni.showToast({
  319. title: text,
  320. icon: 'none',
  321. duration: 1500
  322. }); // 显示失败提示框
  323. var showModel = (title, content) => {
  324. uni.hideToast();
  325. uni.showModal({
  326. title,
  327. content: JSON.stringify(content),
  328. showCancel: false
  329. });
  330. }; //获取列表
  331. function getList(url, userinfo, page, pageSize, callback) {
  332. this.showBusy('加载中...');
  333. var token = uni.getStorageSync('weapp_session_login_data');
  334. uni.request({
  335. url: url,
  336. data: {
  337. skey: userinfo.skey,
  338. tel: userinfo.userinfo.mobile_phone,
  339. page: page,
  340. pageSize: pageSize
  341. },
  342. method: 'POST',
  343. header: {
  344. 'content-type': 'application/x-www-form-urlencoded',
  345. 'Authorization': "Bearer "+token.token
  346. },
  347. success: function (result) {
  348. if (result.data.code == '200') {
  349. uni.hideToast();
  350. var result = result.data;
  351. callback(result);
  352. }
  353. },
  354. fail(error) {
  355. this.showModel('请求失败', error);
  356. return false;
  357. }
  358. });
  359. } //请求数据
  360. function getRequest(url, params, callback) {
  361. this.showBusy('加载中...');
  362. var token = uni.getStorageSync('weapp_session_login_data');
  363. uni.request({
  364. url: url,
  365. data: params,
  366. method: 'POST',
  367. header: {
  368. 'content-type': 'application/json',
  369. 'Authorization': "Bearer "+token.token
  370. },
  371. success: function (result) {
  372. if (result.data.code == '10003' || result.data.code == '20006') {
  373. //未登录
  374. uni.hideToast();
  375. uni.showModal({
  376. title: '提示',
  377. content: '您的登录已失效,请重新登录',
  378. showCancel: false,
  379. success(res) {
  380. if (res.confirm) {
  381. try {
  382. uni.clearStorageSync();
  383. uni.reLaunch({
  384. url: '/pages/main/login/index' //绝对路径
  385. });
  386. } catch (e) {
  387. return false;
  388. }
  389. }
  390. }
  391. }); // WebIM.conn.close();
  392. return false;
  393. }
  394. if (result.data.code == '10000') {
  395. uni.hideToast();
  396. var result = result.data;
  397. callback(result.data);
  398. } else {
  399. uni.hideToast();
  400. uni.showModal({
  401. title: '提示',
  402. content: '加载失败,请重新刷新',
  403. showCancel: false
  404. });
  405. return false;
  406. }
  407. },
  408. fail(error) {
  409. uni.hideToast();
  410. uni.showModal({
  411. title: '提示',
  412. content: '网络异常,请重新刷新',
  413. showCancel: false
  414. });
  415. return false;
  416. }
  417. });
  418. } //请求数据(异步)
  419. function getRequestPromise(url, params, isNone,option) {
  420. var isNone = isNone ? true : false;
  421. if (!isNone) {
  422. this.showBusy('加载中...');
  423. }
  424. var token = uni.getStorageSync('weapp_session_login_data');
  425. return new Promise(function (resolve, reject) {
  426. uni.request({
  427. url: url,
  428. data: params,
  429. method: option?option:'POST',
  430. header: {
  431. 'content-type': 'application/json',
  432. 'Authorization': 'Bearer '+token.token
  433. },
  434. success: function (result) {
  435. if (result.data.code != 1) {
  436. uni.hideToast();
  437. var results = result.data;
  438. resolve(results.data);
  439. } else if (result.data.code == 1 || result.data.code == '20006') {
  440. //未登录
  441. uni.hideToast();
  442. uni.showModal({
  443. title: '提示',
  444. content: '您的登录已失效,请重新登录',
  445. showCancel: false,
  446. success(res) {
  447. if (res.confirm) {
  448. try {
  449. uni.clearStorageSync();
  450. uni.reLaunch({
  451. url: '/pages/main/login/index' //绝对路径
  452. });
  453. } catch (e) {
  454. return false;
  455. }
  456. }
  457. }
  458. }); // WebIM.conn.close();
  459. return false;
  460. } else if (result.data.code == '60001') {
  461. //已录入客户
  462. uni.hideToast();
  463. uni.showToast({
  464. title: '您已经录入了该客户!',
  465. icon: 'none',
  466. duration: 1500
  467. });
  468. return false;
  469. } else if (result.data.code == '20005') {
  470. //已录入客户
  471. uni.hideToast();
  472. uni.showModal({
  473. title: '提示',
  474. content: '原始密码错误',
  475. showCancel: false
  476. });
  477. return false;
  478. } else {
  479. uni.hideToast();
  480. uni.showModal({
  481. title: '提示',
  482. content: result.data.message ? result.data.message : '请求数据失败,请重新尝试',
  483. showCancel: false
  484. });
  485. return false;
  486. }
  487. },
  488. fail(error) {
  489. console.log(error);
  490. uni.hideToast();
  491. uni.showModal({
  492. title: '提示',
  493. content: '网络异常,请重新尝试',
  494. showCancel: false
  495. });
  496. return false;
  497. }
  498. });
  499. });
  500. } //请求数据自定义
  501. function getRequestCustom(url, params, isShow) {
  502. if (isShow != 1) {
  503. this.showBusy('加载中...');
  504. }
  505. var token = uni.getStorageSync('weapp_session_login_data');
  506. return new Promise(function (resolve, reject) {
  507. uni.request({
  508. url: url,
  509. data: params,
  510. method: 'POST',
  511. header: {
  512. 'content-type': 'application/x-www-form-urlencoded',
  513. 'Authorization': "Bearer "+token.token
  514. },
  515. success: function (result) {
  516. if (result.data.data == undefined) {
  517. resolve(result.data);
  518. } else {
  519. if (result.data.success) {
  520. resolve(result.data.data);
  521. } else {
  522. uni.hideToast();
  523. uni.showModal({
  524. title: '提示',
  525. content: result.data.message ? result.data.message : '请求数据失败,请重新尝试',
  526. showCancel: false
  527. });
  528. return false;
  529. }
  530. }
  531. },
  532. fail(error) {
  533. uni.showModal({
  534. title: '提示',
  535. content: '网络异常,请重新尝试',
  536. showCancel: false
  537. });
  538. return false;
  539. }
  540. });
  541. });
  542. } //请求数据(异步)-自定义
  543. function getRequestPromiseCustom(url, params,option) {
  544. this.showBusy('请求中...');
  545. var token = uni.getStorageSync('weapp_session_login_data');
  546. return new Promise(function (resolve, reject) {
  547. uni.request({
  548. url: url,
  549. data: params,
  550. method: option?option:'POST',
  551. header: {
  552. 'content-type': 'application/json',
  553. 'Authorization': "Bearer "+token.token
  554. },
  555. success: function (result) {
  556. uni.hideToast();
  557. var results = result.data;
  558. resolve(results);
  559. },
  560. fail(error) {
  561. uni.hideToast();
  562. uni.showModal({
  563. title: '提示',
  564. content: '网络异常,请重新尝试',
  565. showCancel: false
  566. });
  567. return false;
  568. }
  569. });
  570. });
  571. }
  572. module.exports = {
  573. formatSecond,
  574. formatTime,
  575. formatDate,
  576. encryption,
  577. formatDateTime,
  578. getRandomArray: getRandomArray,
  579. getRandomArrayElement: getRandomArrayElement,
  580. showWarn,
  581. showError,
  582. showBusy,
  583. showSuccess,
  584. showNone,
  585. showModel,
  586. getList: getList,
  587. getRequest: getRequest,
  588. getRequestPromise: getRequestPromise,
  589. getRequestCustom: getRequestCustom,
  590. getRequestPromiseCustom: getRequestPromiseCustom,
  591. getNowFormatDate: getNowFormatDate,
  592. isLicensePlate: isLicensePlate,
  593. checkPhone: checkPhone,
  594. hiddenString: hiddenString,
  595. getUnique: getUnique,
  596. loadXMLStr: loadXMLStr,
  597. formatLongTime: formatLongTime
  598. };