123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- 'use strict';
- const areas = require('./province2');
- /**
- 常用的工具类
- */
- module.exports = {
- getAreas() {
- return areas.all;
- },
- getPC() {
- const province = [];
- const city = [];
- areas.all.forEach(item => {
- item.provinces.forEach(p => {
- province.push({ name: p.provice_name });
- p.cities.forEach(c => {
- city.push({ name: c.city_name });
- });
- });
- });
- return { province, city };
- },
- getC() {
- return areas.all;
- },
- // 生成随机数
- randomNumC() {
- // return 0
- return this.randomNum(0, 10000);
- },
- randomNumT(num = 1) {
- return this.randomNum(0, 24 * 60 * 60 * 1000 / num);
- },
- randomNum(minNum, maxNum) {
- switch (arguments.length) {
- case 1:
- return parseInt(Math.random() * minNum + 1, 10);
- case 2:
- return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
- default:
- return 0;
- }
- },
- // 判断是否是个方法
- isFunction(x) {
- return Object.prototype.toString.call(x) === '[object Function]';
- },
- // 数字三位加逗号方法
- toThousands(num) {
- const result = [];
- let counter = 0;
- num = (num || 0).toString().split('');
- for (let i = num.length - 1; i >= 0; i--) {
- counter++;
- result.unshift(num[i]);
- if (!(counter % 3) && i != 0) {
- result.unshift(',');
- }
- }
- return result.join('');
- },
- // 时间选择器
- isSame(state, queryData, type) {
- let flag = false;
- switch (type) {
- case 0:
- if (state.dayStart == queryData.startTime && state.dayEnd == queryData.endTime) {
- flag = true;
- }
- break;
- case 1:
- if (state.monthStart == queryData.startTime && state.monthEnd == queryData.endTime) {
- flag = true;
- }
- break;
- case 2:
- if (state.yearStart == queryData.startTime && state.yearEnd == queryData.endTime) {
- flag = true;
- }
- break;
- case 'province':
- if (state.provinceStart == queryData.startTime && state.provinceEnd == queryData.endTime) {
- flag = true;
- }
- break;
- case 'city':
- if (state.cityStart == queryData.startTime && state.cityEnd == queryData.endTime) {
- flag = true;
- }
- break;
- default:
- if (state.start == queryData.startTime && state.end == queryData.endTime) {
- flag = true;
- }
- break;
- }
- console.log(flag, '2');
- return flag;
- },
- // 车型车系选择器
- isCarSame(state, queryData, type) {
- let flag = false;
- // eslint-disable-next-line no-cond-assign
- if (queryData.modelCode == undefined) {
- queryData.modelCode = '';
- }
- // eslint-disable-next-line no-cond-assign
- if (queryData.seriesCode == undefined) {
- queryData.seriesCode = '';
- }
- switch (type) {
- case 0:
- if (state.dayModelCode == queryData.modelCode && state.daySeriesCode == queryData.seriesCode) {
- flag = true;
- }
- break;
- case 1:
- if (state.monthModelCode == queryData.modelCode && state.monthSeriesCode == queryData.seriesCode) {
- flag = true;
- }
- break;
- case 2:
- if (state.yearModelCode == queryData.modelCode && state.yearSeriesCode == queryData.seriesCode) {
- flag = true;
- }
- break;
- default:
- if (state.modelCode == queryData.modelCode && state.seriesCode == queryData.seriesCode) {
- if (state.modelCode == '' && state.seriesCode == '') {
- flag = false;
- } else {
- flag = true;
- }
- }
- break;
- }
- console.log(flag, '1');
- return flag;
- },
- // 单个
- isMode(state, queryData, type) {
- let flag = false;
- // eslint-disable-next-line no-cond-assign
- if (queryData.mode == undefined) {
- queryData.mode = '';
- }
- switch (type) {
- case 0:
- if (state.dayMode == queryData.mode) {
- flag = true;
- }
- break;
- case 1:
- if (state.monthMode == queryData.mode) {
- flag = true;
- }
- break;
- case 2:
- if (state.yearMode == queryData.mode) {
- flag = true;
- }
- break;
- default:
- if (state.mode == queryData.mode) {
- if (state.mode == '') {
- flag = false;
- } else {
- flag = true;
- }
- }
- break;
- }
- console.log(flag, '3');
- return flag;
- },
- isRcType(state, queryData, type) {
- let flag = false;
- // eslint-disable-next-line no-cond-assign
- if (queryData.rcType == undefined) {
- queryData.rcType = '';
- }
- switch (type) {
- case 0:
- if (state.dayRcType == queryData.rcType) {
- flag = true;
- }
- break;
- case 1:
- if (state.monthRcType == queryData.rcType) {
- flag = true;
- }
- break;
- case 2:
- if (state.yearRcType == queryData.rcType) {
- flag = true;
- }
- break;
- default:
- if (state.rcType == queryData.rcType) {
- if (state.rcType == '') {
- flag = false;
- } else {
- flag = true;
- }
- }
- break;
- }
- console.log(flag, '4');
- return flag;
- },
- isRcResult(state, queryData, type) {
- let flag = false;
- // eslint-disable-next-line no-cond-assign
- if (queryData.rcResult == undefined) {
- queryData.rcResult = '';
- }
- switch (type) {
- case 0:
- if (state.dayRcResult == queryData.rcResult) {
- flag = true;
- }
- break;
- case 1:
- if (state.monthRcResult == queryData.rcResult) {
- flag = true;
- }
- break;
- case 2:
- if (state.yearRcResult == queryData.rcResult) {
- flag = true;
- }
- break;
- default:
- if (state.rcResult == queryData.rcResult) {
- if (state.rcResult == '') {
- flag = false;
- } else {
- flag = true;
- }
- }
- break;
- }
- console.log(flag, '5');
- return flag;
- },
- // 二个title
- isState(state, queryData) {
- let flag = false;
- if (state.state == queryData.state) {
- flag = true;
- }
- return flag;
- },
- /**
- * 分割数组创建二维数组封装
- * @param data 数组
- * @param senArrLen 需要分割成子数组的长度
- */
- splitArr(data, senArrLen) {
- // 处理成len个一组的数据
- const data_len = data.length;
- const arrOuter_len = data_len % senArrLen === 0 ? data_len / senArrLen : parseInt((data_len / senArrLen) + '') + 1;
- const arrSec_len = data_len > senArrLen ? senArrLen : data_len;// 内层数组的长度
- const arrOuter = new Array(arrOuter_len);// 最外层数组
- let arrOuter_index = 0;// 外层数组的子元素下标
- // console.log(data_len % len);
- for (let i = 0; i < data_len; i++) {
- if (i % senArrLen === 0) {
- arrOuter_index++;
- let len = arrSec_len * arrOuter_index;
- // 将内层数组的长度最小取决于数据长度对len取余,平时最内层由下面赋值决定
- arrOuter[arrOuter_index - 1] = new Array(data_len % senArrLen);
- if (arrOuter_index === arrOuter_len)// 最后一组
- {
- data_len % senArrLen === 0 ?
- len = data_len % senArrLen + senArrLen * arrOuter_index :
- len = data_len % senArrLen + senArrLen * (arrOuter_index - 1);
- }
- let arrSec_index = 0;// 第二层数组的索引
- for (let k = i; k < len; k++) { // 第一层数组的开始取决于第二层数组长度*当前第一层的索引
- arrOuter[arrOuter_index - 1][arrSec_index] = data[k];
- arrSec_index++;
- }
- }
- }
- // console.log(arrOuter,"我是总的素组");
- return arrOuter;
- },
- // 防止饼 相加超过或者少于100%的最好方法
- precentV(arr, sum, filed = 'value', oldFiled = 'count') {
- if (!sum) {
- sum = arr.reduce((pre, item) => {
- if (item._id != 'Other') {
- return pre + item[oldFiled];
- }
- return pre;
- }, 0);
- }
- if (sum == 0) {
- return arr;
- }
- const tempArr = arr.map(item => {
- if (item._id != 'Other') {
- item[filed] = parseInt(item[oldFiled] * 100 / sum);
- item.temp = item[oldFiled] * 100 % sum;
- }
- return item;
- });
- const p = 100 - tempArr.reduce((pre, item) => {
- if (item._id != 'Other') {
- return pre + item[filed];
- }
- return pre;
- }, 0);
- const sortArr = tempArr.sort((a, b) => parseInt(b.temp) - parseInt(a.temp));
- const dataArr = sortArr.map((item, index) => {
- if (index < p) {
- item[filed] = item[filed] + 1;
- }
- return item;
- });
- return dataArr;
- },
- // 判断性别
- isGender(der) {
- if (der == 'M') {
- return '男';
- }
- return '女';
- },
- // 身份证号带*****
- hideIdCard(value) {
- const len = (value + '').length;
- if (len === 0) {
- return '';
- }
- if ((len !== 15) && (len !== 18))// 身份证号码只能为15位或18位其它不合法
- {
- return '';
- }
- const str1 = value.slice(0, 6);
- let str2 = '';
- const str3 = value.slice(14);
- if (value.length === 15) {
- str2 = '******';
- } else {
- str2 = '********';
- }
- return str1 + str2 + str3;
- },
- // 通过身份证号获取生日
- getBirthday(identityCard) {
- const len = (identityCard + '').length;
- if (len === 0) {
- return '';
- }
- if ((len !== 15) && (len !== 18))// 身份证号码只能为15位或18位其它不合法
- {
- return '';
- }
- let strBirthday = '';
- if (len === 18)// 处理18位的身份证号码从号码中得到生日和性别代码
- {
- strBirthday = identityCard.substr(6, 4) + '-' + identityCard.substr(10, 2) + '-' + identityCard.substr(12, 2);
- }
- if (len === 15) {
- strBirthday = '19' + identityCard.substr(6, 2) + '-' + identityCard.substr(8, 2) + '-' + identityCard.substr(10, 2);
- }
- return strBirthday;
- },
- // 输入三个数求各自占比
- getpie(value1, value2, value3) {
- let sum = 100;
- if ((value1 + value2 + value3) !== 0) {
- sum = value1 + value2 + value3;
- }
- return [ Math.round(value1 / sum), Math.round(value2 / sum), Math.round(value3 / sum) ];
- },
- // 判断分值区间
- judgmentScore(number) {
- let value = '';
- if (number >= 90) {
- value = '90-100';
- } else if (number >= 75) {
- value = '75-90';
- } else if (number >= 60) {
- value = '60-75';
- } else {
- value = '0-60';
- }
- return value;
- },
- };
|