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

import com.alibaba.fastjson.JSONException;
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.general.company.domain.Company;
import com.huaheng.pc.general.company.service.ICompanyService;
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.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/mobile/")
@Api(tags = {"MobileUserController"}, description = "移动端用户信息")
public class MobileUserController  extends BaseController {

    @Autowired
    private IMenuService menuService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICompanyService 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("warehouseId") == null)
            throw new JSONException("warehouseId(仓库id)不能为空");
        if  (param.get("warehouseCode") == null)
            throw new JSONException("warehouseCode(仓库编码)不能为空");

        User user = ShiroUtils.getUser();
        user.setWarehouseId(Integer.valueOf(param.get("warehouseId")));
        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()
    {
        Company condition = new Company();
        condition.setDeleted(false);
        List<Company> companies = companyService.selectListEntityByEqual(condition);
        List<CompanyInfo> companyInfos = new ArrayList<>();
        for(Company company : companies) {
            companyInfos.add(new CompanyInfo(company.getId(), company.getCode()));
        }
        return AjaxResult.success(companyInfos);
    }
}