All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ape9527.core.controller.SysUserController Maven / Gradle / Ivy

The newest version!
package com.ape9527.core.controller;

import com.ape9527.auth.annotation.LoginAuth;
import com.ape9527.auth.service.SysDeptService;
import com.ape9527.auth.utils.UserUtil;
import com.ape9527.core.constant.HttpStatus;
import com.ape9527.core.controller.vo.EditPasswordVo;
import com.ape9527.core.controller.vo.ResetPasswordVo;
import com.ape9527.core.controller.vo.UserListVo;
import com.ape9527.core.controller.vo.UserSaveVo;
import com.ape9527.core.model.AjaxResult;
import com.ape9527.core.service.BaseDataService;
import com.ape9527.utils.string.StringUtil;
import com.ape9527.core.entity.SysUser;
import com.ape9527.core.model.base.BaseQueryVo;
import com.ape9527.core.model.base.BaseSaveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户 控制层
 *
 * @author YuanShuai[[email protected]]
 */
@Api(tags = "SysUserController", description = "系统用户控制层")
@RestController
@RequestMapping("/api/sys/user")
public class SysUserController extends BaseController {

    private final BaseDataService baseDataService;

    private final SysDeptService deptService;

    public SysUserController(SysDeptService deptService, BaseDataService baseDataService) {
        this.deptService = deptService;
        this.baseDataService = baseDataService;
    }

    @ApiOperation("获取个人信息")
    @PostMapping("/profile")
    @LoginAuth
    public AjaxResult profile() {
        String userId = getLoginUserId();
        BaseQueryVo vo = new BaseQueryVo("sys_user");
        vo.setUuid(userId);
        SysUser sysUser = baseDataService.queryById(vo, SysUser.class);
        sysUser.setPassword(null);
        return AjaxResult.success(sysUser);
    }

    @ApiOperation("查询用户列表")
    @PostMapping("/list")
    @LoginAuth
    public AjaxResult getList(@RequestBody UserListVo userListVo) {
        BaseQueryVo vo = new BaseQueryVo("sys_user");
        vo.enablePage(true, userListVo.getPageNum(), userListVo.getPageSize());
        vo.setEnableCorrelateQuery(true);
        if (userListVo.getUserName() != null) {
            vo.like("user_name", "%" + userListVo.getUserName() + "%");
        }
        if (userListVo.getUserStatus() != null) {
            vo.eq("user_status", userListVo.getUserStatus());
        }
        if (!"".equals(userListVo.getDeptCode())) {
            //获取部门下所有子部门
            List deptCodes = deptService.queryChildDeptCodes(userListVo.getDeptCode());
            vo.in("dept_code", deptCodes);
        }

        return AjaxResult.success(baseDataService.queryList(vo));
    }

    @ApiOperation("保存用户")
    @PostMapping("/save")
    @LoginAuth
    public AjaxResult userSave(@RequestBody UserSaveVo userSaveVo) {
        BaseSaveVo vo = new BaseSaveVo("sys_user");
        if (StringUtil.isNotEmpty(userSaveVo.getUuid())) {
            vo.eq("uuid", userSaveVo.getUuid());
        }
        vo.addField("user_name", userSaveVo.getUser_name());
        vo.addField("nick_name", userSaveVo.getNick_name());
        vo.addField("dept_code", userSaveVo.getDept_code());
        vo.addField("role_code", userSaveVo.getRole_code());
        vo.addField("user_status", userSaveVo.getUser_status());
        int flag;
        if (userSaveVo.getUuid() == null) {
            BaseQueryVo queryVo = new BaseQueryVo("sys_user");
            queryVo.eq("user_name", userSaveVo.getUser_name()).eq("del", 0);
            Map map = baseDataService.queryOne(queryVo);
            if (map != null) {
                return AjaxResult.error("用户【" + userSaveVo.getUser_name() + "】已存在!");
            }
            vo.addField("password", UserUtil.passwordEncrypt(userSaveVo.getUser_name(), "123456"));
            flag = baseDataService.save(vo);
        } else {
            flag = baseDataService.update(vo);
        }
        return flag == 1 ? AjaxResult.success() : AjaxResult.error("保存失败!");
    }

    @ApiOperation("重置密码")
    @LoginAuth
    @PostMapping("/password/reset")
    public AjaxResult resetPassword(@RequestBody ResetPasswordVo resetVo) {
        BaseQueryVo vo = new BaseQueryVo("sys_user");
        vo.setUuid(resetVo.getUuid());
        SysUser sysUser = baseDataService.queryById(vo, SysUser.class);
        String newPassword = UserUtil.passwordEncrypt(sysUser.getUserName(), resetVo.getNewPassword());

        BaseSaveVo saveVo = new BaseSaveVo("sys_user");
        saveVo.eq("uuid", resetVo.getUuid());
        saveVo.addField("password", newPassword);
        return baseDataService.update(saveVo) == 1 ? AjaxResult.success() : AjaxResult.error("重置密码失败!");
    }

    @ApiOperation("修改密码")
    @LoginAuth
    @PostMapping("/password/edit")
    public AjaxResult editPassword(@RequestBody EditPasswordVo editVo) {
        String userId = getLoginUserId();
        BaseQueryVo vo = new BaseQueryVo("sys_user");
        vo.setUuid(userId);
        SysUser sysUser = baseDataService.queryById(vo, SysUser.class);

        String oldPassword = UserUtil.passwordEncrypt(sysUser.getUserName(), editVo.getOldPassword());
        if(!oldPassword.equals(sysUser.getPassword())){
            return AjaxResult.error(HttpStatus.AUTH_OLD_PASSWORD_ERROR);
        }

        String newPassword = UserUtil.passwordEncrypt(sysUser.getUserName(), editVo.getNewPassword());

        BaseSaveVo saveVo = new BaseSaveVo("sys_user");
        saveVo.eq("uuid", userId);
        saveVo.addField("password", newPassword);
        return baseDataService.update(saveVo) == 1 ? AjaxResult.success() : AjaxResult.error("修改密码失败!");
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy