init
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
import { GeneralResponse } from '@/define/model/generalResponse'
|
||||
|
||||
/**
|
||||
* 返回成功的消息,包含code,data,message
|
||||
* @param {*} data 返回的数据
|
||||
* @param {*} message 成功消息
|
||||
* @param {*} service 消息日志类型
|
||||
* @returns
|
||||
*/
|
||||
function successMessage(
|
||||
data?: any,
|
||||
message?: string,
|
||||
service?: string
|
||||
): GeneralResponse.SuccessItem {
|
||||
if (service) {
|
||||
global.logger.success(service, message ? message : '成功返回数据')
|
||||
}
|
||||
return {
|
||||
code: 1,
|
||||
data: data,
|
||||
message: message
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回失败的消息,
|
||||
* @param {*} message 错误信息
|
||||
* @param {*} service 错误日志类型
|
||||
* @returns
|
||||
*/
|
||||
function errorMessage(message: string, service?: string): GeneralResponse.ErrorItem {
|
||||
if (service) {
|
||||
global.logger.error(service, message ? message : '未知报错,没有捕获的错误')
|
||||
}
|
||||
return {
|
||||
code: 0,
|
||||
message: message
|
||||
}
|
||||
}
|
||||
|
||||
function infoMessage(message: string, service?: string): GeneralResponse.ErrorItem {
|
||||
if (service) {
|
||||
global.logger.info(service, message)
|
||||
}
|
||||
return {
|
||||
code: 0,
|
||||
message: message
|
||||
}
|
||||
}
|
||||
|
||||
function warningMessage(message: string, service?: string): GeneralResponse.ErrorItem {
|
||||
if (service) {
|
||||
global.logger.warn(service, message)
|
||||
}
|
||||
return {
|
||||
code: 0,
|
||||
message: message
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 主动发送消息到前端,用作数据返回
|
||||
* @param data 要返回的数据
|
||||
* @param message_name 消息名称
|
||||
*/
|
||||
function SendReturnMessage(data: GeneralResponse.MessageResponse, message_name: string) {
|
||||
global.wins[0].webContents.send(message_name, data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串的值是不是存在,不是null,不是undefined,不是空字符串,存在的话添加指定的后缀
|
||||
* @param {*} value 要检查的字符串
|
||||
* @param {*} suffix 要添加的后缀
|
||||
* @returns
|
||||
*/
|
||||
export function checkStringValueAddSuffix(value: string, suffix: string): string {
|
||||
if (value && value !== null && value !== undefined && value !== '') {
|
||||
return value + suffix
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串的值是不是存在,不是null,不是undefined,不是空字符串,存在的话添加指定的前缀
|
||||
* @param {*} value 要检查的值
|
||||
* @param {*} prefix 要添加的前缀
|
||||
* @returns
|
||||
*/
|
||||
|
||||
export function checkStringValueAddPrefix(value: string, prefix: string): string {
|
||||
if (value && value !== null && value !== undefined && value !== '') {
|
||||
return prefix + value
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建一个函数,判断传入的字符串的值是不是存在,存在的话删除后面指定数量的字符
|
||||
* @param {*} value 要删除的字符串
|
||||
* @param {*} suffix 删除的后缀
|
||||
* @returns
|
||||
*/
|
||||
export function checkStringValueDeleteSuffix(value: string, suffix: string): string {
|
||||
// 增加一个判断,当前删除的数量是不是大于字符串的长度
|
||||
if (value && value !== null && value !== undefined && value !== '') {
|
||||
if (suffix.length > value.length) {
|
||||
return ''
|
||||
} else {
|
||||
return value.slice(0, value.length - suffix.length)
|
||||
}
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建一个函数,判断传入的字符串的值是不是存在,存在的话删除前面指定数量的字符
|
||||
* @param {*} value 操作的字符串
|
||||
* @param {*} prefix 要删除的字符串
|
||||
* @returns
|
||||
*/
|
||||
export function checkStringValueDeletePrefix(value: string, prefix: string): string {
|
||||
// 增加一个判断,当前删除的数量是不是大于字符串的长度
|
||||
if (value && value !== null && value !== undefined && value !== '') {
|
||||
if (prefix.length > value.length) {
|
||||
return ''
|
||||
} else {
|
||||
return value.slice(prefix.length)
|
||||
}
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
export { successMessage, errorMessage, infoMessage, warningMessage, SendReturnMessage }
|
||||
Reference in New Issue
Block a user