From c13207a19a92c845fcb8c356e01f073142d9084e Mon Sep 17 00:00:00 2001
From: ZengTao <2773468879@qq.com>
Date: 星期五, 19 一月 2024 14:32:42 +0800
Subject: [PATCH] 测试后代码更新

---
 springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java |  201 ++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 169 insertions(+), 32 deletions(-)

diff --git a/springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java b/springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java
index 2200978..517cc19 100644
--- a/springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java
+++ b/springboot-vue3/src/main/java/com/example/springboot/controller/UserController.java
@@ -1,56 +1,193 @@
 package com.example.springboot.controller;
 
-import com.example.springboot.common.Result;
+import com.example.springboot.security.constant.SystemConstant;
+import com.example.springboot.security.util.SecurityUtil;
 import com.example.springboot.entity.User;
-import com.example.springboot.mapper.UserMapper;
+import com.example.springboot.entity.vo.Result;
+import com.example.springboot.entity.vo.UserVo;
 import com.example.springboot.service.UserService;
-
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.authz.annotation.RequiresAuthentication;
+import org.apache.shiro.authz.annotation.RequiresPermissions;
+import org.apache.shiro.authz.annotation.RequiresRoles;
+import org.apache.shiro.crypto.hash.SimpleHash;
+import org.apache.shiro.util.ByteSource;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
 
-import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
-//@CrossOrigin
 @RestController
-@RequestMapping("/user")
+@Slf4j
+@RequestMapping("/api/user")
+@Api(tags = "鐢ㄦ埛")
 public class UserController {
-
     @Autowired
-    UserMapper userMapper;
+    private UserService userService;
 
-    @Autowired
-    UserService userservice;
 
-    @GetMapping("/page") // /user/all?name=xxxx
-    public Result selectAll(@RequestParam String name, @RequestParam Integer start, @RequestParam Integer pageSize) {
-        // return userMapper.selectAll("%" + name + "%");
-        List<User> userList = userMapper.selectPage(name, start, pageSize);
-        Integer total = userMapper.selectTotal(name);
+    @ApiOperation(value = "淇敼瀵嗙爜")
+    @PostMapping("/changePassword")
+    @RequiresAuthentication
+    public Result changePassword(@RequestBody Map<String, String> request) {
+        User currentUser = SecurityUtil.getCurrentUser();
+        String oldPassword = request.get("oldPassword");
+        String newPassword = request.get("newPassword");
 
-        Map<String, Object> map = new HashMap<>();
-        map.put("list", userList);
-        map.put("total", total);
-        return Result.success(map);
+        // 鏍¢獙鏃у瘑鐮佹槸鍚︽纭�
+        if (!verifyPassword(currentUser, oldPassword)) {
+            return Result.fail("鏃у瘑鐮佷笉姝g‘");
+        }
+
+        // 鏇存柊瀵嗙爜
+        updatePassword(currentUser, newPassword);
+
+        return Result.success("瀵嗙爜淇敼鎴愬姛");
+    }
+    /**
+     * 楠岃瘉瀵嗙爜鏄惁姝g‘
+     */
+    private boolean verifyPassword(User user, String password) {
+        Object salt = ByteSource.Util.bytes(SystemConstant.JWT_SECRET_KEY);
+        String md5 = new SimpleHash("MD5", password, salt, 1024).toHex();
+        return md5.equals(user.getPassword());
     }
 
-    @PostMapping("/save")
-    public Result save(@RequestBody User user) {
-        userservice.Save(user);
+    /**
+     * 鏇存柊瀵嗙爜
+     */
+    private void updatePassword(User user, String newPassword) {
+        Object salt = ByteSource.Util.bytes(SystemConstant.JWT_SECRET_KEY);
+        String md5 = new SimpleHash("MD5", newPassword, salt, 1024).toHex();
+        user.setPassword(md5);
+        userService.saveOrUpdate(user);
+    }
+
+    @ApiOperation(value = "閲嶇疆瀵嗙爜")
+    @PostMapping("/resetPass")
+    @RequiresRoles({"admin"})
+
+    public Result resetPassword(@RequestBody UserVo userVO) {
+        User user = userService.getById(userVO.getId());
+        if (user == null) {
+            return Result.fail("鐢ㄦ埛涓嶅瓨鍦�");
+        }
+
+        // 鐢熸垚榛樿瀵嗙爜
+        String newPassword = "123456";
+
+        // 鏇存柊瀵嗙爜
+        updatePassword(user, newPassword);
+
+        return Result.success("瀵嗙爜閲嶇疆鎴愬姛锛屾柊瀵嗙爜涓猴細" + newPassword);
+    }
+
+    /**
+     * 鏇存柊瀵嗙爜
+     */
+
+
+
+    @ApiOperation(value = "鍒嗛〉鏌ヨ鐢ㄦ埛")
+    @GetMapping("/selectPage")
+//    @RequiresRoles({"admin"})
+    @RequiresPermissions({"user:select"})
+    public Result selectPage(UserVo userVO) {
+        return Result.success(userService.selectPage(userVO));
+    }
+
+    @ApiOperation(value = "娣诲姞鎴栦慨鏀圭敤鎴�")
+    @PostMapping("/saveOrUpdate")
+    @RequiresRoles({"admin"})
+    @RequiresPermissions({"user:update", "user:add"})
+    public Result saveOrUpdate(@RequestBody User user) {
+        if ("admin".equals(user.getUsername())) {
+            return Result.fail("绠$悊鍛樹笉鍙互琚鐢�");
+        }
+        Integer count = userService.lambdaQuery()
+                .eq(User::getUsername, user.getUsername())
+                .ne(user.getId() != null, User::getId, user.getId())
+                .count();
+        if (count > 0) {
+            return Result.fail("鐢ㄦ埛鍚嶅凡瀛樺湪");
+        }
+
+        // 濡傛灉瀵嗙爜鏈慨鏀癸紝鍒欎笉杩涜鍔犲瘑鎿嶄綔
+        if (user.getId() != null) {
+            User existingUser = userService.getById(user.getId());
+            if (existingUser != null && existingUser.getPassword().equals(user.getPassword())) {
+                user.setPassword(existingUser.getPassword());
+            } else {
+                // 瀵嗙爜鍙戠敓浜嗗彉鍖栵紝杩涜鍔犲瘑鎿嶄綔
+                Object salt = ByteSource.Util.bytes(SystemConstant.JWT_SECRET_KEY);
+                String md5 = new SimpleHash("MD5", user.getPassword(), salt, 1024).toHex();
+                user.setPassword(md5);
+            }
+        }
+
+        userService.saveOrUpdate(user);
         return Result.success();
     }
 
-    @PutMapping("/update")
-    public Result update(@RequestBody User user) {
-       userservice.Save(user);
+    @ApiOperation(value = "閫氳繃id鍒犻櫎鐢ㄦ埛")
+    @PostMapping("/removeById")
+    @RequiresRoles({"admin"})
+    @RequiresPermissions({"user:delete"})
+    public Result removeById(@RequestBody UserVo userVO) {
+        userService.removeById(userVO.getId());
         return Result.success();
     }
 
-    @DeleteMapping("/del")
-    public Result delete(@RequestParam Integer id) {
-        userMapper.delete(id);
-        return Result.success();
+    @ApiOperation(value = "閫氳繃id鏌ヨ鐢ㄦ埛")
+    @GetMapping("/getById")
+    @RequiresPermissions({"user:select"})
+    public Result selectById(UserVo userVO) {
+        return Result.success(userService.getById(userVO.getId()));
     }
 
-}
+
+
+    @ApiOperation(value = "娉ㄩ攢鐧诲綍锛屽墠鎻愭槸鍦ㄧ櫥褰曠姸鎬�")
+    @PostMapping("/loginOut")
+    public Result loginOut() {
+        User currentUser = SecurityUtil.getCurrentUser();
+        if (currentUser == null) {
+            return Result.fail("鎮ㄦ殏鏈櫥褰�");
+        }
+        SecurityUtils.getSubject().logout();
+        return Result.success("娉ㄩ攢鎴愬姛");
+    }
+
+    private class UpdatePasswordRequest {
+    }
+
+    @ApiOperation(value = "鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛鐨勭敤鎴峰悕")
+    @GetMapping("/currentUsername")
+    @RequiresAuthentication // 纭繚鍙湁璁よ瘉杩囩殑鐢ㄦ埛鍙互璁块棶姝ゆ帴鍙�
+    public Result getCurrentUsername() {
+        User currentUser = SecurityUtil.getCurrentUser();
+        if (currentUser != null) {
+            // 鍋囪User绫绘湁getUsername鏂规硶鍙互鑾峰彇鐢ㄦ埛鍚�
+            String username = currentUser.getUsername();
+            return Result.success(username);
+        } else {
+            return Result.fail("鐢ㄦ埛鏈櫥褰曟垨浼氳瘽宸茶繃鏈�");
+        }
+    }
+
+
+
+
+
+
+
+
+
+}
\ No newline at end of file

--
Gitblit v1.8.0