kaiyu
2020-09-17 613dd76a3aded439f1002d904d85d8332ddb03d1
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
43
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.moral.controller;
 
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.moral.common.exception.WebAuthException;
import com.moral.common.util.BeanUtils;
import com.moral.common.util.RedisHashUtil;
import com.moral.common.util.WebTokenUtils;
import com.moral.common.webAnno.UserLoginToken;
import com.moral.entity.AreaNames;
import com.moral.entity.Organization;
import com.moral.service.AccountService;
import com.moral.service.DictionaryDataService;
import com.moral.service.OrganizationService;
import com.moral.service.WebTokenService;
import com.moral.util.LatLngTransformation;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
import static com.moral.common.util.WebUtils.getParametersStartingWith;
 
@RestController
@RequestMapping("/web")
@CrossOrigin(origins = "*", maxAge = 3600)
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
public class WebController {
    @Resource
    AccountService accountService;
    @Resource
    DictionaryDataService dictionaryDataService;
    OrganizationService organizationService;
    @Resource
    WebTokenService webTokenService;
    @Resource
    RedisHashUtil redisHashUtil;
 
    @PostMapping("login")
    public Map<String, Object> login(@RequestBody Map<String, Object> parameters) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (!(parameters.containsKey("account") && parameters.containsKey("password"))) {
            resultMap.put("msg", "用户名及密码不允许为空!");
            resultMap.put("accountId", -1);
        } else {
            resultMap = accountService.webLogin(parameters);
            String accountId = String.valueOf(resultMap.get("accountId"));
            if (!accountId.equals("-1")) {
                redisHashUtil.deleteMapVal("webToken",accountId);
                resultMap.put("token", webTokenService.getToken(accountId));
            }
        }
        return resultMap;
    }
 
    @UserLoginToken
    @PostMapping("logout")
    public Map<String, Object> logout(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<>();
        String token = request.getHeader("token");
        String id =  WebTokenUtils.getIdBytoken(token);
        redisHashUtil.addMapOne("webToken", String.valueOf(id),token);
        resultMap.put("msg", "退出成功!");
        return resultMap;
    }
 
    @UserLoginToken
    @GetMapping("getAccountInfo")
    public Map<String, Object> getAccountInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        String id = "";
        try {
            id = WebTokenUtils.getIdBytoken(token);
        } catch (JWTDecodeException e) {
            throw new WebAuthException("401,token无效");
        }
        Map<String, Object> resultMap = accountService.getAccountInfoById(id);
        Object orgId = resultMap.get("orgId");
        if (resultMap.get("orgId") != null && resultMap.get("orgId") instanceof Integer) {
            StringBuilder areaNamesBuilder = new StringBuilder("中国");
            //判断是否为本公司开发者
            if (!((Integer) orgId).equals(dictionaryDataService.querySupperOrgId())) {
                //不是本公司开发者则获取用户所属地区
                Organization organization = organizationService.getOrganizationById((Integer) orgId);
                if (organization.getAreaNames() != null) {
                    Map<String, String> areaNameMap = BeanUtils.beanToMap(organization.getAreaNames());
                    List<String> names = areaNameMap.entrySet().stream().filter(item -> {
                        return item.getValue() != null;
                    }).map(item -> {
                        return item.getValue();
                    }).collect(Collectors.toList());
                    AreaNames areaNames = organization.getAreaNames();
                    areaNamesBuilder.append("/");
                    areaNamesBuilder.append(String.join("/", names));
                }
                // 企业用户
                if (organization.getRank() != null && organization.getRank() == 0) {
                    resultMap.put("type", "enterprise");
                } else {
                    resultMap.put("type", "government");
                }
                Number mapAreaCode = null;
                if (organization.getVillageCode() != null) {
                    mapAreaCode = organization.getVillageCode();
                } else if (organization.getTownCode() != null) {
                    mapAreaCode = organization.getTownCode();
                } else if (organization.getAreaCode() != null) {
                    mapAreaCode = organization.getAreaCode();
                } else if (organization.getCityCode() != null) {
                    mapAreaCode = organization.getCityCode();
                } else if (organization.getProvinceCode() != null) {
                    mapAreaCode = organization.getProvinceCode();
                }
                resultMap.put("mapAreaCode", mapAreaCode.toString());
            }
            resultMap.put("mapPath", areaNamesBuilder.toString());
            String accountId = String.valueOf(resultMap.get("accountId"));
            resultMap.put("token", webTokenService.getToken(accountId));
        }
        return resultMap;
    }
 
    //弃用
    @UserLoginToken
    @RequestMapping("getAccountInfoTest")
    public Map<String, Object> getAccountInfoTest(@RequestBody Map<String, Object> parameters) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        System.out.println(parameters);
        if (!(parameters.containsKey("account") && parameters.containsKey("password"))) {
            resultMap.put("msg", "用户名及密码不允许为空!");
            resultMap.put("accountId", -1);
        } else {
            resultMap = accountService.screenLoginNew(parameters);
            // 添加返回行政区信息字符串操作
            Object orgId = resultMap.get("orgId");
            if (resultMap.get("orgId") != null && resultMap.get("orgId") instanceof Integer) {
                StringBuilder areaNamesBuilder = new StringBuilder("中国");
                //判断是否为本公司开发者
                if (!((Integer) orgId).equals(dictionaryDataService.querySupperOrgId())) {
                    //不是本公司开发者则获取用户所属地区
                    Organization organization = organizationService.getOrganizationById((Integer) orgId);
                    if (organization.getAreaNames() != null) {
                        Map<String, String> areaNameMap = BeanUtils.beanToMap(organization.getAreaNames());
                        List<String> names = areaNameMap.entrySet().stream().filter(item -> {
                            return item.getValue() != null;
                        }).map(item -> {
                            return item.getValue();
                        }).collect(Collectors.toList());
                        AreaNames areaNames = organization.getAreaNames();
                        areaNamesBuilder.append("/");
                        areaNamesBuilder.append(String.join("/", names));
                    }
                    // 企业用户
                    if (organization.getRank() != null && organization.getRank() == 0) {
                        resultMap.put("type", "enterprise");
                    } else {
                        resultMap.put("type", "government");
                    }
                    Number mapAreaCode = null;
                    if (organization.getVillageCode() != null) {
                        mapAreaCode = organization.getVillageCode();
                    } else if (organization.getTownCode() != null) {
                        mapAreaCode = organization.getTownCode();
                    } else if (organization.getAreaCode() != null) {
                        mapAreaCode = organization.getAreaCode();
                    } else if (organization.getCityCode() != null) {
                        mapAreaCode = organization.getCityCode();
                    } else if (organization.getProvinceCode() != null) {
                        mapAreaCode = organization.getProvinceCode();
                    }
                    resultMap.put("mapAreaCode", mapAreaCode.toString());
                }
                resultMap.put("mapPath", areaNamesBuilder.toString());
                String accountId = String.valueOf(resultMap.get("accountId"));
                resultMap.put("token", webTokenService.getToken(accountId));
            }
        }
        return resultMap;
    }
 
    @UserLoginToken
    @GetMapping("test")
    public String add() {
        return "test success!";
    }
}