UNPKG

maycur-business

Version:

maycur business react components of web

696 lines (561 loc) 19.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _lodash = _interopRequireDefault(require("lodash")); var _windowOrGlobal = _interopRequireDefault(require("window-or-global")); var _numeral = _interopRequireDefault(require("numeral")); var _moment = _interopRequireDefault(require("moment")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var placesHashMap = {}, bankPlacesHashMap = {}, locationNodes = []; var history = undefined; var mobile = { isAndroid: function isAndroid() { return navigator.userAgent.match(/Android/i); }, isBlackBerry: function isBlackBerry() { return navigator.userAgent.match(/BlackBerry/i); }, isIOS: function isIOS() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, isOpera: function isOpera() { return navigator.userAgent.match(/Opera Mini/i); }, isWindows: function isWindows() { return navigator.userAgent.match(/IEMobile/i); } }; var prefixCls = 'mkbs'; // 预设热门城市 // 北京(ZH1_0_00)、上海(ZH4_0_00)、广州(ZH9_4_01)、深圳(ZH9_15_01)、杭州(ZH32_1_01)、 // 天津(ZH2_0_00)、成都(ZH28_3_01)、重庆(ZH33_0_00)、哈尔滨(ZH15_3_01)、南京(ZH19_4_01) // 武汉(ZH16_9_01)、南昌(ZH20_6_01)、西安(ZH27_7_01)、合肥(ZH3_8_01)、长沙(ZH17_1_01)、 // 大连(ZH21_4_01)、石家庄(ZH13_8_01)、济南(ZH25_5_01) var hotCity = ['ZH1_0_00', 'ZH4_0_00', 'ZH9_4_01', 'ZH9_15_01', 'ZH32_1_01', 'ZH2_0_00', 'ZH28_3_01', 'ZH33_0_00' // 'ZH15_3_01', 'ZH19_4_01', // 'ZH16_9_01', // 'ZH20_6_01', 'ZH27_7_01', 'ZH3_8_01', 'ZH17_1_01', 'ZH21_4_01', 'ZH13_8_01', 'ZH25_5_01' ]; //resetNumeral(); //设置numeral组件为floor模式 /*function resetNumeral() { numeral.fn._format = numeral.fn.format; numeral.fn.format = function (a, b) { return numeral.fn._format.call(this, a, b || numeral._.defaultRoundingFunction); }; numeral._.defaultRoundingFunction = Math.round; numeral.setDefaultRoundingFunction = (fn) => { numeral._.defaultRoundingFunction = fn || numeral._.defaultRoundingFunction; } numeral.setDefaultRoundingFunction(Math.floor); }*/ function formatPlaceData(locationData, type) { var nodes = []; Object.keys(locationData).forEach(function (key) { type === 'fee' ? nodes.push(_convertNode(locationData[key], key, placesHashMap)) : nodes.push(_convertNode(locationData[key], key, bankPlacesHashMap)); }); if (type === 'fee') { locationNodes = nodes; } return nodes; } function _convertNode(source, countryType, placesHashMap) { var childrenProp = ['countries', 'provinces', 'cities', 'regions']; var prefix = ['country', 'province', 'city', 'region']; var children = null; for (var i = 0; i < childrenProp.length; i += 1) { var property = childrenProp[i]; children = source[property]; if (children) { source.children = source[property]; break; } } for (var j = 0; j < prefix.length; j += 1) { var p = prefix[j]; if (source.code) { // source.code = source.code; // source.name = source.name; break; } else if (source[p + 'Code']) { source.code = source[p + 'Code']; source.name = source[p + 'Name']; break; } } setPlacesHashMap(source, countryType, placesHashMap); if (children) { for (var k = 0; k < children.length; k += 1) { var child = children[k]; _convertNode(child, countryType, placesHashMap); } } else { placesHashMap[source.code].fuzzy = source.fuzzy; placesHashMap[source.code].isLeaf = true; } return source; } function getCityName(locationCode, type) { var locationName = ''; if (locationCode) { var lastStrikethroughIndex = locationCode.lastIndexOf('-'), slashIndex, placeCode = locationCode.substr(lastStrikethroughIndex + 1), locationFullName, hashMap = type === 'fee' ? placesHashMap : bankPlacesHashMap; var place = hashMap[placeCode]; if (place) { locationFullName = place.fullName; slashIndex = locationFullName.indexOf('/'); locationName = locationFullName.substr(slashIndex + 1); } } return locationName; } function getSimpleCityName(locationCode, type) { type = type ? type : 'fee'; var locationName = ''; if (locationCode) { var lastStrikethroughIndex = locationCode.lastIndexOf('-'), placeCode = locationCode.substr(lastStrikethroughIndex + 1), hashMap = type === 'fee' ? placesHashMap : bankPlacesHashMap; var place = hashMap[placeCode]; if (place) { locationName = place.name; } } return locationName; } function getPlaceDataByCode(code, type) { type = type || 'fee'; if (type === 'fee') { return placesHashMap && placesHashMap[code]; } else { return bankPlacesHashMap && bankPlacesHashMap[code]; } } function getPlaceDataByCodes(codes, type) { type = type || 'fee'; var places = []; _lodash["default"].each(codes, function (code) { if (code) { if (type === 'fee') { places.push(placesHashMap[code]); } else { places.push(bankPlacesHashMap[code]); } } }); return places; } function setPlacesHashMap(source, countryType, placesHashMap) { var fullName = '', fullCode = ''; if (source.code) { placesHashMap[source.code] = { code: source.code, name: source.name, children: source.children }; var lastIndex = source.code.lastIndexOf('_'); if (lastIndex === -1) { if (source.code === countryType) { fullName = ''; fullCode = source.code; } else { var countryFullName = placesHashMap[countryType].fullName, countryFullCode = placesHashMap[countryType].fullCode; fullName = countryFullName ? countryFullName + '-' + source.name : source.name; fullCode = countryFullCode ? countryFullCode + '-' + source.code : source.code; } } else { fullName = placesHashMap[source.code.substring(0, lastIndex)].fullName + '-' + source.name; fullCode = placesHashMap[source.code.substring(0, lastIndex)].fullCode + '-' + source.code; } placesHashMap[source.code].fullName = fullName; placesHashMap[source.code].fullCode = fullCode; } } function getPlacesHashMap() { return placesHashMap; } function getBankPlacesHashMap() { return bankPlacesHashMap; } // 将定位到的地理位置转换为系统内的地址 function getCurrentLocationData(province, city, district) { var domestic = locationNodes[0]; var filter = function filter(str) { var result = str.replace('市', ''); result = result.replace('省', ''); result = result.replace('县', ''); result = result.replace('区', ''); return result; }; province = filter(province); city = filter(city); district = filter(district); for (var i in domestic.children) { var provinceNode = domestic.children[i]; if (provinceNode.name.indexOf(province) >= 0) { for (var j in provinceNode.children) { var cityNode = provinceNode.children[j]; if (cityNode.name.indexOf(city) >= 0) { for (var k in cityNode.children) { var districtNode = cityNode.children[k]; if (districtNode.name.indexOf(district) >= 0) { return placesHashMap[districtNode.code]; } } return placesHashMap[cityNode.children[0].code]; } } } } return { code: 'error', name: '定位出错' }; } /* 多余字段替换为...方法 */ function extractText(_ref) { var maxWidth = _ref.maxWidth, text = _ref.text, replaceText = _ref.replaceText; var replaceString = replaceText ? replaceText : '…'; if (text && maxWidth && text.length > maxWidth) { return text.substr(0, maxWidth) + replaceString; } else { return text; } } /* 获取url参数 */ function getUrlArgs(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = _windowOrGlobal["default"].location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } function hashCode(str) { str = str || ''; if (Array.prototype.reduce) { return str.split('').reduce(function (a, b) { a = (a << 5) - a + b.charCodeAt(0); return a & a; }, 0); } var hash = 0; if (str.length === 0) return hash; for (var i = 0; i < str.length; i++) { var character = str.charCodeAt(i); hash = (hash << 5) - hash + character; hash = hash & hash; // Convert to 32bit integer } return hash; } function setHistory(parameter) { history = parameter; } /* 银行卡显示 */ function bankCardNumDisplay(bankCardCode) { return bankCardCode.substring(0, 4) + ' **** **** ' + bankCardCode.substr(-4); } var ScrollHandle = function ScrollHandle() { var _this = this; _classCallCheck(this, ScrollHandle); var keys = { 37: 1, 38: 1, 39: 1, 40: 1 }; var preventDefault = function preventDefault(e) { e = e || _windowOrGlobal["default"].event; if (e.preventDefault) e.preventDefault(); e.returnValue = false; }; var preventDefaultForScrollKeys = function preventDefaultForScrollKeys(e) { if (keys[e.keyCode]) { _this.preventDefault(e); return false; } }; this.disableScroll = function () { if (_windowOrGlobal["default"].addEventListener) // older FF _windowOrGlobal["default"].addEventListener('DOMMouseScroll', preventDefault, false); _windowOrGlobal["default"].onwheel = preventDefault; // modern standard _windowOrGlobal["default"].onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE _windowOrGlobal["default"].ontouchmove = preventDefault; // mobile document.onkeydown = preventDefaultForScrollKeys; }; this.enableScroll = function () { if (_windowOrGlobal["default"].removeEventListener) _windowOrGlobal["default"].removeEventListener('DOMMouseScroll', preventDefault, false); _windowOrGlobal["default"].onmousewheel = document.onmousewheel = null; _windowOrGlobal["default"].onwheel = null; _windowOrGlobal["default"].ontouchmove = null; document.onkeydown = null; }; }; function getFileType(name) { if (name) { return name.slice(name.lastIndexOf('.') + 1).toLowerCase(); } return ''; } function getFileSize(size) { if (size) { if (size / 1024 / 1024 >= 1) { return parseFloat(size / 1024 / 1024).toFixed(2) + 'M'; } if (size / 1024 >= 1) { return parseFloat(size / 1024).toFixed(2) + 'KB'; } else { return size + 'B'; } } return null; } function isImage(name) { var type = getFileType(name); var imgRegex = /^jpg|png|jpeg|bmp|gif+$/; return imgRegex.test(type); } function isZip(name) { var type = getFileType(name); var zipRegex = /^zip|rar+$/; return zipRegex.test(type); } function makeId() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) { text += possible.charAt(Math.floor(Math.random() * possible.length)); } return text; } function getUUID() { var d = new Date().getTime(); if (_windowOrGlobal["default"].performance && typeof _windowOrGlobal["default"].performance.now === "function") { d += performance.now(); //use high-precision timer if available } var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c === 'x' ? r : r & 0x3 | 0x8).toString(16); }); return uuid; } function formatCurrency(value) { var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0,0.00'; var symbol = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '¥'; return symbol + (0, _numeral["default"])(value).format(format); } function removeLoadPage() { var loadPage = document.getElementById('load-page'); /* 移除load page */ if (loadPage) { loadPage.remove(1); } } function getLocationOrigin() { if (_windowOrGlobal["default"].location.hostname === 'localhost') { return '*'; } else { return _windowOrGlobal["default"].location.origin; } } function apiOK(data) { return data && data.code === 'ACK'; } function isEmpty(str) { if (typeof str === 'string') { str = str.trim(); return str === ""; } else { console.warn('not validate type'); } } function generateRows(formFields) { var weight = 0; var rowChild = []; var rows = []; _lodash["default"].forEach(formFields, function (field, index) { var _ref2 = field.property || {}, webSize = _ref2.webSize; var fieldWeight = webSize === 0.5 ? 1 : 2; if (fieldWeight === 2 && weight % 2 !== 0) { /* 当出现100%占行的时候,前面累积的field都必须成行 */ rows.push({ fieldList: rowChild }); rowChild = []; weight += 1; } rowChild.push(field); weight += fieldWeight; if (weight % 2 === 0 || index === formFields.length - 1) { /* 当权重为模2为0或者是最后一个field元素的时候 */ rows.push({ fieldList: rowChild }); rowChild = []; } }); return rows; } function getAbbrName(name) { var chineseReg = new RegExp("[\u4E00-\uFA29\uE7C7-\uE7F3]+", 'g'); var wordReg = new RegExp('[A-Za-z]{1,4}'); var result = ''; var m = (name || '').match(chineseReg); if (m && m.length !== 0) { result = m[0]; if (result && result.length > 2) { result = result.substring(result.length - 2, result.length); } } if (!result) { m = (name || '').match(wordReg); if (m && m.length !== 0) { result = m[0]; } } if (!result && name) { result = name.substr(0, 1); } return result; } function getAvatarClass(name) { var colors = ['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10', 'c11', 'c12', 'c13', 'c14', 'c15', 'c16']; var idx = Math.abs(hashCode(name)) % colors.length; return colors[idx]; } var bizEquals = function bizEquals(o1, o2) { return o1.code === o2.code; }; var formatBankAcctNumber = function formatBankAcctNumber(str) { return str && str.replace(/(\d{4})/g, '$1 ').replace(/\s*$/, ''); }; var getConsumeLocation = function getConsumeLocation(value) { var pairValue = value && value.cityPair; var showPairValue = ''; if (pairValue) { showPairValue += pairValue.departure && getSimpleCityName(pairValue.departure, 'fee') || '无'; showPairValue += ' ~ ' + (pairValue.destination && getSimpleCityName(pairValue.destination, 'fee') || '无'); } return showPairValue ? showPairValue : getSimpleCityName(value && value.location, 'fee'); }; var getConsumeTime = function getConsumeTime(value) { var pairValue = value && value.timeRange; var showPairValue = ''; if (pairValue) { showPairValue += pairValue.startDate && (0, _moment["default"])(pairValue.startDate).format('YYYY/MM/DD') || '无'; showPairValue += ' ~ ' + (pairValue.endDate && (0, _moment["default"])(pairValue.endDate).format('YYYY/MM/DD') || '无'); } return showPairValue ? showPairValue : (0, _moment["default"])(value.currentTime).format('YYYY/MM/DD'); }; var emailMosaic = function emailMosaic() { var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var reg = /(\S{0,3})\S*(@\S)/; return str.replace(reg, '$1***$2'); }; var mobileMosaic = function mobileMosaic() { var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var reg = /(\d{3})\d*(\d{2})/; return str.replace(reg, '$1******$2'); }; var isEmail = function isEmail(str) { var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; return pattern.test(str); }; function asyncForEach() { return _asyncForEach.apply(this, arguments); } function _asyncForEach() { _asyncForEach = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee() { var array, callback, index, _args = arguments; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: array = _args.length > 0 && _args[0] !== undefined ? _args[0] : []; callback = _args.length > 1 ? _args[1] : undefined; if (array) { _context.next = 4; break; } return _context.abrupt("return"); case 4: index = 0; case 5: if (!(index < array.length)) { _context.next = 11; break; } _context.next = 8; return callback(array[index], index, array); case 8: index++; _context.next = 5; break; case 11: case "end": return _context.stop(); } } }, _callee); })); return _asyncForEach.apply(this, arguments); } function dateRemoveTime(d) { return d.setHours(0, 0, 0, 0); } var _default = { prefixCls: prefixCls, hotCity: hotCity, getCityName: getCityName, getSimpleCityName: getSimpleCityName, formatPlaceData: formatPlaceData, getPlaceDataByCode: getPlaceDataByCode, getPlaceDataByCodes: getPlaceDataByCodes, getPlacesHashMap: getPlacesHashMap, getBankPlacesHashMap: getBankPlacesHashMap, extractText: extractText, getUrlArgs: getUrlArgs, hashCode: hashCode, setHistory: setHistory, history: history, bankCardNumDisplay: bankCardNumDisplay, getCurrentLocationData: getCurrentLocationData, ScrollHandle: ScrollHandle, getFileType: getFileType, getFileSize: getFileSize, isImage: isImage, isZip: isZip, mobile: mobile, makeId: makeId, getUUID: getUUID, formatCurrency: formatCurrency, removeLoadPage: removeLoadPage, getLocationOrigin: getLocationOrigin, apiOK: apiOK, isEmpty: isEmpty, generateRows: generateRows, getAbbrName: getAbbrName, getAvatarClass: getAvatarClass, bizEquals: bizEquals, formatBankAcctNumber: formatBankAcctNumber, getConsumeLocation: getConsumeLocation, getConsumeTime: getConsumeTime, emailMosaic: emailMosaic, mobileMosaic: mobileMosaic, isEmail: isEmail, asyncForEach: asyncForEach, dateRemoveTime: dateRemoveTime, noop: function noop() {} }; exports["default"] = _default;