gocron/public/resource/javascript/main.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-04-07 09:26:46 +00:00
/**
* Created by ouqiang on 2017/4/1.
*/
function Util() {
var util = {};
2017-04-20 01:36:42 +00:00
var SUCCESS = 0; // 操作成功
var FAILURE_MESSAGE = '操作失败';
2017-04-23 06:11:53 +00:00
// ajax成功处理
2017-04-20 01:36:42 +00:00
util.ajaxSuccess = function(response, callback) {
if (response.code === undefined) {
swal(FAILURE_MESSAGE, '服务端返回值无法解析', 'error');
return;
}
if (response.code != SUCCESS) {
swal(FAILURE_MESSAGE, response.message ,'error');
return;
}
2017-04-21 05:36:45 +00:00
if (callback !== undefined) {
callback(response.code, response.message, response.data);
}
2017-04-20 01:36:42 +00:00
};
2017-04-23 06:11:53 +00:00
// ajax错误处理
2017-04-20 01:36:42 +00:00
util.ajaxFailure = function() {
// todo 错误处理
swal(FAILURE_MESSAGE, '未知错误', 'error');
};
2017-04-23 06:11:53 +00:00
// get请求
2017-04-20 01:36:42 +00:00
util.get = function(url, callback) {
$.get(
url,
function(response) {
util.ajaxSuccess(response, callback);
},
'json'
).error(util.ajaxFailure);
};
2017-04-23 06:11:53 +00:00
// post请求
2017-04-20 01:36:42 +00:00
util.post = function(url, params, callback) {
2017-04-07 09:26:46 +00:00
$.post(
url,
2017-04-23 06:11:53 +00:00
util.objectTrim(params),
2017-04-07 09:26:46 +00:00
function(response) {
2017-04-20 01:36:42 +00:00
util.ajaxSuccess(response, callback);
2017-04-07 09:26:46 +00:00
},
'json'
2017-04-20 01:36:42 +00:00
).error(util.ajaxFailure);
2017-04-07 09:26:46 +00:00
};
2017-04-23 06:11:53 +00:00
// 弹出确认框
2017-04-13 09:35:59 +00:00
util.confirm = function(message, callback) {
swal({
title: '操作确认',
text: message,
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
confirmButtonText: '删除',
cancelButtonColor: '#d33',
cancelButtonText: "取消",
closeOnConfirm: false,
closeOnCancel: true
},
function(isConfirm) {
if (!isConfirm) {
return;
}
callback();
}
);
};
2017-04-23 06:11:53 +00:00
// 删除确认
2017-04-13 09:35:59 +00:00
util.removeConfirm = function(url) {
util.confirm("确定要删除吗?", function () {
util.post(url, {}, function () {
location.reload();
});
});
};
2017-04-07 09:26:46 +00:00
2017-04-23 06:11:53 +00:00
// 剔除对象元素首尾空格
util.objectTrim = function(fields) {
for (key in fields) {
fields[key] = $.trim(fields[key]);
}
return fields;
};
2017-04-07 09:26:46 +00:00
return util;
2017-04-13 09:35:59 +00:00
}
2017-04-23 06:11:53 +00:00
// 验证select关联字段
function registerSelectFormValidation(type, $form, $select, selectName) {
$.fn.form.settings.rules[type] = function(value) {
var success = true;
var selectedIndex = $($form).form("get value", selectName);
$($select).find("option").each(function() {
var value = $(this).val();
var match = $(this).data("match");
var validateType = $(this).data("validate-type");
if (selectedIndex == value && validateType == type && match) {
var matchValue = $($form).form("get value", match);
if (!$.trim(matchValue)) {
success = false;
return false;
}
}
});
return success;
};
}
2017-04-13 09:35:59 +00:00
var util = new Util();