用户管理功能

后端

# 获取用户列表
@bp.get("/user/list")
def user_list():
    users = UserModel.query.order_by(UserModel.join_time.desc()).all()
    user_list = [user.to_dict() for user in users]
    return restful.ok(data=user_list)

# 用户是否可用
@bp.post("/user/active")
def active_user():
    is_active = request.form.get('is_active', type=int)
    user_id = request.form.get("id")
    user = UserModel.query.get(user_id)
    user.is_active = bool(is_active)
    db.session.commit()
    return restful.ok(data=user.to_dict())

前端

<template>
  <div>
    <el-space direction="vertical" :size="20">
      <h1>用户管理</h1>
      <el-table :data="users" style="width: 100%">
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="join_time" label="加入时间" />
        <el-table-column label="员工">
          <template #default="scope">
            <el-tag v-if="scope.row.is_staff"></el-tag>
            <el-tag v-else type="danger"></el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <el-tag v-if="scope.row.is_active" type="success"></el-tag>
            <el-tag v-else type="danger"></el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              type="danger"
              circle
              size="mini"
              @click="onActiveUserClick(scope.$index)"
            >
              <el-icon><delete /></el-icon>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-space>

    <!-- 删除轮播图确认对话框 -->
  <el-dialog
    v-model="confirmDialogVisible"
    title="提示"
    width="30%"
  >
    <span>{{message}}</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="confirmDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onConfirmActiveUserClick">确定</el-button>
      </span>
    </template>
  </el-dialog>
  </div>
</template>

<script>
import {Delete} from "@element-plus/icons";
import { ElMessage } from 'element-plus';
export default {
    name: "User",
    data(){
      return {
        confirmDialogVisible: false,
        users: [],
        activingIndex: 0,
        message: ""
      }
    },
    mounted(){
      this.getUserList(1);
    },
    methods: {
      getUserList(page){
        this.$http.getUserList(page).then(res => {
          this.users = res.data;
        });
      },
      onActiveUserClick(index){
        this.activingIndex = index;
        this.confirmDialogVisible = true;
        const user = this.users[index];
        if(user.is_active){
          this.message = "您确定要拉黑此用户吗?"
        }else{
          this.message = "您确定要取消拉黑此用户吗?"
        }
      },
      onConfirmActiveUserClick(){
        let user = this.users[this.activingIndex];
        let is_active = user.is_active?0:1;
        this.$http.activeUser(user.id, is_active).then(res => {
          if(res && res['code'] == 200){
            ElMessage.success("操作成功!");
            this.confirmDialogVisible = false;
            let user = res.data;
            this.users.splice(this.activingIndex, 1, user);
          }else{
            ElMessage.info("操作失败!");
            this.confirmDialogVisible = false;
          }
        })
      }
    },
    components: {
      Delete
    }
}
</script>

<style scoped>
.el-space {
  display: block;
}
</style>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

季布,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值