|
|
@@ -1,72 +1,483 @@
|
|
1
|
1
|
package com.water.mobile.service;
|
|
|
2
|
+
|
|
2
|
3
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
3
|
4
|
import com.water.mobile.entity.*;
|
|
4
|
5
|
import com.water.mobile.mapper.*;
|
|
|
6
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
5
|
7
|
import lombok.RequiredArgsConstructor;
|
|
|
8
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
9
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
10
|
+import org.springframework.http.ResponseEntity;
|
|
6
|
11
|
import org.springframework.stereotype.Service;
|
|
|
12
|
+import org.springframework.web.client.RestTemplate;
|
|
|
13
|
+
|
|
|
14
|
+import java.time.LocalDateTime;
|
|
7
|
15
|
import java.util.*;
|
|
8
|
|
-@Service @RequiredArgsConstructor
|
|
|
16
|
+
|
|
|
17
|
+/**
|
|
|
18
|
+ * 移动APP统一API聚合服务
|
|
|
19
|
+ * 通过RestTemplate调用后端各微服务获取真实数据
|
|
|
20
|
+ */
|
|
|
21
|
+@Service
|
|
|
22
|
+@RequiredArgsConstructor
|
|
|
23
|
+@Slf4j
|
|
9
|
24
|
public class MobileApiService {
|
|
|
25
|
+
|
|
10
|
26
|
private final MobileUserMapper userMapper;
|
|
11
|
27
|
private final PushNotificationMapper pushMapper;
|
|
12
|
28
|
private final AppVersionMapper verMapper;
|
|
|
29
|
+ private final MobileDeviceMapper deviceMapper;
|
|
|
30
|
+ private final RestTemplate restTemplate = new RestTemplate();
|
|
|
31
|
+
|
|
|
32
|
+ @Value("${microservice.water.url:http://localhost:9010}")
|
|
|
33
|
+ private String waterServiceUrl;
|
|
|
34
|
+
|
|
|
35
|
+ @Value("${microservice.patrol.url:http://localhost:9020}")
|
|
|
36
|
+ private String patrolServiceUrl;
|
|
|
37
|
+
|
|
|
38
|
+ @Value("${microservice.billing.url:http://localhost:9030}")
|
|
|
39
|
+ private String billingServiceUrl;
|
|
|
40
|
+
|
|
|
41
|
+ // ==================== APP-04: 统一登录 ====================
|
|
|
42
|
+
|
|
|
43
|
+ /**
|
|
|
44
|
+ * 统一登录(APP-04)
|
|
|
45
|
+ * @param username 用户名
|
|
|
46
|
+ * @param password 明文密码(前端传输前应加密)
|
|
|
47
|
+ * @param appModule 请求登录的模块 WATER/PATROL/BILLING/ALL
|
|
|
48
|
+ * @return 登录结果(含Sa-Token)
|
|
|
49
|
+ */
|
|
|
50
|
+ public Map<String, Object> login(String username, String password, String appModule) {
|
|
|
51
|
+ MobileUser user = userMapper.selectOne(new LambdaQueryWrapper<MobileUser>()
|
|
|
52
|
+ .eq(MobileUser::getUsername, username));
|
|
|
53
|
+
|
|
|
54
|
+ if (user == null) {
|
|
|
55
|
+ throw new RuntimeException("用户不存在");
|
|
|
56
|
+ }
|
|
|
57
|
+ if (user.getStatus() != 1) {
|
|
|
58
|
+ throw new RuntimeException("用户已被禁用");
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
|
61
|
+ // BCrypt 密码校验
|
|
|
62
|
+ if (!BCryptUtil.matches(password, user.getPassword())) {
|
|
|
63
|
+ throw new RuntimeException("密码错误");
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ // 模块权限校验(ALL可访问所有模块)
|
|
|
67
|
+ if (!"ALL".equals(user.getAppModule()) && !user.getAppModule().equals(appModule)) {
|
|
|
68
|
+ throw new RuntimeException("无权访问该模块: " + appModule);
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ // Sa-Token 登录
|
|
|
72
|
+ StpUtil.login(user.getId());
|
|
|
73
|
+
|
|
|
74
|
+ // 更新最后登录时间
|
|
|
75
|
+ user.setLastLoginTime(LocalDateTime.now());
|
|
|
76
|
+ userMapper.updateById(user);
|
|
|
77
|
+
|
|
|
78
|
+ return Map.of(
|
|
|
79
|
+ "userId", user.getId(),
|
|
|
80
|
+ "realName", user.getRealName(),
|
|
|
81
|
+ "appModule", user.getAppModule(),
|
|
|
82
|
+ "phone", user.getPhone() != null ? user.getPhone() : "",
|
|
|
83
|
+ "avatar", user.getAvatar() != null ? user.getAvatar() : "",
|
|
|
84
|
+ "token", StpUtil.getTokenValue(),
|
|
|
85
|
+ "tokenName", StpUtil.getTokenName()
|
|
|
86
|
+ );
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ /**
|
|
|
90
|
+ * 登出
|
|
|
91
|
+ */
|
|
|
92
|
+ public void logout() {
|
|
|
93
|
+ StpUtil.logout();
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ /**
|
|
|
97
|
+ * 获取当前登录用户信息
|
|
|
98
|
+ */
|
|
|
99
|
+ public Map<String, Object> getCurrentUser() {
|
|
|
100
|
+ long userId = StpUtil.getLoginIdAsLong();
|
|
|
101
|
+ MobileUser user = userMapper.selectById(userId);
|
|
|
102
|
+ if (user == null) {
|
|
|
103
|
+ throw new RuntimeException("用户不存在");
|
|
|
104
|
+ }
|
|
|
105
|
+ return Map.of(
|
|
|
106
|
+ "userId", user.getId(),
|
|
|
107
|
+ "realName", user.getRealName(),
|
|
|
108
|
+ "appModule", user.getAppModule(),
|
|
|
109
|
+ "phone", user.getPhone() != null ? user.getPhone() : "",
|
|
|
110
|
+ "avatar", user.getAvatar() != null ? user.getAvatar() : "",
|
|
|
111
|
+ "lastLoginTime", user.getLastLoginTime() != null ? user.getLastLoginTime().toString() : ""
|
|
|
112
|
+ );
|
|
|
113
|
+ }
|
|
|
114
|
+
|
|
|
115
|
+ // ==================== APP-01: 供水管理 ====================
|
|
13
|
116
|
|
|
14
|
|
- public Map<String,Object> login(String username, String password, String appModule) {
|
|
15
|
|
- MobileUser u = userMapper.selectOne(new LambdaQueryWrapper<MobileUser>()
|
|
16
|
|
- .eq(MobileUser::getUsername, username)
|
|
17
|
|
- .eq(MobileUser::getAppModule, appModule));
|
|
18
|
|
- if (u == null || !u.getPassword().equals(password)) throw new RuntimeException("登录失败");
|
|
19
|
|
- return Map.of("userId", u.getId(), "realName", u.getRealName(),
|
|
20
|
|
- "appModule", u.getAppModule(), "token", "mock-token-" + u.getId());
|
|
21
|
|
- }
|
|
22
|
|
- // 供水API聚合
|
|
23
|
|
- public Map<String,Object> getWaterOverview() {
|
|
24
|
|
- return Map.of("todaySupply", 12500.0, "yesterdaySupply", 11800.0,
|
|
25
|
|
- "waterQuality", "合格", "alertCount", 3, "deviceOnlineRate", 0.95);
|
|
26
|
|
- }
|
|
27
|
|
- // 巡检API聚合
|
|
28
|
|
- public List<Map<String,Object>> getPatrolTasks(Long userId) {
|
|
29
|
|
- return List.of(
|
|
30
|
|
- Map.of("id", 1L, "taskName", "主管网巡检A线", "status", "待执行", "deadline", "今日18:00"),
|
|
31
|
|
- Map.of("id", 2L, "taskName", "消防栓检查-区域B", "status", "进行中", "deadline", "今日17:00")
|
|
|
117
|
+ /**
|
|
|
118
|
+ * 供水总览(APP-01)
|
|
|
119
|
+ * 调用供水生产管理平台后端聚合数据
|
|
|
120
|
+ */
|
|
|
121
|
+ public Map<String, Object> getWaterOverview() {
|
|
|
122
|
+ try {
|
|
|
123
|
+ ResponseEntity<Map> resp = restTemplate.getForEntity(
|
|
|
124
|
+ waterServiceUrl + "/api/water/overview", Map.class);
|
|
|
125
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
126
|
+ return resp.getBody();
|
|
|
127
|
+ }
|
|
|
128
|
+ } catch (Exception e) {
|
|
|
129
|
+ log.warn("供水服务不可用,返回降级数据: {}", e.getMessage());
|
|
|
130
|
+ }
|
|
|
131
|
+ // 降级数据(服务不可用时)
|
|
|
132
|
+ return Map.of(
|
|
|
133
|
+ "todaySupply", 0.0, "yesterdaySupply", 0.0,
|
|
|
134
|
+ "waterQuality", "未知", "alertCount", 0,
|
|
|
135
|
+ "deviceOnlineRate", 0.0, "degraded", true
|
|
32
|
136
|
);
|
|
33
|
137
|
}
|
|
34
|
|
- // 收费API聚合
|
|
35
|
|
- public Map<String,Object> getBillingSummary(Long userId) {
|
|
36
|
|
- return Map.of("unpaidCount", 2, "totalAmount", 156.80,
|
|
37
|
|
- "recentBills", List.of(
|
|
38
|
|
- Map.of("period", "2025-01", "amount", 78.40, "status", "未缴费"),
|
|
39
|
|
- Map.of("period", "2024-12", "amount", 82.50, "status", "已缴费")
|
|
40
|
|
- ));
|
|
|
138
|
+
|
|
|
139
|
+ /**
|
|
|
140
|
+ * 供水实时监测(APP-01)
|
|
|
141
|
+ */
|
|
|
142
|
+ public Map<String, Object> getWaterRealtime() {
|
|
|
143
|
+ try {
|
|
|
144
|
+ ResponseEntity<Map> resp = restTemplate.getForEntity(
|
|
|
145
|
+ waterServiceUrl + "/api/water/realtime", Map.class);
|
|
|
146
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
147
|
+ return resp.getBody();
|
|
|
148
|
+ }
|
|
|
149
|
+ } catch (Exception e) {
|
|
|
150
|
+ log.warn("供水实时数据不可用: {}", e.getMessage());
|
|
|
151
|
+ }
|
|
|
152
|
+ return Map.of("degraded", true, "message", "实时数据服务暂时不可用");
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ /**
|
|
|
156
|
+ * 供水报警列表(APP-01)
|
|
|
157
|
+ */
|
|
|
158
|
+ public List<Map<String, Object>> getWaterAlerts() {
|
|
|
159
|
+ try {
|
|
|
160
|
+ ResponseEntity<List> resp = restTemplate.getForEntity(
|
|
|
161
|
+ waterServiceUrl + "/api/water/alerts?status=active", List.class);
|
|
|
162
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
163
|
+ return resp.getBody();
|
|
|
164
|
+ }
|
|
|
165
|
+ } catch (Exception e) {
|
|
|
166
|
+ log.warn("供水报警服务不可用: {}", e.getMessage());
|
|
|
167
|
+ }
|
|
|
168
|
+ return Collections.emptyList();
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ // ==================== APP-02: 巡检管理 ====================
|
|
|
172
|
+
|
|
|
173
|
+ /**
|
|
|
174
|
+ * 巡检任务列表(APP-02)
|
|
|
175
|
+ * 调用巡检管理系统后端
|
|
|
176
|
+ */
|
|
|
177
|
+ public List<Map<String, Object>> getPatrolTasks(Long userId) {
|
|
|
178
|
+ try {
|
|
|
179
|
+ ResponseEntity<List> resp = restTemplate.getForEntity(
|
|
|
180
|
+ patrolServiceUrl + "/api/patrol/tasks?assigneeId=" + userId, List.class);
|
|
|
181
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
182
|
+ return resp.getBody();
|
|
|
183
|
+ }
|
|
|
184
|
+ } catch (Exception e) {
|
|
|
185
|
+ log.warn("巡检服务不可用: {}", e.getMessage());
|
|
|
186
|
+ }
|
|
|
187
|
+ return Collections.emptyList();
|
|
|
188
|
+ }
|
|
|
189
|
+
|
|
|
190
|
+ /**
|
|
|
191
|
+ * 巡检任务详情(APP-02)
|
|
|
192
|
+ */
|
|
|
193
|
+ public Map<String, Object> getPatrolTaskDetail(Long taskId) {
|
|
|
194
|
+ try {
|
|
|
195
|
+ ResponseEntity<Map> resp = restTemplate.getForEntity(
|
|
|
196
|
+ patrolServiceUrl + "/api/patrol/tasks/" + taskId, Map.class);
|
|
|
197
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
198
|
+ return resp.getBody();
|
|
|
199
|
+ }
|
|
|
200
|
+ } catch (Exception e) {
|
|
|
201
|
+ log.warn("巡检任务详情不可用: {}", e.getMessage());
|
|
|
202
|
+ }
|
|
|
203
|
+ return Map.of("degraded", true);
|
|
|
204
|
+ }
|
|
|
205
|
+
|
|
|
206
|
+ /**
|
|
|
207
|
+ * 提交巡检上报(APP-02)
|
|
|
208
|
+ */
|
|
|
209
|
+ public Map<String, Object> submitPatrolReport(Long taskId, Map<String, Object> reportData) {
|
|
|
210
|
+ try {
|
|
|
211
|
+ ResponseEntity<Map> resp = restTemplate.postForEntity(
|
|
|
212
|
+ patrolServiceUrl + "/api/patrol/tasks/" + taskId + "/report", reportData, Map.class);
|
|
|
213
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
214
|
+ return resp.getBody();
|
|
|
215
|
+ }
|
|
|
216
|
+ } catch (Exception e) {
|
|
|
217
|
+ log.warn("巡检上报失败: {}", e.getMessage());
|
|
|
218
|
+ }
|
|
|
219
|
+ return Map.of("success", false, "message", "上报服务暂时不可用");
|
|
|
220
|
+ }
|
|
|
221
|
+
|
|
|
222
|
+ // ==================== APP-03: 营业收费 ====================
|
|
|
223
|
+
|
|
|
224
|
+ /**
|
|
|
225
|
+ * 收费账单汇总(APP-03)
|
|
|
226
|
+ * 调用营收系统后端
|
|
|
227
|
+ */
|
|
|
228
|
+ public Map<String, Object> getBillingSummary(Long userId) {
|
|
|
229
|
+ try {
|
|
|
230
|
+ ResponseEntity<Map> resp = restTemplate.getForEntity(
|
|
|
231
|
+ billingServiceUrl + "/api/revenue/billing/summary?userId=" + userId, Map.class);
|
|
|
232
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
233
|
+ return resp.getBody();
|
|
|
234
|
+ }
|
|
|
235
|
+ } catch (Exception e) {
|
|
|
236
|
+ log.warn("收费服务不可用: {}", e.getMessage());
|
|
|
237
|
+ }
|
|
|
238
|
+ return Map.of("degraded", true, "unpaidCount", 0, "totalAmount", 0.0);
|
|
|
239
|
+ }
|
|
|
240
|
+
|
|
|
241
|
+ /**
|
|
|
242
|
+ * 账单列表(APP-03)
|
|
|
243
|
+ */
|
|
|
244
|
+ public List<Map<String, Object>> getBillingList(Long userId, String status) {
|
|
|
245
|
+ try {
|
|
|
246
|
+ String url = billingServiceUrl + "/api/revenue/billing/list?userId=" + userId;
|
|
|
247
|
+ if (status != null && !status.isEmpty()) {
|
|
|
248
|
+ url += "&status=" + status;
|
|
|
249
|
+ }
|
|
|
250
|
+ ResponseEntity<List> resp = restTemplate.getForEntity(url, List.class);
|
|
|
251
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
252
|
+ return resp.getBody();
|
|
|
253
|
+ }
|
|
|
254
|
+ } catch (Exception e) {
|
|
|
255
|
+ log.warn("账单列表服务不可用: {}", e.getMessage());
|
|
|
256
|
+ }
|
|
|
257
|
+ return Collections.emptyList();
|
|
41
|
258
|
}
|
|
42
|
|
- // 消息通知
|
|
|
259
|
+
|
|
|
260
|
+ /**
|
|
|
261
|
+ * 在线缴费(APP-03)
|
|
|
262
|
+ */
|
|
|
263
|
+ public Map<String, Object> payBill(Long billId, Map<String, Object> payData) {
|
|
|
264
|
+ try {
|
|
|
265
|
+ ResponseEntity<Map> resp = restTemplate.postForEntity(
|
|
|
266
|
+ billingServiceUrl + "/api/revenue/billing/" + billId + "/pay", payData, Map.class);
|
|
|
267
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
268
|
+ return resp.getBody();
|
|
|
269
|
+ }
|
|
|
270
|
+ } catch (Exception e) {
|
|
|
271
|
+ log.warn("在线缴费失败: {}", e.getMessage());
|
|
|
272
|
+ }
|
|
|
273
|
+ return Map.of("success", false, "message", "缴费服务暂时不可用");
|
|
|
274
|
+ }
|
|
|
275
|
+
|
|
|
276
|
+ /**
|
|
|
277
|
+ * 报装申请(APP-03)
|
|
|
278
|
+ */
|
|
|
279
|
+ public Map<String, Object> submitInstallationApply(Map<String, Object> applyData) {
|
|
|
280
|
+ try {
|
|
|
281
|
+ ResponseEntity<Map> resp = restTemplate.postForEntity(
|
|
|
282
|
+ billingServiceUrl + "/api/revenue/installation/apply", applyData, Map.class);
|
|
|
283
|
+ if (resp.getStatusCode().is2xxSuccessful() && resp.getBody() != null) {
|
|
|
284
|
+ return resp.getBody();
|
|
|
285
|
+ }
|
|
|
286
|
+ } catch (Exception e) {
|
|
|
287
|
+ log.warn("报装申请提交失败: {}", e.getMessage());
|
|
|
288
|
+ }
|
|
|
289
|
+ return Map.of("success", false, "message", "报装服务暂时不可用");
|
|
|
290
|
+ }
|
|
|
291
|
+
|
|
|
292
|
+ // ==================== APP-05: 消息通知 ====================
|
|
|
293
|
+
|
|
|
294
|
+ /**
|
|
|
295
|
+ * 获取通知列表(APP-05)
|
|
|
296
|
+ */
|
|
43
|
297
|
public List<PushNotification> getNotifications(Long userId, Boolean unreadOnly) {
|
|
44
|
298
|
LambdaQueryWrapper<PushNotification> w = new LambdaQueryWrapper<PushNotification>()
|
|
45
|
|
- .eq(PushNotification::getUserId, userId);
|
|
46
|
|
- if (Boolean.TRUE.equals(unreadOnly)) w.eq(PushNotification::getIsRead, 0);
|
|
|
299
|
+ .eq(PushNotification::getUserId, userId);
|
|
|
300
|
+ if (Boolean.TRUE.equals(unreadOnly)) {
|
|
|
301
|
+ w.eq(PushNotification::getIsRead, 0);
|
|
|
302
|
+ }
|
|
47
|
303
|
return pushMapper.selectList(w.orderByDesc(PushNotification::getCreatedTime));
|
|
48
|
304
|
}
|
|
|
305
|
+
|
|
|
306
|
+ /**
|
|
|
307
|
+ * 标记已读(APP-05)
|
|
|
308
|
+ */
|
|
49
|
309
|
public void markRead(Long notificationId) {
|
|
50
|
310
|
PushNotification n = pushMapper.selectById(notificationId);
|
|
51
|
|
- if (n != null) { n.setIsRead(1); pushMapper.updateById(n); }
|
|
|
311
|
+ if (n != null) {
|
|
|
312
|
+ n.setIsRead(1);
|
|
|
313
|
+ pushMapper.updateById(n);
|
|
|
314
|
+ }
|
|
52
|
315
|
}
|
|
|
316
|
+
|
|
|
317
|
+ /**
|
|
|
318
|
+ * 批量标记已读(APP-05)
|
|
|
319
|
+ */
|
|
|
320
|
+ public int markAllRead(Long userId) {
|
|
|
321
|
+ LambdaQueryWrapper<PushNotification> w = new LambdaQueryWrapper<PushNotification>()
|
|
|
322
|
+ .eq(PushNotification::getUserId, userId)
|
|
|
323
|
+ .eq(PushNotification::getIsRead, 0);
|
|
|
324
|
+ List<PushNotification> unread = pushMapper.selectList(w);
|
|
|
325
|
+ for (PushNotification n : unread) {
|
|
|
326
|
+ n.setIsRead(1);
|
|
|
327
|
+ pushMapper.updateById(n);
|
|
|
328
|
+ }
|
|
|
329
|
+ return unread.size();
|
|
|
330
|
+ }
|
|
|
331
|
+
|
|
|
332
|
+ /**
|
|
|
333
|
+ * 发送通知(APP-05)
|
|
|
334
|
+ */
|
|
53
|
335
|
public Long sendNotification(Long userId, String title, String content, String type, String appModule) {
|
|
54
|
336
|
PushNotification n = new PushNotification();
|
|
55
|
|
- n.setUserId(userId); n.setTitle(title); n.setContent(content);
|
|
56
|
|
- n.setType(type); n.setAppModule(appModule); n.setIsRead(0);
|
|
|
337
|
+ n.setUserId(userId);
|
|
|
338
|
+ n.setTitle(title);
|
|
|
339
|
+ n.setContent(content);
|
|
|
340
|
+ n.setType(type);
|
|
|
341
|
+ n.setAppModule(appModule);
|
|
|
342
|
+ n.setIsRead(0);
|
|
|
343
|
+ n.setPushChannel("WEBSOCKET");
|
|
|
344
|
+ n.setPushStatus(1);
|
|
57
|
345
|
pushMapper.insert(n);
|
|
|
346
|
+
|
|
|
347
|
+ // 异步推送到设备(根据设备token选择推送渠道)
|
|
|
348
|
+ pushToDevice(userId, title, content);
|
|
|
349
|
+
|
|
58
|
350
|
return n.getId();
|
|
59
|
351
|
}
|
|
60
|
|
- // 版本检查
|
|
61
|
|
- public Map<String,Object> checkUpdate(String appModule, String currentVersion) {
|
|
|
352
|
+
|
|
|
353
|
+ /**
|
|
|
354
|
+ * 未读消息计数(APP-05)
|
|
|
355
|
+ */
|
|
|
356
|
+ public Map<String, Object> getUnreadCount(Long userId) {
|
|
|
357
|
+ LambdaQueryWrapper<PushNotification> w = new LambdaQueryWrapper<PushNotification>()
|
|
|
358
|
+ .eq(PushNotification::getUserId, userId)
|
|
|
359
|
+ .eq(PushNotification::getIsRead, 0);
|
|
|
360
|
+ Long total = pushMapper.selectCount(w);
|
|
|
361
|
+
|
|
|
362
|
+ // 按类型统计
|
|
|
363
|
+ Map<String, Long> byType = new HashMap<>();
|
|
|
364
|
+ for (String t : List.of("ALERT", "TODO", "NOTICE", "BILLING")) {
|
|
|
365
|
+ Long c = pushMapper.selectCount(new LambdaQueryWrapper<PushNotification>()
|
|
|
366
|
+ .eq(PushNotification::getUserId, userId)
|
|
|
367
|
+ .eq(PushNotification::getIsRead, 0)
|
|
|
368
|
+ .eq(PushNotification::getType, t));
|
|
|
369
|
+ byType.put(t, c);
|
|
|
370
|
+ }
|
|
|
371
|
+
|
|
|
372
|
+ return Map.of("total", total, "byType", byType);
|
|
|
373
|
+ }
|
|
|
374
|
+
|
|
|
375
|
+ // ==================== APP更新检查 ====================
|
|
|
376
|
+
|
|
|
377
|
+ /**
|
|
|
378
|
+ * 检查APP更新
|
|
|
379
|
+ */
|
|
|
380
|
+ public Map<String, Object> checkUpdate(String appModule, String currentVersion) {
|
|
62
|
381
|
AppVersion v = verMapper.selectOne(new LambdaQueryWrapper<AppVersion>()
|
|
63
|
|
- .eq(AppVersion::getAppModule, appModule)
|
|
64
|
|
- .eq(AppVersion::getStatus, 1)
|
|
65
|
|
- .orderByDesc(AppVersion::getCreatedTime).last("LIMIT 1"));
|
|
66
|
|
- if (v == null || v.getVersionNo().equals(currentVersion))
|
|
|
382
|
+ .eq(AppVersion::getAppModule, appModule)
|
|
|
383
|
+ .eq(AppVersion::getStatus, 1)
|
|
|
384
|
+ .orderByDesc(AppVersion::getCreatedTime).last("LIMIT 1"));
|
|
|
385
|
+
|
|
|
386
|
+ if (v == null) {
|
|
|
387
|
+ // 尝试 ALL 模块
|
|
|
388
|
+ v = verMapper.selectOne(new LambdaQueryWrapper<AppVersion>()
|
|
|
389
|
+ .eq(AppVersion::getAppModule, "ALL")
|
|
|
390
|
+ .eq(AppVersion::getStatus, 1)
|
|
|
391
|
+ .orderByDesc(AppVersion::getCreatedTime).last("LIMIT 1"));
|
|
|
392
|
+ }
|
|
|
393
|
+
|
|
|
394
|
+ if (v == null || v.getVersionNo().equals(currentVersion)) {
|
|
67
|
395
|
return Map.of("hasUpdate", false);
|
|
68
|
|
- return Map.of("hasUpdate", true, "versionNo", v.getVersionNo(),
|
|
69
|
|
- "downloadUrl", v.getDownloadUrl(), "changelog", v.getChangelog(),
|
|
70
|
|
- "forceUpdate", v.getForceUpdate() == 1);
|
|
|
396
|
+ }
|
|
|
397
|
+
|
|
|
398
|
+ return Map.of(
|
|
|
399
|
+ "hasUpdate", true,
|
|
|
400
|
+ "versionNo", v.getVersionNo(),
|
|
|
401
|
+ "downloadUrl", v.getDownloadUrl() != null ? v.getDownloadUrl() : "",
|
|
|
402
|
+ "changelog", v.getChangelog() != null ? v.getChangelog() : "",
|
|
|
403
|
+ "forceUpdate", v.getForceUpdate() == 1
|
|
|
404
|
+ );
|
|
|
405
|
+ }
|
|
|
406
|
+
|
|
|
407
|
+ // ==================== 设备管理 ====================
|
|
|
408
|
+
|
|
|
409
|
+ /**
|
|
|
410
|
+ * 注册设备
|
|
|
411
|
+ */
|
|
|
412
|
+ public void registerDevice(Long userId, String deviceToken, String deviceType,
|
|
|
413
|
+ String deviceModel, String osVersion, String appVersion) {
|
|
|
414
|
+ // 先清理同token的旧记录
|
|
|
415
|
+ deviceMapper.delete(new LambdaQueryWrapper<MobileDevice>()
|
|
|
416
|
+ .eq(MobileDevice::getDeviceToken, deviceToken));
|
|
|
417
|
+
|
|
|
418
|
+ MobileDevice d = new MobileDevice();
|
|
|
419
|
+ d.setUserId(userId);
|
|
|
420
|
+ d.setDeviceToken(deviceToken);
|
|
|
421
|
+ d.setDeviceType(deviceType);
|
|
|
422
|
+ d.setDeviceModel(deviceModel);
|
|
|
423
|
+ d.setOsVersion(osVersion);
|
|
|
424
|
+ d.setAppVersion(appVersion);
|
|
|
425
|
+ d.setStatus(1);
|
|
|
426
|
+ d.setLastActiveTime(LocalDateTime.now());
|
|
|
427
|
+ deviceMapper.insert(d);
|
|
|
428
|
+
|
|
|
429
|
+ // 更新用户表的device_token
|
|
|
430
|
+ MobileUser u = userMapper.selectById(userId);
|
|
|
431
|
+ if (u != null) {
|
|
|
432
|
+ u.setDeviceToken(deviceToken);
|
|
|
433
|
+ u.setDeviceType(deviceType);
|
|
|
434
|
+ userMapper.updateById(u);
|
|
|
435
|
+ }
|
|
|
436
|
+ }
|
|
|
437
|
+
|
|
|
438
|
+ // ==================== 私有方法 ====================
|
|
|
439
|
+
|
|
|
440
|
+ /**
|
|
|
441
|
+ * 推送消息到设备
|
|
|
442
|
+ */
|
|
|
443
|
+ private void pushToDevice(Long userId, String title, String content) {
|
|
|
444
|
+ List<MobileDevice> devices = deviceMapper.selectList(
|
|
|
445
|
+ new LambdaQueryWrapper<MobileDevice>()
|
|
|
446
|
+ .eq(MobileDevice::getUserId, userId)
|
|
|
447
|
+ .eq(MobileDevice::getStatus, 1));
|
|
|
448
|
+ for (MobileDevice d : devices) {
|
|
|
449
|
+ try {
|
|
|
450
|
+ // 实际推送逻辑根据推送渠道实现
|
|
|
451
|
+ log.info("推送到设备: userId={}, token={}, title={}", userId, d.getDeviceToken(), title);
|
|
|
452
|
+ } catch (Exception e) {
|
|
|
453
|
+ log.error("推送失败: userId={}, token={}", userId, d.getDeviceToken(), e);
|
|
|
454
|
+ }
|
|
|
455
|
+ }
|
|
|
456
|
+ }
|
|
|
457
|
+
|
|
|
458
|
+ /**
|
|
|
459
|
+ * 密码工具
|
|
|
460
|
+ * 生产环境应使用 BCryptPasswordEncoder,此处兼容明文(旧数据)和BCrypt
|
|
|
461
|
+ */
|
|
|
462
|
+ private static class BCryptUtil {
|
|
|
463
|
+ public static boolean matches(String rawPassword, String encodedPassword) {
|
|
|
464
|
+ if (encodedPassword == null || encodedPassword.isEmpty()) {
|
|
|
465
|
+ return false;
|
|
|
466
|
+ }
|
|
|
467
|
+ // BCrypt hash 以 $2a$ / $2b$ 开头
|
|
|
468
|
+ if (encodedPassword.startsWith("$2a$") || encodedPassword.startsWith("$2b$")) {
|
|
|
469
|
+ // 使用 Spring Security BCrypt 校验
|
|
|
470
|
+ try {
|
|
|
471
|
+ org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder encoder =
|
|
|
472
|
+ new org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder();
|
|
|
473
|
+ return encoder.matches(rawPassword, encodedPassword);
|
|
|
474
|
+ } catch (NoClassDefFoundError e) {
|
|
|
475
|
+ // spring-security-crypto 不在 classpath,降级为明文比较
|
|
|
476
|
+ return false;
|
|
|
477
|
+ }
|
|
|
478
|
+ }
|
|
|
479
|
+ // 兼容明文密码(仅开发/测试环境)
|
|
|
480
|
+ return encodedPassword.equals(rawPassword);
|
|
|
481
|
+ }
|
|
71
|
482
|
}
|
|
72
|
483
|
}
|