UserOperationController.java
21 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package com.topdraw.business.process.rest;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.annotation.AnonymousAccess;
import com.topdraw.business.module.member.profile.domain.MemberProfile;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.user.iptv.domain.UserTv;
import com.topdraw.business.module.user.iptv.service.UserTvService;
import com.topdraw.business.module.user.iptv.service.dto.UserTvDTO;
import com.topdraw.business.module.user.weixin.domain.UserWeixin;
import com.topdraw.business.module.user.weixin.service.UserWeixinService;
import com.topdraw.business.module.user.weixin.service.dto.UserWeixinDTO;
import com.topdraw.business.process.domian.TempIptvUser;
import com.topdraw.business.process.domian.weixin.BindBean;
import com.topdraw.business.process.domian.weixin.SubscribeBean;
import com.topdraw.business.process.domian.weixin.SubscribeBeanEvent;
import com.topdraw.business.process.domian.weixin.WeiXinUserBean;
import com.topdraw.business.process.service.UserOperationService;
import com.topdraw.common.ResultInfo;
import com.topdraw.config.RedisKeyUtil;
import com.topdraw.exception.BadRequestException;
import com.topdraw.exception.EntityNotFoundException;
import com.topdraw.resttemplate.RestTemplateClient;
import com.topdraw.util.Base64Util;
import com.topdraw.util.JSONUtil;
import com.topdraw.utils.RedisUtils;
import com.topdraw.utils.StringUtils;
import com.topdraw.weixin.util.WeChatConstants;
import com.topdraw.weixin.util.WeixinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.*;
import java.util.stream.Collectors;
@Api("账户处理")
@RestController
@RequestMapping(value = "/ucEngine/api/userOperation")
@Slf4j
public class UserOperationController {
@Autowired
private UserOperationService userTvOperationService;
@Autowired
private UserWeixinService userWeixinService;
@Autowired
private UserTvService userTvService;
@Autowired
private MemberService memberService;
@Autowired
private RedisUtils redisUtils;
@Autowired
private UserOperationService userOperationService;
private static final String SUBSCRIBE = "subscribe";
private static final String UNSUBSCRIBE = "unsubscribe";
private static final Integer SUBSCRIBE_STATUS = 1;
@PostMapping(value = "/createUserAndCreateMember")
@ApiOperation("新增大屏账户同时创建会员信息")
@AnonymousAccess
public ResultInfo createUserAndCreateMember(@Validated @RequestBody TempIptvUser resources) {
log.info("createUserAndCreateMember ==> input ==> [{}]",resources);
UserTv userTv = new UserTv();
BeanUtils.copyProperties(resources,userTv);
boolean result = this.userTvOperationService.createMemberByUserTv(userTv);
return ResultInfo.success(result);
}
@PostMapping(value = "/createWeixinUserAndCreateMember")
@ApiOperation("新增小屏账户同时创建会员信息")
@AnonymousAccess
public ResultInfo createWeixinUserAndCreateMember(@Validated @RequestBody UserWeixin resources) {
log.info("createWeixinUserAndCreateMember ==> input ==> [{}]",resources);
String appId = resources.getAppid();
if (StringUtils.isBlank(appId))
throw new NullPointerException("appId is null !");
String openId = resources.getOpenid();
if (StringUtils.isBlank(openId))
throw new NullPointerException("openId is null !");
String unionId = resources.getUnionid();
if (StringUtils.isBlank(unionId))
throw new NullPointerException("unionId is null !");
UserWeixinDTO result = this.userTvOperationService.createWeixinUserAndCreateMember(resources);
return ResultInfo.success(result);
}
@GetMapping(value = "/findBindByPlatformAccount/{platformAccount}")
@AnonymousAccess
public ResultInfo findBindByPlatformAccount(@PathVariable("platformAccount") String platformAccount) {
log.info("findBindByPlatformAccount ==> input ==> [{}]",platformAccount);
List<MemberDTO> result = this.userTvOperationService.findBindByPlatformAccount(platformAccount);
return ResultInfo.success(result);
}
@PostMapping(value = "/sendQrCodeMessage")
@ApiOperation("带参二维码")
@AnonymousAccess
public ResultInfo sendQrCodeMessage(@RequestBody String content) {
log.info("sendQrCodeMessage ==> input ==> [{}]",content);
boolean result = this.userTvOperationService.sendQrCodeMessage(content);
return ResultInfo.success(result);
}
@PostMapping(value = "/deleteAllCollection")
@ApiOperation("删除全部收藏")
@AnonymousAccess
public ResultInfo deleteAllCollection(@RequestBody String content) {
log.info("deleteAllCollection ==> input ==> [{}]",content);
boolean result = this.userTvOperationService.deleteAllCollection(content);
return ResultInfo.success(result);
}
@PostMapping(value = "/deleteCollection")
@ApiOperation("删除收藏")
@AnonymousAccess
public ResultInfo deleteCollection(@RequestBody String content) {
log.info("deleteCollection ==> input ==> [{}]",content);
boolean result = this.userTvOperationService.deleteCollection(content);
return ResultInfo.success(result);
}
@PostMapping(value = "/addCollection")
@ApiOperation("添加收藏")
@AnonymousAccess
public ResultInfo addCollection(@RequestBody String content) {
log.info("addCollection ==> input ==> [{}]",content);
boolean result = this.userTvOperationService.addCollection(content);
return ResultInfo.success(result);
}
@RequestMapping(value = "/unbind")
@ApiOperation("大屏用户解绑")
@AnonymousAccess
public ResultInfo unbind(@Validated @RequestBody TempIptvUser resources) {
log.info("unbind ==> input ==> [{}]",resources);
UserTv userTv = new UserTv();
BeanUtils.copyProperties(resources,userTv);
String unionid = resources.getUnionid();
String memberCode1 = resources.getMemberCode();
if (Objects.nonNull(memberCode1)) {
MemberDTO memberDTO = this.memberService.getByCode(memberCode1);
String memberCode = memberDTO.getCode();
if (StringUtils.isNotBlank(memberCode)) {
String platformAccount = userTv.getPlatformAccount();
UserTvDTO userTvDTO = this.userTvService.findByPlatformAccount(platformAccount);
if (Objects.isNull(userTvDTO))
throw new EntityNotFoundException(UserTvDTO.class,"platformAccount","大屏账户不存在");
// 解绑
userTv.setMemberCode(memberCode);
this.userTvOperationService.unbind(userTv);
// 设置默认账号,以时间最早的为准
List<MemberDTO> memberDTOList = this.memberService.findByUserIptvId(userTvDTO.getId());
if (CollectionUtils.isNotEmpty(memberDTOList)) {
List<MemberDTO> collect = memberDTOList.stream().filter(memberDTO1 -> !memberDTO1.getCode().equalsIgnoreCase(memberCode1)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collect) ){
if(collect.size() > 1) {
collect.sort(new Comparator<MemberDTO>() {
@Override
public int compare(MemberDTO memberDTO, MemberDTO t1) {
return t1.getBindIptvTime().compareTo(memberDTO.getBindIptvTime());
}
});
}
// 有其他会员的话,绑定最早的一个会员
MemberDTO memberDTO1 = collect.get(0);
userTvDTO.setPriorityMemberCode(memberDTO1.getCode());
BeanUtils.copyProperties(userTvDTO,userTv);
this.userTvService.update(userTv);
}
}
} else {
throw new EntityNotFoundException(MemberDTO.class,"code","会员信息不存在!!");
}
}
return ResultInfo.success();
}
@RequestMapping(value = "/changeMainAccount")
@ApiOperation("大屏更换主账号")
@AnonymousAccess
public ResultInfo changeMainAccount(@Validated @RequestBody TempIptvUser resources) {
log.info("changeMainAccount ==> input ==> [{}]",resources);
UserTv userTv = new UserTv();
BeanUtils.copyProperties(resources,userTv);
Long memberId = resources.getMemberId();
String memberCode1 = resources.getMemberCode();
if (Objects.nonNull(memberCode1)) {
MemberDTO memberDTO = this.memberService.getByCode(memberCode1);
String memberCode = memberDTO.getCode();
if (StringUtils.isNotBlank(memberCode)) {
userTv.setMemberCode(memberCode);
this.userTvOperationService.changeMainAccount(userTv);
}
} else {
String unionid = resources.getUnionid();
this.userTvOperationService.changeMainAccountByUnionId(userTv,unionid);
}
return ResultInfo.success("update success");
}
@PostMapping("/serviceLogin")
@ApiOperation("微信服务号(H5)登录")
@AnonymousAccess
public ResultInfo serviceLogin(@Validated @RequestBody WeiXinUserBean resources) {
Object o = this.userTvOperationService.serviceLogin(resources);
return ResultInfo.success(o);
}
@PostMapping("/appletLogin")
@ApiOperation("微信小程序登录")
@AnonymousAccess
public ResultInfo appletLogin(@Validated @RequestBody WeiXinUserBean resources) {
log.info("appletLogin ==> input ==> [{}]",resources);
UserWeixinDTO result = this.userTvOperationService.appletLogin(resources);
return ResultInfo.success(result);
}
@PostMapping("/appletBind")
@ApiOperation("微信小程序绑定大屏")
@AnonymousAccess
public ResultInfo appletBind(@Validated @RequestBody BindBean resources) {
log.info("appletBind ==> input ==> [{}]",resources);
String unionId = resources.getUnionid();
if (StringUtils.isBlank(unionId))
Assert.state(StrUtil.isNotBlank(unionId), "跨屏绑定,请先进行授权");
String platformAccount = resources.getPlatformAccount();
if (Objects.isNull(platformAccount))
Assert.state(StrUtil.isNotBlank(platformAccount), "大屏账户不得为空");
UserTvDTO userTvDTO = this.userTvService.findByPlatformAccount(platformAccount);
resources.setPlatformUserId(userTvDTO.getId());
resources.setPlatformAccount(platformAccount);
boolean result = this.userTvOperationService.appletBind(resources);
return ResultInfo.success(result);
}
@PostMapping("/subscribe")
@ApiOperation("微信公众号关注")
@AnonymousAccess
public ResultInfo subscribe(@Validated @RequestBody SubscribeBeanEvent data) throws IOException {
String content = data.getContent();
log.info("subscribe ==> input ==> [{}]",content);
SubscribeBean subscribeBean = JSONUtil.parseMsg2Object(content, SubscribeBean.class);
this.parseSubscribe(subscribeBean);
boolean result = this.userTvOperationService.subscribe(subscribeBean);
return ResultInfo.success(result);
}
/**
*
* @param subscribeBean
* @throws IOException
*/
private void parseSubscribe(SubscribeBean subscribeBean) throws IOException {
if (Objects.nonNull(subscribeBean)) {
String appId = subscribeBean.getAppId();
// appId不得为空
if (StringUtils.isBlank(appId))
throw new BadRequestException("appId 不存在!");
// openId
String openId = subscribeBean.getOpenId();
if (StringUtils.isBlank(openId))
throw new BadRequestException("openId 不存在!");
// unionId
String unionId = subscribeBean.getUnionid();
if (StringUtils.isBlank(unionId))
throw new BadRequestException("unionId 不存在!");
// 匹配配置文件中的微信列表信息
Map<String, String> wxInfoMap = WeixinUtil.getWeixinInfoByAppid(appId);
if (Objects.nonNull(wxInfoMap)) {
// 程序类型
String appType = wxInfoMap.get("appType");
// 非订阅号,暂不处理。返回暂不支持
if (ObjectUtil.notEqual(appType, WeChatConstants.WX_SUBSCRIPTION))
throw new BadRequestException("非订阅号");
}
// 大屏账户信息
JSONObject iptvUserInfo = null;
// 缓存的大屏信息,使用unionid即可
String content = (String) this.redisUtils.get(RedisKeyUtil.genSeSuSubscribeKey(unionId));
if (StringUtils.isNotBlank(content)) {
// 大屏信息
iptvUserInfo = JSONObject.parseObject(content);
} else {
String eventKey = subscribeBean.getEventKey();
log.info(" eventKey ==> [{}] ", eventKey);
if (StringUtils.isNotBlank(eventKey)) {
// 用户扫描带参二维码关注。发消息
// 去除固定前缀,获取二维码参数
eventKey = eventKey.substring(8);
iptvUserInfo = JSONObject.parseObject(eventKey);
}
}
// 用户自己搜索关注就没有大屏信息,否则表示扫码关注
if (Objects.nonNull(iptvUserInfo)) {
subscribeBean.setIptvUserInfo(iptvUserInfo);
String headimgurl = iptvUserInfo.get("headimgurl").toString();
String nickname = iptvUserInfo.get("nickname").toString();
if (StringUtils.isNotBlank(nickname)) {
String nicknameDecode = URLDecoder.decode(nickname, "UTF-8");
String nicknameEncode = Base64Util.encode(nicknameDecode);
subscribeBean.setNickname(nicknameEncode);
}
if (StringUtils.isNotBlank(headimgurl)) {
String headimgurlDecode = URLDecoder.decode(headimgurl, "UTF-8");
subscribeBean.setHeadimgurl(headimgurlDecode);
}
}
}
}
@PostMapping("/unsubscribe")
@ApiOperation("微信公众号取关")
@AnonymousAccess
public ResultInfo unsubscribe(@Validated @RequestBody SubscribeBeanEvent data) {
String content = data.getContent();
log.info("unsubscribe ==> input ==> [{}]",content);
SubscribeBean subscribeBean = JSONUtil.parseMsg2Object(content, SubscribeBean.class);
boolean result = this.userTvOperationService.unsubscribe(subscribeBean);
return ResultInfo.success(result);
}
/**
* @param data
* @description 通过大屏关注的订阅号,因为订阅号不支持带参二维码,
* 所以需要先跳到H5(带大屏参数),
* 再做服务号授权(订阅号只能按钮触发获取信息),
* 再将服务号信息和大屏参数缓存下来(该接口的功能)
* 然后跳转到订阅号关注页
* 关注后回调处理时,根据unionid进行相关逻辑
*
* 业务场景:
* 1.未关注-绑定
* 新用户
* 2.先关注-绑定
* 1).未绑定
* 2).已绑定
* 2.1).已绑定当前大屏账户
* 2.2).换绑
* 2.2.1).
* @author Hongyan Wang
* @date 2021/8/24 4:54 下午
*/
@PostMapping(value = "/saveUserInfo")
@ApiOperation("保存大屏侧信息")
@AnonymousAccess
public ResultInfo saveUserInfo(@RequestBody String data) {
log.info("saveUserInfo ==> input ==> [{}]",data);
Assert.notNull(data, "用户数据不可为空");
JSONObject json = JSONObject.parseObject(data);
String unionid = json.getString("unionid");
Assert.state(StrUtil.isNotBlank(unionid), "unionid不可为空");
List<Object> resultList = new ArrayList<>();
// 大屏侧通过返回值来展示对应的小程序页面
String result = SUBSCRIBE;
String platformAccount1 = "";
// 保存大小屏信息到redis同时返回小屏信息
UserWeixinDTO userWeixinDTO = this.userTvOperationService.saveUserInfo(data);
// 小屏会员
MemberDTO memberDTO = this.memberService.findById(userWeixinDTO.getMemberId());
// 小屏用户不存在或者关注状态为未关注(0),返回未关注
if (Objects.isNull(userWeixinDTO) || Objects.isNull(userWeixinDTO.getId()) || userWeixinDTO.getStatus() != SUBSCRIBE_STATUS) {
result = UNSUBSCRIBE;
if (Objects.nonNull(memberDTO)) {
// 检查是否绑定,返回绑定的大屏账户
UserTvDTO userTvDTO = this.userOperationService.checkBind(memberDTO);
if (Objects.nonNull(userTvDTO)) {
platformAccount1 = userTvDTO.getPlatformAccount();
}
} else {
log.info("userWeixinDTO ==>> [{}]",userWeixinDTO);
throw new EntityNotFoundException(MemberDTO.class,"code","member is null !!");
}
resultList.add(result);
resultList.add(platformAccount1);
log.info("saveUserInfo ==>> result ==>> [{}]",resultList);
return ResultInfo.success(resultList);
}
/***************************************************************************************/
// 关注未绑定
if (result.equalsIgnoreCase(SUBSCRIBE)) {
// redis中的大小屏信息
String content = (String) this.redisUtils.get(RedisKeyUtil.genSeSuSubscribeKey(unionid));
JSONObject iptvUserInfo = JSONObject.parseObject(content);
// redis中的大小屏信息
log.info("saveUserInfo ==> redis content iptvUserInfo ==> [{}]",iptvUserInfo);
// 大屏账户
String platformAccount = iptvUserInfo.getString("platformAccount");
try {
String headimgurl = iptvUserInfo.get("headimgurl").toString();
String nickname = iptvUserInfo.get("nickname").toString();
if (StringUtils.isNotBlank(nickname)) {
String nicknameDecode = URLDecoder.decode(nickname, "UTF-8");
String nicknameEncode = Base64Util.encode(nicknameDecode);
memberDTO.setNickname(nicknameEncode);
}
if (StringUtils.isNotBlank(headimgurl)) {
String headimgurlDecode = URLDecoder.decode(headimgurl, "UTF-8");
String imageEncode = Base64Util.encode(headimgurlDecode);
String image = RestTemplateClient.chooseImage(imageEncode, "image");
memberDTO.setAvatarUrl(StringUtils.isNotBlank(image) == true ? image:headimgurlDecode);
}
}catch (Exception e) {
log.info("headimgurl , nickname ===>> encode error!");
e.printStackTrace();
}
// 大小屏绑定,如果已经绑定了别的大屏,则不进行处理,返回已绑定的大屏信息
// 已绑定的大屏信息
UserTvDTO userTvDTO = this.userOperationService.bind(memberDTO, platformAccount);
if (userTvDTO != null) {
platformAccount1 = userTvDTO.getPlatformAccount();
}
}
/****************************************************************************************/
resultList.add(result);
resultList.add(platformAccount1);
// return ["subscribe","platform_account"]
ResultInfo<Object> success = ResultInfo.success(resultList);
log.info("saveUserInfo ==> ResultInfo ==> [{}]",success);
return success;
}
/**
*
* @param resources
* @return
*/
@PostMapping(value = "/saveUserWeixinPhone")
@ApiOperation("保存用户手机号信息")
@AnonymousAccess
public ResultInfo saveUserWeixinPhone(@RequestBody WeiXinUserBean resources) {
log.info("saveUserWeixinPhone ==> input ==> [{}]",resources);
MemberProfile s = this.userTvOperationService.saveUserWeixinPhone(resources);
return ResultInfo.success(s);
}
}