HM-SPMS/web/nginx/html/js/common.js

271 lines
6.3 KiB
JavaScript
Raw Normal View History

2026-01-06 04:47:40 +08:00
$.ajaxSetup({
error : function(xhr, textStatus, errorThrown) {
var status = xhr.status; // http status
var msg = xhr.responseText;
var json = xhr.responseJSON;
var message = "";
if (status == 400) {
console.error(message);
} else if (status == 401) {
console.error('access_token过期');
// sessionStorage.removeItem("access_token");
// sessionStorage.removeItem("refresh_token");
location.href = '/';
} else if (status == 403) {
message = "未授权";
console.error(message);
} else if (status == 404) {
if(msg.path== "/login"){
location.href = '/';
}
} else if (status == 500) {
message = '系统错误:' + message + ',请刷新页面,或者联系管理员';
console.error(message);
} else if (status == 504) {
message = '服务器关闭或请求超时:' + message;
console.error(message);
}else{
if(msg != undefined && msg != ""){
try {
var response = JSON.parse(msg);
var exception = response.exception;
} catch (error) {
var exception = msg;
}
if(exception){
message = exception;
}else{
message = response.message;
}
}
}
}
});
$.i18n=function(opt){
if(!opt){opt={}}
if(opt.language===undefined){
opt.language=localStorage.getItem('i18n');
if(!opt.language){
opt.language=navigator.language=='zh-CN'?'cn':'en';
}
}
if(typeof opt.url!=='string'||opt.url==''){
opt.url='/locales/'+opt.language+'/translation.json'
}
$.ajax({
url:opt.url,
type:'get',
dataType:'json',
success:function(res){
var temp=res;
$('[data-i18n]').each(function(i,e){
var p=$(e).attr('data-i18n').split(/[\]\.]/);
if(p[0].indexOf('[')===-1){
p.unshift('');
}
var len=p.length;
var d=temp[p[1]];
while(d[p[p.length+1-(--len)]]){
d=d[p[p.length+1-len]];
}
if(typeof d!=='string'&&typeof d!=='number'){d='';}
p[0]==='[value'?$(e).val(d):p[0]===''?$(e).html(d):$(e).attr(p[0].substring(1),d);
});
if(typeof opt.finish==='function'){
opt.finish(temp);
}else{
$.i18nData=temp;
}
}
});
}
function iframeSrc(id,src){
document.getElementById(id).setAttribute('src',src);
}
function getLocal(){
return window.navData===undefined?parent.location:location;
}
function initSocket(obj) {
var defaul = {
url: 'websocket',
flag: 1,
socket: '',
Timer: '',
times:5,
data:{},
autoReConnect: 5,
//关闭连接
closeWebSocket: function () {
this.socket.close();
},
//发送消息
send: function (message) {
this.data=message;
message.flag=1;
this.socket.send(JSON.stringify(message));
},
//发送心跳包
heatPack: function () {
var me=this;
this.Timer = setTimeout(function () {
me.socket.send(JSON.stringify({flag: 0}));
}, 60000);
},
connection: function () {
try {
var me=this;
if (typeof (WebSocket) == "undefined") {
console.log("您的浏览器不支持WebSocket");
} else {
//获取域名和端口
var _local_ = getLocal().href.split('//')[1].split('/')[0];
//实现化WebSocket对象指定要连接的服务器地址与端口 建立连接
var protocol=window.location.protocol==='https:'?'wss://':'ws://';
me.socket = new WebSocket(protocol + _local_ + "/ws/" + me.url);
// me.socket = new WebSocket( "ws://192.168.77.147:1120/ws/" + me.url);
//打开事件
me.socket.onopen = function () {
// me.times=me.autoReConnect;
me.open(me);
};
//获得消息事件
me.socket.onmessage = function (msg) {
//清除上一个心跳任务,添加下一个心跳任务
clearTimeout(me.Timer);
me.heatPack();
var data = new Function('return '+msg.data)();
if (data.flag != 0) {
me.times=me.autoReConnect;
me.success(data);
}
//发现消息进入 调后台获取
//getCallingList();
};
//关闭事件
me.socket.onclose = function () {
if(--me.times>=0){
setTimeout(function(){
me.reconn();
},8000)
}
me.close();
};
//发生了错误事件
me.socket.onerror = function () {
console.log('err')
me.error();
}
}
} catch (error) {
console.error(error)
}
},
open:function(){
var me=this;
me.send(me.data);
},
close:function(){
},
error:function(){
},
reconn:function(){
var me=this;
if(me.socket.readyState<1.5){
me.closeWebSocket();
}
// me.open=function(){
// me.send(me.data);
// }
me.socket='';
me.connection();
},
success: function (msg) {
console.log(msg)
}
}
Object.assign(defaul, obj);
defaul.connection();
return defaul;
}
function offCheckAll(name){
$('input[name="'+name+'"]').attr('checked',false)
}
/**
* @desc 图表展示数据通用过滤方法
* @param param 入参对象
* key 默认值:false, 操作字段的键名,当该值不为字符串时,默认取数组每个下标
* sub 默认值:'', 不是数字类型的字段用该值替换
* decimal 默认值:0, 保留小数位数
* strict 默认值:false, 是否强制保留小数位数
* offset 默认值:0, 偏移量
* magni 默认值:1 倍率
* @return 返回一个对象,包括:
* arr 转换后的数组,
* max 最大值,
* min 最小值,
* sum 求和,
* avglen 有数值的个数.
*/
Array.prototype.itemToNumber=function(param){
if(typeof param!=='object'){param={}}
var defalut={
key:false, //操作字段的键名,当该值不为字符串时,默认取数组每个下标
sub:'', //不是数字类型的字段用该值替换
decimal:0, //保留小数位数
strict:false, //是否强制保留小数位数
offset:0, //偏移量
magni:1 //倍率
}
var o=Object.assign(defalut,param);
for(var i=0,arr=[],max,min,sum,len=0;i<this.length;i++){
var d=this[i];
if(typeof o.key==='string'){
d=d[o.key]
}
if(d===null||d===''||isNaN(d)){
d=o.sub;
}else{
d=Number(d*o.magni+o.offset).toFixed(o.decimal);
if(!o.strict){
d=Number(d);
}
if(i===0){
max=d;
min=d;
sum=Number(d);
len++;
}else{
if(Number(d)>max){
max=d;
}else if(Number(d)<min){
min=d;
}
sum+=Number(d);
len++;
}
}
arr.push(d);
}
return {
arr:arr,
max:max,
min:min,
sum:sum,
avglen:len
}
}