<template>
    <div>
        <!-- 对外接口 {{ decrypted }} -->
        <el-button type="primary" @click="updateAk()" style="margin-bottom: 20px;">更新秘钥</el-button>
        <el-table v-loading="loading" :data="tableData" style="width: 100%" border
            :header-cell-style="{ background: '#f5f7fa' }">
            <!-- 解密后的秘钥 -->
            <el-table-column prop="decryptedKey" label="秘钥" align="center">
                <template slot-scope="scope">
                    {{ hideSensitiveInfo(scope.row.decryptedKey) }}
                </template>
            </el-table-column>
            <!-- <el-table-column prop="k" label="解密秘钥" align="center">
            </el-table-column> -->
            <el-table-column label="操作" align="center">
                <el-link type="primary" @click="copy()">复制秘钥</el-link>
                <el-link type="primary" @click="getDoc()">接口文档</el-link>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import { mapGetters } from 'vuex';
import CryptoJS from 'crypto-js';
export default {
    data() {
        return {
            tableData: [],
            loading: false,
        }
    },

    computed: {
        ...mapGetters(['site_info', 'user_info']),
        // decrypted() {
        //     if (!(this.tableData && this.tableData.length > 0)) return "";

        //     const keyBytes = CryptoJS.enc.Base64.parse(this.tableData[0].k);

        //     const res = CryptoJS.AES.decrypt(this.tableData[0].v, keyBytes, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })

        //     return res.toString(CryptoJS.enc.Utf8);
        // }
    },

    filter: {},
    created() {
        this.getAk();
    },
    methods: {
        // 获取秘钥
        getAk() {
            this.loading = true
            let url = `/org/ak/${this.user_info.site_id}`
            this.$axiosUser('get', url).then(res => {
                this.loading = false
                if (res.data.code) {
                    if (res.data.data) {
                        this.tableData.push(res.data.data)
                        if (!(this.tableData && this.tableData.length > 0)) return
                        this.tableData.forEach(item => {
                            const keyBytes = CryptoJS.enc.Base64.parse(item.k); // 对称解密秘钥
                            // 用秘钥解密
                            const res = CryptoJS.AES.decrypt(item.v, keyBytes, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
                            item.decryptedKey = res.toString(CryptoJS.enc.Utf8)
                        })
                    }
                } else {
                    this.$message.error("获取数据失败")
                }
            })
        },

        // 更新秘钥key
        updateAk() {
            this.loading = true
            let url = `/org/ak/${this.user_info.site_id}`
            this.$axiosUser('post', url).then(res => {
                this.loading = false
                this.tableData = [];
                if (res.data.code) {
                    if (res.data.data) {
                        this.tableData.push(res.data.data)
                        if (!(this.tableData && this.tableData.length > 0)) return
                        this.tableData.forEach(item => {
                            const keyBytes = CryptoJS.enc.Base64.parse(item.k); // 对称解密秘钥
                            // 用秘钥解密
                            const res = CryptoJS.AES.decrypt(item.v, keyBytes, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
                            item.decryptedKey = res.toString(CryptoJS.enc.Utf8)
                        })
                    }
                } else {
                    this.$message.error("获取数据失败")
                }
            })
        },

        copy() {
            if (!(this.tableData && this.tableData.length > 0)) return
            //创建input标签
            var input = document.createElement('input')
            //将input的值设置为需要复制的内容
            input.value = this.tableData[0].decryptedKey;
            //添加input标签
            document.body.appendChild(input)
            //选中input标签
            input.select()
            //执行复制
            document.execCommand('copy')
            //成功提示信息
            this.$message.success('已复制')
            //移除input标签
            document.body.removeChild(input)
        },

        getDoc() {
            window.open('https://new.gdy.g107.com/system/doc');
        },

        // 显示星号
        hideSensitiveInfo(value, start = 5, end = 5) {
            if (!value) return '';
            const length = value.length;
            const visibleStart = value.slice(0, start);
            const visibleEnd = value.slice(length - end);
            const stars = '*'.repeat(length - start - end);
            return `${visibleStart}${stars}${visibleEnd}`;
        }
    }
}
</script>

<style scoped lang="scss"></style>