import moment from 'moment'; // import Schema from 'async-validator'; const BaseTool = {}; BaseTool.UserUtil = { transImg(imgArr = []) { const temp = [] if (BaseTool.Object.isBlank(imgArr)) { return temp } imgArr.forEach(item => { if (item.url.substr(0, 4) !== 'http') { item.url = BaseTool.UserUtil.getFileServerUrl() + item.url } temp.push(item) }) return temp }, getUploadUrl: () => { return BaseTool.UserUtil.getBaseUrl() + '/upms/files/upload'; }, getUploadUrlIgnores: () => { return BaseTool.UserUtil.getBaseUrl() + '/ignores/files/upload'; }, getFileUrl: url => { // #ifdef H5 if (process.env.NODE_ENV === 'development') { return 'http://localhost:5000'+ url; } else if (process.env.NODE_ENV === 'production') { return 'https://xiamen.m.litian2017.com/api'+ url; } // #endif // #ifndef H5 // eslint-disable-next-line no-unreachable return BaseTool.UserUtil.getBaseUrl() + url; // #endif }, getImageCode: () => { return BaseTool.UserUtil.getBaseUrl() + '/verify/captcha/'; }, NOT_LOGIN: ['/verify/sms/code/send', '/upms/users/register', '/upms/users/check/', '/ignores/'], getUserInfo: () => { return uni.getStorageSync('userInfo').userInfo; }, isRepairRole: () => { if (uni.getStorageSync('userInfo').userInfo == null) { return false; } else if (uni.getStorageSync('userInfo').userInfo.roleName === '维修人员') { return true; } else { return false; } }, isManagerRole: () => { if (uni.getStorageSync('userInfo').userInfo == null) { return false; } else if (uni.getStorageSync('userInfo').userInfo.roleName === '超级管理员') { return true; } else { return false; } }, isUseRole: () => { if (uni.getStorageSync('userInfo').userInfo == null) { return false; } else if (uni.getStorageSync('userInfo').userInfo.roleName === '使用人员') { return true; } else { return false; } }, getStateUserInfo: (that) => { return that.$store.state.userInfo.userInfo; }, getToken: url => { for (const notUrl of BaseTool.UserUtil.NOT_LOGIN) { if (url.indexOf(notUrl) !== -1) { return ''; } } let token = ''; const storeToken = uni.getStorageSync('accessToken'); if (url.indexOf('/oauth/token') !== -1) { token = 'Basic ZnJvbnQ6ZnJvbnQ='; } else if (url.indexOf('/alipay') !== -1) { return false; } else if (storeToken) { token = 'Bearer ' + storeToken; } console.log(11221) console.log(token) return token; }, getBaseUrl: () => { // #ifdef H5 return '/api'; }, getFileServerUrl: () => { // #ifdef H5 return '/api'; } }; // BaseTool.Validator = { // getParams: (dataMap = {}) => { // const params = {}; // Object.keys(dataMap).forEach(key => { // params[key] = dataMap[key].value; // }); // return params; // }, // getListParams: (dataList = []) => { // const params = {}; // dataList.forEach(item => { // params[item.name] = item.value; // }); // return params; // }, // getSchemaParams: (dataMap = {}) => { // return dataMap; // }, // getListSchemaParams: (dataList = []) => { // const schemaParams = {}; // dataList.forEach(item => { // schemaParams[item.name] = item; // }); // return schemaParams; // }, // validateRules: (dataMap = {}) => { // return new Promise((resolve, reject) => { // const schemaData = {}; // const params = {}; // Object.keys(dataMap).forEach(key => { // schemaData[key] = dataMap[key].rules; // params[key] = dataMap[key].value; // }); // const validator = new Schema(schemaData); // validator // .validate(params, (errors, fields) => { // if (errors) { // uni.showToast({ icon: 'none', title: errors[0].message }); // reject(errors); // } else { // resolve(params); // } // }) // .then( // r => {}, // ({ errors, fields }) => {} // ); // }); // }, // validateParamsRules: (params = {}, rules = {}) => { // return new Promise((resolve, reject) => { // const validator = new Schema(rules); // validator // .validate(params, (errors, fields) => { // if (errors) { // uni.showToast({ icon: 'none', title: errors[0].message }); // reject(errors); // } else { // resolve(params); // } // }) // .then( // r => {}, // ({ errors, fields }) => {} // ); // }); // }, // validate: (dataMap = {}) => { // return new Promise((resolve, reject) => { // const validator = new Schema(dataMap); // const params = BaseTool.Validator.getParams(dataMap); // validator // .validate(params, (errors, fields) => { // if (errors) { // console.log(111111, errors); // uni.showToast({ icon: 'none', title: errors[0].message }); // reject(errors); // } else { // resolve(params); // } // }) // .then( // r => {}, // ({ errors, fields }) => {} // ); // }); // }, // validateListRules: (dataList = []) => { // return new Promise((resolve, reject) => { // const schemaData = {}; // const params = {}; // dataList.forEach(item => { // params[item.name] = item.value; // schemaData[item.name] = item.rules; // }); // const validator = new Schema(schemaData); // validator // .validate(params, (errors, fields) => { // if (errors) { // uni.showToast({ icon: 'none', title: errors[0].message }); // reject(errors); // } else { // resolve(params); // } // }) // .then( // r => {}, // ({ errors, fields }) => {} // ); // }); // }, // validateList: (dataList = []) => { // return new Promise((resolve, reject) => { // const validator = new Schema(BaseTool.Validator.getListSchemaParams(dataList)); // const params = BaseTool.Validator.getListParams(dataList); // validator // .validate(params, (errors, fields) => { // if (errors) { // uni.showToast({ icon: 'none', title: errors[0].message }); // reject(errors); // } else { // resolve(params); // } // }) // .then( // r => {}, // err => {} // ); // }); // } // }; /** * 时间对象 * 转换时使用这个方法,因为moment暂用的空间还是有点大,小程序从936k->1.22M * h5:也多了100-200k */ BaseTool.Date = { PICKER_NORM_YEAR: 'YYYY', PICKER_NORM_MONTH: 'MM', PICKER_NORM_YEAR_MONTH: 'YYYY-MM', PICKER_NORM_DATE_PATTERN: 'YYYY-MM-DD', PICKER_NORM_TIME_PATTERN: 'HH:mm:ss', PICKER_NORM_DATETIME_PATTERN: 'YYYY-MM-DD HH:mm:ss', /** * 将字符串日期格式化 * @param date * @param pattern * @returns {string} */ formatter(date, pattern) { if (BaseTool.Object.isBlank(date)) { return ''; } else { return moment(date, BaseTool.Date.PICKER_NORM_DATETIME_PATTERN).format(pattern); } }, getCountBetween(date1, date2, type = 1) { const startTime = new Date(date1); // 开始时间 const endTime = new Date(date2); // 结束时间 const usedTime = endTime - startTime; // 相差的毫秒数 const days = Math.floor(usedTime / (24 * 3600 * 1000)); // 计算出天数 const leavel = usedTime % (24 * 3600 * 1000); // 计算天数后剩余的时间 const hours = Math.floor(leavel / (3600 * 1000)); // 计算剩余的小时数 const leavel2 = leavel % (3600 * 1000); // 计算剩余小时后剩余的毫秒数 const minutes = Math.floor(leavel2 / (60 * 1000)); // 计算剩余的分钟数 if (type === 1) { return days; } if (type === 2) { return hours; } if (type === 2) { return minutes; } } }; BaseTool.String = { isBlank(text) { return text === null || text === undefined || text === 'undefined' || text === ''; }, isNotBlank(text) { return !this.isBlank(text); } }; BaseTool.Object = { isBlank(text) { if (typeof text === 'string') { return BaseTool.String.isBlank(text); } return text === null || text === undefined; }, isNotBlank(text) { return !this.isBlank(text); }, /** * 获取对象属性 * @param obj * @param field * @returns {*} */ getField(obj, field) { if (BaseTool.String.isBlank(obj) || BaseTool.String.isBlank(field)) { return ''; } return obj[field]; }, /** * 深克隆 * @param data * @returns {{}|any} */ copy: function (data) { if (BaseTool.Object.isBlank(data)) { return {}; } return JSON.parse(JSON.stringify(data)); } }; BaseTool.Amount = { /** * 元换算为万元 * @param num : * @returns {number} : */ divTenThousand(num) { if (num == null) { return 0; } return BaseTool.Number.div(num, 10000, 3); // return num }, /** * 元换算为万元,并格式化 * @param num : * @returns {string} : */ divTenThousandFormatter(num) { if (BaseTool.String.isBlank(num)) { return num; } return this.formatter(this.divTenThousand(num)); }, /** * 格式化金额 * @param value * @returns {string} */ formatter: value => { if (BaseTool.String.isBlank(value)) { return ''; } return `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ','); }, /** * 格式化利率 * @param value * @returns {string|*} */ formatterRate: value => { if (BaseTool.String.isBlank(value)) { return value; } return value + '%'; }, /** * 去掉格式化金额 * @param value * @returns {*} */ parser: value => value.replace(/\$\s?|(,*)/g, ''), /** * 万元换算为元 * @param num * @returns {*} */ mulTenThousand(num) { if (BaseTool.String.isBlank(num)) { return 0; } return BaseTool.Number.mul(num, 10000, 6); // return num }, /** * 元换算为万元,并格式化 * @param num : * @returns {string} : */ default(value) { if (BaseTool.String.isBlank(value) || value === 0) { return '0.00'; } return value; }, /** * 元换算为万元,并格式化 * @param num : * @returns {string} : */ defaultDivTenThousandFormatter(num) { if (BaseTool.String.isBlank(num) || num === 0) { return '0.00'; } return this.formatter(this.divTenThousand(num)); } }; BaseTool.Number = { add(arg1, arg2) { if (BaseTool.String.isBlank(arg1)) { arg1 = 0; } if (BaseTool.String.isBlank(arg2)) { arg2 = 0; } let r1, r2; try { r1 = arg1.toString().split('.')[1].length; } catch (e) { r1 = 0; } try { r2 = arg2.toString().split('.')[1].length; } catch (e) { r2 = 0; } const m = Math.pow(10, Math.max(r1, r2)); const n = r1 >= r2 ? r1 : r2; return ((arg1 * m + arg2 * m) / m).toFixed(n); }, sub(arg1, arg2) { if (BaseTool.String.isBlank(arg1)) { arg1 = 0; } if (BaseTool.String.isBlank(arg2)) { arg2 = 0; } let re1, re2; try { re1 = arg1.toString().split('.')[1].length; } catch (e) { re1 = 0; } try { re2 = arg2.toString().split('.')[1].length; } catch (e) { re2 = 0; } const m = Math.pow(10, Math.max(re1, re2)); const n = re1 >= re2 ? re1 : re2; return ((arg1 * m - arg2 * m) / m).toFixed(n); }, mul(arg1, arg2) { if (BaseTool.String.isBlank(arg1)) { arg1 = 0; } if (BaseTool.String.isBlank(arg2)) { arg2 = 0; } let m = 0; const s1 = arg1.toString(); const s2 = arg2.toString(); try { m += s1.split('.')[1].length; } catch (e) {} try { m += s2.split('.')[1].length; } catch (e) {} return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m); }, /** * 除法 注意如果整除了 小数点将无效 * @param arg1 : 被除数 * @param arg2 : 除数 * @param digit :精确位数 * @returns {number} */ div(arg1, arg2, digit) { if (BaseTool.String.isBlank(arg1)) { arg1 = 0; } if (BaseTool.String.isBlank(arg2)) { arg2 = 1; } let t1 = 0; let t2 = 0; try { t1 = arg1.toString().split('.')[1].length; } catch (e) {} try { t2 = arg2.toString().split('.')[1].length; } catch (e) {} const r1 = Number(arg1.toString().replace('.', '')); const r2 = Number(arg2.toString().replace('.', '')); // 获取小数点后的计算值 const result = ((r1 / r2) * Math.pow(10, t2 - t1)).toString(); let result2 = result.split('.')[1]; if (result2 !== undefined) { result2 = result2.substring(0, digit > result2.length ? result2.length : digit); } else { result2 = ''; } while (result2.length < digit) { result2 = result2 + '0'; } if (digit === 0) { return Number(result.split('.')[0]); } return Number(result.split('.')[0] + '.' + result2); } }; BaseTool.Util = { isPhone: value => { const reg = /^1[3456789]\d{9}$/; return reg.test(value); }, isIdCard: value => { const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; return reg.test(value); }, isBankCard: value => { const reg = /^\d+$/; return reg.test(value); }, isAmount: value => { const reg = /((^[1-9]\d*)|^0)(\.\d{0,2})?$/; return reg.test(value); }, isDev: () => { return process.env.NODE_ENV === 'development'; }, isProd: () => { return process.env.NODE_ENV === 'production'; }, isIOS: () => { const ua = navigator.userAgent; const isiOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); if (isiOS) { return true; } else { return false; } } }; export default BaseTool;