当前位置:   article > 正文

月木学途开发 2.前台用户模块

月木学途开发 2.前台用户模块

概述

效果展

 

 

数据库设计

会员表

  1. DROP TABLE IF EXISTS `user_type`;
  2. CREATE TABLE `user_type` (
  3.   `userTypeId` int(11) NOT NULL AUTO_INCREMENT,
  4.   `userTypeName` varchar(255) DEFAULT NULL,
  5.   `userTypeDesc` varchar(255) DEFAULT NULL,
  6.   PRIMARY KEY (`userTypeId`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;

用户表

  1. DROP TABLE IF EXISTS `user`;
  2. CREATE TABLE `user` (
  3.   `userId` int(11) NOT NULL AUTO_INCREMENT,
  4.   `username` varchar(255) DEFAULT NULL,
  5.   `password` varchar(255) DEFAULT NULL,
  6.   `phone` varchar(255) DEFAULT NULL,
  7.   `nickname` varchar(255) DEFAULT NULL,
  8.   `userImg` longtext,
  9.   `email` varchar(255) DEFAULT NULL,
  10.   `userIntroduce` longtext,
  11.   `userSex` varchar(255) DEFAULT NULL,
  12.   `signature` longtext,
  13.   `userStatus` varchar(255) DEFAULT NULL,
  14.   PRIMARY KEY (`userId`)
  15. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;

会员_用户中间表

  1. DROP TABLE IF EXISTS `user_user_type`;
  2. CREATE TABLE `user_user_type` (
  3.   `userId` int(11) DEFAULT NULL,
  4.   `userTypeId` int(11) DEFAULT NULL
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

实体类设计

会员类型

  1. /**
  2.  * 会员类型
  3.  */
  4. @Data
  5. public class UserType {
  6.     @TableId
  7.     private Integer userTypeId;
  8.     private String userTypeName;//名称【普通会员/黄金会员/钻石会员/至尊会员/骇客会员】
  9.     private String userTypeDesc;//详情
  10. }

用户表

  1. /**
  2.  * 用户
  3.  */
  4. @Data
  5. public class User implements Serializable {
  6.     @TableId
  7.     private Integer userId;
  8.     private String userImg; // 头像
  9.     private String nickname; // 昵称
  10.     private String username; // 账号
  11.     private String password; // 密码
  12.     private String phone; // 手机号
  13.     private String email;//邮箱
  14.     private String userSex; // 性别
  15.     private String signature;//个性签名
  16.     private String userIntroduce;//自我介绍
  17.     private String userStatus; // 用户状态(1激活 0未激活)
  18. }

Mapper层开发

用户mapper

  1. public interface UserMapper extends BaseMapper<User> {
  2. }

用户类型mapper

  1. public interface UserTypeMapper extends BaseMapper<UserType> {
  2.     //添加用户_用户类型中间表的数据
  3.     void addUser_userType(@Param("userId") Integer userId, @Param("userTypeId") Integer userTypeId);
  4.     //删除用户_用户类型中间表的数据
  5.     void deleteUser_userType(@Param("userId") Integer userId, @Param("userTypeId") Integer userTypeId);
  6.     //根据用户id删除用户_用户类型中间表的数据
  7.     void deleteUser_userTypeByUserId(Integer userId);
  8.     //根据用户类型id删除用户_用户类型中间表的数据
  9.     void deleteUser_userTypeByUserTypeId(Integer userTypeId);
  10.     //根据用户id查询用户_用户类型中间表的专栏数据
  11.     List<UserType> findUserTypeByUserId(Integer userId);
  12. }

用户类型mapper映射文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3.         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="jkw.mapper.UserTypeMapper">
  6.     <insert id="addUser_userType">
  7.         INSERT INTO user_user_type
  8.         VALUES (#{userId}, #{userTypeId});
  9.     </insert>
  10.     <delete id="deleteUser_userType">
  11.         DELETE
  12.         FROM user_user_type
  13.         WHERE userId = #{userId}
  14.           and userTypeId = #{userTypeId}
  15.     </delete>
  16.     <delete id="deleteUser_userTypeByUserId" parameterType="int">
  17.         DELETE
  18.         FROM user_user_type
  19.         WHERE userId = #{userId}
  20.     </delete>
  21.     <delete id="deleteUser_userTypeByUserTypeId" parameterType="int">
  22.         DELETE
  23.         FROM user_user_type
  24.         WHERE userTypeId = #{userTypeId}
  25.     </delete>
  26.     <select id="findUserTypeByUserId" resultType="jkw.pojo.UserType" parameterType="int">
  27.         select user_type.*
  28.         from user_type
  29.                  left join user_user_type
  30.                            on user_user_type.userTypeId = user_type.userTypeId
  31.         where user_user_type.userId = #{userId}
  32.     </select>
  33. </mapper>

Service层开发

UserService

  1. public interface UserService {
  2.     void add(User user);
  3.     void update(User user);
  4.     void delete(Integer id);
  5.     User findById(Integer id);
  6.     Page<User> search(int page, int size);
  7.     List<User> findAll();
  8.     void updateStatus(Integer id);
  9.     // 注册时向redis保存手机号+验证码
  10.     void saveRegisterCheckCode(String phone, String checkCode);
  11.     // 注册时验证手机号
  12.     void registerCheckCode(String phone, String checkCode);
  13.     // 注册
  14.     void register(User user);
  15.     // 账号密码登录
  16.     String loginPassword(String username, String password);
  17.     // 手机号验证码登录时向redis保存手机号+验证码
  18.     void saveLoginCheckCode(String phone, String checkCode);
  19.     // 手机号验证码登录
  20.     String loginCheckCode(String phone, String checkCode);
  21.     // 获取登录用户
  22.     User getLoginUser(String token);
  23. }

UserServiceImpl

  1. package jkw.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import jkw.exception.MyException;
  5. import jkw.mapper.UserMapper;
  6. import jkw.mapper.UserTypeMapper;
  7. import jkw.pojo.User;
  8. import jkw.service.UserService;
  9. import jkw.utils.JWTUtil;
  10. import jkw.utils.Md5Util;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.data.redis.core.RedisTemplate;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import java.util.List;
  16. import java.util.concurrent.TimeUnit;
  17. @Service
  18. @Transactional
  19. public class UserServiceImpl implements UserService {
  20.     @Autowired
  21.     private UserMapper userMapper;
  22.     @Autowired
  23.     private RedisTemplate redisTemplate;
  24.     @Autowired
  25.     private UserTypeMapper userTypeMapper;
  26.     @Override
  27.     public void add(User user) {
  28.         user.setPassword(Md5Util.encode(user.getPassword()));
  29.         userMapper.insert(user);
  30.     }
  31.     @Override
  32.     public void update(User user) {
  33.         User userFromData = userMapper.selectById(user.getUserId());
  34.         //判断密码是否修改[若密码为空或者密码和数据库中相等,则未修改]
  35.         if (user.getPassword().equals(userFromData.getPassword())) {
  36.             userMapper.updateById(user);
  37.         } else {
  38.             //若修改,则加密后在保存
  39.             user.setPassword(Md5Util.encode(user.getPassword()));
  40.             userMapper.updateById(user);
  41.         }
  42.     }
  43.     @Override
  44.     public void delete(Integer id) {
  45.         //先删除用户_用户类型中间表的数据
  46.         userTypeMapper.deleteUser_userTypeByUserId(id);
  47.         //删除用户
  48.         userMapper.deleteById(id);
  49.     }
  50.     @Override
  51.     public User findById(Integer id) {
  52.         return userMapper.selectById(id);
  53.     }
  54.     @Override
  55.     public Page<User> search(int page, int size) {
  56.         return userMapper.selectPage(new Page<>(page, size), null);
  57.     }
  58.     @Override
  59.     public List<User> findAll() {
  60.         return userMapper.selectList(null);
  61.     }
  62.     @Override
  63.     public void updateStatus(Integer id) {
  64.         User user = userMapper.selectById(id);
  65.         if (user.getUserStatus().equals("1")) {
  66.             user.setUserStatus("0");
  67.             userMapper.updateById(user);
  68.         } else {
  69.             user.setUserStatus("1");
  70.             userMapper.updateById(user);
  71.         }
  72.     }
  73.     @Override
  74.     public void saveRegisterCheckCode(String phone, String checkCode) {
  75.         //向redis缓存注册验证码,手机验证码过期时间设为5分钟
  76.         redisTemplate.opsForValue().set("registerCheckCode" + phone, checkCode, 300, TimeUnit.SECONDS);
  77.     }
  78.     @Override
  79.     public void registerCheckCode(String phone, String checkCode) {
  80.         //获取redis中缓存的注册验证码
  81.         String checkCodeFromRedis = (String) redisTemplate.opsForValue().get("registerCheckCode" + phone);
  82.         if (!checkCodeFromRedis.equals(checkCode)) {
  83.             throw new MyException(604, "验证码错误");
  84.         }
  85.     }
  86.     @Override
  87.     public void register(User user) {
  88.         //验证手机号是否存在
  89.         QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
  90.         queryWrapper1.eq("phone", user.getPhone());
  91.         List<User> users1 = userMapper.selectList(queryWrapper1);
  92.         if (users1 != null && users1.size() > 0) {
  93.             throw new MyException(604, "手机号已存在");
  94.         }
  95.         //验证账号是否存在
  96.         QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
  97.         queryWrapper2.eq("username", user.getPhone());
  98.         List<User> users2 = userMapper.selectList(queryWrapper2);
  99.         if (users2 != null && users2.size() > 0) {
  100.             throw new MyException(604, "账号已存在");
  101.         }
  102.         //新增用户
  103.         user.setUserStatus("1");
  104.         user.setPassword(Md5Util.encode(user.getPassword()));
  105.         userMapper.insert(user);
  106.     }
  107.     @Override
  108.     public String loginPassword(String username, String password) {
  109.         QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  110.         queryWrapper.eq("username", username);
  111.         User user = userMapper.selectOne(queryWrapper);
  112.         //判断用户是否存在
  113.         if (user == null) {
  114.             throw new MyException(605, "用户不存在");
  115.         }
  116.         //判断面是否正确
  117.         boolean verify = Md5Util.verify(password, user.getPassword());
  118.         if (!verify) {
  119.             throw new MyException(605, "密码错误");
  120.         }
  121.         //登录成功,生成jwt令牌
  122.         String token = JWTUtil.token(user);
  123.         return token;
  124.     }
  125.     @Override
  126.     public void saveLoginCheckCode(String phone, String checkCode) {
  127.         //判断该手机号是否有对应的用户
  128.         QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  129.         queryWrapper.eq("phone", phone);
  130.         User user = userMapper.selectOne(queryWrapper);
  131.         if (user != null) {
  132.             //向redis缓存登录验证码,手机验证码过期时间设为5分钟
  133.             redisTemplate.opsForValue().set("loginCheckCode" + phone, checkCode, 300, TimeUnit.SECONDS);
  134.         } else {
  135.             throw new MyException(605, "用户不存在");
  136.         }
  137.     }
  138.     @Override
  139.     public String loginCheckCode(String phone, String checkCode) {
  140.         //获取redis中缓存的登录验证码
  141.         String checkCodeFromRedis = (String) redisTemplate.opsForValue().get("loginCheckCode" + phone);
  142.         if (!checkCodeFromRedis.equals(checkCode)) {
  143.             throw new MyException(604, "验证码错误");
  144.         }
  145.         QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  146.         queryWrapper.eq("phone", phone);
  147.         User user = userMapper.selectOne(queryWrapper);
  148.         //登录成功,生成jwt令牌
  149.         return JWTUtil.token(user);
  150.     }
  151.     @Override
  152.     public User getLoginUser(String token) {
  153.         return userMapper.selectById(JWTUtil.verifyAndGetId(token));
  154.     }
  155. }

UserTypeService

  1. package jkw.service;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import jkw.pojo.UserType;
  4. import java.util.List;
  5. public interface UserTypeService {
  6.     void add(UserType userType);
  7.     void update(UserType userType);
  8.     void delete(Integer id);
  9.     UserType findById(Integer id);
  10.     List<UserType> findAll();
  11.     Page<UserType> search(String search, int page, int size);
  12.     //添加用户_用户类型中间表的数据
  13.     void addUser_userType(Integer userId, Integer userTypeId);
  14.     //删除用户_用户类型中间表的数据
  15.     void deleteUser_userType(Integer userId, Integer userTypeId);
  16.     //根据用户id删除用户_用户类型中间表的数据
  17.     void deleteUser_userTypeByUserId(Integer userId);
  18.     //根据用户类型id删除用户_用户类型中间表的数据
  19.     void deleteUser_userTypeByUserTypeId(Integer userTypeId);
  20.     //根据用户id查询用户_用户类型中间表的专栏数据
  21.     List<UserType> findUserTypeByUserId(Integer userId);
  22. }

UserTypeServiceImpl

  1. package jkw.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import jkw.exception.MyException;
  5. import jkw.mapper.UserMapper;
  6. import jkw.mapper.UserTypeMapper;
  7. import jkw.pojo.User;
  8. import jkw.service.UserService;
  9. import jkw.utils.JWTUtil;
  10. import jkw.utils.Md5Util;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.data.redis.core.RedisTemplate;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import java.util.List;
  16. import java.util.concurrent.TimeUnit;
  17. @Service
  18. @Transactional
  19. public class UserServiceImpl implements UserService {
  20.     @Autowired
  21.     private UserMapper userMapper;
  22.     @Autowired
  23.     private RedisTemplate redisTemplate;
  24.     @Autowired
  25.     private UserTypeMapper userTypeMapper;
  26.     @Override
  27.     public void add(User user) {
  28.         user.setPassword(Md5Util.encode(user.getPassword()));
  29.         userMapper.insert(user);
  30.     }
  31.     @Override
  32.     public void update(User user) {
  33.         User userFromData = userMapper.selectById(user.getUserId());
  34.         //判断密码是否修改[若密码为空或者密码和数据库中相等,则未修改]
  35.         if (user.getPassword().equals(userFromData.getPassword())) {
  36.             userMapper.updateById(user);
  37.         } else {
  38.             //若修改,则加密后在保存
  39.             user.setPassword(Md5Util.encode(user.getPassword()));
  40.             userMapper.updateById(user);
  41.         }
  42.     }
  43.     @Override
  44.     public void delete(Integer id) {
  45.         //先删除用户_用户类型中间表的数据
  46.         userTypeMapper.deleteUser_userTypeByUserId(id);
  47.         //删除用户
  48.         userMapper.deleteById(id);
  49.     }
  50.     @Override
  51.     public User findById(Integer id) {
  52.         return userMapper.selectById(id);
  53.     }
  54.     @Override
  55.     public Page<User> search(int page, int size) {
  56.         return userMapper.selectPage(new Page<>(page, size), null);
  57.     }
  58.     @Override
  59.     public List<User> findAll() {
  60.         return userMapper.selectList(null);
  61.     }
  62.     @Override
  63.     public void updateStatus(Integer id) {
  64.         User user = userMapper.selectById(id);
  65.         if (user.getUserStatus().equals("1")) {
  66.             user.setUserStatus("0");
  67.             userMapper.updateById(user);
  68.         } else {
  69.             user.setUserStatus("1");
  70.             userMapper.updateById(user);
  71.         }
  72.     }
  73.     @Override
  74.     public void saveRegisterCheckCode(String phone, String checkCode) {
  75.         //向redis缓存注册验证码,手机验证码过期时间设为5分钟
  76.         redisTemplate.opsForValue().set("registerCheckCode" + phone, checkCode, 300, TimeUnit.SECONDS);
  77.     }
  78.     @Override
  79.     public void registerCheckCode(String phone, String checkCode) {
  80.         //获取redis中缓存的注册验证码
  81.         String checkCodeFromRedis = (String) redisTemplate.opsForValue().get("registerCheckCode" + phone);
  82.         if (!checkCodeFromRedis.equals(checkCode)) {
  83.             throw new MyException(604, "验证码错误");
  84.         }
  85.     }
  86.     @Override
  87.     public void register(User user) {
  88.         //验证手机号是否存在
  89.         QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
  90.         queryWrapper1.eq("phone", user.getPhone());
  91.         List<User> users1 = userMapper.selectList(queryWrapper1);
  92.         if (users1 != null && users1.size() > 0) {
  93.             throw new MyException(604, "手机号已存在");
  94.         }
  95.         //验证账号是否存在
  96.         QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
  97.         queryWrapper2.eq("username", user.getPhone());
  98.         List<User> users2 = userMapper.selectList(queryWrapper2);
  99.         if (users2 != null && users2.size() > 0) {
  100.             throw new MyException(604, "账号已存在");
  101.         }
  102.         //新增用户
  103.         user.setUserStatus("1");
  104.         user.setPassword(Md5Util.encode(user.getPassword()));
  105.         userMapper.insert(user);
  106.     }
  107.     @Override
  108.     public String loginPassword(String username, String password) {
  109.         QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  110.         queryWrapper.eq("username", username);
  111.         User user = userMapper.selectOne(queryWrapper);
  112.         //判断用户是否存在
  113.         if (user == null) {
  114.             throw new MyException(605, "用户不存在");
  115.         }
  116.         //判断面是否正确
  117.         boolean verify = Md5Util.verify(password, user.getPassword());
  118.         if (!verify) {
  119.             throw new MyException(605, "密码错误");
  120.         }
  121.         //登录成功,生成jwt令牌
  122.         String token = JWTUtil.token(user);
  123.         return token;
  124.     }
  125.     @Override
  126.     public void saveLoginCheckCode(String phone, String checkCode) {
  127.         //判断该手机号是否有对应的用户
  128.         QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  129.         queryWrapper.eq("phone", phone);
  130.         User user = userMapper.selectOne(queryWrapper);
  131.         if (user != null) {
  132.             //向redis缓存登录验证码,手机验证码过期时间设为5分钟
  133.             redisTemplate.opsForValue().set("loginCheckCode" + phone, checkCode, 300, TimeUnit.SECONDS);
  134.         } else {
  135.             throw new MyException(605, "用户不存在");
  136.         }
  137.     }
  138.     @Override
  139.     public String loginCheckCode(String phone, String checkCode) {
  140.         //获取redis中缓存的登录验证码
  141.         String checkCodeFromRedis = (String) redisTemplate.opsForValue().get("loginCheckCode" + phone);
  142.         if (!checkCodeFromRedis.equals(checkCode)) {
  143.             throw new MyException(604, "验证码错误");
  144.         }
  145.         QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  146.         queryWrapper.eq("phone", phone);
  147.         User user = userMapper.selectOne(queryWrapper);
  148.         //登录成功,生成jwt令牌
  149.         return JWTUtil.token(user);
  150.     }
  151.     @Override
  152.     public User getLoginUser(String token) {
  153.         return userMapper.selectById(JWTUtil.verifyAndGetId(token));
  154.     }
  155. }

控制层开发

UserCon

  1. package jkw.controller.back;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import jkw.pojo.User;
  4. import jkw.service.UserService;
  5. import jkw.vo.BaseResult;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.security.access.prepost.PreAuthorize;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.util.List;
  10. @CrossOrigin
  11. @RequestMapping("/back/user")
  12. @RestController
  13. public class UserCon {
  14.     @Autowired
  15.     private UserService userService;
  16.     /**
  17.      * 新增
  18.      *
  19.      * @param user
  20.      * @return
  21.      */
  22.     @PostMapping("/add")
  23.     @PreAuthorize("hasAnyAuthority('/user')")
  24.     public BaseResult add(User user) {
  25.         userService.add(user);
  26.         return BaseResult.ok();
  27.     }
  28.     /**
  29.      * 修改
  30.      *
  31.      * @param user
  32.      * @return
  33.      */
  34.     @PostMapping("/update")
  35.     @PreAuthorize("hasAnyAuthority('/user')")
  36.     public BaseResult update(User user) {
  37.         userService.update(user);
  38.         return BaseResult.ok();
  39.     }
  40.     /**
  41.      * 删除
  42.      *
  43.      * @param userId
  44.      * @return
  45.      */
  46.     @DeleteMapping("/delete")
  47.     @PreAuthorize("hasAnyAuthority('/user')")
  48.     public BaseResult delete(Integer userId) {
  49.         userService.delete(userId);
  50.         return BaseResult.ok();
  51.     }
  52.     /**
  53.      * 根据id查询
  54.      *
  55.      * @param userId
  56.      * @return
  57.      */
  58.     @GetMapping("/findById")
  59.     @PreAuthorize("hasAnyAuthority('/user')")
  60.     public BaseResult findById(Integer userId) {
  61.         User user = userService.findById(userId);
  62.         return BaseResult.ok(user);
  63.     }
  64.     /**
  65.      * 查询所有
  66.      *
  67.      * @return
  68.      */
  69.     @GetMapping("/findAll")
  70.     @PreAuthorize("hasAnyAuthority('/user')")
  71.     public BaseResult findAll() {
  72.         List<User> all = userService.findAll();
  73.         return BaseResult.ok(all);
  74.     }
  75.     /**
  76.      * 分页查询
  77.      *
  78.      * @param page
  79.      * @param size
  80.      * @return
  81.      */
  82.     @GetMapping("/search")
  83.     @PreAuthorize("hasAnyAuthority('/user')")
  84.     public BaseResult search(int page, int size) {
  85.         Page<User> brandPage = userService.search(page, size);
  86.         return BaseResult.ok(brandPage);
  87.     }
  88.     /**
  89.      * 修改状态
  90.      *
  91.      * @param userId
  92.      * @return
  93.      */
  94.     @PostMapping("/updateStatus")
  95.     @PreAuthorize("hasAnyAuthority('/user')")
  96.     public BaseResult updateStatus(Integer userId) {
  97.         userService.updateStatus(userId);
  98.         return BaseResult.ok();
  99.     }
  100. }

FrontUserCon

  1. package jkw.controller.front;
  2. import jkw.pojo.User;
  3. import jkw.service.MessageService;
  4. import jkw.service.UserService;
  5. import jkw.utils.RandomUtil;
  6. import jkw.vo.BaseResult;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.*;
  9. @RestController
  10. @CrossOrigin
  11. @RequestMapping("/front/user")
  12. public class FrontUserCon {
  13.     @Autowired
  14.     private UserService userService;
  15.     @Autowired
  16.     private MessageService messageService;
  17.     /**
  18.      * 发送注册短信
  19.      *
  20.      * @param phone 注册手机号
  21.      * @return 操作结果
  22.      */
  23.     @GetMapping("/sendMessage")
  24.     public BaseResult sendMessage(String phone) {
  25.         // 1.生成随机四位数
  26.         String checkCode = RandomUtil.buildCheckCode(4);
  27.         // 2.发送短信
  28.         BaseResult result = messageService.sendMessage(phone, checkCode);
  29.         // 3.发送成功,将验证码保存到redis中,发送失败,返回发送结果
  30.         if (200 == result.getCode()) {
  31.             userService.saveRegisterCheckCode(phone, checkCode);
  32.             return BaseResult.ok();
  33.         } else {
  34.             return result;
  35.         }
  36.     }
  37.     /**
  38.      * 验证用户注册验证码
  39.      *
  40.      * @param phone     手机号
  41.      * @param checkCode 验证码
  42.      * @return 200验证成功,605验证码不正确
  43.      */
  44.     @GetMapping("/registerCheckCode")
  45.     public BaseResult register(String phone, String checkCode) {
  46.         userService.registerCheckCode(phone, checkCode);
  47.         return BaseResult.ok();
  48.     }
  49.     /**
  50.      * 用户注册
  51.      *
  52.      * @param user 用户信息
  53.      * @return 注册结果
  54.      */
  55.     @PostMapping("/register")
  56.     public BaseResult register(User user) {
  57.         userService.register(user);
  58.         return BaseResult.ok();
  59.     }
  60.     /**
  61.      * 用户名密码登录
  62.      *
  63.      * @param user 用户对象
  64.      * @return 登录结果
  65.      */
  66.     @PostMapping("/loginPassword")
  67.     public BaseResult loginPassword(User user) {
  68.         String token = userService.loginPassword(user.getUsername(), user.getPassword());
  69.         return BaseResult.ok(token);
  70.     }
  71.     /**
  72.      * 发送登录短信验证码
  73.      *
  74.      * @param phone 手机号
  75.      * @return 操作结果
  76.      */
  77.     @GetMapping("/sendLoginCheckCode")
  78.     public BaseResult sendLoginCheckCode(String phone) {
  79.         // 1.生成随机四位数
  80.         String checkCode = RandomUtil.buildCheckCode(4);
  81.         // 2.发送短信
  82.         BaseResult result = messageService.sendMessage(phone, checkCode);
  83.         // 3.发送成功,将验证码保存到redis中,发送失败,返回发送结果
  84.         if (200 == result.getCode()) {
  85.             userService.saveLoginCheckCode(phone, checkCode);
  86.             return BaseResult.ok();
  87.         } else {
  88.             return result;
  89.         }
  90.     }
  91.     /**
  92.      * 手机号验证码登录
  93.      *
  94.      * @param phone     手机号
  95.      * @param checkCode 验证码
  96.      * @return 登录结果
  97.      */
  98.     @PostMapping("/loginCheckCode")
  99.     public BaseResult loginCheckCode(String phone, String checkCode) {
  100.         String token = userService.loginCheckCode(phone, checkCode);
  101.         return BaseResult.ok(token);
  102.     }
  103.     /**
  104.      * 获取登录用户
  105.      *
  106.      * @param token 令牌
  107.      * @return 用户名
  108.      */
  109.     @GetMapping("/getUser")
  110.     public BaseResult getUser(String token) {
  111.         User u = userService.getLoginUser(token);
  112.         return BaseResult.ok(u);
  113.     }
  114.     /**
  115.      * 根据id查询
  116.      *
  117.      * @param userId
  118.      * @return
  119.      */
  120.     @GetMapping("/findById")
  121.     public BaseResult findById(Integer userId) {
  122.         User user = userService.findById(userId);
  123.         return BaseResult.ok(user);
  124.     }
  125. }

FrontUserTypeCon

  1. package jkw.controller.back;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import jkw.pojo.UserType;
  4. import jkw.service.UserTypeService;
  5. import jkw.vo.BaseResult;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.security.access.prepost.PreAuthorize;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.util.List;
  10. @CrossOrigin
  11. @RestController
  12. @RequestMapping("/back/userType")
  13. public class UserTypeCon {
  14.     @Autowired
  15.     private UserTypeService userTypeService;
  16.     /**
  17.      * 新增
  18.      *
  19.      * @param userType
  20.      * @return
  21.      */
  22.     @PostMapping("/add")
  23.     @PreAuthorize("hasAnyAuthority('/user')")
  24.     public BaseResult add(UserType userType) {
  25.         userTypeService.add(userType);
  26.         return BaseResult.ok();
  27.     }
  28.     /**
  29.      * 修改
  30.      *
  31.      * @param userType
  32.      * @return
  33.      */
  34.     @PostMapping("/update")
  35.     @PreAuthorize("hasAnyAuthority('/user')")
  36.     public BaseResult update(UserType userType) {
  37.         userTypeService.update(userType);
  38.         return BaseResult.ok();
  39.     }
  40.     /**
  41.      * 删除
  42.      *
  43.      * @param userTypeId
  44.      * @return
  45.      */
  46.     @DeleteMapping("/delete")
  47.     @PreAuthorize("hasAnyAuthority('/user')")
  48.     public BaseResult delete(Integer userTypeId) {
  49.         userTypeService.delete(userTypeId);
  50.         return BaseResult.ok();
  51.     }
  52.     /**
  53.      * 根据id查询
  54.      *
  55.      * @param userTypeId
  56.      * @return
  57.      */
  58.     @GetMapping("/findById")
  59.     @PreAuthorize("hasAnyAuthority('/user')")
  60.     public BaseResult findById(Integer userTypeId) {
  61.         UserType userType = userTypeService.findById(userTypeId);
  62.         return BaseResult.ok(userType);
  63.     }
  64.     /**
  65.      * 查询所有
  66.      *
  67.      * @return
  68.      */
  69.     @GetMapping("/findAll")
  70.     @PreAuthorize("hasAnyAuthority('/user')")
  71.     public BaseResult findAll() {
  72.         List<UserType> all = userTypeService.findAll();
  73.         return BaseResult.ok(all);
  74.     }
  75.     /**
  76.      * 分页查询
  77.      *
  78.      * @param page
  79.      * @param size
  80.      * @return
  81.      */
  82.     @GetMapping("/search")
  83.     @PreAuthorize("hasAnyAuthority('/user')")
  84.     public BaseResult search(String search, int page, int size) {
  85.         Page<UserType> brandPage = userTypeService.search(search, page, size);
  86.         return BaseResult.ok(brandPage);
  87.     }
  88.     /**
  89.      * 添加用户_用户类型中间表的数据
  90.      *
  91.      * @param userId
  92.      * @param userTypeId
  93.      * @return
  94.      */
  95.     @PostMapping("/addUserType")
  96.     @PreAuthorize("hasAnyAuthority('/user')")
  97.     public BaseResult addUserType(Integer userId, Integer userTypeId) {
  98.         userTypeService.addUser_userType(userId, userTypeId);
  99.         return BaseResult.ok();
  100.     }
  101.     /**
  102.      * 删除用户_用户类型中间表的数据
  103.      *
  104.      * @param userId
  105.      * @param userTypeId
  106.      * @return
  107.      */
  108.     @DeleteMapping("/deleteUserType")
  109.     @PreAuthorize("hasAnyAuthority('/user')")
  110.     public BaseResult deleteColumn(Integer userId, Integer userTypeId) {
  111.         userTypeService.deleteUser_userType(userId, userTypeId);
  112.         return BaseResult.ok();
  113.     }
  114.     /**
  115.      * 根据用户id查询用户_用户类型中间表的专栏数据
  116.      *
  117.      * @param userId
  118.      * @return
  119.      */
  120.     @GetMapping("/findUserTypeByUserId")
  121.     @PreAuthorize("hasAnyAuthority('/user')")
  122.     public BaseResult findColumnByBlogId(Integer userId) {
  123.         List<UserType> userTypeByUserId = userTypeService.findUserTypeByUserId(userId);
  124.         return BaseResult.ok(userTypeByUserId);
  125.     }
  126. }

UserTypeCon

  1. package jkw.controller.back;
  2. import jkw.pojo.UserType;
  3. import jkw.service.UserTypeService;
  4. import jkw.vo.BaseResult;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.CrossOrigin;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import java.util.List;
  11. @CrossOrigin
  12. @RestController
  13. @RequestMapping("/front/userType")
  14. public class FrontUserTypeCon {
  15.     @Autowired
  16.     private UserTypeService userTypeService;
  17.     /**
  18.      * 查询所有
  19.      *
  20.      * @return
  21.      */
  22.     @GetMapping("/findAll")
  23.     public BaseResult findAll() {
  24.         List<UserType> all = userTypeService.findAll();
  25.         return BaseResult.ok(all);
  26.     }
  27.     /**
  28.      * 根据用户id查询用户_用户类型中间表的专栏数据
  29.      *
  30.      * @param userId
  31.      * @return
  32.      */
  33.     @GetMapping("/findUserTypeByUserId")
  34.     public BaseResult findColumnByBlogId(Integer userId) {
  35.         List<UserType> userTypeByUserId = userTypeService.findUserTypeByUserId(userId);
  36.         return BaseResult.ok(userTypeByUserId);
  37.     }
  38. }

后台ui代码

会员等级

  1. <template>
  2.   <div class="data-container">
  3.     <!--添加 start-->
  4.     <div class="data-header">
  5.       <el-input class="input" @keyup.enter="searchHandle" v-model="searchInfo" size="large"
  6.                 placeholder="请输入关键字"></el-input>
  7.       <el-button @click="searchHandle" class="button" size="large" type="primary" plain>搜索</el-button>
  8.       <el-button round @click="addHander" size="large" type="primary">
  9.         <el-icon>
  10.           <DocumentAdd/>
  11.         </el-icon>
  12.         <span>新增</span>
  13.       </el-button>
  14.     </div>
  15.     <!--添加 end-->
  16.     <!--表格数据展示 start-->
  17.     <div class="data-table">
  18.       <el-table :data="dataList.list" style="width: 600px;">
  19.         <el-table-column label="id" prop="userTypeId" align="center" width="60"></el-table-column>
  20.         <el-table-column label="名称" prop="userTypeName" align="center" width="120">
  21.           <template #default="scope">
  22.             <el-tag round type="warning" >
  23.               {{ scope.row.userTypeName }}
  24.             </el-tag>
  25.           </template>
  26.         </el-table-column>
  27.         <el-table-column show-overflow-tooltip label="详情" prop="userTypeDesc" align="center"
  28.                          width="200"></el-table-column>
  29.         <el-table-column label="操作" align="center" width="220">
  30.           <template #default="scope">
  31.             <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
  32.             <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
  33.           </template>
  34.         </el-table-column>
  35.       </el-table>
  36.       <!--分页 start-->
  37.       <div class="page">
  38.         <el-pagination background
  39.                        layout="prev,pager,next,jumper"
  40.                        :default-page-size="defaultPageSize"
  41.                        :total="totalData"
  42.                        @current-change="currentChangeHaddler"></el-pagination>
  43.       </div>
  44.       <!--分页 end-->
  45.     </div>
  46.     <!--表格数据展示 end-->
  47.     <!--添加对话框 start-->
  48.     <el-dialog draggable destroy-on-close v-model="dialogAddVisible" title="添加" width="50%" center>
  49.       <el-form inline :model="addFormInfo" label-width="150px">
  50.         <el-form-item label="名称">
  51.           <el-input v-model="addFormInfo.userTypeName"></el-input>
  52.         </el-form-item>
  53.         <el-form-item label="详情">
  54.           <el-input type="textarea" rows="10" style="width: 500px" v-model="addFormInfo.userTypeDesc"></el-input>
  55.         </el-form-item>
  56.       </el-form>
  57.       <template #footer>
  58.             <span class="dialog-footer">
  59.                 <el-button @click="dialogAddVisible = false">取消</el-button>
  60.                 <el-button type="primary" @click="sureHandler">确定</el-button>
  61.             </span>
  62.       </template>
  63.     </el-dialog>
  64.     <!--添加对话框 end-->
  65.     <!--编辑对话框 start-->
  66.     <!--destroy-on-close:每次关闭对话框时直接销毁对话框,没有缓存-->
  67.     <el-dialog
  68.         draggable
  69.         destroy-on-close
  70.         v-model="dialogEditorVisible"
  71.         title="编辑"
  72.         width="50%"
  73.         center>
  74.       <el-form inline :model="editorFormInfo" label-width="150px">
  75.         <el-form-item label="名称">
  76.           <el-input v-model="editorFormInfo.userTypeName"></el-input>
  77.         </el-form-item>
  78.         <el-form-item label="详情">
  79.           <el-input type="textarea" rows="10" style="width: 500px" v-model="editorFormInfo.userTypeDesc"></el-input>
  80.         </el-form-item>
  81.       </el-form>
  82.       <template #footer>
  83.             <span class="dialog-footer">
  84.                 <el-button @click="dialogEditorVisible = false">取消</el-button>
  85.                 <el-button type="primary" @click="sureEditorHandler">确定</el-button>
  86.             </span>
  87.       </template>
  88.     </el-dialog>
  89.     <!--编辑对话框 end-->
  90.   </div>
  91. </template>
  92. <script setup>
  93. import axios from "../../api/index.js"
  94. import {onMounted, reactive, ref} from "vue";
  95. import {ElMessage} from "element-plus";
  96. //初始化分页查询数据
  97. const dataList = reactive({
  98.   list: []
  99. })
  100. //初始化总条数
  101. const totalData = ref(0)
  102. //当前页
  103. const currentPage = ref(1)
  104. //初始化分页显示条数
  105. const defaultPageSize = ref(10)
  106. //搜索初始化状态
  107. const searchInfo = ref("")
  108. //添加添加对话框控制器
  109. const dialogAddVisible = ref(false)
  110. //初始化添加对话框状态
  111. const addFormInfo = reactive({
  112.   userTypeName: "",
  113.   userTypeDesc: "",
  114. })
  115. //编辑对话框控制器
  116. const dialogEditorVisible = ref(false)
  117. //初始化编辑对话框状态
  118. const editorFormInfo = reactive({
  119.   userTypeId: '',
  120.   userTypeName: "",
  121.   userTypeDesc: "",
  122. })
  123. /**
  124.  * 网路请求:分页查询
  125.  *  */
  126. const http = (search, page, size) => {
  127.   axios.user_type_search({
  128.     search: search,
  129.     page: page,
  130.     size: size
  131.   }).then(res => {
  132.     if (res.data.code == 200) {
  133.       dataList.list = res.data.data.records
  134.       totalData.value = res.data.data.total
  135.     } else {
  136.       ElMessage.error(res.data.message)
  137.     }
  138.   })
  139. }
  140. onMounted(() => {
  141.   http(searchInfo.value, currentPage.value, defaultPageSize.value)
  142. })
  143. /**
  144.  * 分页
  145.  */
  146. const currentChangeHaddler = (nowPage) => {
  147.   http(searchInfo.value, nowPage, defaultPageSize.value)
  148.   currentPage.value = nowPage
  149. }
  150. /**
  151.  * 搜索按钮
  152.  */
  153. const searchHandle = () => {
  154.   http(searchInfo.value, currentPage.value, defaultPageSize.value)
  155. }
  156. /**
  157.  * 添加对话框弹出事件
  158.  */
  159. const addHander = () => {
  160.   dialogAddVisible.value = true
  161. }
  162. /**
  163.  * 添加对话框 确定事件
  164.  */
  165. const sureHandler = () => {
  166.   axios.user_type_add({
  167.     userTypeName: addFormInfo.userTypeName,
  168.     userTypeDesc: addFormInfo.userTypeDesc,
  169.   }).then(res => {
  170.     if (res.data.code == 200) {
  171.       dialogAddVisible.value = false
  172.       http(searchInfo.value, currentPage.value, defaultPageSize.value)
  173.     } else {
  174.       ElMessage.error(res.data.message)
  175.     }
  176.   })
  177. }
  178. /**
  179.  * 编辑对话框 弹出事件
  180.  *  */
  181. const handleEdit = (index, row) => {
  182.   dialogEditorVisible.value = true
  183.   axios.user_type_findById({
  184.     userTypeId: row.userTypeId
  185.   }).then(res => {
  186.     if (res.data.code == 200) {
  187.       editorFormInfo.userTypeId = res.data.data.userTypeId;
  188.       editorFormInfo.userTypeName = res.data.data.userTypeName;
  189.       editorFormInfo.userTypeDesc = res.data.data.userTypeDesc;
  190.     } else {
  191.       ElMessage.error(res.data.data.message)
  192.     }
  193.   })
  194. }
  195. /**
  196.  * 编辑对话框 确定事件
  197.  */
  198. const sureEditorHandler = () => {
  199.   axios.user_type_update({
  200.     userTypeId: editorFormInfo.userTypeId,
  201.     userTypeName: editorFormInfo.userTypeName,
  202.     userTypeDesc: editorFormInfo.userTypeDesc,
  203.   }).then(res => {
  204.     if (res.data.code == 200) {
  205.       dialogEditorVisible.value = false
  206.       http(searchInfo.value, currentPage.value, defaultPageSize.value)
  207.     } else {
  208.       ElMessage.error(res.data.message)
  209.     }
  210.   })
  211. }
  212. /**删除 */
  213. const handleDelete = (index, row) => {
  214.   ElMessageBox.confirm(
  215.       '确定删除么',
  216.       '删除',
  217.       {
  218.         confirmButtonText: '确定',
  219.         cancelButtonText: '取消',
  220.         type: 'warning',
  221.       }
  222.   ).then(() => {
  223.     //确认删除
  224.     axios.user_type_delete({
  225.       userTypeId: row.userTypeId
  226.     }).then(res => {
  227.       if (res.data.code == 200) {
  228.         ElMessage({
  229.           type: 'success',
  230.           message: "删除成功!!!",
  231.         })
  232.         //刷新
  233.         http(searchInfo.value, currentPage.value, defaultPageSize.value)
  234.       } else {
  235.         ElMessage({
  236.           type: 'error',
  237.           message: res.data.message,
  238.         })
  239.       }
  240.     })
  241.   }).catch(error => {
  242.     ElMessage({
  243.       type: 'info',
  244.       message: "取消删除",
  245.     })
  246.   })
  247. }
  248. </script>
  249. <style scoped>
  250. .data-container {
  251.   background: linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  252.   background: -o-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  253.   background: -ms-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  254.   background: -moz-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  255.   background: -webkit-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  256.   height: 800px;
  257. }
  258. .data-header {
  259.   padding: 20px;
  260. }
  261. .data-header .input {
  262.   width: 300px;
  263. }
  264. .data-table {
  265.   padding: 20px;
  266. }
  267. .page {
  268.   position: fixed;
  269.   right: 10px;
  270.   bottom: 10px;
  271. }
  272. </style>

用户

  1. <template>
  2.   <div class="data-container">
  3.     <!--添加 start-->
  4.     <div class="data-header">
  5.       <el-button round @click="addHander" size="large" type="primary">
  6.         <el-icon>
  7.           <DocumentAdd/>
  8.         </el-icon>
  9.         <span>新增</span>
  10.       </el-button>
  11.     </div>
  12.     <!--添加 end-->
  13.     <!--表格数据展示 start-->
  14.     <div class="data-table">
  15.       <el-table :data="dataList.list" style="width: 1400px;">
  16.         <el-table-column label="头像" prop="userImg" align="center">
  17.           <template #default="scope">
  18.             <img :src="scope.row.userImg" style="height:60px"/>
  19.           </template>
  20.         </el-table-column>
  21.         <el-table-column show-overflow-tooltip label="昵称" prop="nickname" align="center"
  22.                          width="120"></el-table-column>
  23.         <el-table-column label="账号" prop="username" align="center" width="140"></el-table-column>
  24.         <el-table-column label="手机号" prop="phone" align="center" width="140"></el-table-column>
  25.         <el-table-column show-overflow-tooltip label="邮箱" prop="email" align="center" width="160"></el-table-column>
  26.         <el-table-column label="性别" prop="userSex" align="center" width="60"></el-table-column>
  27.         <el-table-column label="状态" prop="userStatus" align="center">
  28.           <template #default="scope">
  29.             <el-tag round :type="scope.row.userStatus == '1' ? 'success' : 'info'">
  30.               {{ scope.row.userStatus == '1' ? '激活' : '未激活' }}
  31.             </el-tag>
  32.           </template>
  33.         </el-table-column>
  34.         <el-table-column show-overflow-tooltip label="个性签名" prop="signature" align="center"></el-table-column>
  35.         <el-table-column show-overflow-tooltip label="自我介绍" prop="userIntroduce" align="center"></el-table-column>
  36.         <el-table-column label="操作" align="center" width="300">
  37.           <template #default="scope">
  38.             <!-- 会员模块 start-->
  39.             <el-popover trigger="click" placement="top" :width="300" offset="20">
  40.               <template #reference>
  41.                 <el-button @click="handTypes(scope.$index, scope.row)" size="small" round type="warning">会员</el-button>
  42.               </template>
  43.               <div style="margin: 15px">
  44.                 <el-button @click="handleAddType(scope.$index, scope.row)" style="margin-right: 10px" type="primary"
  45.                            size="small">添加
  46.                 </el-button>
  47.                 <el-select v-model="choseTypeId" size="small">
  48.                   <el-option v-for="item in userTypeList.list" :key="item.userTypeId"
  49.                              :label="item.userTypeName"
  50.                              :value="item.userTypeId">
  51.                   </el-option>
  52.                 </el-select>
  53.               </div>
  54.               <div>
  55.                 <el-tag style="margin: 4px"
  56.                         type="warning"
  57.                         v-for="(item,index) in user_userTypeList.list" :key="index"
  58.                         @close="handleCloseType(scope.row,item.userTypeId)"
  59.                         closable round>
  60.                   {{ item.userTypeName }}
  61.                 </el-tag>
  62.               </div>
  63.             </el-popover>
  64.             <!--  会员模块 end-->
  65.             <el-button size="small" type="success" @click="handleColumn(scope.$index, scope.row)">专栏</el-button>
  66.             <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
  67.             <el-button size="small" type="primary" @click="handleStatus(scope.$index, scope.row)">状态</el-button>
  68.             <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
  69.           </template>
  70.         </el-table-column>
  71.       </el-table>
  72.       <!--分页 start-->
  73.       <div class="page">
  74.         <el-pagination background
  75.                        layout="prev,pager,next,jumper"
  76.                        :default-page-size="defaultPageSize"
  77.                        :total="totalData"
  78.                        @current-change="currentChangeHaddler"></el-pagination>
  79.       </div>
  80.       <!--分页 end-->
  81.     </div>
  82.     <!--表格数据展示 end-->
  83.     <!--添加对话框 start-->
  84.     <el-dialog draggable destroy-on-close v-model="dialogAddVisible" title="添加" width="70%" center>
  85.       <el-form inline :model="addFormInfo" label-width="150px">
  86.         <el-form-item label="头像">
  87.           <el-input v-model="addFormInfo.userImg"></el-input>
  88.         </el-form-item>
  89.         <el-form-item label="昵称">
  90.           <el-input v-model="addFormInfo.nickname"></el-input>
  91.         </el-form-item>
  92.         <el-form-item label="账号">
  93.           <el-input v-model="addFormInfo.username"></el-input>
  94.         </el-form-item>
  95.         <el-form-item label="密码">
  96.           <el-input v-model="addFormInfo.password"></el-input>
  97.         </el-form-item>
  98.         <el-form-item label="手机号">
  99.           <el-input v-model="addFormInfo.phone"></el-input>
  100.         </el-form-item>
  101.         <el-form-item label="邮箱">
  102.           <el-input v-model="addFormInfo.email"></el-input>
  103.         </el-form-item>
  104.         <el-form-item label="性别">
  105.           <el-input v-model="addFormInfo.userSex"></el-input>
  106.         </el-form-item>
  107.         <el-form-item label="个性签名">
  108.           <el-input type="textarea" rows="3" style="width: 800px" v-model="addFormInfo.signature"></el-input>
  109.         </el-form-item>
  110.         <el-form-item label="自我介绍">
  111.           <el-input type="textarea" rows="6" style="width: 800px" v-model="addFormInfo.userIntroduce"></el-input>
  112.         </el-form-item>
  113.       </el-form>
  114.       <template #footer>
  115.             <span class="dialog-footer">
  116.                 <el-button @click="dialogAddVisible = false">取消</el-button>
  117.                 <el-button type="primary" @click="sureHandler">确定</el-button>
  118.             </span>
  119.       </template>
  120.     </el-dialog>
  121.     <!--添加对话框 end-->
  122.     <!--编辑对话框 start-->
  123.     <!--destroy-on-close:每次关闭对话框时直接销毁对话框,没有缓存-->
  124.     <el-dialog
  125.         draggable
  126.         destroy-on-close
  127.         v-model="dialogEditorVisible"
  128.         title="编辑"
  129.         width="70%"
  130.         center>
  131.       <el-form inline :model="editorFormInfo" label-width="150px">
  132.         <el-form-item label="头像">
  133.           <el-input v-model="editorFormInfo.userImg"></el-input>
  134.         </el-form-item>
  135.         <el-form-item label="昵称">
  136.           <el-input v-model="editorFormInfo.nickname"></el-input>
  137.         </el-form-item>
  138.         <el-form-item label="账号">
  139.           <el-input v-model="editorFormInfo.username"></el-input>
  140.         </el-form-item>
  141.         <el-form-item label="密码">
  142.           <el-input v-model="editorFormInfo.password"></el-input>
  143.         </el-form-item>
  144.         <el-form-item label="手机号">
  145.           <el-input v-model="editorFormInfo.phone"></el-input>
  146.         </el-form-item>
  147.         <el-form-item label="邮箱">
  148.           <el-input v-model="editorFormInfo.email"></el-input>
  149.         </el-form-item>
  150.         <el-form-item label="性别">
  151.           <el-input v-model="editorFormInfo.userSex"></el-input>
  152.         </el-form-item>
  153.         <el-form-item label="个性签名">
  154.           <el-input type="textarea" rows="3" style="width: 800px" v-model="editorFormInfo.signature"></el-input>
  155.         </el-form-item>
  156.         <el-form-item label="自我介绍">
  157.           <el-input type="textarea" rows="6" style="width: 800px" v-model="editorFormInfo.userIntroduce"></el-input>
  158.         </el-form-item>
  159.       </el-form>
  160.       <template #footer>
  161.             <span class="dialog-footer">
  162.                 <el-button @click="dialogEditorVisible = false">取消</el-button>
  163.                 <el-button type="primary" @click="sureEditorHandler">确定</el-button>
  164.             </span>
  165.       </template>
  166.     </el-dialog>
  167.     <!--编辑对话框 end-->
  168.   </div>
  169. </template>
  170. <script setup>
  171. import axios from "@/utils/request.js"
  172. import axios2 from "../../api/index.js"
  173. import {onMounted, reactive, ref} from "vue";
  174. import {ElMessage} from "element-plus";
  175. import {useRouter} from 'vue-router'
  176. const router = useRouter()
  177. //初始化分页查询数据
  178. const dataList = reactive({
  179.   list: []
  180. })
  181. //初始化用户类型列表
  182. const userTypeList = reactive({list: []})
  183. //用户的类型
  184. const user_userTypeList= reactive({list: []})
  185. //选择的类型id
  186. const choseTypeId=ref(0)
  187. //初始化总条数
  188. const totalData = ref(0)
  189. //当前页
  190. const currentPage = ref(1)
  191. //初始化分页显示条数
  192. const defaultPageSize = ref(10)
  193. //添加添加对话框控制器
  194. const dialogAddVisible = ref(false)
  195. //初始化添加对话框状态
  196. const addFormInfo = reactive({
  197.   userImg: '',
  198.   nickname: '',
  199.   username: '',
  200.   password: '',
  201.   phone: '',
  202.   email: '',
  203.   userSex: '',
  204.   signature: '',
  205.   userIntroduce: '',
  206.   userStatus: '1',
  207. })
  208. //编辑对话框控制器
  209. const dialogEditorVisible = ref(false)
  210. //初始化编辑对话框状态
  211. const editorFormInfo = reactive({
  212.   userId: '',
  213.   userImg: '',
  214.   nickname: '',
  215.   username: '',
  216.   password: '',
  217.   phone: '',
  218.   email: '',
  219.   userSex: '',
  220.   signature: '',
  221.   userIntroduce: '',
  222.   userStatus: '',
  223. })
  224. /**
  225.  * 网路请求:分页查询
  226.  *  */
  227. const http = (page, size) => {
  228.   axios.get('back/user/search', {
  229.     params: {
  230.       page: page,
  231.       size: size
  232.     }
  233.   }).then(res => {
  234.     if (res.data.code == 200) {
  235.       dataList.list = res.data.data.records
  236.       totalData.value = res.data.data.total
  237.     } else {
  238.       ElMessage.error(res.data.message)
  239.     }
  240.   })
  241.   axios2.user_type_findAll().then(res => {
  242.     userTypeList.list = res.data.data
  243.   })
  244. }
  245. onMounted(() => {
  246.   http(currentPage.value, defaultPageSize.value)
  247. })
  248. /**
  249.  * 分页
  250.  */
  251. const currentChangeHaddler = (nowPage) => {
  252.   http(nowPage, defaultPageSize.value)
  253.   currentPage.value = nowPage
  254. }
  255. //专栏
  256. const handleColumn = (index, row) => {
  257.   router.push("/blog/column/" + row.userId)
  258. }
  259. /**
  260.  * 添加对话框弹出事件
  261.  */
  262. const addHander = () => {
  263.   dialogAddVisible.value = true
  264. }
  265. /**
  266.  * 添加对话框 确定事件
  267.  */
  268. const sureHandler = () => {
  269.   axios.post('back/user/add', {
  270.     userImg: addFormInfo.userImg,
  271.     nickname: addFormInfo.nickname,
  272.     username: addFormInfo.username,
  273.     password: addFormInfo.password,
  274.     phone: addFormInfo.phone,
  275.     email: addFormInfo.email,
  276.     userSex: addFormInfo.userSex,
  277.     signature: addFormInfo.signature,
  278.     userIntroduce: addFormInfo.userIntroduce,
  279.     userStatus: addFormInfo.userStatus,
  280.   }).then(res => {
  281.     if (res.data.code == 200) {
  282.       dialogAddVisible.value = false
  283.       http(currentPage.value, defaultPageSize.value)
  284.     } else {
  285.       ElMessage.error(res.data.message)
  286.     }
  287.   })
  288. }
  289. /**
  290.  * 编辑对话框 弹出事件
  291.  *  */
  292. const handleEdit = (index, row) => {
  293.   dialogEditorVisible.value = true
  294.   axios.get('back/user/findById', {
  295.     params: {
  296.       userId: row.userId
  297.     }
  298.   }).then(res => {
  299.     if (res.data.code == 200) {
  300.       editorFormInfo.userId = res.data.data.userId;
  301.       editorFormInfo.userImg = res.data.data.userImg;
  302.       editorFormInfo.nickname = res.data.data.nickname;
  303.       editorFormInfo.username = res.data.data.username;
  304.       editorFormInfo.password = res.data.data.password;
  305.       editorFormInfo.phone = res.data.data.phone;
  306.       editorFormInfo.email = res.data.data.email;
  307.       editorFormInfo.userSex = res.data.data.userSex;
  308.       editorFormInfo.signature = res.data.data.signature;
  309.       editorFormInfo.userIntroduce = res.data.data.userIntroduce;
  310.       editorFormInfo.userStatus = res.data.data.userStatus;
  311.     } else {
  312.       ElMessage.error(res.data.data.message)
  313.     }
  314.   })
  315. }
  316. /**
  317.  * 编辑对话框 确定事件
  318.  */
  319. const sureEditorHandler = () => {
  320.   axios.post('back/user/update', {
  321.     userId: editorFormInfo.userId,
  322.     userImg: editorFormInfo.userImg,
  323.     nickname: editorFormInfo.nickname,
  324.     username: editorFormInfo.username,
  325.     password: editorFormInfo.password,
  326.     phone: editorFormInfo.phone,
  327.     email: editorFormInfo.email,
  328.     userSex: editorFormInfo.userSex,
  329.     signature: editorFormInfo.signature,
  330.     userIntroduce: editorFormInfo.userIntroduce,
  331.     userStatus: editorFormInfo.userStatus,
  332.   }).then(res => {
  333.     if (res.data.code == 200) {
  334.       dialogEditorVisible.value = false
  335.       http(currentPage.value, defaultPageSize.value)
  336.     } else {
  337.       //添加失败:给出提示信息(element-plus/反馈组件/message信息提示)
  338.       ElMessage.error(res.data.message)
  339.     }
  340.   })
  341. }
  342. /**删除 */
  343. const handleDelete = (index, row) => {
  344.   ElMessageBox.confirm(
  345.       '确定删除么',
  346.       '删除',
  347.       {
  348.         confirmButtonText: '确定',
  349.         cancelButtonText: '取消',
  350.         type: 'warning',
  351.       }
  352.   ).then(() => {
  353.     //确认删除
  354.     axios.delete("back/user/delete", {
  355.       params: {
  356.         userId: row.userId
  357.       }
  358.     }).then(res => {
  359.       if (res.data.code == 200) {
  360.         ElMessage({
  361.           type: 'success',
  362.           message: "删除成功!!!",
  363.         })
  364.         //刷新
  365.         http(currentPage.value, defaultPageSize.value)
  366.       } else {
  367.         ElMessage({
  368.           type: 'error',
  369.           message: res.data.message,
  370.         })
  371.       }
  372.     })
  373.   }).catch(error => {
  374.     ElMessage({
  375.       type: 'info',
  376.       message: "取消删除",
  377.     })
  378.   })
  379. }
  380. /**
  381.  * 封装查询用户拥有的会员
  382.  */
  383. const searchTypes = (userId) => {
  384.   axios2.user_type_findUserTypeByUserId({
  385.     userId: userId
  386.   }).then(res => {
  387.     if (res.data.code == 200) {
  388.       user_userTypeList.list = res.data.data
  389.     }
  390.   })
  391. }
  392. /**
  393.  * 点击类型事件
  394.  */
  395. const handTypes = (index, row) => {
  396.   //查询博客拥有的标签
  397.   searchTypes(row.userId)
  398.   //查询所有标签列表
  399.   axios2.user_type_findAll().then(res => {
  400.     if (res.data.code == 200) {
  401.       userTypeList.list = res.data.data
  402.     }
  403.   })
  404. }
  405. /**
  406.  * 添加类型
  407.  */
  408. const handleAddType = (index, row) => {
  409.   axios2.user_type_addUserType({
  410.     userId: row.userId,
  411.     userTypeId: choseTypeId.value
  412.   }).then(res => {
  413.     if (res.data.code == 200) {
  414.       ElMessage.success('添加成功')
  415.       //查询用户的类型
  416.       searchTypes(row.userId)
  417.     }
  418.   })
  419. }
  420. /**
  421.  * 删除类型
  422.  */
  423. const handleCloseType = (row, userTypeId) => {
  424.   axios2.user_type_deleteUserType({
  425.     userId: row.userId,
  426.     userTypeId: userTypeId,
  427.   }).then(res => {
  428.     if (res.data.code == 200) {
  429.       ElMessage.success('删除成功')
  430.       //查询用户的类型
  431.       searchTypes(row.userId)
  432.     }
  433.   })
  434. }
  435. /**
  436.  * 修改用户状态
  437.  */
  438. const handleStatus = (index, row) => {
  439.   axios.post("back/user/updateStatus", {userId: row.userId}).then(res => {
  440.     if (res.data.code == 200) {
  441.       ElMessage.success("状态已更改")
  442.       http(currentPage.value, defaultPageSize.value)
  443.     }
  444.   })
  445. }
  446. </script>
  447. <style scoped>
  448. .data-container {
  449.   background: linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  450.   background: -o-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  451.   background: -ms-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  452.   background: -moz-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  453.   background: -webkit-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
  454.   height: 800px;
  455. }
  456. .data-header {
  457.   padding: 20px;
  458. }
  459. .data-table {
  460.   padding: 20px;
  461. }
  462. .page {
  463.   position: fixed;
  464.   right: 10px;
  465.   bottom: 10px;
  466. }
  467. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/395897
推荐阅读
相关标签
  

闽ICP备14008679号