SysProfileController.java 3.73 KB
package com.huaheng.system.controller;

import com.huaheng.system.api.domain.SysRole;
import com.huaheng.system.service.CompanyService;
import com.huaheng.system.service.ISysRoleService;
import com.huaheng.system.service.ISysUserService;
import com.huaheng.system.service.WarehouseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.huaheng.common.core.web.controller.BaseController;
import com.huaheng.common.core.web.domain.AjaxResult;
import com.huaheng.common.log.annotation.Log;
import com.huaheng.common.log.enums.BusinessType;
import com.huaheng.common.security.utils.SecurityUtils;
import com.huaheng.system.api.domain.SysUser;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 个人信息 业务处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/user/profile")
@Api(value = "个人信息", tags = "个人信息")
public class SysProfileController extends BaseController {
    @Resource
    private ISysUserService userService;
    @Resource
    private ISysRoleService roleService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private CompanyService companyService;
    /**
     * 个人信息
     */
    @GetMapping
    @ApiOperation(value = "查询个人信息", notes = "查询个人信息")
    public AjaxResult profile() {
        String username = SecurityUtils.getUsername();
        SysUser user = userService.selectUserByUserName(username);
        AjaxResult ajax = AjaxResult.success(user);
        List<SysRole> roles = roleService.selectRoleAll();
        ajax.put("roles", SysUser.isAdmin(user.getUserId()) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        ajax.put("companies", companyService.selectCompanyByUserId(user.getUserId()));
        ajax.put("warehouseCodeList", warehouseService.selectWarehouseByUserId(user.getUserId()));
        ajax.put(AjaxResult.DATA_TAG, userService.selectUserById(user.getUserId()));
        ajax.put("roleIds", roleService.selectRoleListByUserId(user.getUserId()));
        return ajax;
    }

    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改个人信息", notes = "修改个人信息")
    public AjaxResult updateProfile(@RequestBody SysUser user) {
        if (userService.updateUserProfile(user) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("修改个人信息异常,请联系管理员");
    }

    /**
     * 重置密码
     */
    @ApiOperation(value = "重置密码", notes = "重置密码")
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePwd")
    public AjaxResult updatePwd(@ApiParam(value = "旧密码", required = true) @RequestParam(value = "oldPassword") String oldPassword,
                                @ApiParam(value = "新密码", required = true) @RequestParam(value = "newPassword") String newPassword) {
        String username = SecurityUtils.getUsername();
        SysUser user = userService.selectUserByUserName(username);
        String password = user.getPassword();
        if (!SecurityUtils.matchesPassword(oldPassword, password)) {
            return AjaxResult.error("修改密码失败,旧密码错误");
        }
        if (SecurityUtils.matchesPassword(newPassword, password)) {
            return AjaxResult.error("新密码不能与旧密码相同");
        }
        if (userService.resetUserPwd(username, SecurityUtils.encryptPassword(newPassword)) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("修改密码异常,请联系管理员");
    }
}