import Vue from 'vue'
import Cookies from 'js-cookie'
import CryptoJS from 'crypto-js'

const TokenKey = 'vue-bolg-template-token' // Token键值
const cookieTime = 7 // cookie过期时间

const cache = {
    // token
    getToken: function () {
        return Cookies.get(TokenKey)
    },
    setToken: function (token) {
        return Cookies.set(TokenKey, token, { expires: cookieTime })
    },
    removeToken: function () {
        return Cookies.remove(TokenKey)
    },
    // cookie
    getCookie: function (name) {
        return Cookies.get(name)
    },
    setCookie: function (name, val) {
        return Cookies.set(name, val, { expires: cookieTime })
    },
    removeCookie: function (name) {
        return Cookies.remove(name)
    },
    // seesion
    getSession: function (name) {
        if (!name) return
        return window.sessionStorage.getItem(name)
    },
    setSession: function (name, content) {
        if (!name) return
        if (typeof content !== 'string') {
            content = JSON.stringify(content)
        }
        window.sessionStorage.setItem(name, content)
    },
    removeSession: function (name) {
        if (!name) return
        window.sessionStorage.removeItem(name)
    },
    getLocal: function (name) {
        if (!name) return
        return window.localStorage.getItem(name)
    },
    // local
    setLocal: function (name, content) {
        if (!name) return
        if (typeof content !== 'string') {
            content = JSON.stringify(content)
        }
        window.localStorage.setItem(name, content)
    },
    removeLocal: function (name) {
        if (!name) return
        window.localStorage.removeItem(name)
    },
    // 设置cookie
    setCookieEncryption: function(portId, psw, days) {
        // Encrypt，加密账号密码
        let cipherPortId = CryptoJS.AES.encrypt(
            portId + '', 'secretkey123').toString()
        let cipherPsw = CryptoJS.AES.encrypt(psw + '', 'secretkey123').toString()
        console.log(cipherPortId + '/' + cipherPsw) // 打印一下看看有没有加密成功

        let date = new Date() // 获取时间
        date.setTime(date.getTime() + 24 * 60 * 60 * 1000 * date) // 保存的天数
        // 字符串拼接cookie，为什么这里用了==，因为加密后的字符串也有个=号，影响下面getcookie的字符串切割，你也可以使用更炫酷的符号。
        window.document.cookie =
            'currentPortId' + '==' +
            cipherPortId +
            ';path=/;expires=' +
            date.toGMTString()
        window.document.cookie =
            'password' +
            '==' +
            cipherPsw +
            ';path=/;expires=' +
            date.toGMTString()
    },
    // 读取cookie
    getCookieEncryption: function() {
        let info = {
            username: '',
            password: ''
        }
        if (document.cookie.length > 0) {
            var arr = document.cookie.split('; ') // 这里显示的格式请根据自己的代码更改
            for (var i = 0; i < arr.length; i++) {
                var arr2 = arr[i].split("=="); //根据==切割
                //判断查找相对应的值
                if (arr2[0] == "currentPortId") {
                    // Decrypt，将解密后的内容赋值给账号
                    var bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123");
                    info.username = bytes.toString(CryptoJS.enc.Utf8)-0
                } else if (arr2[0] == "password") {
                    // Decrypt，将解密后的内容赋值给密码
                    var bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123");
                    info.password = bytes.toString(CryptoJS.enc.Utf8)
                }
            }
        return info
        }
    },
    // 清除cookie
    clearCookie: function() {
        this.setCookieEncryption('', '', -1)
    }
}

Vue.prototype.$cache = cache

export default cache
