import moment from 'moment'
|
import md5 from 'md5'
|
|
const service = window.GLOB.service ? (/\/$/.test(window.GLOB.service) ? window.GLOB.service : window.GLOB.service + '/') : ''
|
|
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 md5加密
|
* @return {String} str 加密串
|
* @return {String} timestamp 时间戳
|
*/
|
static encrypt (str, timestamp) {
|
let salt = 'mingke' // 盐值
|
return md5(str + salt + timestamp)
|
}
|
|
/**
|
* @description sql加密
|
* @return {String} value
|
*/
|
static formatOptions (value) {
|
if (!value) return ''
|
|
let salt = 'minKe' // 盐值
|
// 关键字转换规则
|
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)
|
})
|
|
// 1、替换%符(数据库中解析后sql报错),2、去除收尾多余空格
|
value = value.replace(/%/ig, 'mpercent')
|
value = value.replace(/(^\s|\s$)/ig, '')
|
|
// value = value.replace(/>/ig, 'greateror')
|
// .replace(/</ig, 'lessor')
|
// .replace(/!=/ig, 'noequal')
|
// .replace(/=/ig, 'equal')
|
// .replace(/,/ig, 'comma')
|
// .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')
|
|
// 1、encode编码(中文字符超出base64加密范围),2、base64加密
|
value = window.btoa(window.encodeURIComponent(value))
|
|
// 随机插入字符
|
let index = Math.floor(Math.random() * value.length)
|
value = value.slice(0, index) + salt + value.slice(index)
|
|
// base64加密
|
value = window.btoa(value)
|
|
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 + '/' + service
|
} else {
|
baseurl = 'http://qingqiumarket.cn/' + service
|
}
|
let realurl = url.match(/^http/) || url.match(/^\/\//) ? url : baseurl + url
|
return realurl
|
}
|
|
/**
|
* @description 删除存储过程sql
|
* @return {String} name 存储过程名称
|
*/
|
static dropfunc (name) {
|
return `IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('${name}') AND type in (N'P', N'PC')) mdrpk PROCEDURE ${name}`
|
}
|
|
/**
|
* @description 创建存储过程
|
* @return {String}
|
*/
|
static getfunc (param) {
|
let form = ''
|
if (param.fields && param.fields.length > 0) {
|
let _fields = []
|
param.fields.forEach(item => {
|
if (item.field) {
|
_fields.push(item.field)
|
}
|
})
|
|
let field1 = _fields.join(',')
|
let field2 = _fields.join(',@')
|
let field3 = _fields.map(cell => {
|
return cell + '=@' + cell
|
})
|
|
field2 = field2 ? '@' + field2 : ''
|
field3 = field3.join(',')
|
|
form = `
|
insert into ${param.name} (${field1}) select ${field2},@UserID
|
|
update ${param.name} set ${field3},modifydate=getdate(),modifyuserid=@UserID
|
`
|
}
|
|
let Ltext = `create proc ${param.name}
|
(
|
@BID nvarchar(50)='',
|
@ID nvarchar(50)='',
|
表单传值,文本用nvarchar(50)='',日期用datetime is null ,数值用decimal(18,小数点位数)=0,
|
@sEPTMenuNo='', --传菜单参数
|
@lang nvarchar(50)='',
|
@debug nvarchar(50)='',
|
@LoginUID nvarchar(50)='',
|
@SessionUid nvarchar(50)='',
|
@UserID nvarchar(50),
|
@ErrorCode nvarchar(50) out,
|
@retmsg nvarchar(4000) out
|
)
|
as
|
begin
|
declare @BegindateTest datetime,@EnddateTest datetime
|
select @BegindateTest=getdate()
|
set @ErrorCode=''
|
set @retmsg=''
|
BEGIN TRY
|
/*事务操作*/
|
BEGIN TRAN
|
/*具体业务操作*/
|
|
/*
|
select top 10 * from sProcExcep order by id desc
|
|
declare @UserName nvarchar(50),@FullName nvarchar(50)
|
|
select @UserName=UserName,@FullName=FullName from SUsers where UID=@UserID
|
${form}
|
if 1=2
|
begin
|
set @ErrorCode='E'
|
set @retmsg='在此写报错'
|
goto GOTO_RETURN
|
end
|
|
insert into sNote (remark,createuserid,CreateUser,CreateStaff)
|
select '在此写日志',@UserID,@UserName,@FullName
|
*/
|
|
COMMIT TRAN
|
SET NOCOUNT ON
|
RETURN
|
END TRY
|
BEGIN CATCH
|
/*错误处理*/
|
ROLLBACK TRAN
|
DECLARE @ErrorMessage NVARCHAR(4000);
|
DECLARE @ErrorSeverity INT;
|
DECLARE @ErrorState INT;
|
|
/*把自定义的友好的错误信息提示加上*/
|
set @ErrorCode=cast(ERROR_NUMBER() as nvarchar(50))
|
SET @retmsg=ERROR_MESSAGE();
|
SELECT @ErrorMessage=ERROR_MESSAGE(),
|
@ErrorSeverity=ERROR_SEVERITY(),
|
@ErrorState=ERROR_STATE();
|
|
RAISERROR(@ErrorMessage, /*-- Message text.*/
|
@ErrorSeverity, /*-- Severity.*/
|
@ErrorState /*-- State.*/
|
);
|
END CATCH
|
|
GOTO_RETURN:
|
ROLLBACK TRAN
|
|
END`
|
Ltext = Ltext.replace(/\n\s{4}/ig, 'mchr13k')
|
console.log(Ltext)
|
return Ltext
|
}
|
}
|