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 = []
|
let d = new Date()
|
let options = '0123456789abcdefghigklmnopqrstuv'
|
for (let i = 0; i < 19; i++) {
|
uuid.push(options.substr(Math.floor(Math.random() * 0x20), 1))
|
}
|
uuid = moment().format('YYYYMMDDHHmmss') + d.getMilliseconds() + uuid.join('')
|
return uuid.toUpperCase()
|
}
|
|
/**
|
* @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} searches 格式化后结果
|
*/
|
static initMainSearch (searches) {
|
if (!searches || searches.length === 0) return []
|
|
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)
|
})
|
|
return newsearches
|
}
|
|
/**
|
* @description 拼接搜索条件
|
* @param {Array} searches 搜索条件
|
* @return {String} searchText 拼接结果
|
*/
|
static joinMainSearchkey (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' || 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') {
|
let timetail = ' 00:00:00.000'
|
if (item.match === '<' || item.match === '<=') {
|
timetail = ' 23:59:59.999'
|
}
|
// eslint-disable-next-line
|
searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value + timetail + '\')'
|
} else if (item.type === 'datemonth') {
|
// eslint-disable-next-line
|
searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value + '\')'
|
} else if (item.type === 'dateweek') {
|
let stimetail = ' 00:00:00.000'
|
let etimetail = ' 23:59:59.999'
|
// eslint-disable-next-line
|
searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + stimetail + '\' AND \'' + item.value[1] + etimetail + '\')'
|
} else if (item.type === 'daterange') {
|
let stimetail = ' 00:00:00.000'
|
let etimetail = ' 23:59:59.999'
|
// eslint-disable-next-line
|
searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + stimetail + '\' AND \'' + item.value[1] + etimetail + '\')'
|
} 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 使用系统函数时(sPC_TableData_InUpDe ),生成sql语句
|
* @return {String} type 执行类型
|
* @return {String} table 表名
|
*/
|
static getSysDefaultSql (btn, setting, formdata) {
|
console.log(btn)
|
console.log(formdata)
|
let _sql = ''
|
if (btn.OpenType === 'pop' && btn.sqlType === 'insert') {
|
let keys = []
|
let values = []
|
formdata.forEach(item => {
|
if (!item.readonly && item.type === 'number') {
|
keys.push(item.key)
|
values.push(item.value)
|
} else if (!item.readonly) {
|
keys.push(item.key)
|
values.push('\'' + item.value + '\'')
|
}
|
})
|
keys = keys.join(',')
|
values = values.join(',')
|
|
_sql = `insert into ${btn.sql} (${keys}, createuserid) select ${values},@userid`
|
} else if (btn.OpenType === 'pop' && btn.sqlType === 'update') {
|
let _form = []
|
formdata.forEach(item => {
|
if (!item.readonly && item.type === 'number') {
|
_form.push(item.key + '=' + item.value)
|
} else if (!item.readonly) {
|
_form.push(item.key + '=\'' + item.value + '\'')
|
}
|
})
|
_form = _form.join(',')
|
_sql = `update ${btn.sql} set ${_form},modifydate=getdate(),modifyuserid=@userid where ${setting.primaryKey}=@id`
|
} else if ((btn.OpenType === 'prompt' || btn.OpenType === 'exec') && btn.sqlType === 'LogicDelete') { // 逻辑删除
|
_sql = `update ${btn.sql} set deleted=1,modifydate=getdate(),modifyuserid=@userid where ${setting.primaryKey}=@id`
|
} else if ((btn.OpenType === 'prompt' || btn.OpenType === 'exec') && btn.sqlType === 'delete') {
|
_sql = `insert into snote (remark,createuserid) select '删除表:${btn.sql} 数据: id='+@id,@userid delete ${btn.sql} where ${setting.primaryKey}=@id`
|
}
|
//insert into 表名 (表单字段,逗号分隔,如果主键为可编辑则传填写内容,只读或没有配置表单,则传前端生成id,+createuserid)
|
// +select 表单字段值+@userid
|
|
// 修改:update 表名 set 表单非只读字段=表单值,modifydate=getdate(),modifyuserid=@userid where 主键=@id行id
|
|
// 逻辑删除:update 表名 set deleted=1,modifydate=getdate(),modifyuserid=@userid where 主键=@id行id
|
|
// 物理删除:insert into snote (remark,createuserid) select '删除表 '+表名+'数据 '+id=@id delete 表名 where 主键=@id行id
|
console.log(_sql)
|
return _sql
|
}
|
|
/**
|
* @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 = ''
|
let formParam = ''
|
|
if (param.fields && param.fields.length > 0) {
|
let _fields = []
|
param.fields.forEach(item => {
|
if (item.field) {
|
let type = ''
|
if (item.type.match(/date/ig)) {
|
type = 'datetime=null'
|
} else if (item.type === 'number') {
|
type = `decimal(18,${item.decimal})=0`
|
} else {
|
type = 'nvarchar(50)=\'\''
|
}
|
formParam = formParam + `mchr13k@${item.field} ${type},`
|
|
_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},createuserid) select ${field2},@UserID
|
|
update ${param.name} set ${field3},modifydate=getdate(),modifyuserid=@UserID
|
`
|
}
|
|
let Ltext = `create proc ${param.funcName}
|
(
|
@BID nvarchar(50)='',
|
@ID nvarchar(50)='',${formParam}
|
@sEPTMenuNo nvarchar(50)='${param.menuNo}',
|
@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
|
}
|
}
|