//系統ajax(有sweetalert)
function system_ajax_with_alert(
sendUri,
sendData,
sendType,
exec_callback,
fail_callback,
) {
$.ajax({
url: sendUri,
data: sendData,
type: sendType,
success: function (data) {
console.log(data);
HoldOn.close();
if (data.result == 1) {
swal(
{
title: "操作成功",
text: data.text,
type: "success",
confirmButtonText: "確認",
},
function () {
exec_callback(data);
},
);
} else {
swal(
{
title: "操作失敗",
text: data.text,
type: "error",
confirmButtonText: "確認",
},
function () {
fail_callback(data);
},
);
}
},
beforeSend: function () {
HoldOn.open({
theme: "sk-bounce",
message: "
Loading
",
});
},
error: function (xhr) {
console.log(xhr);
HoldOn.close();
swal("Error", "系統發生錯誤,請聯繫工程人員", "error");
},
});
}
//系統ajax(成功無sweetalert)
function system_ajax_no_alert(
sendUri,
sendData,
sendType,
exec_callback,
fail_callback,
) {
$.ajax({
url: sendUri,
data: sendData,
type: sendType,
success: function (data) {
console.log(data);
HoldOn.close();
if (data.result == 1) {
exec_callback(data);
} else {
swal(
{
title: "操作失敗",
text: data.text,
type: "error",
confirmButtonText: "確認",
},
function () {
fail_callback(data);
},
);
}
},
beforeSend: function () {
HoldOn.open({
theme: "sk-bounce",
message: "Loading
",
});
},
error: function (xhr) {
console.log(xhr);
HoldOn.close();
swal("Error", "系統發生錯誤,請聯繫工程人員", "error");
},
});
}
//系統ajax(無loading)
function system_ajax_no_alert_and_loading(
sendUri,
sendData,
sendType,
exec_callback,
fail_callback,
) {
$.ajax({
url: sendUri,
data: sendData,
type: sendType,
success: function (data) {
console.log(data);
if (data.result == 1) {
exec_callback(data);
} else {
swal(
{
title: "操作失敗",
text: data.text,
type: "error",
confirmButtonText: "確認",
},
function () {
fail_callback(data);
},
);
}
},
error: function (xhr) {
console.log(xhr);
swal("Error", "系統發生錯誤,請聯繫工程人員", "error");
},
});
}
//系統ajax
function system_ajax(
sendUri,
sendData,
sendType,
exec_callback,
fail_callback,
) {
$.ajax({
url: sendUri,
data: sendData,
type: sendType,
success: function (data) {
console.log(data);
//HoldOn.close();
if (data.result == 1) {
swal(
{
title: "操作成功",
text: "",
type: "success",
confirmButtonText: "確認",
},
function () {
exec_callback(data);
},
);
} else {
swal(
{
title: "操作失敗",
text: data.text,
type: "error",
confirmButtonText: "確認",
},
function () {
fail_callback(data);
},
);
}
},
beforeSend: function () {
/*HoldOn.open({
theme: 'sk-bounce',
message: "Loading
"
});*/
},
error: function (xhr) {
console.log(xhr);
//HoldOn.close();
swal("Error", "系統發生錯誤,請聯繫工程人員", "error");
},
});
}
//date format validate
function dateValidationCheck(dateString) {
var regEx = /^\d{4}-\d{2}-\d{2}$/;
if (!dateString.match(regEx)) {
return false;
} else {
return true;
}
}
//千分位
function numberWithCommas(x) {
result = x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
if (result && result != "NaN") {
return result;
}
return "";
}
// bootstrap tooltips
$(function () {
$('[data-toggle="tooltip"]').tooltip();
});
//檢查webview
function checkIfWebview() {
var u = navigator.userAgent,
ua = navigator.userAgent.toLowerCase(),
isLineApp = u.indexOf("Line") > -1, // Line 內建瀏覽器
isFbApp = u.indexOf("FBAN") > -1; // FB App 內建瀏覽器
// 如果是 Line 內建瀏覽器
if (isLineApp || isFbApp) {
return true;
}
return false;
}
function lineRedirect() {
// 沒有包含參數就重新導向
// 網址沒有參數 ? 時, 直接加參數
if (location.href.indexOf("?") < 0) {
location.href += "?openExternalBrowser=1";
} else {
// 網址有參數 ? 時, 用 & 加參數
location.href += "&openExternalBrowser=1";
}
}
function fbRedirect(u) {
// 重新導向用chrome開啟網頁
if (u.indexOf("iPhone")) {
location.href =
"googlechrome://" +
location.hostname +
location.pathname +
location.search;
} else if (u.indexOf("Android")) {
location.href =
"googlechrome://navigate?url=" +
location.hostname +
location.pathname +
location.search;
}
}
function webviewRedirect() {
var u = navigator.userAgent,
ua = navigator.userAgent.toLowerCase(),
isLineApp = u.indexOf("Line") > -1, // Line 內建瀏覽器
isFbApp = u.indexOf("FBAN") > -1; // FB App 內建瀏覽器
// 如果是 Line 內建瀏覽器
if (isLineApp) {
lineRedirect();
}
// 如果是 FB App 內建瀏覽器
if (isFbApp) {
fbRedirect(u);
}
}
// LINE:自動導向外部瀏覽器;FB 等其他 webview:swal 提示手動切換
// 回傳 true 表示已攔截(呼叫端可 return false 中止後續動作)
function handleWebviewOnLoad() {
var u = navigator.userAgent;
var isLineApp = u.indexOf("Line") > -1;
var isFbApp = u.indexOf("FBAN") > -1;
if (isLineApp) {
lineRedirect();
return true;
}
if (isFbApp) {
swal({
title: "提醒",
text: "您目前使用 Facebook 內建瀏覽器,購物時可能發生錯誤,請點擊右上角「⋯」選單,選擇「在瀏覽器中開啟」後繼續購物。",
type: "warning",
confirmButtonText: "我知道了",
});
return true;
}
return false;
}
function deleteConfirm(
url,
sendData,
name,
del = true,
onSuccess = null,
onCancel = null,
) {
var content = del == true ? "確認刪除 " + name + " ?" : name;
$.confirm({
title: false,
content: content,
alignMiddle: true,
columnClass: "col-md-6 offset-md-3",
confirm: function (button) {
system_ajax(
url,
sendData,
"POST",
function (data) {
if (onSuccess) {
onSuccess(data);
} else {
window.location.reload();
}
},
function (data) {
if (onCancel) {
onCancel(data);
}
},
);
},
cancel: function (button) {},
confirmButton: "確認",
confirmButtonClass: "btn btn-primary",
cancelButton: "取消",
cancelButtonClass: "btn btn-secondary",
});
}
/**
* 通知對話框(成功/失敗)
* @param {string} message - 通知消息
* @param {string} type - 類型:'success' 或 'error'
*/
function showNotification(message, type) {
var alertType = type === "success" ? "success" : "error";
$.confirm({
title: type === "success" ? "成功" : "失敗",
content: message,
alignMiddle: true,
columnClass: "col-md-6 offset-md-3",
buttons: {
confirm: {
text: "確認",
btnClass: "btn btn-primary",
},
},
});
}
/**
* 初始化日期選擇器(含今日藍色高亮樣式)
* @param {string|jQuery} selector - 選擇器或 jQuery 對象
* @param {object} options - 日期選擇器選項(可選)
*/
function initializeDatepicker(selector, options = {}) {
// 注入樣式(如果尚未注入)
if (!document.getElementById("datepicker-today-style")) {
const style = document.createElement("style");
style.id = "datepicker-today-style";
style.textContent = `
.datepicker .today {
background-color: #007bff !important;
color: white;
}
`;
document.head.appendChild(style);
}
// 預設配置
const defaultOptions = {
format: "yyyy-mm-dd",
language: "zh-TW",
autoclose: true,
todayHighlight: true,
};
// 合併選項
const mergedOptions = Object.assign({}, defaultOptions, options);
// 初始化日期選擇器
$(selector).datepicker(mergedOptions);
}