accountList.vue 5.37 KB
<!--
 * @Description: 账户管理页面
 * @Author: huyue
 * @Date: 2023-02-21 17:16:18
 * @LastEditors: huyue
 * @LastEditTime: 2023-07-24 18:23:05
-->
<template>
  <div class="wrapper-content">
    <div>
      <el-input
        :size="data.size"
        class="search-input"
        v-model="data.search"
        clearable
        placeholder="请输入要搜索的用户名"
      ></el-input>
      <el-button
        type="primary"
        :size="data.size"
        @click="searchList({ name: data.search })"
        >搜索</el-button
      ><el-button type="primary" :size="data.size" @click="creatAccout"
        >新建用户</el-button
      >
    </div>

    <el-table
      :data="data.tableData"
      style="width: 100%"
      border
      :row-class-name="tableRowClassName"
    >
      <el-table-column prop="updateDate" label="更新时间" width="250" />
      <el-table-column prop="acount" label="用户名" width="180" />
      <el-table-column prop="auth" label="权限" width="180">
        <template #default="scope">
          {{ scope.row.auth == 'write' ? '编辑' : '查看' }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-input
            v-show="data.showPassword[scope.$index]"
            class="pwd-input"
            :size="data.size"
            v-model="data.password[scope.$index]"
            placeholder="请输入新密码"
          ></el-input>
          <el-button
            :size="data.size"
            v-show="
              !data.showPassword[scope.$index] && scope.row.acount != 'admin'
            "
            @click="handleEdit(scope.row, scope.$index)"
            >修改密码</el-button
          >
          <el-button
            :size="data.size"
            v-show="data.showPassword[scope.$index]"
            @click="handleEdit(scope.row, scope.$index)"
            >确认修改密码</el-button
          >
          <el-button
            :size="data.size"
            v-show="data.showPassword[scope.$index]"
            @click="handleCancel(scope.$index)"
            >取消</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <createAccount
      v-if="data.createModalVisible"
      v-model="data.createModalVisible"
      @confirm="confirm"
    />
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive } from "vue";
import { getAccountList, userEdit } from "@/api/login";
import { ElMessage, ElMessageBox } from "element-plus";

import createAccount from "./createAccount.vue";
// 表格内用户数据接口
interface User {
  id: number;
  acount: string;
  name: string;
  updateDate: string;
  auth: string;
}
// data数据接口
interface DataType {
  tableData: User[];
  password: string[];
  showPassword: boolean[];
  search: string;
  size: string;
  createModalVisible: boolean;
}

const tableRowClassName = ({ rowIndex }: { row: User; rowIndex: number }) => {
  if (rowIndex % 3 == 2) {
    return "warning-row";
  } else if (rowIndex % 3 == 1) {
    return "success-row";
  }
  return "";
};

const data = reactive<DataType>({
  tableData: [],
  password: [],
  showPassword: [],
  search: "",
  size: "small",
  createModalVisible: false,
});
// 修改密码
const handleEdit = async (scope: User, index: number) => {
  console.log(scope, data.showPassword[index]);
  let showPassword = data.showPassword[index];
  if (showPassword) {
    if (!data.password[index]) {
      ElMessage.error("请输入需要修改的用户新密码");
      return;
    }
    ElMessageBox.confirm("您确定要修改此用户密码吗?", "Warning", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        // 确认修改密码
        const params = {
          password: data.password[index],
          name: scope.name,
          id: scope.id,
        };
        console.log(params);
        let { code, message } = await userEdit(params);
        if (code == 200) {
          ElMessage.success(message);
          data.showPassword[index] = !data.showPassword[index];
          data.password[index] = "";
        } else {
          ElMessage.error(message);
        }
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消修改",
        });
      });
  } else {
    data.showPassword[index] = !data.showPassword[index];
  }
};
const handleCancel = (index: number) => {
  data.showPassword[index] = false;
  data.password[index] = "";
};
// 搜索
const searchList = async (params?: { name: string }) => {
  const list = await getAccountList(params);
  data.tableData = list.data;
};
// 新建
const creatAccout = async () => {
  data.createModalVisible = true;
};

const confirm = () => {
  searchList();
};

onMounted(() => {
  searchList();
});
</script>

<style>
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}
.wrapper-content {
  width: 800px;
  margin: 20px auto;
}
.search-input {
  width: 300px;
  margin-bottom: 20px;
  margin-right: 20px;
}
.pwd-input {
  width: 150px;
  /* margin-right: 10px; */
}
</style>