xg-project-library/js/common.js
2024-11-04 16:40:52 +08:00

103 lines
3.1 KiB
JavaScript

const projectBaseURL = "https://offer.gter.net"
if (["localhost", "127.0.0.1"].includes(location.hostname)) {
axios.defaults.headers = {
"Authorization": "i0pmeq8tsx1orf5q8ywr5p0p4nr1j6b4",
}
}
function $ajax(url) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}
url = url.indexOf("https://") > -1 ? url : projectBaseURL + url
data["authorization"] = "i0pmeq8tsx1orf5q8ywr5p0p4nr1j6b4"
return new Promise(function (resolve, reject) {
axios
.post(url, data, {
emulateJSON: true,
withCredentials: true,
})
.then(function (res) {
var data = typeof res.data == "string" ? JSON.parse(res.data) : res.data
if (data.code == 401) reject()
resolve(data)
})
.catch(err => {
if (err.response.status == 401)
showWindow("login", "https://passport.gter.net/login/ajax", "get", -1, {
cover: true,
})
})
})
}
function $ajaxget(url, data) {
url = url.indexOf("https://") > -1 ? url : projectBaseURL + url
url += objectToQueryString(data)
return new Promise(function (resolve, reject) {
axios
.get(
url,
{},
{
emulateJSON: true,
withCredentials: true,
}
)
.then(function (res) {
var data = typeof res.data == "string" ? JSON.parse(res.data) : res.data
if (data.code == 401) reject()
resolve(data)
})
.catch(error => {
reject(error)
})
})
}
function randomString(n) {
let str = "abcdefghijklmnopqrstuvwxyz9876543210"
let tmp = "",
i = 0,
l = str.length
for (i = 0; i < n; i++) {
tmp += str.charAt(Math.floor(Math.random() * l))
}
return tmp
}
function formatNumberWithSpaces(number) {
if (typeof number != "string") number = ""
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
}
// 将一个JavaScript对象转换为路由参数的形式将键值对转换为key=value的形式
function objectToQueryString(obj = {}) {
const queryString = Object.keys(obj)
.map(key => encodeURIComponent(key) + "=" + encodeURIComponent(obj[key]))
.join("&")
return queryString ? "?" + queryString : ""
}
function decodeKey(encrypted, key) {
return new Promise((resolve, reject) => {
try {
var decodekey = "894e129983f3a898"
const encryptedData = CryptoJS.enc.Base64.parse(encrypted)
const decrypted = CryptoJS.AES.decrypt({
ciphertext: encryptedData
}, CryptoJS.enc.Utf8.parse(decodekey), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
})
const text = JSON.parse(decrypted.toString(CryptoJS.enc.Utf8))
resolve(text)
} catch (error) {
reject()
}
})
}