utils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. 'use strict';
  2. const areas = require('./province2');
  3. /**
  4. 常用的工具类
  5. */
  6. module.exports = {
  7. getAreas() {
  8. return areas.all;
  9. },
  10. getPC() {
  11. const province = [];
  12. const city = [];
  13. areas.all.forEach(item => {
  14. item.provinces.forEach(p => {
  15. province.push({ name: p.provice_name });
  16. p.cities.forEach(c => {
  17. city.push({ name: c.city_name });
  18. });
  19. });
  20. });
  21. return { province, city };
  22. },
  23. getC() {
  24. return areas.all;
  25. },
  26. // 生成随机数
  27. randomNumC() {
  28. // return 0
  29. return this.randomNum(0, 10000);
  30. },
  31. randomNumT(num = 1) {
  32. return this.randomNum(0, 24 * 60 * 60 * 1000 / num);
  33. },
  34. randomNum(minNum, maxNum) {
  35. switch (arguments.length) {
  36. case 1:
  37. return parseInt(Math.random() * minNum + 1, 10);
  38. case 2:
  39. return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
  40. default:
  41. return 0;
  42. }
  43. },
  44. // 判断是否是个方法
  45. isFunction(x) {
  46. return Object.prototype.toString.call(x) === '[object Function]';
  47. },
  48. // 数字三位加逗号方法
  49. toThousands(num) {
  50. const result = [];
  51. let counter = 0;
  52. num = (num || 0).toString().split('');
  53. for (let i = num.length - 1; i >= 0; i--) {
  54. counter++;
  55. result.unshift(num[i]);
  56. if (!(counter % 3) && i != 0) {
  57. result.unshift(',');
  58. }
  59. }
  60. return result.join('');
  61. },
  62. // 时间选择器
  63. isSame(state, queryData, type) {
  64. let flag = false;
  65. switch (type) {
  66. case 0:
  67. if (state.dayStart == queryData.startTime && state.dayEnd == queryData.endTime) {
  68. flag = true;
  69. }
  70. break;
  71. case 1:
  72. if (state.monthStart == queryData.startTime && state.monthEnd == queryData.endTime) {
  73. flag = true;
  74. }
  75. break;
  76. case 2:
  77. if (state.yearStart == queryData.startTime && state.yearEnd == queryData.endTime) {
  78. flag = true;
  79. }
  80. break;
  81. case 'province':
  82. if (state.provinceStart == queryData.startTime && state.provinceEnd == queryData.endTime) {
  83. flag = true;
  84. }
  85. break;
  86. case 'city':
  87. if (state.cityStart == queryData.startTime && state.cityEnd == queryData.endTime) {
  88. flag = true;
  89. }
  90. break;
  91. default:
  92. if (state.start == queryData.startTime && state.end == queryData.endTime) {
  93. flag = true;
  94. }
  95. break;
  96. }
  97. console.log(flag, '2');
  98. return flag;
  99. },
  100. // 车型车系选择器
  101. isCarSame(state, queryData, type) {
  102. let flag = false;
  103. // eslint-disable-next-line no-cond-assign
  104. if (queryData.modelCode == undefined) {
  105. queryData.modelCode = '';
  106. }
  107. // eslint-disable-next-line no-cond-assign
  108. if (queryData.seriesCode == undefined) {
  109. queryData.seriesCode = '';
  110. }
  111. switch (type) {
  112. case 0:
  113. if (state.dayModelCode == queryData.modelCode && state.daySeriesCode == queryData.seriesCode) {
  114. flag = true;
  115. }
  116. break;
  117. case 1:
  118. if (state.monthModelCode == queryData.modelCode && state.monthSeriesCode == queryData.seriesCode) {
  119. flag = true;
  120. }
  121. break;
  122. case 2:
  123. if (state.yearModelCode == queryData.modelCode && state.yearSeriesCode == queryData.seriesCode) {
  124. flag = true;
  125. }
  126. break;
  127. default:
  128. if (state.modelCode == queryData.modelCode && state.seriesCode == queryData.seriesCode) {
  129. if (state.modelCode == '' && state.seriesCode == '') {
  130. flag = false;
  131. } else {
  132. flag = true;
  133. }
  134. }
  135. break;
  136. }
  137. console.log(flag, '1');
  138. return flag;
  139. },
  140. // 单个
  141. isMode(state, queryData, type) {
  142. let flag = false;
  143. // eslint-disable-next-line no-cond-assign
  144. if (queryData.mode == undefined) {
  145. queryData.mode = '';
  146. }
  147. switch (type) {
  148. case 0:
  149. if (state.dayMode == queryData.mode) {
  150. flag = true;
  151. }
  152. break;
  153. case 1:
  154. if (state.monthMode == queryData.mode) {
  155. flag = true;
  156. }
  157. break;
  158. case 2:
  159. if (state.yearMode == queryData.mode) {
  160. flag = true;
  161. }
  162. break;
  163. default:
  164. if (state.mode == queryData.mode) {
  165. if (state.mode == '') {
  166. flag = false;
  167. } else {
  168. flag = true;
  169. }
  170. }
  171. break;
  172. }
  173. console.log(flag, '3');
  174. return flag;
  175. },
  176. isRcType(state, queryData, type) {
  177. let flag = false;
  178. // eslint-disable-next-line no-cond-assign
  179. if (queryData.rcType == undefined) {
  180. queryData.rcType = '';
  181. }
  182. switch (type) {
  183. case 0:
  184. if (state.dayRcType == queryData.rcType) {
  185. flag = true;
  186. }
  187. break;
  188. case 1:
  189. if (state.monthRcType == queryData.rcType) {
  190. flag = true;
  191. }
  192. break;
  193. case 2:
  194. if (state.yearRcType == queryData.rcType) {
  195. flag = true;
  196. }
  197. break;
  198. default:
  199. if (state.rcType == queryData.rcType) {
  200. if (state.rcType == '') {
  201. flag = false;
  202. } else {
  203. flag = true;
  204. }
  205. }
  206. break;
  207. }
  208. console.log(flag, '4');
  209. return flag;
  210. },
  211. isRcResult(state, queryData, type) {
  212. let flag = false;
  213. // eslint-disable-next-line no-cond-assign
  214. if (queryData.rcResult == undefined) {
  215. queryData.rcResult = '';
  216. }
  217. switch (type) {
  218. case 0:
  219. if (state.dayRcResult == queryData.rcResult) {
  220. flag = true;
  221. }
  222. break;
  223. case 1:
  224. if (state.monthRcResult == queryData.rcResult) {
  225. flag = true;
  226. }
  227. break;
  228. case 2:
  229. if (state.yearRcResult == queryData.rcResult) {
  230. flag = true;
  231. }
  232. break;
  233. default:
  234. if (state.rcResult == queryData.rcResult) {
  235. if (state.rcResult == '') {
  236. flag = false;
  237. } else {
  238. flag = true;
  239. }
  240. }
  241. break;
  242. }
  243. console.log(flag, '5');
  244. return flag;
  245. },
  246. // 二个title
  247. isState(state, queryData) {
  248. let flag = false;
  249. if (state.state == queryData.state) {
  250. flag = true;
  251. }
  252. return flag;
  253. },
  254. /**
  255. * 分割数组创建二维数组封装
  256. * @param data 数组
  257. * @param senArrLen 需要分割成子数组的长度
  258. */
  259. splitArr(data, senArrLen) {
  260. // 处理成len个一组的数据
  261. const data_len = data.length;
  262. const arrOuter_len = data_len % senArrLen === 0 ? data_len / senArrLen : parseInt((data_len / senArrLen) + '') + 1;
  263. const arrSec_len = data_len > senArrLen ? senArrLen : data_len;// 内层数组的长度
  264. const arrOuter = new Array(arrOuter_len);// 最外层数组
  265. let arrOuter_index = 0;// 外层数组的子元素下标
  266. // console.log(data_len % len);
  267. for (let i = 0; i < data_len; i++) {
  268. if (i % senArrLen === 0) {
  269. arrOuter_index++;
  270. let len = arrSec_len * arrOuter_index;
  271. // 将内层数组的长度最小取决于数据长度对len取余,平时最内层由下面赋值决定
  272. arrOuter[arrOuter_index - 1] = new Array(data_len % senArrLen);
  273. if (arrOuter_index === arrOuter_len)// 最后一组
  274. {
  275. data_len % senArrLen === 0 ?
  276. len = data_len % senArrLen + senArrLen * arrOuter_index :
  277. len = data_len % senArrLen + senArrLen * (arrOuter_index - 1);
  278. }
  279. let arrSec_index = 0;// 第二层数组的索引
  280. for (let k = i; k < len; k++) { // 第一层数组的开始取决于第二层数组长度*当前第一层的索引
  281. arrOuter[arrOuter_index - 1][arrSec_index] = data[k];
  282. arrSec_index++;
  283. }
  284. }
  285. }
  286. // console.log(arrOuter,"我是总的素组");
  287. return arrOuter;
  288. },
  289. // 防止饼 相加超过或者少于100%的最好方法
  290. precentV(arr, sum, filed = 'value', oldFiled = 'count') {
  291. if (!sum) {
  292. sum = arr.reduce((pre, item) => {
  293. if (item._id != 'Other') {
  294. return pre + item[oldFiled];
  295. }
  296. return pre;
  297. }, 0);
  298. }
  299. if (sum == 0) {
  300. return arr;
  301. }
  302. const tempArr = arr.map(item => {
  303. if (item._id != 'Other') {
  304. item[filed] = parseInt(item[oldFiled] * 100 / sum);
  305. item.temp = item[oldFiled] * 100 % sum;
  306. }
  307. return item;
  308. });
  309. const p = 100 - tempArr.reduce((pre, item) => {
  310. if (item._id != 'Other') {
  311. return pre + item[filed];
  312. }
  313. return pre;
  314. }, 0);
  315. const sortArr = tempArr.sort((a, b) => parseInt(b.temp) - parseInt(a.temp));
  316. const dataArr = sortArr.map((item, index) => {
  317. if (index < p) {
  318. item[filed] = item[filed] + 1;
  319. }
  320. return item;
  321. });
  322. return dataArr;
  323. },
  324. // 判断性别
  325. isGender(der) {
  326. if (der == 'M') {
  327. return '男';
  328. }
  329. return '女';
  330. },
  331. // 身份证号带*****
  332. hideIdCard(value) {
  333. const len = (value + '').length;
  334. if (len === 0) {
  335. return '';
  336. }
  337. if ((len !== 15) && (len !== 18))// 身份证号码只能为15位或18位其它不合法
  338. {
  339. return '';
  340. }
  341. const str1 = value.slice(0, 6);
  342. let str2 = '';
  343. const str3 = value.slice(14);
  344. if (value.length === 15) {
  345. str2 = '******';
  346. } else {
  347. str2 = '********';
  348. }
  349. return str1 + str2 + str3;
  350. },
  351. // 通过身份证号获取生日
  352. getBirthday(identityCard) {
  353. const len = (identityCard + '').length;
  354. if (len === 0) {
  355. return '';
  356. }
  357. if ((len !== 15) && (len !== 18))// 身份证号码只能为15位或18位其它不合法
  358. {
  359. return '';
  360. }
  361. let strBirthday = '';
  362. if (len === 18)// 处理18位的身份证号码从号码中得到生日和性别代码
  363. {
  364. strBirthday = identityCard.substr(6, 4) + '-' + identityCard.substr(10, 2) + '-' + identityCard.substr(12, 2);
  365. }
  366. if (len === 15) {
  367. strBirthday = '19' + identityCard.substr(6, 2) + '-' + identityCard.substr(8, 2) + '-' + identityCard.substr(10, 2);
  368. }
  369. return strBirthday;
  370. },
  371. // 输入三个数求各自占比
  372. getpie(value1, value2, value3) {
  373. let sum = 100;
  374. if ((value1 + value2 + value3) !== 0) {
  375. sum = value1 + value2 + value3;
  376. }
  377. return [ Math.round(value1 / sum), Math.round(value2 / sum), Math.round(value3 / sum) ];
  378. },
  379. // 判断分值区间
  380. judgmentScore(number) {
  381. let value = '';
  382. if (number >= 90) {
  383. value = '90-100';
  384. } else if (number >= 75) {
  385. value = '75-90';
  386. } else if (number >= 60) {
  387. value = '60-75';
  388. } else {
  389. value = '0-60';
  390. }
  391. return value;
  392. },
  393. };