OpenAppServiceImpl.java
3.19 KB
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
package com.diligrp.rider.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diligrp.rider.common.exception.BizException;
import com.diligrp.rider.entity.OpenApp;
import com.diligrp.rider.mapper.OpenAppMapper;
import com.diligrp.rider.service.OpenAppService;
import com.diligrp.rider.util.SignUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class OpenAppServiceImpl implements OpenAppService {
private final OpenAppMapper openAppMapper;
@Override
public OpenApp create(String appName, Long cityId, Long storeId, String webhookUrl, String webhookEvents, String remark) {
if (cityId == null || cityId < 1) throw new BizException("请选择关联城市/租户");
OpenApp app = new OpenApp();
app.setAppName(appName);
app.setAppKey(SignUtil.generateAppKey());
app.setAppSecret(SignUtil.generateAppSecret());
app.setCityId(cityId);
app.setStoreId(storeId != null ? storeId : 0L);
app.setStatus(1);
app.setWebhookUrl(webhookUrl);
app.setWebhookEvents(webhookEvents);
app.setRemark(remark);
app.setCreateTime(System.currentTimeMillis() / 1000);
openAppMapper.insert(app);
return app;
}
@Override
public List<OpenApp> list(int page) {
int offset = (page - 1) * 20;
return openAppMapper.selectList(new LambdaQueryWrapper<OpenApp>()
.orderByDesc(OpenApp::getId)
.last("LIMIT " + offset + ",20"));
}
@Override
public String resetSecret(Long appId) {
OpenApp app = openAppMapper.selectById(appId);
if (app == null) throw new BizException("应用不存在");
String newSecret = SignUtil.generateAppSecret();
openAppMapper.update(null, new LambdaUpdateWrapper<OpenApp>()
.eq(OpenApp::getId, appId)
.set(OpenApp::getAppSecret, newSecret));
return newSecret;
}
@Override
public void setStatus(Long appId, int status) {
openAppMapper.update(null, new LambdaUpdateWrapper<OpenApp>()
.eq(OpenApp::getId, appId)
.set(OpenApp::getStatus, status));
}
@Override
public void updateWebhook(Long appId, String webhookUrl, String webhookEvents) {
openAppMapper.update(null, new LambdaUpdateWrapper<OpenApp>()
.eq(OpenApp::getId, appId)
.set(OpenApp::getWebhookUrl, webhookUrl)
.set(OpenApp::getWebhookEvents, webhookEvents));
}
@Override
public OpenApp getByAppKey(String appKey) {
return openAppMapper.selectOne(new LambdaQueryWrapper<OpenApp>()
.eq(OpenApp::getAppKey, appKey)
.last("LIMIT 1"));
}
@Override
public boolean verifySign(String appKey, String timestamp, String nonce, String sign) {
OpenApp app = getByAppKey(appKey);
if (app == null || app.getStatus() != 1) return false;
return SignUtil.verify(appKey, timestamp, nonce, sign, app.getAppSecret());
}
}