WeGroupCodeController.java
9.44 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.linkwechat.web.controller.wecom;
import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.constant.HttpStatus;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.common.utils.QREncode;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.file.FileUtils;
import com.linkwechat.framework.web.domain.server.SysFile;
import com.linkwechat.framework.web.service.FileService;
import com.linkwechat.wecom.domain.WeGroupCode;
import com.linkwechat.wecom.domain.WeGroupCodeActual;
import com.linkwechat.wecom.service.IWeGroupCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* 客户群活码Controller
*
* @author ruoyi
* @date 2020-10-07
*/
@Api(tags = "群活码")
@RestController
@RequestMapping("/wecom/groupCode")
public class WeGroupCodeController extends BaseController {
@Autowired
IWeGroupCodeService groupCodeService;
@Autowired
FileService fileService;
@Value("${H5.domainPrefix}")
private String h5DomainPrefix;
/**
* 查询客户群活码列表
*/
@ApiOperation(value = "查询客户群活码列表", httpMethod = "GET")
// @PreAuthorize("@ss.hasPermi('drainageCode:group:list')")
@GetMapping("/list")
public TableDataInfo list(WeGroupCode weGroupCode) {
startPage();
List<WeGroupCode> list = groupCodeService.selectWeGroupCodeList(weGroupCode);
return getDataTable(list);
}
/**
* 批量下载群活码
*/
@ApiOperation(value = "批量下载群活码", httpMethod = "GET")
// @PreAuthorize("@ss.hasPermi('drainageCode:group:downloadBatch')")
@Log(title = "群活码批量下载", businessType = BusinessType.OTHER)
@GetMapping("/downloadBatch")
public void downloadBatch(String ids, HttpServletRequest request, HttpServletResponse response) {
// 构建文件信息列表
List<Map<String, String>> fileList = Arrays
.stream(Optional.ofNullable(ids).orElse("").split(","))
.filter(StringUtils::isNotEmpty)
.map(id -> {
WeGroupCode code = groupCodeService.getById(id);
Map<String, String> fileMap = new HashMap<>();
fileMap.put("fileName", code.getActivityName() + ".png");
fileMap.put("url", code.getCodeUrl());
return fileMap;
})
.collect(Collectors.toList());
try {
FileUtils.batchDownloadFile(fileList, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 下载群活码
*/
@ApiOperation(value = "群活码下载", httpMethod = "GET")
// @PreAuthorize("@ss.hasPermi('drainageCode:group:download')")
@Log(title = "群活码下载", businessType = BusinessType.OTHER)
@GetMapping("/download")
public void download(String id, HttpServletResponse response) {
WeGroupCode weGroupCode = groupCodeService.getById(Long.valueOf(id));
try {
FileUtils.downloadFile(weGroupCode.getCodeUrl(), response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取客户群活码详细信息
*/
@ApiOperation(value = "获取客户群活码详细信息", httpMethod = "GET")
// @PreAuthorize("@ss.hasPermi('drainageCode:group:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id) {
WeGroupCode weGroupCode = groupCodeService.getById(id);
if (StringUtils.isNull(weGroupCode)) {
return AjaxResult.error(HttpStatus.NOT_FOUND, "数据不存在");
}
List<WeGroupCodeActual> weGroupCodeActualList = groupCodeService.selectActualList(weGroupCode.getId());
weGroupCode.setActualList(weGroupCodeActualList);
return AjaxResult.success(weGroupCode);
}
/**
* 新增客户群活码
*/
@ApiOperation(value = "新增客户群活码", httpMethod = "POST")
// @PreAuthorize("@ss.hasPermi('drainageCode:group:add')")
@Log(title = "客户群活码", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@Validated @RequestBody WeGroupCode weGroupCode) {
// 活码名唯一性检查
if (groupCodeService.isNameOccupied(weGroupCode)) {
return AjaxResult.error("添加群活码失败,活码名称 " + weGroupCode.getActivityName() + " 已存在");
}
AjaxResult ajax = AjaxResult.success();
weGroupCode.setCreateBy(SecurityUtils.getUsername());
weGroupCode.setCreateTime(new Date());
try {
// 二维码内容,即该二维码扫码后跳转的页面URL
String content = h5DomainPrefix + "/mobile/#/groupCode?id=" + weGroupCode.getId();
SysFile sysFile = fileService.upload(QREncode.getQRCodeMultipartFile(content, weGroupCode.getAvatarUrl()));
weGroupCode.setCodeUrl(sysFile.getImgUrlPrefix() + sysFile.getFileName());
} catch (Exception e) {
return AjaxResult.error("创建群活码失败,请稍后再试");
}
groupCodeService.insertWeGroupCode(weGroupCode);
ajax.put("id", weGroupCode.getId());
return ajax;
}
/**
* 修改客户群活码
*/
@ApiOperation(value = "修改客户群活码", httpMethod = "PUT")
// @PreAuthorize("@ss.hasPermi('drainageCode:group:edit')")
@Log(title = "客户群活码", businessType = BusinessType.UPDATE)
@PutMapping(value = "/{id}")
public AjaxResult edit(@PathVariable("id") Long id, @RequestBody WeGroupCode weGroupCode) {
// 活码名唯一性检查
weGroupCode.setId(id);
if (groupCodeService.isNameOccupied(weGroupCode)) {
return AjaxResult.error("添加群活码失败,活码名称 " + weGroupCode.getActivityName() + " 已存在");
}
weGroupCode.setUpdateBy(SecurityUtils.getUsername());
weGroupCode.setUpdateTime(new Date());
try {
// 二维码内容,即该二维码扫码后跳转的页面URL TODO
String content = h5DomainPrefix + "/mobile/#/groupCode?id=" + weGroupCode.getId();
SysFile sysFile = fileService.upload(QREncode.getQRCodeMultipartFile(content, weGroupCode.getAvatarUrl()));
weGroupCode.setCodeUrl(sysFile.getImgUrlPrefix() + sysFile.getFileName());
} catch (Exception e) {
return AjaxResult.error("创建群活码失败,请稍后再试");
}
return toAjax(groupCodeService.updateWeGroupCode(weGroupCode));
}
/**
* 删除客户群活码
*/
@ApiOperation(value = "删除客户群活码", httpMethod = "DELETE")
// @PreAuthorize("@ss.hasPermi('drainageCode:group:remove')")
@Log(title = "客户群活码", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult batchRemove(@PathVariable Long[] ids) {
return toAjax(groupCodeService.deleteWeGroupCodeByIds(ids));
}
/**
* 从群活码获取第一个可用的实际码(实际埋点记录扫码次数)
*/
@ApiOperation(value = "从群活码获取第一个可用的实际码", httpMethod = "GET")
@GetMapping("/getActualCode/{groupCodeId}")
public AjaxResult getActual(@PathVariable("groupCodeId") String id) {
WeGroupCode groupCode = groupCodeService.getById(id);
//统计扫码次数
groupCodeService.countScanTimes(groupCode);
List<WeGroupCodeActual> actualCodeList = groupCodeService.selectActualList(groupCode.getId());
WeGroupCodeActual groupCodeActual = null;
for (WeGroupCodeActual item : actualCodeList) {
// 获取第一个可用的实际码
if (item.getStatus().intValue() == WeConstans.WE_GROUP_CODE_ENABLE) {
groupCodeActual = item;
break;
}
}
if (StringUtils.isNotNull(groupCodeActual)) {
AjaxResult ajax = AjaxResult.success();
HashMap<String, String> data = new HashMap<>();
data.put("activityName", groupCode.getActivityName());
data.put("tipMsg", groupCode.getTipMsg());
data.put("guide", groupCode.getGuide());
data.put("actualQRCode", groupCodeActual.getActualGroupQrCode());
data.put("isOpenTip", groupCode.getShowTip().toString());
data.put("serviceQrCode", groupCode.getCustomerServerQrCode());
data.put("groupName", groupCodeActual.getChatGroupName());
ajax.put("data", data);
return ajax;
} else {
// 找不到可用的实际群活码也不要抛出错误,否则前端H5页面不好处理。
return AjaxResult.success("没有可用的实际群活码!");
}
}
}