Spring Boot + vue-element 开发个人博客项目实战教程(二十、登录日志、用户、分类管理页面开发)2

简介: Spring Boot + vue-element 开发个人博客项目实战教程(二十、登录日志、用户、分类管理页面开发)2

四、用户功能

首先我们在src/api目录下新建一个user.js,这里面放的是和后端controller层对接的请求接口,增删改查之类的,上边写日志的时候说过,不会的小伙伴去上一篇看看。

下面一共是四个接口,大家应该从接口地址上就能看出大概是什么接口,我这里不多少了。

import request from '@/utils/request'
export function userList(query) {
    return request({
      url: '/user/list',
      method: 'post',
      data: query
    })
}
export function addUser(data) {
    return request({
      url: '/user/create',
      method: 'post',
      data
    })
}
export function updateUser(data) {
    return request({
      url: '/user/update',
      method: 'post',
      data
    })
}
export function deleteUser(id) {
    return request({
      url: '/user/delete',
      method: 'post',
      params: { id }
    })
}

接口创建完之后,我们接下来可以写页面了。

打开src/views/user目录下的list.vue,还是和以前的套路一样,基本的数据展示相信大家基本上已经会了,这里为我们要比日志多了删除,添加和修改这三个功能,展示的部分基本上是一致的,我就不过多的解释了。

下面是基础的代码,展示的功能。

1、列表

<template>
  <el-card class="box-card">
    <el-table v-loading="listLoading" :data="list" fit highlight-current-row style="width: 98%; margin-top:30px;">
      <el-table-column align="center" label="ID" >
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户名">
        <template slot-scope="scope">
          <span>{{ scope.row.userName}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="邮箱">
        <template slot-scope="scope">
          <span>{{ scope.row.email}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="手机号">
        <template slot-scope="scope">
          <span>{{ scope.row.phone}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="昵称">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="上次登录时间">
        <template slot-scope="scope">
          <span>{{ scope.row.lastLoginTime}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="180">
        <template slot-scope="scope">
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script>
  import { userList, deleteUser } from '@/api/user'
export default {
  name: 'UserList',
  created() {
    this.getList()
  },
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
      }
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      var body = this.listQuery;
      userList({body}).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>

我们完成了展示的功能,这时我们运行打开页面看一下。看到这里,我们的用户展示功能已经完成了,然后我们看到上次登录时间和创建时间不对,我们先去后端改一下。

打开User.java,然后在创建时间上边添加注解。然后再将上次登录时间的属性修改一下类型。

/**
* 创建时间
*/
@JsonFormat(timezone = "GMT+8",pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
/**
* 上次登录时间
*/
@JsonFormat(timezone = "GMT+8",pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastLoginTime;

上次登录时间我们还要修改一下。

我们需要写个修改上次登录时间的方法,打开UserService.java,然后添加一个接口。

/**
* 更新上次登录时间
* @param userId
*/
void updateLoginTime(Integer userId);

再写一个实现方法

@Override
public void updateLoginTime(Integer userId) {
    User user = new User();
    user.setId(userId);
    user.setLastLoginTime(LocalDateTime.now());
    userMapper.updateById(user);
 }

我们上边调用了UserMapper.java中的updateById,这个需要我们自己加一个

/**
* 更新上次登录时间
* @param user
*/
void updateById(User user);

紧接着去写一下xml的sql语句。

这里传的参数如果不判断为空的话,只修改某个字段的值的话,其余不修改的会变成null。

<update id="updateById" parameterType="com.blog.personalblog.entity.User">
    update person_user
    <set>
    <if test="userName!=null">
    username = #{userName},
    </if>
    <if test="passWord!=null">
    password = #{passWord},
    </if>
    <if test="email!=null">
    email = #{email},
    </if>
    <if test="lastLoginTime!=null">
    last_login_time = #{lastLoginTime},
    </if>
    <if test="phone!=null">
    phone = #{phone},
    </if>
    <if test="nickname!=null">
    nickname = #{nickname}
    </if>
    </set>
    where id = #{id}
</update>

完成之后,我们将在登录的时候进行修改这个时间点,打开UserController.java类,然后再login的方法中添加以下代码:

//修改上个登录的时间
User user = userService.getUserByUserName(loginModel.getUsername());
userService.updateLoginTime(user.getId());

我们运行项目,再次看一下效果。现在已经都修改好了。接下来我们完成列表最右边操作栏里面的功能,实现添加修改和删除功能。

2、删除

这里我们首先修改一下我们之前请求的接口地址:

/**
* 删除
* @return
*/
@ApiOperation(value = "删除用户")
@PostMapping("/delete")
@OperationLogSys(desc = "删除用户", operationType = OperationType.DELETE)
public JsonResult<Object> userDelete(@RequestParam(value = "id") int id) {
    userService.deleteUser(id);
    return JsonResult.success();
}

然后在页面的操作中添加一个删除的按钮。这里面我们定义了一个删除的deleteUser方法。

<el-button type="danger" size="small" icon="el-icon-delete" @click="deleteUser(scope.row.id)">删除</el-button>

我们先引入接口的方法

import { userList, deleteUser } from '@/api/user'

添加方法,在我们点击删除按钮时,要提示是否要删除该用户的提示。

deleteUser (id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(id).then(response => {
           this.$message({
            type: 'success',
            message: '删除成功!'
          })
           this.getList()
        }).catch(() => {
          console.log('error')
        })
      }).catch(() => {
         this.$message({
            type: 'error',
            message: '你已经取消删除该用户!'
          })
      })
},

我们去页面上先点击删除按钮然后点击确定,删除成功会提示一下信息,这样我们就删除完成了。

3、添加和修改

完成了删除,然后紧接着完成添加和修改,我们需要添加一个添加按钮,点击添加则跳出一个对话框进行填写数据操作。

  <el-button
      type="primary"
      size="small"
      icon="el-icon-plus"
      @click="openModel(null)"
    >
      新增
    </el-button>

在页面操作的那一列中添加一个编辑按钮:

    <el-table-column align="center" label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="openModel(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
     </el-table-column>

此时看一下这两个按钮,同时调用了一个点击事件openModel(),但是传的参数却不同,这个主要是区分是添加还是修改,因为我将这两个功能的对话框放到了一起,所以这里多加了一层调用。我们往下看:

既然写到了这个方法,接下来我们来写这个方法:

先写返回参数:

data() {
  return {
    list: null,
    listLoading: true,
    listQuery: { 
    },
    addOrupdateDialogVisible: false,
    userForm: {
    id: null,
    userName: "",
    email: "",
    passWord: "",
    phone: "",
    nickname: ""
    },
  }
},

下面是如果选择的是添加按钮,则走else语句,因为我们在上边可以看到我们选择的添加按钮传入的值为null,编辑的话走if语句。最后的这个addOrupdateDialogVisible是对话框的控制,我们接下来就写这个对话框。

openModel(user) {
  if (user != null) {
    this.userForm = JSON.parse(JSON.stringify(user));
    this.$refs.userTitle.innerHTML = "修改用户";
  } else {
    this.userForm.id = null;
    this.userForm.userName = "";
    this.userForm.email = "";
    this.userForm.phone = "";
    this.userForm.passWord = "";
    this.userForm.nickname = "";
    this.$refs.userTitle.innerHTML = "添加用户";
  }
  this.addOrupdateDialogVisible = true;
},

我们在页面中写一下对话框,这个再Element官方文档中也可以看到具体的案例,大家可以去学习一下:https://element.eleme.cn/#/zh-CN/component/dialog

 <!-- 添加编辑对话框 -->
    <el-dialog :visible.sync="addOrupdateDialogVisible" width="30%">
      <div class="dialog-title-container" slot="title" ref="userTitle" />
      <el-form label-width="80px" size="medium" :model="userForm">
        <el-form-item label="用户名">
          <el-input v-model="userForm.userName" style="width:220px" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="userForm.passWord" style="width:220px" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userForm.email" style="width:220px" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userForm.phone" style="width:220px" />
        </el-form-item>
         <el-form-item label="昵称">
          <el-input v-model="userForm.nickname" style="width:220px" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addOrupdateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditUser">
          确 定
        </el-button>
      </div>
    </el-dialog>

接下来我们还有一个对接后端的方法没写,就是上边点击确定的addOrEditUser方法。还是和分类的方式基本上差不多。首先引入添加用户和更新用户的方法:

import { userList, deleteUser, addUser, updateUser } from '@/api/user'

然后写下添加方法:

addOrEditUser() {
       var body = this.userForm;
       if(body.id == null){
        addUser(body).then(response => {
          this.$message({
            type: 'success',
            message: '添加分类成功!'
          })
          this.getList()
        }).catch(() => {
          console.log('error')
        })
      } else {
        updateUser(body).then(response => {
          this.$message({
            type: 'success',
            message: '修改分类成功!'
          })
          this.getList()
        }).catch(() => {
          console.log('error')
        })
      }
      this.addOrupdateDialogVisible = false;
   }

写完之后,我们测试一下所有的功能:

添加:修改:完整代码:

<template>
  <el-card class="box-card">
    <el-button
      type="primary"
      size="small"
      icon="el-icon-plus"
      @click="openModel(null)"
    >
      新增
    </el-button>
    <el-table v-loading="listLoading" :data="list" fit highlight-current-row style="width: 98%; margin-top:30px;">
      <el-table-column align="center" label="ID" >
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户名">
        <template slot-scope="scope">
          <span>{{ scope.row.userName}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="邮箱">
        <template slot-scope="scope">
          <span>{{ scope.row.email}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="手机号">
        <template slot-scope="scope">
          <span>{{ scope.row.phone}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="昵称">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="上次登录时间">
        <template slot-scope="scope">
          <span>{{ scope.row.lastLoginTime}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="openModel(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加编辑对话框 -->
    <el-dialog :visible.sync="addOrupdateDialogVisible" width="30%">
      <div class="dialog-title-container" slot="title" ref="userTitle" />
      <el-form label-width="80px" size="medium" :model="userForm">
        <el-form-item label="用户名">
          <el-input v-model="userForm.userName" style="width:220px" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="userForm.passWord" style="width:220px" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userForm.email" style="width:220px" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userForm.phone" style="width:220px" />
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="userForm.nickname" style="width:220px" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addOrupdateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditUser">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>
<script>
  import { userList, deleteUser, addUser, updateUser } from '@/api/user'
export default {
  name: 'UserList',
  created() {
    this.getList()
  },
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: { 
      },
      addOrupdateDialogVisible: false,
      userForm: {
        id: null,
        userName: "",
        email: "",
        phone: "",
        passWord: "",
        nickname: ""
      },
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      var body = this.listQuery;
      userList({body}).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    deleteUser (id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(id).then(response => {
           this.$message({
            type: 'success',
            message: '删除成功!'
          })
           this.getList()
        }).catch(() => {
          console.log('error')
        })
      }).catch(() => {
         this.$message({
            type: 'error',
            message: '你已经取消删除该用户!'
          })
      })
    },
    openModel(user) {
      if (user != null) {
        this.userForm = JSON.parse(JSON.stringify(user));
        this.$refs.userTitle.innerHTML = "修改用户";
      } else {
        this.userForm.id = null;
        this.userForm.userName = "";
        this.userForm.passWord = "";
        this.userForm.email = "";
        this.userForm.phone = "";
        this.userForm.nickname = "";
        this.$refs.userTitle.innerHTML = "添加用户";
      }
      this.addOrupdateDialogVisible = true;
    },
    addOrEditUser() {
       var body = this.userForm;
       if(body.id == null){
        addUser(body).then(response => {
          this.$message({
            type: 'success',
            message: '添加分类成功!'
          })
          this.getList()
        }).catch(() => {
          console.log('error')
        })
      } else {
        updateUser(body).then(response => {
          this.$message({
            type: 'success',
            message: '修改分类成功!'
          })
          this.getList()
        }).catch(() => {
          console.log('error')
        })
      }
      this.addOrupdateDialogVisible = false;
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
 .box-card {
    width: 98%;
    margin: 1%;
  }
  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }
  .clearfix span {
    font-weight: 600;
  }
</style>

好啦,分类管理和用户的所有的功能全部写完了,这篇写的很长了,我们再用一两篇将剩下的写完基本上这个教程就结束了。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
18天前
|
小程序 前端开发 API
微信小程序全栈开发中的异常处理与日志记录
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的异常处理和日志记录,强调其对确保应用稳定性和用户体验的重要性。异常处理涵盖前端(网络、页面跳转、用户输入、逻辑异常)和后端(数据库、API、业务逻辑)方面;日志记录则关注关键操作和异常情况的追踪。实践中,前端可利用try-catch处理异常,后端借助日志框架记录异常,同时采用集中式日志管理工具提升分析效率。开发者应注意安全性、性能和团队协作,以优化异常处理与日志记录流程。
|
2月前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
67 1
|
2月前
|
存储 XML 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南(一)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南
78 0
|
2天前
|
IDE Java 开发工具
Spring Boot DevTools:加速开发的热部署工具
【4月更文挑战第28天】在Spring Boot的开发过程中,快速反馈和效率至关重要。Spring Boot DevTools是一个为开发者设计的模块,支持热部署(hot swapping),能够实现应用的快速重启和自动重载,极大地提高了开发效率。本篇博客将介绍Spring Boot DevTools的核心概念,并通过具体的实战示例展示如何在开发过程中利用这一工具。
10 0
|
6天前
|
开发框架 前端开发 安全
Java从入门到精通:2.2.2学习使用Spring框架进行Web应用开发
Java从入门到精通:2.2.2学习使用Spring框架进行Web应用开发
|
12天前
|
JSON Java fastjson
Spring Boot 底层级探索系列 04 - Web 开发(2)
Spring Boot 底层级探索系列 04 - Web 开发(2)
20 0
|
12天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
27天前
|
开发框架 安全 Java
探索 Spring 框架:企业级应用开发的强大工具
探索 Spring 框架:企业级应用开发的强大工具
20 1
|
29天前
|
缓存 算法 Java
开发必懂的Spring循环依赖图解 Spring 循环依赖
开发必懂的Spring循环依赖图解 Spring 循环依赖
21 1
|
2月前
|
JSON 前端开发 Java
【SpringBoot实战专题】「开发实战系列」全方位攻克你的技术盲区之Spring定义Jackson转换Null的方法和实现案例
【SpringBoot实战专题】「开发实战系列」全方位攻克你的技术盲区之Spring定义Jackson转换Null的方法和实现案例
42 0