Blame view

src/main/java/com/huaheng/mobile/general/MobileUserController.java 4.59 KB
mahuandong authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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/")
xqs authored
43
@Api(tags = {"移动端用户信息"}, value = "移动端用户信息MobileUserController")
mahuandong authored
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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() {
105
        List<Company> companies = companyService.selectCompanyByCurrentUserId();
mahuandong authored
106
107
        List<CompanyInfo> companyInfos = new ArrayList<>();
        for(Company company : companies) {
mahuandong authored
108
            companyInfos.add(new CompanyInfo(company.getId(), company.getCode(), company.getName()));
mahuandong authored
109
110
111
112
        }
        return AjaxResult.success(companyInfos);
    }
}