/* * @Description: 公共方法 * @Author: zhoupeng * @Date: 2020-03-17 10:47:30 * @LastEditTime: 2020-05-25 11:26:42 */ import moment from 'moment'; import 'moment/locale/zh-cn'; import { v4 as uuidv4 } from 'uuid'; import md5 from 'js-md5'; /** * 设置 moment 默认为 Chinese(China) */ moment.locale('zh-cn'); /** * 格式化时间日期 * * @param {*} value 需要格式化的值 * @param {String} format 格式 * */ export const formatDatetime = (value, format = 'YYYY-MM-DD HH:mm:ss') => moment(value).format(format); /** * 只包含数字的字符验证正则 */ export const NUMBER_REGEX = /^[0-9]*$/; /** * 检测一个字符串是否只包含数字 * * @param {String} value 待检测的字符串 */ export const isNumber = value => NUMBER_REGEX.test(value); export const formatCurrency = value => { try { if (value) { // value = value.toFixed(2) return parseFloat(value).toLocaleString('zh-CN', { minimumFractionDigits: 2, useGrouping: true, // style: 'currency', // currency: 'CNY', }); } // console.warn('==', value.toFixed(2)) return parseFloat(value).toFixed(2); } catch (e) { console.log('==', e); return '0.00'; } }; export const browser = { versions: (function() { var u = navigator.userAgent, app = navigator.appVersion; return { //移动终端浏览器版本信息 trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端 ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器 iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部 }; })(), language: (navigator.browserLanguage || navigator.language).toLowerCase(), }; export const appBrowser = () => { if (browser.versions.mobile) { //判断是否是移动设备打开。browser代码在下面 var ua = navigator.userAgent.toLowerCase(); //获取判断用的对象 if (/MicroMessenger/i.test(ua)) { return 'wechat'; } if (/WeiBo/i.test(ua)) { return 'weibo'; } if (/QQ/i.test(ua)) { return 'qq'; } } }; export const MOBILE_NUMBER_REGIX = /^[1]([0-9])[0-9]{9}$/; export const MOBILE_NUMBER_REGEX_STRICT = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/; /** * 检测一个字符串是否为手机号 * * @param {String} value 待检测的值 * @param {Boolean} strict 是否严格模式检测 * * 默认为严格模式,检测正则表达式为: * * ```js * /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/ * ``` * * 已支持的号段有: * * - 中国移动: * - 134 135 136 137 138 139 * - 182 183 184 187 188 * - 150 151 152 157 158 159 * - 165 * - 172 178(4G) * - 147(上网卡) * - 1440(物联网) 148(物联网) * - 198 * - 中国联通 * - 130 131 132 * - 155 156 * - 185 186 * - 171 175 176(4G) * - 145(上网卡) * - 146(物联网) * - 166 * - 中国电信 * - 133 149 153 * - 180 181 189 191 * - 173 174 177(4G) * - 1410(物联网) * - 199 * - 虚拟运营商 * - 170 171 * * 普通模式正则表达式为: * * ```js * /^[1]([3-9])[0-9]{9}$/ * ``` */ export const isMobileNumber = (value, strict = false) => strict ? MOBILE_NUMBER_REGEX_STRICT.test(value) : MOBILE_NUMBER_REGIX.test(value); // 验证身份证号 const PRC_CITIZEN_ID = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; const getPRCCitizenIDCheckCode = function(value) { var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]; if (/[\d]{17}/.test(value)) { var parityIndex = value .substring(0, 17) .split('') .map(function(number, index) { return parseInt(number) * factor[index]; }) .reduce(function(a, b) { return a + b; }, 0) % 11; return '' + parity[parityIndex]; } return ''; }; export const idCardValidate = function(value) { var checkCode = value.substring(17).toUpperCase(); if (PRC_CITIZEN_ID.test(value)) { return getPRCCitizenIDCheckCode(value.substring(0, 17)) === '' + checkCode; } return false; }; /** * 生成全局唯一ID字符串 * * 当前使用的是的是 `uuidv4` */ const uuid = uuidv4(); const md5Uuid = md5(uuid ? uuid : ''); export const sysMobileNum = `${md5Uuid ? md5Uuid.slice(0, 6) : ''}-${uuid}`; /** * 手机系统类型 */ export const getMobileType = () => { var u = navigator.userAgent; var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 if (isiOS) { return 'ios'; } return 'android'; }; export const mobileType = getMobileType(); /** * @description:umi 获取this.props上的参数 * @param {location} * @param {name} * @return: * @author: zhoupeng */ export const getQuery = (location, name) => { if (location && location.query) { return location.query[name]; } return ''; }; /** * @description:获取url上的参数 * @param {location} * @param {name} * @return: * @author: zhoupeng */ export const getQueryVariable = variable => { var query = window.location.search.substring(1); var vars = query.split('&'); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split('='); if (pair[0] == variable) { return pair[1]; } } return false; }; /** * @description:获取url上的参数 * @param {location} * @param {name} * @return: * @author: zhoupeng */ export const getQueryParamUrl = url => { try { if (!url) { return ''; } const pp1 = url.split('#')[1] ? url.split('#')[1] : ''; const allParams = pp1.split('?')[1] ? pp1.split('?')[1] : ''; return allParams; } catch (e) { return ''; } }; export const getQueryVariableBy = (url, variable) => { try { if (!url) { return ''; } const pp1 = url.split('#')[1] ? url.split('#')[1] : ''; const allParams = pp1.split('?')[1] ? pp1.split('?')[1] : ''; if (allParams) { var vars = allParams.split('&'); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split('='); if (pair[0] == variable) { return pair[1]; } } } return false; } catch (e) { return false; } };