50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
const { Service } = require('egg');
|
|
|
|
const sha256 = require("crypto-js/sha256");
|
|
|
|
const CryptoJS = require("crypto-js");
|
|
|
|
const KEY = CryptoJS.enc.Utf8.parse('BDQH');
|
|
|
|
class BaseService extends Service {
|
|
shaForm = (body, salt) => {
|
|
return sha256(sha256(body).toString() + salt).toString();
|
|
}
|
|
|
|
encryptRequest = (body) => {
|
|
return CryptoJS.AES.encrypt(body, KEY, {
|
|
mode: CryptoJS.mode.ECB,
|
|
padding: CryptoJS.pad.Pkcs7
|
|
}).toString();
|
|
}
|
|
|
|
decryptRequest = (body) => {
|
|
return CryptoJS.AES.decrypt(body, KEY, {
|
|
mode: CryptoJS.mode.ECB,
|
|
padding: CryptoJS.pad.Pkcs7
|
|
}).toString(CryptoJS.enc.Utf8);
|
|
}
|
|
|
|
opOptions = (attributes, searchOptions, substring, key, between, sortOptions) => {
|
|
let where = {}, order = [];
|
|
attributes.forEach(element => {
|
|
if (searchOptions[element]) {
|
|
if (substring.includes(element)) {
|
|
where[element] = { [Op.substring]: searchOptions[element] };
|
|
} else if (key.includes(element)) {
|
|
where[element] = searchOptions[element];
|
|
} else if (between.includes(element)) {
|
|
where[element] = { [Op.between]: [searchOptions[element][0], searchOptions[element][1]] };
|
|
}
|
|
}
|
|
if (!sortOptions[element]) {
|
|
order.push([element, 'DESC']);
|
|
} else if (sortOptions[element]) {
|
|
order.push([element, 'ASC']);
|
|
}
|
|
});
|
|
return { where, order }
|
|
}
|
|
}
|
|
|
|
module.exports = BaseService; |