import moment from 'moment' export default class Utils { /** * @description 生成32位uuid string + 时间 * @return {String} uuid */ static getuuid () { let uuid = [] let timestamp = new Date().getTime() let options = '0123456789abcdefghigklmnopqrstuv' for (let i = 0; i < 19; i++) { uuid.push(options.substr(Math.floor(Math.random() * 0x20), 1)) } uuid = timestamp + uuid.join('') return uuid } /** * @description 生成GUID * @return {String} guid */ static getguid () { // 产生一个新的GUID值 let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { let r = Math.random() * 16 | 0 // eslint-disable-next-line let v = (c === 'x') ? r : (r & 0x3 | 0x8) return v.toString(16) }) return uuid } /** * @description sql加密 * @return {String} value */ static formatOptions (value) { // 产生一个新的GUID值 let format = [{ key: 'select', value: ' msltk ' }, { key: 'from', value: ' mfrmk ' }, { key: 'where', value: ' mwhrk ' }, { key: 'order by', value: ' modbk ' }, { key: 'asc', value: ' modack ' }, { key: 'desc', value: ' moddesk ' }, { key: 'top', value: ' mtpk ' }, { key: 'like', value: ' mlkk ' }, { key: 'not like', value: ' mnlkk ' }, { key: 'between', value: ' mbtnk ' }, { key: 'and', value: ' madk ' }, { key: 'insert', value: ' mistk ' }, { key: 'into', value: ' mitk ' }, { key: 'update', value: ' muptk ' }, { key: 'delete', value: ' mdelk ' }, { key: 'begin', value: ' mbgink ' }, { key: 'end', value: ' medk ' }, { key: 'if', value: ' mefk ' }, { key: 'while', value: ' mwilk ' }, { key: 'create', value: ' mcrtk ' }, { key: 'alter', value: ' matek ' }, { key: 'len', value: ' mlnk ' }, { key: 'left', value: ' mlftk ' }, { key: 'right', value: ' mritk ' }, { key: 'union', value: ' munok ' }, { key: 'varchar', value: ' mvcrk ' }, { key: 'getdate', value: ' mgtdtk ' }, { key: 'TRY', value: ' mtryonek ' }, { key: 'TRAN', value: ' mtrnk ' }, { key: 'goto', value: ' mgtk ' }, { key: 'set', value: ' mstk ' }, { key: 'ROLLBACK', value: ' mrlbkk ' }] format.forEach(item => { let reg = new RegExp('(^|\\s)' + item.key + '(\\s|$)', 'ig') value = value.replace(reg, item.value) }) value = value.replace(/(^\s|\s$)/ig, '') value = window.btoa(window.encodeURIComponent(value)) let index = Math.floor(Math.random() * value.length) value = value.slice(0, index) + 'minKe' + value.slice(index) value = window.btoa(value) // value = value.replace(/%/ig, 'mpercent') // .replace(/>/ig, 'greateror') // .replace(/=/ig, 'greaterorequal') // .replace(/<=/ig, 'lessorequal') // .replace(/@/ig, 'matk') // .replace(/\(/ig, 'mlbrktsk') // .replace(/\)/ig, 'mrbrktsk') // .replace(/\*/ig, 'mastrsk') // .replace(/'/ig, 'mqotek') // .replace(/\s/ig, 'mspace') return value } /** * @description 拼接搜索条件 * @param {Array} searches 搜索条件 * @return {String} searchText 拼接结果 */ static mainjointsearchkey (searches) { if (!searches || searches.length === 0) return '' if (searches[0].hasOwnProperty('initval')) { let newsearches = [] searches.forEach(search => { let item = { key: search.field, match: search.match, type: search.type, value: search.initval } if (item.type === 'date') { item.value = item.value ? moment().subtract(item.value, 'days').format('YYYY-MM-DD') : '' } else if (item.type === 'datemonth') { item.value = item.value ? moment().subtract(item.value, 'month').format('YYYY-MM') : '' } else if (item.type === 'dateweek') { item.value = item.value ? [moment().subtract(item.value * 7, 'days').startOf('week').format('YYYY-MM-DD'), moment().subtract(item.value * 7, 'days').endOf('week').format('YYYY-MM-DD')] : '' } else if (item.type === 'daterange') { item.value = item.value ? [moment().subtract(item.value, 'days').format('YYYY-MM-DD'), moment().subtract(item.value === 1 ? 1 : 0, 'days').format('YYYY-MM-DD')] : '' } newsearches.push(item) }) searches = newsearches } let searchText = '' searches.forEach(item => { if (!item.value) return // eslint-disable-next-line searchText += (searchText !== '' ? ' ' + 'AND' + ' ' : '') if (item.type === 'text' || item.type === 'select') { // eslint-disable-next-line let str = item.match === '=' ? '' : '%' // eslint-disable-next-line searchText += item.key + ' ' + item.match + ' ' + '\'' + str + item.value + str + '\'' } else if (item.type === 'date' || item.type === 'datemonth') { // eslint-disable-next-line searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value + '\')' } else if (item.type === 'dateweek') { // eslint-disable-next-line searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + '\' AND \'' + item.value[1] + '\')' } else if (item.type === 'daterange') { // eslint-disable-next-line searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + '\' AND \'' + item.value[1] + '\')' } else { // eslint-disable-next-line searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value + '\')' } }) return searchText } /** * @description 拼接搜索条件 * @param {Array} searches 搜索条件 * @return {String} searchText 拼接结果 */ static jointsearchkey (searches) { if (!searches || searches.length === 0) return '' let searchText = '' searches.forEach(item => { if (!item.value) return // eslint-disable-next-line searchText += (searchText !== '' ? ' ' + 'AND' + ' ' : '') if (item.type === 'text') { let options = item.key.split(',').map(op => { // equal时不添加% // eslint-disable-next-line let str = item.op === 'equal' ? '' : '%' // eslint-disable-next-line return op + ' ' + item.op + ' ' + '"' + str + item.value + str + '"' }) // eslint-disable-next-line searchText += '(' + options.join(' ' + 'OR' + ' ') + ')' } else if (item.type === 'date') { // eslint-disable-next-line searchText += '(' + item.key + ' ' + item.op + ' ' + '"' + item.value + '")' } else { // eslint-disable-next-line searchText += '(' + item.key + ' ' + item.op + ' ' + '"' + item.value + '")' } }) return searchText } /** * @description 获取图片真实路径 * @return {String} url 图片路径 */ static getrealurl (url) { if (!url) return '' let baseurl = '' if (process.env.NODE_ENV === 'production') { baseurl = document.location.origin + '/' } else { baseurl = 'http://qingqiumarket.cn/MKWMS/' } let realurl = url.match(/^http/) || url.match(/^\/\//) ? url : baseurl + url return realurl } }