WebFrontUserController.java 10.5 KB
package com.lhcredit.project.webbusiness.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lhcredit.common.constant.UserConstants;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.security.ShiroUtils;
import com.lhcredit.framework.aspectj.lang.annotation.CheckToken;
import com.lhcredit.framework.aspectj.lang.annotation.Log;
import com.lhcredit.framework.aspectj.lang.enums.BusinessType;
import com.lhcredit.framework.aspectj.lang.enums.OperatorType;
import com.lhcredit.framework.web.controller.BaseController;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.project.business.frontRole.service.IFrontRoleService;
import com.lhcredit.project.business.frontUser.domain.FrontUser;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.frontUser.service.IFrontUserService;
import com.lhcredit.project.business.frontUser.service.TokenManager;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


/**
 * 用户信息对外接口
 *
 * @author lhcredit
 * @date 2023-11-28
 */
@RestController
@RequestMapping("/web/frontUser")
@RequiredArgsConstructor
public class WebFrontUserController extends BaseController {

    @Autowired
    private IFrontUserService frontUserService;

    //    private final IFrontUserMonService frontUserService;
//
    private final TokenManager tokenManager;

    @Autowired
    private IFrontRoleService frontRoleService;
    //
////    @Autowired
////    private IFrontUserMenuService frontUserMenuService;
//
//    @Autowired
//    private IFrontUserMonService frontUserMonService;
//
    /**
     * 查询用户列表接口
     */
    @ApiOperation("查询用户列表")
    @Log(title = "用户", businessType = BusinessType.LIST, operatorType = OperatorType.WEB)
    @GetMapping("/list")
    public AjaxResult list(FrontUser frontUser) {
        FrontUserMon loginUser=getUserInfo();
        frontUser.setId(loginUser.getId());
        startPage();
        List<FrontUser> list = frontUserService.selectFrontUserByPid(frontUser);
        return toAjax(list);
    }
//
//    /**
//     * 查询用户详情接口
//     */
//    @ApiOperation("查询用户详情")
//    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "int", paramType = "path")
//    @Log(title = "用户", businessType = BusinessType.DETAIL, operatorType = OperatorType.WEB)
//    @GetMapping("/{id}")
//    public AjaxResult detail(@PathVariable int id) {
//        FrontUserMon frontUser = frontUserService.changeModel(frontUserService.selectFrontUserById(id));
//        if (StringUtils.isNull(frontUser)) {
//            return AjaxResult.error("该信息不存在");
//        }
//        return toAjax(frontUser);
//    }
//
//    @CheckToken
//    @ResponseBody
//    @PostMapping("/updatePwd")
//    public AjaxResult updatePwd(String newPwd) {
//        return frontUserService.updatePwd(getUserInfo().getId(), newPwd);
//    }
//
    @CheckToken
    @ApiOperation(value = "退出登录")
    @RequestMapping("/logout")
    public AjaxResult logout() {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            tokenManager.loginOff(request.getHeader("Authorization"));
            return success("已退出");
        } catch (Exception e) {
            return loginError("用户登出失败");
        }

    }

    /**
     * 新增保存用户接口
     */
    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "frontUser", value = "用户", dataType = "FrontUser")
    @Log(title = "用户", businessType = BusinessType.INSERT, operatorType = OperatorType.WEB)
    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody FrontUser frontUser) {
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(frontUserService.checkLoginNameUnique(frontUser.getLoginName()))) {
            return error("保存用户'" + frontUser.getLoginName() + "'失败,登录账号已存在");
        }
        if(StringUtils.isNotEmpty(frontUser.getWebRoles())){
            String[] str=frontUser.getWebRoles().split(",");
            Long[] longArray = Arrays.stream(str)
                    .map(Long::parseLong)
                    .toArray(Long[]::new);
            frontUser.setRoleIds(longArray);
        }
        frontUser.setCreateBy(ShiroUtils.getLoginName());
        frontUser.setCreateTime(new Date());
        frontUser.setStorageTime(new Date());
        frontUser.setUpdateBy(ShiroUtils.getLoginName());
        frontUser.setUpdateTime(new Date());
        return toAjax(frontUserService.insertFrontUser(frontUser));
    }

    /**
     * 修改查询接口
     */
    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "frontUser", value = "用户", dataType = "FrontUser")
    @Log(title = "用户", businessType = BusinessType.INSERT, operatorType = OperatorType.WEB)
    @PostMapping("/queryUser")
    public AjaxResult queryUser(@RequestBody FrontUser frontUser) {
        Map map=new HashMap();
        if(frontUser.getId()>0){
            FrontUser frontUserNew =frontUserService.selectFrontUserById(frontUser.getId());
            map.put("frontUser", frontUserNew);
            map.put("roles", frontRoleService.selectRolesByUserId(Long.parseLong(frontUser.getId()+"")));
            return toAjax(map);
        }
        return AjaxResult.error("缺少必要参数");
    }

    /**
     * 修改保存用户接口
     */
    @ApiOperation("修改用户")
    @ApiImplicitParam(name = "frontUser", value = "用户", dataType = "FrontUser")
    @Log(title = "用户", businessType = BusinessType.UPDATE, operatorType = OperatorType.WEB)
    @PostMapping("/updateUser")
    public AjaxResult update(@RequestBody FrontUser frontUser) {
        if (StringUtils.isNull(frontUser) || StringUtils.isNull(frontUser.getId())) {
            return AjaxResult.error("主键id不能为空");
        }
        if(StringUtils.isNotEmpty(frontUser.getWebRoles())){
            String[] str=frontUser.getWebRoles().split(",");
            Long[] longArray = Arrays.stream(str)
                    .map(Long::parseLong)
                    .toArray(Long[]::new);
            frontUser.setRoleIds(longArray);
        }
        frontUser.setUpdateBy(ShiroUtils.getLoginName());
        frontUser.setUpdateTime(new Date());
        return toAjax(frontUserService.updateFrontUser(frontUser));
    }
//
//    /**
//     * 删除用户接口
//     */
//    @ApiOperation("删除用户")
//    @ApiImplicitParam(name = "ids", value = "主键id,多条以英文逗号分隔", required = true, dataType = "String", paramType = "path")
//    @Log(title = "用户", businessType = BusinessType.DELETE, operatorType = OperatorType.WEB)
//    @DeleteMapping("/{ids}")
//    public AjaxResult delete(@PathVariable String ids) {
//        return toAjax(frontUserService.deleteFrontUserByIds(ids));
//    }
//
//
//    /**
//     * 用户登录 zhaobh
//     */
//    @ResponseBody
//    @RequestMapping("/webUserLogin.form")
//    public AjaxResult webUserLogin(HttpServletRequest request, @RequestBody FrontUserMon frontUser) {
//        return frontUserService.webUserLogin(request, frontUser);
//    }
//
//    /**
//     * 查询用户详情接口
//     */
//    @ApiOperation("个人中心详情")
//    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "int", paramType = "path")
//    @Log(title = "用户", businessType = BusinessType.DETAIL, operatorType = OperatorType.WEB)
//    @GetMapping("/backUser")
//    public AjaxResult backUser() {
//        FrontUserMon userInfo = super.getUserInfo();
//
//        return toAjax();
//    }
//
//    /**
//     * 查询用户详情接口
//     */
//    @ApiOperation("个人中心修改")
//    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "int", paramType = "path")
//    @Log(title = "用户", businessType = BusinessType.DETAIL, operatorType = OperatorType.WEB)
//    @GetMapping("/updateBackUser")
//    public AjaxResult updateBackUser(FrontBackUserVo frontBackUserVo) {
//        FrontUserMon userInfo = super.getUserInfo();
//        userInfo.setUserName(frontBackUserVo.getUserName());
//        userInfo.setPassword(frontBackUserVo.getPassword());
//        userInfo.setPhone(frontBackUserVo.getPhone());
//        userInfo.setMail(frontBackUserVo.getEmail());
//        userInfo.setLoginName(frontBackUserVo.getLoginName());
//        userInfo.setId(userInfo.getId());
//        iFrontUserMonService.updateFrontUser(userInfo);
//        //新权限
//        List<Integer> newMenuIds = new ArrayList<>();
//        frontBackUserVo.getFrontMenuList().stream().forEach(f->{
//            if(!ObjectUtils.isEmpty(f.getId()) && f.getIsCheck().equals("1")){
//                newMenuIds.add(f.getId().intValue());
//            }
//        });
//        //先删后增加
//        frontUserMenuService.deleteFrontUserMenuById(userInfo.getId());
//        frontUserMenuService.addMenuIdsById(userInfo.getId(), newMenuIds);
//        return AjaxResult.success();
//    }
    @RequestMapping("/login")
    public AjaxResult webLogin(HttpServletRequest request, @RequestBody FrontUserMon frontUserMon){
//        HttpSession session = request.getSession(true);
//         Object verObj=session.getAttribute("verCode");
//         if (null==verObj){
//             return AjaxResult.other(4003,"验证码过期");
//         }
//        String verCode =verObj.toString();
//        String code = frontUserMon.getCode();
//        if (!verCode.toLowerCase().equals(code.toLowerCase())) {
//            return AjaxResult.loginError("验证码错误!");
//        }
        return frontUserService.webLogin(request, frontUserMon);
    }


    @RequestMapping("/resetPwd")
    public AjaxResult resetPwd( @RequestBody FrontUser user){
        FrontUserMon userInfo = getUserInfo();
        userInfo.setPassword(user.getPassword());
//        user.setModifyPsdPage(0);
        userInfo.setModifyPsdPage(0);
        return toAjax(frontUserService.resetUserPwd(userInfo));
    }


}