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.
 
 
 

670 lignes
17 KiB

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