wzp
2025-05-03 ff76ba9c9c56aa2dd1c6a96d5d6365fbb3db1fad
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
package com.ruoyi.system.service.impl;
 
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.GpsLoginRequest;
import com.ruoyi.system.domain.GpsLoginResponse;
import com.ruoyi.system.service.IGpsCollectService;
import com.ruoyi.system.config.GpsServiceConfig;
import com.ruoyi.common.utils.MD5Util;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.util.HashMap;
import java.util.Map;
import org.springframework.util.StringUtils;
import com.ruoyi.common.utils.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.exception.ServiceException;
 
/**
 * GPS采集服务实现
 */
@Service
public class GpsCollectServiceImpl implements IGpsCollectService {
 
    private final RestTemplate restTemplate;
    private final GpsServiceConfig gpsServiceConfig;
 
    public GpsCollectServiceImpl(GpsServiceConfig gpsServiceConfig) {
        this.restTemplate = new RestTemplate();
        this.gpsServiceConfig = gpsServiceConfig;
    }
 
    @Override
    public GpsLoginResponse login(GpsLoginRequest request) {
        // 参数校验
        if (StringUtils.isEmpty(request.getType()) || StringUtils.isEmpty(request.getFrom())) {
            throw new ServiceException("参数不能为空");
        }
 
        // 构建请求URL
        String url = gpsServiceConfig.getDomain() + "/webapi?action=login";
        
        // 设置请求参数
        Map<String, String> params = new HashMap<>();
        params.put("username", gpsServiceConfig.getUsername());
        params.put("password", MD5Util.md5(gpsServiceConfig.getPassword()));
        params.put("type", request.getType());
        params.put("from", request.getFrom());
        params.put("browser", request.getBrowser());
 
        StringBuilder paramStr = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (paramStr.length() > 0) {
                paramStr.append("&");
            }
            paramStr.append(entry.getKey()).append("=").append(entry.getValue());
        }
 
        // 发送请求
        String response = HttpUtil.post(url,params);
        
        // 解析响应
        GpsLoginResponse loginResponse = new GpsLoginResponse();
        try {
            JSONObject jsonResponse = JSONObject.parseObject(response);
            int status = jsonResponse.getIntValue("status");
            String username = jsonResponse.getString("username");
            
            // 根据返回码设置响应信息
            switch (status) {
                case 0:
                    loginResponse.setSuccess(true);
                    loginResponse.setMessage("登录成功");
                    loginResponse.setToken(jsonResponse.getString("token"));
                    break;
                case -1:
                    loginResponse.setSuccess(false);
                    loginResponse.setMessage("登录失败");
                    break;
                case 1:
                    loginResponse.setSuccess(false);
                    loginResponse.setMessage("密码错误");
                    break;
                case 2:
                    loginResponse.setSuccess(false);
                    loginResponse.setMessage("禁止登录");
                    break;
                case 3:
                    loginResponse.setSuccess(false);
                    loginResponse.setMessage("账号已禁用");
                    break;
                case 4:
                    loginResponse.setSuccess(false);
                    loginResponse.setMessage("设备到期");
                    break;
                case 5:
                    loginResponse.setSuccess(false);
                    loginResponse.setMessage("设备过期");
                    break;
                case 9903:
                    loginResponse.setSuccess(false);
                    loginResponse.setMessage("Token过期");
                    break;
                case 9906:
                    loginResponse.setSuccess(false);
                    loginResponse.setMessage("账号在其他地方登录");
                    break;
                default:
                    loginResponse.setSuccess(false);
                    loginResponse.setMessage("未知错误");
                    break;
            }
        } catch (Exception e) {
            loginResponse.setSuccess(false);
            loginResponse.setMessage("解析响应失败:" + e.getMessage());
        }
        
        return loginResponse;
    }
 
    /**
     * 校验登录来源
     */
    private boolean isValidFrom(String from) {
        if (from == null) {
            return false;
        }
        return "ANDROID".equals(from) || "IPHONE".equals(from) || 
               "WEB".equals(from) || "WEIXIN".equals(from);
    }
 
    /**
     * 校验登录类型
     */
    private boolean isValidType(String type) {
        if (type == null) {
            return false;
        }
        return "USER".equals(type) || "DEVICE".equals(type);
    }