301 lines
9.9 KiB
JavaScript
301 lines
9.9 KiB
JavaScript
var red = {
|
|
showMsg: function(option) {
|
|
var defOption = {msg: "操作成功", /*type:"success",*/ placement: "top"};
|
|
|
|
option = option || defOption;
|
|
for (var k in defOption) {
|
|
option[k] = option[k] || defOption[k]
|
|
}
|
|
|
|
new $.zui.Messager(option.msg, {
|
|
type: option.type // 定义颜色主题
|
|
,placement: option.placement
|
|
}).show();
|
|
},
|
|
showOk(msg = '操作成功') {
|
|
red.showMsg({msg})
|
|
},
|
|
showError(msg = '操作失败 ') {
|
|
red.showMsg({type:"error", msg})
|
|
},
|
|
getData: function(key, defaultValue) {
|
|
var v = localStorage.getItem(key) || defaultValue || "";
|
|
if (typeof(v) == "string" && v.startsWith("{") && v.endsWith("}")) {
|
|
v = JSON.parse(v);
|
|
} else if (typeof(v) == "string" && v.startsWith("[") && v.endsWith("]")) {
|
|
v = JSON.parse(v);
|
|
}
|
|
return v;
|
|
},
|
|
setData: function(key, value) {
|
|
var v = value;
|
|
if (typeof(v) == "object") {
|
|
v = JSON.stringify(value);
|
|
}
|
|
localStorage.setItem(key, v);
|
|
},
|
|
getPlatId: function() {
|
|
var plat = red.getData("sysPlat") || {};
|
|
return plat["_key"];
|
|
},
|
|
getPlatToken: function() {
|
|
let plat = red.getData("sysPlat");
|
|
/*if (!plat) {
|
|
red.showMsg({type:'error', placement: 'center', msg: '登陆过期,请前往登陆'});
|
|
setTimeout(function () {
|
|
location.href = "/user/login.html";
|
|
}, 2000);
|
|
}*/
|
|
return plat["plattoken"];
|
|
},
|
|
getJSON: function (url, params = {}, callback) {
|
|
params["plattoken"] = red.getPlatToken()
|
|
axios.get(url, {params}).then(res => {
|
|
let data = res.data || {}
|
|
red.loginCheck(data)
|
|
if (data.code == -1) {
|
|
red.showMsg({type:"error", msg: data.message})
|
|
return;
|
|
}
|
|
if (data.code == 0) {
|
|
data = data.body
|
|
}
|
|
callback(data)
|
|
})
|
|
},
|
|
get2(url, params = {}) {
|
|
return new Promise(resolve => {
|
|
// 创建一个包含自定义 headers 的配置对象
|
|
const config = {
|
|
method: 'get', // 指定请求方法为 GET
|
|
url: url, // 指定请求的 URL
|
|
headers: { // 设置 Header 参数
|
|
//'Authorization': 'Bearer your-token', // 假设添加一个 Bearer 认证 token
|
|
//'Content-Type': 'application/json', // 或者指定 Content-Type 等其他 Header
|
|
//'Custom-Header': 'Some value' // 自定义 Header 参数
|
|
'token': red.getData("token"),
|
|
'plattoken': red.getPlatToken(),
|
|
},
|
|
params: params,
|
|
};
|
|
|
|
axios.request(config).then(res => {
|
|
// 未登录,前往登录
|
|
if (res.data.retcode === -2) {
|
|
location.href = "/user/login.html"
|
|
}
|
|
resolve(res.data.result)
|
|
}).catch(error => {
|
|
console.error(error); // 处理错误
|
|
});
|
|
})
|
|
},
|
|
post2(url, params = {}) {
|
|
return new Promise(resolve => {
|
|
// 创建一个包含自定义 headers 的配置对象
|
|
const config = {
|
|
method: 'post', // 指定请求方法为 GET
|
|
url: url, // 指定请求的 URL
|
|
data: params,
|
|
headers: { // 设置 Header 参数
|
|
//'Authorization': 'Bearer your-token', // 假设添加一个 Bearer 认证 token
|
|
//'Content-Type': 'application/json', // 或者指定 Content-Type 等其他 Header
|
|
//'Custom-Header': 'Some value' // 自定义 Header 参数
|
|
'token': red.getData("token"),
|
|
'plattoken': red.getPlatToken(),
|
|
}
|
|
};
|
|
|
|
axios.request(config).then(res => {
|
|
// 未登录,前往登录
|
|
if (res.data.retcode === -2) {
|
|
location.href = "/user/login.html"
|
|
}
|
|
if (res.data.retcode !== 0) {
|
|
//console.log(res)
|
|
red.showError(res.data.retinfo)
|
|
return
|
|
}
|
|
|
|
resolve(res.data.result)
|
|
}).catch(error => {
|
|
console.error(error); // 处理错误
|
|
});
|
|
})
|
|
},
|
|
getX(url, params = {}) {
|
|
if (!params['plattoken'])
|
|
params['plattoken'] = red.getPlatToken()
|
|
return new Promise(resolve => {
|
|
axios.get(url, params).then(res => {
|
|
let data = res.data || {}
|
|
red.loginCheck(data)
|
|
if (data.code == -1) {
|
|
red.showMsg({type:"error", msg: data.message})
|
|
return;
|
|
}
|
|
if (data.code == 0) {
|
|
data = data.body
|
|
}
|
|
|
|
resolve(data)
|
|
}).catch(res => {
|
|
//console.log(res)
|
|
red.showMsg({type:"error", msg:'操作失败!'})
|
|
})
|
|
})
|
|
},
|
|
postX(url, params = {}) {
|
|
if (!params['plattoken'])
|
|
params['plattoken'] = red.getPlatToken()
|
|
return new Promise(resolve => {
|
|
axios({
|
|
url,
|
|
method: 'post',
|
|
data: params,
|
|
transformRequest: [data => {
|
|
let _data = ''
|
|
for (k in data) {
|
|
if (data[k] != undefined) {
|
|
_data += k + '=' + data[k] + '&'
|
|
}
|
|
}
|
|
return _data
|
|
}
|
|
]
|
|
}).then(res => {
|
|
let data = res.data || {}
|
|
red.loginCheck(data)
|
|
if (data.code == -1) {
|
|
red.showMsg({type:"error", msg: data.message})
|
|
return;
|
|
}
|
|
else if (data.code == 0) {
|
|
data = data.body
|
|
}
|
|
resolve(data)
|
|
});
|
|
})
|
|
},
|
|
post: function(url, params = {}, callback) {
|
|
//params['plattoken'] = red.getPlatToken()
|
|
axios.post(url, params).then(res => {
|
|
/*let data = red.loginCheck(res.data)
|
|
if (data && data.code == -1) {
|
|
red.showMsg({msg: data.message, type: "error"})
|
|
return;
|
|
}*/
|
|
|
|
if (callback) {
|
|
console.log("xx", res.data.body)
|
|
callback(res.data.code === 0 ? res.data.body : data)
|
|
} else {
|
|
red.showMsg()
|
|
}
|
|
return res
|
|
})
|
|
},
|
|
|
|
//TODO: 提取统一查询、请求,失败提示
|
|
//QTASK find list
|
|
qtaskCall: function (para, callback) {
|
|
|
|
/*$.p$.post("/db/list", {fBean: JSON.stringify(fBean)}, function (json) {
|
|
vm.list = json.body;
|
|
});*/
|
|
$.getJSON("/qtask/call", para, function (json) {
|
|
callback(json);
|
|
});
|
|
},
|
|
|
|
//db find
|
|
dbQuery: function (para, callback) {
|
|
$.getJSON("/db/list", para, function (json) {
|
|
red.loginCheck(json);
|
|
if (json.code == -1) {
|
|
console.log("json.code == -1")
|
|
}
|
|
|
|
callback(json);
|
|
});
|
|
},
|
|
|
|
putAll: function(t, s) {
|
|
t = t || {};
|
|
s = s || {};
|
|
|
|
for (var k in s) {
|
|
t[k] = s[k];
|
|
}
|
|
return t;
|
|
},
|
|
|
|
timeFmt: function (date,fmt){
|
|
if (!this.isValidDate(date)) {
|
|
return "";
|
|
}
|
|
fmt = fmt || "yyyy-MM-dd HH:mm:ss";
|
|
var o = {
|
|
"M+" : date.getMonth()+1, //月份
|
|
"d+" : date.getDate(), //日
|
|
"H+" : date.getHours(), //小时
|
|
"m+" : date.getMinutes(), //分
|
|
"s+" : date.getSeconds(), //秒
|
|
"q+" : Math.floor((date.getMonth()+3)/3), //季度
|
|
"S" : date.getMilliseconds() //毫秒
|
|
};
|
|
|
|
if(/(y+)/.test(fmt))
|
|
fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
|
|
for(var k in o)
|
|
if(new RegExp("("+ k +")").test(fmt))
|
|
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
|
|
return fmt;
|
|
},
|
|
loginCheck: function (json) {
|
|
if (json && (json['code'] == -2 || json['referid'])) {
|
|
red.showMsg({type:'error', placement: 'center', msg: '登陆过期,请前往登陆'});
|
|
setTimeout(function () {
|
|
location.href = "/user/login.html";
|
|
}, 2000);
|
|
}
|
|
return json.data
|
|
},
|
|
replaceAll: function (d, s, t) {
|
|
let reg=new RegExp(s,"g"); //创建正则RegExp对象
|
|
let str = JSON.stringify(d);
|
|
str = str.replace(reg, t)
|
|
return JSON.parse(str);
|
|
},
|
|
replacePoint: function(d) {
|
|
return red.replaceAll(d, "[.]", "$");
|
|
},
|
|
replace$: function (d) {
|
|
return red.replaceAll(d, "[$]", ".");
|
|
},
|
|
wait(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
},
|
|
deepClone: function (obj) {
|
|
if (obj === null) return obj;
|
|
if (obj instanceof Date) return new Date(obj);
|
|
if (obj instanceof RegExp) return new RegExp(obj);
|
|
if (typeof obj !== 'object') return obj;
|
|
|
|
let cloneObj = new obj.constructor();
|
|
for (let key in obj) {
|
|
if (obj.hasOwnProperty(key)) {
|
|
cloneObj[key] = this.deepClone(obj[key]);
|
|
}
|
|
}
|
|
return cloneObj;
|
|
},
|
|
isValidDate: function (date) {
|
|
return date instanceof Date && !isNaN(date.getTime());
|
|
}
|
|
}
|
|
|
|
String.prototype.replaceAll=function(s,t){
|
|
return red.replaceAll(this, s, t);
|
|
}
|