|
@@ -13,21 +13,20 @@ import { base64ToBlob } from "./file";
|
|
|
import { checkLogin } from "@/api";
|
|
|
import { LoginDetector } from "@/utils/starter";
|
|
|
import { $alert, $confirm } from "@/components/shared/message";
|
|
|
-import {i18n} from '@/lang'
|
|
|
+import { i18n } from "@/lang";
|
|
|
|
|
|
let vue = new Vue();
|
|
|
|
|
|
-
|
|
|
// 空函数
|
|
|
-const noop = function() {};
|
|
|
+const noop = function () {};
|
|
|
// 请求回调队列
|
|
|
let postQueue = [];
|
|
|
|
|
|
// 阻止多个弹窗
|
|
|
-let isDiglog = false
|
|
|
+let isDiglog = false;
|
|
|
|
|
|
const statusCode = {
|
|
|
- NEXT: '__not_important__', //继续执行
|
|
|
+ NEXT: "__not_important__", //继续执行
|
|
|
SUCCESS: 0, //成功
|
|
|
|
|
|
FAILURE_CODE_3001: 3001, // "对象不存在"
|
|
@@ -37,7 +36,6 @@ const statusCode = {
|
|
|
FAILURE_CODE_3006: 3006, //作品已被删除,无法编辑
|
|
|
FAILURE_CODE_3007: 3007, //素材已被引用,无法删除。
|
|
|
|
|
|
-
|
|
|
// 301X -> 权限问题类
|
|
|
FAILURE_CODE_3010: 3010, // "Token为空"
|
|
|
FAILURE_CODE_3011: 3011, // "没有操作权限"
|
|
@@ -80,26 +78,29 @@ export const showLoginTips = () => {
|
|
|
showLoginTips.__is_show = true;
|
|
|
|
|
|
return $confirm({
|
|
|
- title: i18n.t('tips_code.tips'),
|
|
|
- content: i18n.t('tips_code.relogin'),
|
|
|
- okText: i18n.t('tips_code.goto_login'),
|
|
|
- noText: i18n.t('tips_code.login_success'),
|
|
|
+ title: i18n.t("tips_code.tips"),
|
|
|
+ content: i18n.t("tips_code.relogin"),
|
|
|
+ okText: i18n.t("tips_code.goto_login"),
|
|
|
+ noText: i18n.t("tips_code.login_success"),
|
|
|
okLink: "/",
|
|
|
- ok: function() {
|
|
|
+ ok: function () {
|
|
|
showLoginTips.__is_show = false;
|
|
|
return false;
|
|
|
},
|
|
|
- no: function() {
|
|
|
+ no: function () {
|
|
|
checkLogin().then((response) => {
|
|
|
if (response.code === statusCode.SUCCESS) {
|
|
|
postQueue.length && postQueue.forEach((item) => item());
|
|
|
postQueue = [];
|
|
|
LoginDetector.valid();
|
|
|
- location.reload()
|
|
|
- } else if (response.code === statusCode.FAILURE_CODE_5001||response.code === statusCode.FAILURE_CODE_5002) {
|
|
|
+ location.reload();
|
|
|
+ } else if (
|
|
|
+ response.code === statusCode.FAILURE_CODE_5001 ||
|
|
|
+ response.code === statusCode.FAILURE_CODE_5002
|
|
|
+ ) {
|
|
|
showLoginTips();
|
|
|
}
|
|
|
- })
|
|
|
+ });
|
|
|
showLoginTips.__is_show = false;
|
|
|
},
|
|
|
});
|
|
@@ -115,107 +116,105 @@ function getCookie(objname) {
|
|
|
}
|
|
|
|
|
|
export function getToken() {
|
|
|
- return (
|
|
|
- localStorage.getItem("token") || getCookie("fdkankantoken") || ""
|
|
|
- );
|
|
|
+ return localStorage.getItem("token") || getCookie("fdkankantoken") || "";
|
|
|
}
|
|
|
|
|
|
export function statusCodesHandler(result, callback) {
|
|
|
-
|
|
|
-
|
|
|
if (result.code == statusCode.FAILURE_CODE_3002) {
|
|
|
- $alert({ content: i18n.t('tips_code.need_one') });
|
|
|
- return false
|
|
|
+ $alert({ content: i18n.t("tips_code.need_one") });
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
if (result.code == statusCode.FAILURE_CODE_3003) {
|
|
|
- $alert({ content: i18n.t('tips_code.not_less_than') })
|
|
|
- return false
|
|
|
+ $alert({ content: i18n.t("tips_code.not_less_than") });
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
if (result.code == statusCode.FAILURE_CODE_3005) {
|
|
|
- $alert({ content: i18n.t('tips_code.FAILURE_3011') });
|
|
|
- return false
|
|
|
+ $alert({ content: i18n.t("tips_code.FAILURE_3011") });
|
|
|
+ return false;
|
|
|
}
|
|
|
if (result.code == statusCode.FAILURE_CODE_3011) {
|
|
|
- $alert({ content: i18n.t('tips_code.FAILURE_3011') });
|
|
|
- return false
|
|
|
+ $alert({ content: i18n.t("tips_code.FAILURE_3011") });
|
|
|
+ return false;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
if (result.code == statusCode.FAILURE_CODE_3006) {
|
|
|
- $alert({ content: i18n.t('tips_code.work_had_delete') });
|
|
|
- return
|
|
|
+ $alert({ content: i18n.t("tips_code.work_had_delete") });
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
if (result.code == statusCode.FAILURE_CODE_3007) {
|
|
|
- $alert({ content: i18n.t('tips_code.material_can_not_delete') });
|
|
|
- return
|
|
|
+ $alert({ content: i18n.t("tips_code.material_can_not_delete") });
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
if (result.code == statusCode.FAILURE_CODE_3101) {
|
|
|
- $alert({ content: i18n.t('tips_code.FAILURE_3101') });
|
|
|
- return
|
|
|
+ $alert({ content: i18n.t("tips_code.FAILURE_3101") });
|
|
|
+ return;
|
|
|
}
|
|
|
if (result.code == statusCode.FAILURE_CODE_3104) {
|
|
|
- $alert({ content: i18n.t('tips_code.FAILURE_3104') });
|
|
|
- return
|
|
|
+ $alert({ content: i18n.t("tips_code.FAILURE_3104") });
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- if (result.code == statusCode.FAILURE_CODE_5001||result.code === statusCode.FAILURE_CODE_5002) {
|
|
|
+ if (
|
|
|
+ result.code == statusCode.FAILURE_CODE_5001 ||
|
|
|
+ result.code === statusCode.FAILURE_CODE_5002
|
|
|
+ ) {
|
|
|
callback(result.code);
|
|
|
showLoginTips();
|
|
|
- return false
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
if (result.code == statusCode.FAILURE_CODE_5003) {
|
|
|
- $alert({ content: i18n.t('tips_code.illegality_image') })
|
|
|
- return false
|
|
|
+ $alert({ content: i18n.t("tips_code.illegality_image") });
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
if (result.code == statusCode.FAILURE_CODE_5004) {
|
|
|
- vue.$msg.error(i18n.t('tips_code.password_error'));
|
|
|
- return false
|
|
|
+ vue.$msg.error(i18n.t("tips_code.password_error"));
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
if (result.code == statusCode.FAILURE_CODE_3024) {
|
|
|
if (!isDiglog) {
|
|
|
- isDiglog = true
|
|
|
+ isDiglog = true;
|
|
|
$alert({
|
|
|
- content: i18n.t('tips_code.FAILURE_3024'),
|
|
|
+ content: i18n.t("tips_code.FAILURE_3024"),
|
|
|
forceOK: true,
|
|
|
ok: () => {
|
|
|
- isDiglog = false
|
|
|
+ isDiglog = false;
|
|
|
},
|
|
|
});
|
|
|
}
|
|
|
- return false
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- if (result.code != statusCode.SUCCESS) {
|
|
|
+ if (![statusCode.SUCCESS,200].includes(result.code) ) {
|
|
|
console.log(result.msg);
|
|
|
$alert({ content: `${result.msg}` });
|
|
|
- return false
|
|
|
+ return false;
|
|
|
}
|
|
|
return statusCode.NEXT;
|
|
|
}
|
|
|
|
|
|
$.ajaxSetup({
|
|
|
headers: {},
|
|
|
- beforeSend: function(xhr) {
|
|
|
+ beforeSend: function (xhr) {
|
|
|
const token = getToken();
|
|
|
if (token) {
|
|
|
xhr.setRequestHeader("token", token);
|
|
|
}
|
|
|
},
|
|
|
- error: function() {
|
|
|
+ error: function () {
|
|
|
// 出错时默认的处理函数
|
|
|
if (__showNetworkError) {
|
|
|
return;
|
|
|
}
|
|
|
__showNetworkError = true;
|
|
|
$alert({
|
|
|
- content: i18n.t('tips_code.FAILURE_error'),
|
|
|
+ content: i18n.t("tips_code.FAILURE_error"),
|
|
|
forceOK: true,
|
|
|
ok: () => {
|
|
|
__showNetworkError = false;
|
|
@@ -223,9 +222,8 @@ $.ajaxSetup({
|
|
|
});
|
|
|
return;
|
|
|
},
|
|
|
- success: function() {},
|
|
|
- complete: function(data) {
|
|
|
- },
|
|
|
+ success: function () {},
|
|
|
+ complete: function (data) {},
|
|
|
});
|
|
|
|
|
|
export const http = {
|
|
@@ -239,25 +237,28 @@ export const http = {
|
|
|
}
|
|
|
|
|
|
xhr.done((result) => {
|
|
|
- if (typeof result.code !== "undefined") {
|
|
|
- const flag = statusCodesHandler(result, function(code) {
|
|
|
+ if (typeof result.code !== "undefined") {
|
|
|
+ const flag = statusCodesHandler(result, function (code) {
|
|
|
// 需要登录的状态
|
|
|
- if (code == statusCode.FAILURE_CODE_5001||code === statusCode.FAILURE_CODE_5002) {
|
|
|
- postQueue.push(function() {
|
|
|
+ if (
|
|
|
+ code == statusCode.FAILURE_CODE_5001 ||
|
|
|
+ code === statusCode.FAILURE_CODE_5002
|
|
|
+ ) {
|
|
|
+ postQueue.push(function () {
|
|
|
http[method](url, data, done, fail);
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
|
|
|
if (!flag) {
|
|
|
- fail(result)
|
|
|
+ fail(result);
|
|
|
}
|
|
|
|
|
|
if (flag === statusCode.NEXT) {
|
|
|
done(result, result.code == 0);
|
|
|
}
|
|
|
} else {
|
|
|
- console.log('ajax返回数据里没有code.')
|
|
|
+ console.log("ajax返回数据里没有code.");
|
|
|
done(result);
|
|
|
}
|
|
|
});
|
|
@@ -384,6 +385,26 @@ export const http = {
|
|
|
fail
|
|
|
);
|
|
|
},
|
|
|
+ postJsonWithHeader(url, data = {}, headers, done, fail) {
|
|
|
+ return this.__request(
|
|
|
+ $.ajax({
|
|
|
+ type: "POST",
|
|
|
+ url: url,
|
|
|
+ contentType: "application/json",
|
|
|
+ data: JSON.stringify(data),
|
|
|
+ headers: headers,
|
|
|
+ beforeSend: function (xhr) {
|
|
|
+ xhr.headers = null;
|
|
|
+ // console.log(xhr.headers)
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ "postJson",
|
|
|
+ url,
|
|
|
+ data,
|
|
|
+ done,
|
|
|
+ fail
|
|
|
+ );
|
|
|
+ },
|
|
|
/**
|
|
|
* Post 表单 支持文件上传
|
|
|
* @param {String} url 请求地址
|
|
@@ -400,19 +421,18 @@ export const http = {
|
|
|
processData: false,
|
|
|
contentType: false,
|
|
|
data: formData,
|
|
|
- xhr: function() {
|
|
|
+ xhr: function () {
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
- xhr.upload.addEventListener("progress", function(e) {
|
|
|
+ xhr.upload.addEventListener("progress", function (e) {
|
|
|
onProgress(e.loaded / e.total);
|
|
|
});
|
|
|
xhr.onabort = () => {
|
|
|
- console.log('xhr aborted.');
|
|
|
- }
|
|
|
+ console.log("xhr aborted.");
|
|
|
+ };
|
|
|
return xhr;
|
|
|
},
|
|
|
// 覆盖全局配置的失败回调。因为无论是上传子集失败还是用户取消了上传,都不需要弹窗。
|
|
|
- error: function() {
|
|
|
- },
|
|
|
+ error: function () {},
|
|
|
}),
|
|
|
"postForm",
|
|
|
url,
|
|
@@ -454,13 +474,13 @@ export const http = {
|
|
|
.fail(def.reject.bind(def));
|
|
|
};
|
|
|
|
|
|
- img.onerror = function() {
|
|
|
+ img.onerror = function () {
|
|
|
retry > 0
|
|
|
? setTimeout(() => load(), 1e3)
|
|
|
- : def.reject(`[${url}]${i18n.t('tips_code.loading_fail')}`);
|
|
|
+ : def.reject(`[${url}]${i18n.t("tips_code.loading_fail")}`);
|
|
|
};
|
|
|
|
|
|
- img.onload = function() {
|
|
|
+ img.onload = function () {
|
|
|
def.resolve(img);
|
|
|
};
|
|
|
|