

import axios from 'axios';
import {Base64} from "js-base64";
import CryptoJS from 'crypto-js'

let JQ_SECRET = "jq:jqsecret";
let COOKIE = 'bGNnd3lwdF9hdA';
const instance = axios.create({
    baseURL: process.env.VUE_APP_BASE_PATH,
    withCredentials: true,
    timeout: 60000
});
// request 拦截器
instance.interceptors.request.use(
    config => {
        if (config.url == "/authentication/form") {
            let LoginToken = Base64.encode(JQ_SECRET);
            config.headers.Authorization = `Basic ${LoginToken}`;
        } else {
            let token = getCookie(COOKIE);
            config.headers.Authorization = `bearer ${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    })

export const singleLogin = data => {
    return instance({
        url: "/authentication/form",
        method: "post",
        data
    });
};

export const aesEncrypt = word => {
    let KEY_WORD='XwKsGlMcdPMEhR1B';
    var key = CryptoJS.enc.Utf8.parse(KEY_WORD);
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
    return encrypted.toString();
}

