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-30 17:12:07 +00:00
|
|
|
util.alertSuccess = function() {
|
|
|
|
swal("操作成功", '保存成功', 'success');
|
|
|
|
};
|
2017-04-23 06:11:53 +00:00
|
|
|
// ajax成功处理
|
2017-09-02 03:43:33 +00:00
|
|
|
util.ajaxSuccess = function(response, callback, failureCallback) {
|
2017-04-20 01:36:42 +00:00
|
|
|
if (response.code === undefined) {
|
|
|
|
swal(FAILURE_MESSAGE, '服务端返回值无法解析', 'error');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (response.code != SUCCESS) {
|
|
|
|
swal(FAILURE_MESSAGE, response.message ,'error');
|
2017-09-02 03:43:33 +00:00
|
|
|
if (failureCallback !== undefined) {
|
|
|
|
failureCallback(response.code, response.message);
|
|
|
|
}
|
2017-04-20 01:36:42 +00:00
|
|
|
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 错误处理
|
2017-06-21 11:03:22 +00:00
|
|
|
swal(FAILURE_MESSAGE, '操作失败', 'error');
|
2017-04-20 01:36:42 +00:00
|
|
|
};
|
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-09-02 03:43:33 +00:00
|
|
|
util.post = function(url, params, callback, failureCallback) {
|
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-09-02 03:43:33 +00:00
|
|
|
util.ajaxSuccess(response, callback, failureCallback);
|
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',
|
2017-09-05 14:31:16 +00:00
|
|
|
confirmButtonText: '确定',
|
2017-04-13 09:35:59 +00:00
|
|
|
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-30 17:12:07 +00:00
|
|
|
util.renderTemplate = function($element, data) {
|
|
|
|
var template = Handlebars.compile($($element).html());
|
|
|
|
var html = template(data);
|
|
|
|
|
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
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关联字段
|
2017-04-22 15:39:33 +00:00
|
|
|
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();
|