ibms-dome/FrontendWebApi/wwwroot/js/site.js

264 lines
7.6 KiB
JavaScript

$(function () {
$.extend(true, $.fn.dataTable.defaults, {
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": true,
"info": true,
"autoWidth": false,
"responsive": true,
"language": {
"emptyTable": "無資料...",
"processing": "處理中...",
"loadingRecords": "載入中...",
"lengthMenu": "顯示 _MENU_ 項結果",
"zeroRecords": "沒有符合的結果",
"info": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項",
"infoEmpty": "顯示第 0 至 0 項結果,共 0 項",
"infoFiltered": "(從 _MAX_ 項結果中過濾)",
"infoPostFix": "",
"search": "搜尋:",
"paginate": {
"first": "第一頁",
"previous": "上一頁",
"next": "下一頁",
"last": "最後一頁"
},
"aria": {
"sortAscending": ": 升冪排列",
"sortDescending": ": 降冪排列"
}
}
});
//驗證是否為空白
jQuery.validator.addMethod("filterspace", function (value, element) {
var str = value;
if (str.indexOf(" ") > -1) {
return false;
} else {
return true;
}
}, "不可包含空白");
//加入regex用法
$.validator.addMethod(
"regex",
function (value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please check your input."
);
})
//#region 個人資訊表單驗證
$(function () {
$.ajaxSetup({
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", 'Bearer ' + localStorage.getItem('JWT-Authorization'));
},
xhrFields: {
withCredentials: true
},
//完成请求后触发。即在success或error触发后触发
complete: function (XMLHttpRequest, status) {
if ('499' == XMLHttpRequest.status) {
window.location.href = "/Login/Index";
}
},
})
$("#personal-info-form").validate({
rules: {
name_modal: {
required: true,
maxlength: 50
},
email_modal: {
email: true,
},
},
});
$("#btn-personal-info").click(function (e) {
var url = "/User/GetPersonalInfo"
$.post(url, null, function (rel) {
if (rel.code != "0000") {
toast_error(rel.msg);
}
$("#name_modal").val(rel.data.name);
$("#account_modal").val(rel.data.account);
$("#email_modal").val(rel.data.email);
$("#phone_modal").val(rel.data.phone);
$('#personal-info-modal').modal();
}, 'json');
});
})
//#endregion
//#region 修改個人資料
function SavePersonalInfo() {
if ($("#personal-info-form").valid()) {
var url = "/User/SavePersonalInfo";
var send_data = {
Name: $("#name_modal").val(),
Account: $("#account_modal").val(),
Email: $("#email_modal").val(),
Phone: $("#phone_modal").val()
}
$.post(url, send_data, function (rel) {
if (rel.code == "9999") {
toast_error(rel.msg);
return;
}
else if (rel.code == "9998") {
toast_error(rel.msg);
setTimeout(function () {
location.href = "/Login/Logout";
}, 5000)
}
toast_ok(rel.msg);
$('#personal-info-modal').modal('hide');
}, 'json');
}
}
//#endregion
//#region 變更密碼表單驗證
$(function () {
jQuery.validator.addMethod("pwcheck", function (value, element) {
var xxx = /^[A-Za-z0-9\d!@#$%^&*()_=|]*$/.test(value);
return xxx;// consists of only these
//&& /[a-z]/.test(value) // has a lowercase letter
//&& /\d/.test(value) // has a digit
});
$("#change-password-form").validate({
rules: {
old_password_modal: {
required: true,
//rangelength: [6, 12]
},
new_password_modal: {
required: true,
rangelength: [6, 12],
pwcheck: true
},
again_password_modal: {
required: true,
rangelength: [6, 12],
pwcheck: true,
equalTo: "#new_password_id_modal",
}
},
messages: {
new_password_modal: {
pwcheck: "密碼格式錯誤,格式需為大小寫英文或數字或特殊符號"
},
again_password_modal: {
pwcheck: "密碼格式錯誤,格式需為大小寫英文或數字或特殊符號",
equalTo: "兩次密碼輸入不同"
}
}
});
$("#btn-change-password").click(function (e) {
$("#change-password-form").trigger("reset");
$('#change-password-modal').modal();
});
})
//#endregion
//#region 變更密碼
function ChangePassword() {
if ($("#change-password-form").valid()) {
var url = "/User/ChangePassword";
var send_data = {
OldPassword: $("#old_password_id_modal").val(),
NewPassword: $("#new_password_id_modal").val(),
AgainPassword: $("#again_password_id_modal").val()
}
$.post(url, send_data, function (rel) {
if (rel.code == "9999") {
toast_error(rel.msg);
return;
}
else if (rel.code == "9998") {
toast_error(rel.msg);
setTimeout(function () {
location.href = "/Login/Logout";
}, 5000)
return;
} else if (rel.code == "0001") {
toast_error(rel.msg);
return;
}
toast_ok(rel.msg);
$('#change-password-modal').modal('hide');
}, 'json');
}
}
//#endregion
function SaveSpinner(status) { //1:Spinner 0:還原儲存
if (status == 1) {
$('.modal-footer').find('.btn-primary').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>').attr("disabled", true);
} else {
$('.modal-footer').find('.btn-primary').html('儲存').attr("disabled", false);
}
}
/**
* 下載檔案
*/
function download(res, blob, filename, fetchOption, callback = null) {
// 擷取回傳 header > content-disposition (後端定義檔案名稱)
let disposition = res.getResponseHeader("Content-Disposition");
let sDowName = "";
if (disposition) {
// 解析出名稱
sDowName = disposition.split(/filename\*=(?:utf-8'')?(.*)/gi)[1];
if (sDowName.toLowerCase().startsWith("utf-8''"))
sDowName = decodeURIComponent(sDowName.replace(/utf-8''/i, ""));
else sDowName = sDowName.replace(/['"]/g, "");
sDowName = decodeURIComponent(sDowName);
}
// 隱形a標籤
const a = document.createElement("a");
let href = URL.createObjectURL(blob);
if (href) {
a.href = href;
a.download = filename || sDowName || "";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
callback ? callback() : "";
return true;
} else {
return false;
}
}
$(document).ready(function () {
});