<template>
    <div class="login-container">
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
          <el-button @click="resetForm('loginFormRef')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>
  
  <script>

import crypto from 'crypto';
import { bookApi } from '../service/getData';

import { htAesEncry } from '../utils/mUtils';
class EncryptionUtil {
  // 添加 PKCS7 填充
  addPKCS7Padding(data) {
    const blockSize = 16;
    const padLength = blockSize - (data.length % blockSize);
    const padding = Buffer.alloc(padLength, padLength);
    return Buffer.concat([Buffer.from(data), padding]);
  }

  // 加密函数
  encrypt (data, keyStr="Huad400e94beTeng") {
    // const algorithm = 'aes-128-ecb';
    // const cipher = crypto.createCipheriv(algorithm, aesKey, null);
    // let encrypted = cipher.update(this.addPKCS7Padding(data), 'utf8', 'binary');
    // encrypted += cipher.final('binary');
    // return Buffer.from(encrypted, 'binary').toString('base64');



    const algorithm = 'aes-128-ecb';
    const iv = crypto.randomBytes(16);

    // 将字符串密钥转换为固定长度的字节数组
    const key = crypto.createHash('sha256').update(keyStr).digest();

    const cipher = crypto.createCipheriv(algorithm, key, iv);

    // const data = '这是要加密的数据';
    let encrypted = cipher.update(data, 'utf8', 'hex');
    encrypted += cipher.final('hex');

    console.log('加密后的数据：', encrypted);

    //base 64

    // const data = '这是要编码的数据';
const base64Encoded = Buffer.from(encrypted).toString('base64');
console.log('Base64 编码后的数据：', base64Encoded);
    return base64Encoded;
  }
}



  export default {
    data() {
      return {
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, message: '密码长度至少为6位', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      handleLogin() {
        this.$refs.loginFormRef.validate((valid) => {
          if (valid) {
            // var pwd  = this.encrypt(this.loginForm.password)

            var pwd  = htAesEncry(this.loginForm.password)
            debugger
            bookApi.user.login({
              telphone: this.loginForm.username,
              password: pwd
            }).then(res => {
                debugger
            })
            console.log(pwd)
            debugger
            // 这里可以添加实际的登录逻辑，比如发送请求到后端
            console.log('登录信息：', this.loginForm);
          } else {
            console.log('登录表单验证失败');
            return false;
          }
        });
      },

      encrypt(data){
 
        const util = new EncryptionUtil();
       
        const encryptedData = util.encrypt(data);
        console.log('加密后的数据：', encryptedData);
        return encryptedData
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  };
  </script>
  
  <style scoped>
  .login-container {
    border: 1px solid #ccc;
    padding: 20px;
    width: 300px;
    margin: 100px auto;
  }
  </style>