GroupRelationController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package com.cyksj.web.controller.group;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.thread.ThreadUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.extra.servlet.ServletUtil;
  7. import cn.hutool.json.JSONObject;
  8. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  9. import com.cyksj.common.constant.Constant;
  10. import com.cyksj.common.exception.BusinessRuntimeException;
  11. import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
  12. import com.cyksj.common.util.GoogleGenerator;
  13. import com.cyksj.common.util.StringUtil;
  14. import com.cyksj.dto.Result;
  15. import com.cyksj.enums.GatewayResponse;
  16. import com.cyksj.mapper.*;
  17. import com.cyksj.model.entity.*;
  18. import com.cyksj.model.manage.views.GroupsRelationView;
  19. import com.cyksj.model.request.OrderPayRequest;
  20. import com.cyksj.model.views.RenewalView;
  21. import com.cyksj.model.views.UserTicketView;
  22. import com.cyksj.redis.RedisService;
  23. import com.cyksj.service.order.OrderDonService;
  24. import com.cyksj.service.relation.GroupRelationFrontService;
  25. import com.cyksj.service.user.UserService;
  26. import com.cyksj.web.util.StpUserUtil;
  27. import com.ejlchina.searcher.BeanSearcher;
  28. import com.ejlchina.searcher.param.Operator;
  29. import com.ejlchina.searcher.util.MapUtils;
  30. import lombok.RequiredArgsConstructor;
  31. import lombok.extern.slf4j.Slf4j;
  32. import org.springframework.web.bind.annotation.*;
  33. import javax.servlet.http.HttpServletRequest;
  34. import java.util.List;
  35. import java.util.concurrent.TimeUnit;
  36. /**
  37. * @author chan
  38. * @date 2022/9/27 11:48 AM
  39. */
  40. @Slf4j
  41. @RequiredArgsConstructor
  42. @RestController
  43. @RequestMapping("/applets/group/relation")
  44. public class GroupRelationController {
  45. private final BeanSearcher beanSearcher;
  46. private final OrderDonService orderDonService;
  47. private final GroupsRelationMapper relationMapper;
  48. private final GroupRelationFrontService groupRelationFrontService;
  49. private final UserService userService;
  50. private final ChatGptPwdViewRecordMapper chatGptPwdViewRecordMapper;
  51. private final HttpServletRequest request;
  52. private final AccountDoubleVerifyRecordMapper accountDoubleVerifyRecordMapper;
  53. private final DoubleVerifyClickRecordMapper doubleVerifyRecordMapper;
  54. private final RedisService redisService;
  55. private final SysConfigMapper sysConfigMapper;
  56. private final GlobalThreadPoolTaskExecutor THREAD_POOL;
  57. @RequestMapping("/get/renewal")
  58. public Result<List<RenewalView>> get(Boolean isLoginPopularize) {
  59. long userId = StpUserUtil.getLoginIdAsLong();
  60. List<RenewalView> list = groupRelationFrontService.getMyTicket(userId, isLoginPopularize);
  61. return GatewayResponse.SUCCESS.newBuilder().toResult(list);
  62. }
  63. @PostMapping("/post/renewal")
  64. public Result<OrderDon> renewal(@RequestBody OrderPayRequest request) throws Exception {
  65. long userId = StpUserUtil.getLoginIdAsLong();
  66. request.setUserId(userId);
  67. OrderDon orderDon = orderDonService.renewal(request);
  68. return GatewayResponse.SUCCESS.newBuilder().toResult(orderDon);
  69. }
  70. @PutMapping("/update/set/account")
  71. public Result<String> setAccount(@RequestBody GroupsRelation relation) {
  72. long userId = StpUserUtil.getLoginIdAsLong();
  73. GroupsRelation re = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getId, relation.getId()).eq(GroupsRelation::getUserId, userId).last(" limit 1"));
  74. if (re == null) {
  75. throw new BusinessRuntimeException("该车票不存在,请联系客服.");
  76. }
  77. relationMapper.update(relation, Wrappers.lambdaUpdate(GroupsRelation.class).set(GroupsRelation::getAccount, relation.getAccount()).eq(GroupsRelation::getId, relation.getId()));
  78. return GatewayResponse.SUCCESS.newBuilder().toResult();
  79. }
  80. /**
  81. * 查询是否加入了企业微信
  82. */
  83. @GetMapping("/check/joinCorpWx")
  84. public Result<Boolean> isJoinCorpWx() {
  85. long userId = StpUserUtil.getLoginIdAsLong();
  86. Boolean flag = groupRelationFrontService.isJoinCorpWx(userId, false, false);
  87. return GatewayResponse.SUCCESS.newBuilder().toResult(flag);
  88. }
  89. /**
  90. * 获取全部车票
  91. */
  92. @GetMapping("/get")
  93. public Result<List<UserTicketView>> getUserTicketView() {
  94. long userId = StpUserUtil.getLoginIdAsLong();
  95. List<Long> userIds = userService.getRelationUserIdList(userId, null);
  96. List<UserTicketView> ticketViews = beanSearcher.searchAll(UserTicketView.class, MapUtils.builder()
  97. .field(UserTicketView::getUserId, userIds).op(Operator.InList)
  98. .orderBy(UserTicketView::getRelationId).asc()
  99. .build());
  100. return GatewayResponse.SUCCESS.newBuilder().toResult(ticketViews);
  101. }
  102. /**
  103. * 记录查看ChatGPT车票密码
  104. */
  105. @GetMapping("/recordView/{relationId}")
  106. public Result<String> recordGPTPwdView(@PathVariable Long relationId) {
  107. long userId = StpUserUtil.getLoginIdAsLong();
  108. List<Long> userIds = userService.getRelationUserIdList(userId, null);
  109. GroupsRelationView groupsRelationView = beanSearcher.searchFirst(GroupsRelationView.class, MapUtils.builder()
  110. .field(GroupsRelationView::getUserId, userIds).op(Operator.InList)
  111. .field(GroupsRelationView::getId, relationId).build());
  112. if (groupsRelationView.getAccountId() == null) return GatewayResponse.SUCCESS.newBuilder().toResult();
  113. if (groupRelationFrontService == null) throw BusinessRuntimeException.getInstance("车票已不存在");
  114. ChatGptPwdViewRecord viewRecord = new ChatGptPwdViewRecord();
  115. viewRecord.setUserId(userId);
  116. viewRecord.setRelationId(relationId);
  117. viewRecord.setSkuId(groupsRelationView.getSkuId());
  118. viewRecord.setOperateLocation(StringUtil.getRealAddressByIP(ServletUtil.getClientIP(request)));
  119. chatGptPwdViewRecordMapper.insert(viewRecord);
  120. if (StrUtil.isEmpty(groupsRelationView.getApiSecret())) {
  121. THREAD_POOL.execute(() -> {
  122. //车队所有人查看密码
  123. Integer num = groupsRelationView.getGtNum();
  124. Integer viewNum = relationMapper.selectViewPwdCount(groupsRelationView.getGroupsId());
  125. if (num <= viewNum) {
  126. AccountDoubleVerifyRecord exist = accountDoubleVerifyRecordMapper.selectOne(Wrappers.lambdaQuery(AccountDoubleVerifyRecord.class)
  127. .eq(AccountDoubleVerifyRecord::getAccountId, groupsRelationView.getAccountId())
  128. .eq(AccountDoubleVerifyRecord::getDeleted, true).last("limit 1"));
  129. if (exist == null) {
  130. if (redisService.setNx(String.format("%s-%s", groupsRelationView.getAccountId(), groupsRelationView.getId()), groupsRelationView.getId(), 60l)) {
  131. //记录
  132. AccountDoubleVerifyRecord accountDoubleVerifyRecord = new AccountDoubleVerifyRecord();
  133. accountDoubleVerifyRecord.setAccountId(groupsRelationView.getAccountId());
  134. accountDoubleVerifyRecord.setAccount(groupsRelationView.getTripsAccount());
  135. accountDoubleVerifyRecord.setGroupsId(groupsRelationView.getGroupsId());
  136. accountDoubleVerifyRecord.setSkuId(groupsRelationView.getSkuId());
  137. accountDoubleVerifyRecord.setRemark("车队所有人看过密码");
  138. accountDoubleVerifyRecordMapper.insert(accountDoubleVerifyRecord);
  139. }
  140. }
  141. }
  142. });
  143. }
  144. return GatewayResponse.SUCCESS.newBuilder().toResult(groupsRelationView.getTripsPassword());
  145. }
  146. /**
  147. * 获取AI类 ChatGPT产品谷歌验证码
  148. */
  149. @GetMapping("/get/authCode")
  150. public Result<String> getAuthCode(Long relationId) {
  151. long userId = StpUserUtil.getLoginIdAsLong();
  152. List<Long> userIds = userService.getRelationUserIdList(userId, null);
  153. //每个人 每月只能获取两次
  154. if (relationId == null) throw BusinessRuntimeException.getInstance("车票不存在");
  155. GroupsRelationView groupsRelationView = beanSearcher.searchFirst(GroupsRelationView.class, MapUtils.builder()
  156. .field(GroupsRelationView::getId, relationId)
  157. .field(GroupsRelationView::getUserId, userIds).op(Operator.InList).build());
  158. if (groupsRelationView == null) throw BusinessRuntimeException.getInstance("车票不存在");
  159. if (StrUtil.isEmpty(groupsRelationView.getApiSecret())) throw BusinessRuntimeException.getInstance("未开启验证");
  160. DateTime now = DateTime.now();
  161. Integer count = doubleVerifyRecordMapper.selectCount(Wrappers.lambdaQuery(DoubleVerifyClickRecord.class)
  162. .in(DoubleVerifyClickRecord::getUserId, userIds)
  163. .eq(DoubleVerifyClickRecord::getRelationId, relationId)
  164. .eq(DoubleVerifyClickRecord::getAccountId, groupsRelationView.getAccountId())
  165. .between(DoubleVerifyClickRecord::getCreatedTime, DateUtil.beginOfMonth(now), DateUtil.endOfMonth(now)));
  166. SysConfig double_verify_num = sysConfigMapper.selectOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, Constant.ACCOUNT_DOUBLE_VERIFY_KEY));
  167. int num = 2;
  168. if (double_verify_num != null) {
  169. num = Integer.parseInt(double_verify_num.getSysValue());
  170. }
  171. if (count >= num) throw BusinessRuntimeException.getInstance("获取双重验证码次数上限");
  172. int second = now.second();
  173. if (second < 30 && 30 - second < 3) {
  174. ThreadUtil.sleep(30 - second + 1, TimeUnit.SECONDS);
  175. } else if (second > 30 && 60 - second < 3) {
  176. ThreadUtil.sleep(60 - second + 1, TimeUnit.SECONDS);
  177. }
  178. StringBuilder sb = new StringBuilder();
  179. try {
  180. long code = GoogleGenerator.getCode(groupsRelationView.getApiSecret(), now.getTime());
  181. sb.append(code);
  182. while (sb.length() < 6) {
  183. sb.insert(0, 0);
  184. }
  185. DoubleVerifyClickRecord doubleVerifyClickRecord = new DoubleVerifyClickRecord();
  186. doubleVerifyClickRecord.setRelationId(relationId);
  187. doubleVerifyClickRecord.setUserId(userId);
  188. doubleVerifyClickRecord.setAccountId(groupsRelationView.getAccountId());
  189. doubleVerifyClickRecord.setAccount(groupsRelationView.getTripsAccount());
  190. doubleVerifyClickRecord.setSkuId(groupsRelationView.getSkuId());
  191. doubleVerifyRecordMapper.insert(doubleVerifyClickRecord);
  192. redisService.set(String.format("%s-%s", userId, sb.toString()), 0, 60 * 60l);
  193. redisService.set(String.format("%s/%s", userId, sb.toString()), groupsRelationView.getApiSecret(), 60 * 60l);
  194. } catch (Exception e) {
  195. throw BusinessRuntimeException.getInstance("获取双重验证码错误,请联系客服");
  196. }
  197. JSONObject re = new JSONObject();
  198. re.putOpt("code", sb.toString());
  199. int nowSecond = DateTime.now().second();
  200. int time = 0;
  201. if (nowSecond < 30) {
  202. time = 30 - nowSecond;
  203. } else {
  204. time = 60 - nowSecond;
  205. }
  206. re.putOpt("time", time);
  207. return GatewayResponse.SUCCESS.newBuilder().toResult(re.toString());
  208. }
  209. /**
  210. * 刷新验证码
  211. */
  212. @GetMapping("/refresh/code")
  213. public Result<String> refreshCode(String code) {
  214. long userId = StpUserUtil.getLoginIdAsLong();
  215. String key = String.format("%s-%s", userId, code);
  216. String apiSecretKey = String.format("%s/%s", userId, code);
  217. Object value = redisService.get(key);
  218. if (value == null) {
  219. throw BusinessRuntimeException.getInstance("请重新获取双重验证码");
  220. }
  221. Integer refreshNum = Integer.parseInt(value.toString());
  222. if (refreshNum >= 3) {
  223. redisService.del(key);
  224. redisService.del(apiSecretKey);
  225. throw BusinessRuntimeException.getInstance("刷新次数已达到3次,请重新获取验证码");
  226. }
  227. String apiSecret = redisService.getStr(apiSecretKey);
  228. StringBuilder sb = new StringBuilder();
  229. try {
  230. long c = GoogleGenerator.getCode(apiSecret, DateTime.now().getTime());
  231. sb.append(c);
  232. while (sb.length() < 6) {
  233. sb.insert(0, 0);
  234. }
  235. redisService.incr(key, 1l);
  236. } catch (Exception e) {
  237. throw BusinessRuntimeException.getInstance("刷新双重验证码错误,请联系客服");
  238. }
  239. return GatewayResponse.SUCCESS.newBuilder().toResult(sb.toString());
  240. }
  241. }