chushih
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
// 此vm参数为页面的实例,可以通过它引用vuex中的变量
|
||||
import Vue from "vue";
|
||||
import $store from "@/store";
|
||||
import { encrypt, decrypt } from "./secret";
|
||||
import md5 from "js-md5";
|
||||
const http = (api, method, params, options) => {
|
||||
let header = {
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
};
|
||||
let url = Vue.prototype.$baseApi.base;
|
||||
let timeout = 10000;
|
||||
let dataType = "json";
|
||||
let data = params ? params : {};
|
||||
|
||||
if (options && options.gapi) {
|
||||
url = Vue.prototype.$baseApi.gapi;
|
||||
//默认跳转语言
|
||||
const language = localStorage.getItem("language") || "en";
|
||||
let lang = "zh-CN";
|
||||
if (language == "tw") {
|
||||
lang = "zh-CN";
|
||||
} else if (language == "en") {
|
||||
lang = "en-US";
|
||||
} else if (language == "yn") {
|
||||
lang = "vi-VN";
|
||||
} else if (language == "kr") {
|
||||
lang = "en-US";
|
||||
} else {
|
||||
lang = "zh-CN";
|
||||
}
|
||||
const gapiParams = {
|
||||
account: $store.state.app.userInfo.username,
|
||||
lang: lang,
|
||||
time: Math.round(new Date().getTime() / 1000).toString(),
|
||||
...params,
|
||||
};
|
||||
const newkey = Object.keys(gapiParams).sort();
|
||||
let sign = "";
|
||||
newkey.forEach((key) => {
|
||||
sign += `${key}=${gapiParams[key]}`;
|
||||
});
|
||||
data = {
|
||||
...gapiParams,
|
||||
sign: md5(sign + "354b335dd5dbc6740a8a55d4461249b9"),
|
||||
};
|
||||
} else {
|
||||
params.language = localStorage.getItem("language") || "en";
|
||||
const encryptData = encrypt(JSON.stringify(params)).toString();
|
||||
data = { encryptData };
|
||||
}
|
||||
if (options) {
|
||||
if (options.api) {
|
||||
url = Vue.prototype.$baseApi[options.api];
|
||||
}
|
||||
if (options.header) {
|
||||
header = options.header;
|
||||
}
|
||||
if (options.dataType) {
|
||||
dataType = options.dataType;
|
||||
}
|
||||
if (typeof options.timeout === "number") {
|
||||
timeout = options.timeout;
|
||||
}
|
||||
}
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
header: header,
|
||||
url: url + api,
|
||||
method: method,
|
||||
timeout: timeout,
|
||||
dataType: dataType,
|
||||
data: data,
|
||||
success: (res) => {
|
||||
switch (res.statusCode) {
|
||||
case 200:
|
||||
if (res.data) {
|
||||
if (options && options.gapi) {
|
||||
// console.log(res)
|
||||
} else {
|
||||
res.data.Data = res.data.Data
|
||||
? JSON.parse(decrypt(res.data.Data))
|
||||
: {};
|
||||
}
|
||||
} else {
|
||||
res.data = {};
|
||||
}
|
||||
resolve(res.data);
|
||||
break;
|
||||
case 500:
|
||||
Vue.$toast({
|
||||
message: "服务器繁忙",
|
||||
duration: 2000,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
reject(res.data);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
},
|
||||
});
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
const ajax = {
|
||||
get(api, params, options) {
|
||||
return http(api, "GET", params, options);
|
||||
},
|
||||
post(api, params, options) {
|
||||
return http(api, "POST", params, options);
|
||||
},
|
||||
};
|
||||
export default ajax;
|
||||
Reference in New Issue
Block a user