#
luxiaotao1123
2024-01-30 f955449bf78c8704290b48612218a66e9a7b9eed
#
5个文件已修改
3个文件已添加
205 ■■■■■ 已修改文件
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/annotation/OperationLog.java 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/controller/AuthController.java 66 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/controller/BaseController.java 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/controller/param/UpdatePasswordParam.java 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/entity/Menu.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/service/UserService.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/service/impl/UserServiceImpl.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/utils/Utils.java 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/annotation/OperationLog.java
New file
@@ -0,0 +1,41 @@
package com.zy.asrs.wcs.common.annotation;
import java.lang.annotation.*;
/**
 * 操作日志记录注解
 *
 * @author vincent
 * @since 2020-03-21 17:03:08
 */
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLog {
    /**
     * 操作功能
     */
    String value() default "";
    /**
     * 操作模块
     */
    String module() default "";
    /**
     * 备注
     */
    String comments() default "";
    /**
     * 是否记录请求参数
     */
    boolean param() default true;
    /**
     * 是否记录返回结果
     */
    boolean result() default true;
}
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/controller/AuthController.java
@@ -1,23 +1,27 @@
package com.zy.asrs.wcs.sys.controller;
import com.zy.asrs.common.web.BaseController;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.common.R;
import com.zy.asrs.wcs.common.annotation.OperationLog;
import com.zy.asrs.wcs.common.config.ConfigProperties;
import com.zy.asrs.wcs.common.security.JwtSubject;
import com.zy.asrs.wcs.sys.controller.param.LoginParam;
import com.zy.asrs.wcs.sys.controller.param.UpdatePasswordParam;
import com.zy.asrs.wcs.sys.controller.result.LoginResult;
import com.zy.asrs.wcs.sys.entity.Menu;
import com.zy.asrs.wcs.sys.entity.User;
import com.zy.asrs.wcs.sys.entity.UserLogin;
import com.zy.asrs.wcs.sys.service.RoleMenuService;
import com.zy.asrs.wcs.sys.service.UserLoginService;
import com.zy.asrs.wcs.sys.service.UserService;
import com.zy.asrs.wcs.utils.JwtUtil;
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 com.zy.asrs.wcs.utils.Utils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * 认证控制器
@@ -34,6 +38,8 @@
    private UserService userService;
    @Resource
    private UserLoginService userLoginService;
    @Resource
    private RoleMenuService roleMenuService;
    @PostMapping("/login")
    public R login(@RequestBody LoginParam param, HttpServletRequest request) {
@@ -55,5 +61,55 @@
        return R.ok("登录成功").add(new LoginResult(accessToken, user));
    }
    @GetMapping("/auth/user")
    public R userInfo() {
        return R.ok(userService.getByIdRel(getLoginUserId()));
    }
    @GetMapping("/auth/menu")
    public R userMenu() {
        List<Menu> menus = roleMenuService.listMenuByUserId(getLoginUserId(), Menu.TYPE_MENU);
        return R.ok().add(Utils.toTreeData(menus, 0, Menu::getParentId, Menu::getId, Menu::setChildren));
    }
    @PreAuthorize("hasAuthority('sys:auth:user')")
    @OperationLog
    @PutMapping("/auth/user")
    public R updateInfo(@RequestBody User user) {
        user.setId(getLoginUserId());
        // 不能修改的字段
        user.setUsername(null);
        user.setPassword(null);
        user.setEmailVerified(null);
        user.setHostId(null);
        user.setStatus(null);
        if (userService.updateById(user)) {
            return R.ok().add(userService.getByIdRel(user.getId()));
        }
        return R.error("保存失败");
    }
    @PreAuthorize("hasAuthority('sys:auth:password')")
    @OperationLog
    @PutMapping("/auth/password")
    public R updatePassword(@RequestBody UpdatePasswordParam param) {
        if (Cools.isEmpty(param.getOldPassword(), param.getPassword())) {
            return R.error("参数不能为空");
        }
        Long userId = getLoginUserId();
        if (userId == null) {
            return R.error("未登录");
        }
        if (!userService.comparePassword(userService.getById(userId).getPassword(), param.getOldPassword())) {
            return R.error("原密码输入不正确");
        }
        User user = new User();
        user.setId(userId);
        user.setPassword(userService.encodePassword(param.getPassword()));
        if (userService.updateById(user)) {
            return R.ok("修改成功");
        }
        return R.error("修改失败");
    }
}
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/controller/BaseController.java
New file
@@ -0,0 +1,32 @@
package com.zy.asrs.wcs.sys.controller;
import com.zy.asrs.wcs.sys.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
/**
 * Created by vincent on 1/30/2024
 */
public class BaseController {
    public User getLoginUser() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null) {
                Object object = authentication.getPrincipal();
                if (object instanceof User) {
                    return (User) object;
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
    public Long getLoginUserId() {
        User loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getId();
    }
}
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/controller/param/UpdatePasswordParam.java
New file
@@ -0,0 +1,16 @@
package com.zy.asrs.wcs.sys.controller.param;
import lombok.Data;
import java.io.Serializable;
@Data
public class UpdatePasswordParam implements Serializable {
    private static final long serialVersionUID = 1L;
    private String oldPassword;
    private String password;
}
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/entity/Menu.java
@@ -1,6 +1,7 @@
package com.zy.asrs.wcs.sys.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zy.asrs.framework.common.Cools;
@@ -15,6 +16,7 @@
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Data
@TableName("sys_menu")
@@ -160,6 +162,9 @@
    @ApiModelProperty(value= "备注")
    private String memo;
    @TableField(exist = false)
    private List<Menu> children;
    public Menu() {}
    public Menu(String name,Long parentId,String parentName,String path,String pathName,String route,String brief,String code,Integer type,String authority,String icon,Integer sort,String meta,Long hostId,Integer status,Integer deleted,Date createTime,Long createBy,Date updateTime,Long updateBy,String memo) {
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/service/UserService.java
@@ -7,6 +7,8 @@
    User getByUsername(String username, Long hostId);
    User getByIdRel(Long userId);
    boolean comparePassword(String dbPassword, String inputPassword);
    String encodePassword(String password);
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/sys/service/impl/UserServiceImpl.java
@@ -36,6 +36,16 @@
    }
    @Override
    public User getByIdRel(Long userId) {
        User user = this.getById(userId);
        if (user != null) {
            user.setRoles(userRoleService.listByUserId(user.getId()));
            user.setAuthorities(roleMenuService.listMenuByUserId(user.getId(), null));
        }
        return user;
    }
    @Override
    public boolean comparePassword(String dbPassword, String inputPassword) {
        return bCryptPasswordEncoder.matches(inputPassword, dbPassword);
    }
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/utils/Utils.java
@@ -3,7 +3,11 @@
import com.zy.asrs.framework.common.Cools;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
/**
 * Created by vincent on 2023/3/14
@@ -11,6 +15,35 @@
public class Utils {
    /**
     * List转为树形结构
     *
     * @param data           List
     * @param parentId       顶级的parentId
     * @param parentIdMapper 获取parentId的Function
     * @param idMapper       获取id的Function
     * @param consumer       赋值children的Consumer
     * @param <T>            数据的类型
     * @param <R>            parentId的类型
     * @return List<T>
     */
    public static <T, R> List<T> toTreeData(List<T> data, R parentId,
                                            Function<? super T, ? extends R> parentIdMapper,
                                            Function<? super T, ? extends R> idMapper,
                                            BiConsumer<T, List<T>> consumer) {
        List<T> result = new ArrayList<>();
        for (T d : data) {
            R dParentId = parentIdMapper.apply(d);
            if (parentId.equals(dParentId)) {
                R dId = idMapper.apply(d);
                List<T> children = toTreeData(data, dId, parentIdMapper, idMapper, consumer);
                consumer.accept(d, children);
                result.add(d);
            }
        }
        return result;
    }
    /**
     * 数组倒序
     * @param bytes
     * @param <T>