MobileUserController.java 4.69 KB
package com.huaheng.mobile.general;

import com.alibaba.fastjson.JSONException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.system.menu.domain.Menu;
import com.huaheng.pc.system.menu.service.IMenuService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @author Enzo Cotter
 * @date 2019/12/15
 */
@RestController
@RequestMapping("/mobile/")
@Api(tags = {"MobileUserController"}, description = "移动端用户信息")
public class MobileUserController extends BaseController {

    @Resource
    private IMenuService menuService;
    @Resource
    private IUserService userService;
    @Resource
    private CompanyService companyService;

    @PostMapping("/login")
    @ApiOperation("用户登陆")
    public AjaxResult login(@RequestBody @ApiParam(value="code和password的Map集合") Map<String, String> param) {
        if  (param.get("code") == null) {
            throw new JSONException("code(用户名)不能为空");
        }
        if  (param.get("password") == null) {
            throw new JSONException("password(密码)不能为空");
        }
        UsernamePasswordToken token = new UsernamePasswordToken(param.get("code"), param.get("password"), false);
        Subject subject = SecurityUtils.getSubject();
        SecurityUtils.getSubject().getSession().setTimeout(-1000L);
        try {
            subject.login(token);
            List<Map<String, Object>> list = userService.getWarehouseByUserCode(param.get("code"));
            return AjaxResult.success(list);
        } catch (AuthenticationException e) {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }

    @PostMapping("/getModules")
    @ApiOperation("获取当前用户模块列表")
    public AjaxResult  getModules(@RequestBody @ApiParam(value="WarehouseId和warehouseCode的Map集合") Map<String, String> param) {
        if  (param.get("warehouseCode") == null) {
            throw new JSONException("warehouseCode(仓库编码)不能为空");
        }
        User user = ShiroUtils.getUser();
        user.setWarehouseCode(param.get("warehouseCode"));
        ShiroUtils.setUser(user);
        List<Company> companys = companyService.selectCompanyByCurrentUserId();
        user.setCompanyIdList(companys.stream().map(X -> X.getId()).collect(Collectors.toList()));
        user.setCompanyCodeList(companys.stream().map(X -> X.getCode()).collect(Collectors.toList()));
        ShiroUtils.setUser(user);
        List<Menu> menus = menuService.selectMobileMenusByUserId(ShiroUtils.getUserId());
        return AjaxResult.success(menus);
    }

    @PostMapping("/heartbeat")
    @ApiOperation("心跳接口,用于延长cookie有效期")
    public AjaxResult heartbeat()
    {
        return AjaxResult.success("success");
    }

    @PostMapping("/getCompanyInfo")
    @ApiOperation("获取公司信息")
    public AjaxResult getCompanyInfo() {
        LambdaQueryWrapper<Company> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Company::getDeleted, false);

        List<Company> companies = companyService.list(queryWrapper);
        List<CompanyInfo> companyInfos = new ArrayList<>();
        for(Company company : companies) {
            companyInfos.add(new CompanyInfo(company.getId(), company.getCode(), company.getName()));
        }
        return AjaxResult.success(companyInfos);
    }
}