UserServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package com.cyksj.service.user.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.RandomUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  9. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. import com.cyksj.common.constant.Constant;
  11. import com.cyksj.common.exception.BusinessRuntimeException;
  12. import com.cyksj.common.i18n.I18nMessageUtil;
  13. import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
  14. import com.cyksj.common.util.Codec;
  15. import com.cyksj.common.util.Jsons;
  16. import com.cyksj.common.util.StringUtil;
  17. import com.cyksj.dto.UserSharedDto;
  18. import com.cyksj.mapper.GroupsRelationMapper;
  19. import com.cyksj.mapper.OrderDonMapper;
  20. import com.cyksj.mapper.PhoneCodeMapper;
  21. import com.cyksj.mapper.UserMapper;
  22. import com.cyksj.mapper.manage.distribute.UserDistributeSharedMapper;
  23. import com.cyksj.mapper.market.UserBindDetailMapper;
  24. import com.cyksj.model.dto.OrderDonPostDataDto;
  25. import com.cyksj.model.dto.UserWhoami;
  26. import com.cyksj.model.entity.*;
  27. import com.cyksj.model.request.ExchangeCodeTicket;
  28. import com.cyksj.model.request.LoginReq;
  29. import com.cyksj.model.request.OrderBindCommonReq;
  30. import com.cyksj.redis.RedisService;
  31. import com.cyksj.service.exchange.ExchangeCodeService;
  32. import com.cyksj.service.user.UserService;
  33. import lombok.RequiredArgsConstructor;
  34. import lombok.extern.slf4j.Slf4j;
  35. import org.springframework.dao.DuplicateKeyException;
  36. import org.springframework.stereotype.Service;
  37. import org.springframework.util.Assert;
  38. import java.util.ArrayList;
  39. import java.util.Date;
  40. import java.util.List;
  41. import java.util.Optional;
  42. /**
  43. * @author chan
  44. * @date 2021/10/10 下午3:27
  45. */
  46. @Slf4j
  47. @Service
  48. @RequiredArgsConstructor
  49. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  50. private final RedisService redisService;
  51. private final UserBindDetailMapper userBindDetailMapper;
  52. private final UserMapper userMapper;
  53. private final PhoneCodeMapper phoneCodeMapper;
  54. private final UserDistributeSharedMapper userDistributeSharedMapper;
  55. private final OrderDonMapper orderDonMapper;
  56. private final ExchangeCodeService exchangeCodeService;
  57. private final GroupsRelationMapper relationMapper;
  58. private static final GlobalThreadPoolTaskExecutor TASK_POOL = GlobalThreadPoolTaskExecutor.getInstance();
  59. @Override
  60. public UserWhoami whoami(long userId) {
  61. User user = this.getById(userId);
  62. Assert.notNull(user, I18nMessageUtil.getI18nMsg("login_code_expired"));
  63. UserWhoami userWhoami = new UserWhoami();
  64. if (StrUtil.isEmpty(user.getShowId())) {
  65. user.setShowId(getUserShowId(userId));
  66. }
  67. BeanUtil.copyProperties(user, userWhoami);
  68. UserBindDetail bindPhone = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  69. .eq(UserBindDetail::getUserId, userId).last("limit 1"));
  70. //手机号
  71. if (bindPhone != null) {
  72. userWhoami.setBindPhone(bindPhone.getPhone());
  73. userWhoami.setBindEmail(bindPhone.getEmail());
  74. }
  75. Integer type = 3;
  76. if (StrUtil.isNotBlank(user.getLoginPhone())) {
  77. type = 1;
  78. } else if (StrUtil.isNotBlank(user.getEmail())) {
  79. type = 2;
  80. }
  81. userWhoami.setType(type);
  82. return userWhoami;
  83. }
  84. @Override
  85. public List<Long> getRelationUserIdList(Long userId, User selectUser) {
  86. List<Long> userIds = new ArrayList<>();
  87. userIds.add(userId);
  88. UserBindDetail userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  89. .eq(UserBindDetail::getUserId, userId).last("limit 1"));
  90. if (userBindDetail == null) {
  91. User user = Optional.ofNullable(selectUser).orElse(this.getById(userId));
  92. if (StrUtil.isEmpty(user.getOpenId()) && StrUtil.isNotEmpty(user.getLoginPhone())) {
  93. userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  94. .eq(UserBindDetail::getPhone, user.getLoginPhone()).last("limit 1"));
  95. if (userBindDetail != null) {
  96. userIds.add(userBindDetail.getUserId());
  97. }
  98. }
  99. } else {
  100. if (StrUtil.isNotBlank(userBindDetail.getPhone())) {
  101. User otherUser = this.getOne(Wrappers.lambdaQuery(User.class)
  102. .eq(User::getLoginPhone, userBindDetail.getPhone()).last("limit 1"));
  103. if (otherUser != null) {
  104. userIds.add(otherUser.getId());
  105. }
  106. }
  107. }
  108. return userIds;
  109. }
  110. @Override
  111. public List<Long> getUserIdsByNickname(String nickname) {
  112. return userMapper.selectListByNickname(nickname);
  113. }
  114. @Override
  115. public String getUserShowId(Long userId) {
  116. Integer update = 0;
  117. String showId = null;
  118. while (update != 1) {
  119. showId = StringUtil.randomString(5);
  120. try {
  121. update = userMapper.update(null, Wrappers.lambdaUpdate(User.class)
  122. .set(User::getShowId, showId)
  123. .eq(User::getId, userId));
  124. } catch (DuplicateKeyException e) {
  125. update = 0;
  126. }
  127. }
  128. return showId;
  129. }
  130. @Override
  131. public Long getUserIdByShowId(String showId) {
  132. return userMapper.getUserIdByShowId(showId);
  133. }
  134. @Override
  135. public User webLogin(LoginReq loginReq) throws Exception {
  136. LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).last("limit 1");
  137. if (loginReq.getType() == null || loginReq.getType() < 1 || loginReq.getType() > 3) {
  138. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
  139. }
  140. if (loginReq.getType() == 1) {
  141. if (StrUtil.hasEmpty(loginReq.getPhone(), loginReq.getCode())) {
  142. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("params_error"));
  143. }
  144. DateTime now = DateTime.now();
  145. PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, loginReq.getPhone()).last("limit 1"));
  146. if (phoneCode == null || StrUtil.isEmpty(phoneCode.getCode())) {
  147. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("phone_code_valid_error"));
  148. }
  149. //3分钟失效
  150. Date updateTime = phoneCode.getUpdateTime();
  151. DateTime afterFiveMinutes = DateUtil.offsetMinute(updateTime, 3);
  152. if (now.isAfter(afterFiveMinutes)) {
  153. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("phone_code_valid_error"));
  154. }
  155. if (!StrUtil.equals(loginReq.getCode(), phoneCode.getCode())) {
  156. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("verify_code_error"));
  157. }
  158. wrapper.eq(User::getLoginPhone, loginReq.getPhone());
  159. }
  160. if (loginReq.getType() == 2) {
  161. if (StrUtil.isEmpty(loginReq.getEmail())) {
  162. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("params_error"));
  163. }
  164. String code = redisService.getStr(RedisService.key.EMAIL_CODE_KEY.getNameFormat(loginReq.getEmail()));
  165. if (StrUtil.isEmpty(code)) {
  166. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("email_code_valid_error"));
  167. }
  168. if (!StrUtil.equals(loginReq.getCode(), code)) {
  169. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("verify_code_error"));
  170. }
  171. wrapper.eq(User::getEmail, loginReq.getEmail());
  172. redisService.del(RedisService.key.EMAIL_CODE_KEY.getNameFormat(loginReq.getEmail()));
  173. }
  174. if (loginReq.getType() == 3) {
  175. wrapper.eq(User::getEmail, loginReq.getEmail());
  176. }
  177. //保存用户信息
  178. User user = this.getOne(wrapper);
  179. String dsCode = loginReq.getDsCode();
  180. Long sharedId = loginReq.getSharedId();
  181. Long dsPopularizeId = null;
  182. Integer dsType = loginReq.getDsType();
  183. Boolean isNewUser = false;
  184. if (StrUtil.isNotBlank(dsCode)) {
  185. dsCode = dsCode.trim();
  186. User sharedUser = this.getOne(Wrappers.lambdaQuery(User.class)
  187. .eq(User::getShowId, dsCode).last("limit 1"));
  188. if (sharedUser != null) {
  189. sharedId = sharedUser.getId();
  190. }
  191. }
  192. if (user == null) {
  193. user = new User();
  194. isNewUser = true;
  195. StringBuilder name = new StringBuilder();
  196. name.append(Constant.DEFAULT_NAME);
  197. if (loginReq.getPopularizeId() != null) {
  198. user.setPopularizeId(loginReq.getPopularizeId());
  199. }
  200. if (loginReq.getType() == 1) {
  201. user.setLoginPhone(loginReq.getPhone());
  202. }
  203. if (loginReq.getType() == 2 || loginReq.getType() == 3) {
  204. user.setEmail(loginReq.getEmail());
  205. name.append("@");
  206. }
  207. user.setNickname(String.format("%s%s", name.toString(), Codec.DoDigest.custom().setAlgorithm(Codec.DoDigest.Algorithm.MD5).setStringData(String.format("%s%s", RandomUtil.randomString(6), loginReq.getType() == 1 ? user.getLoginPhone() : user.getEmail())).toHexString().substring(0, 6)));
  208. //默认头像
  209. user.setHeadimgurl(Constant.DEFAULT_HEAD_IMG);
  210. String registerEnv = loginReq.getRegisterEnv();
  211. user.setRegisterEnv(StrUtil.isEmpty(registerEnv) ? User.RegisterEnv.pc.name() : registerEnv);
  212. if (loginReq.getType() == 3) {
  213. user.setEmail(loginReq.getEmail());
  214. }
  215. this.save(user);
  216. //缓存用户注册 回传信息
  217. String callback = loginReq.getCallback();
  218. String callbackType = loginReq.getCallbackType();
  219. if (StrUtil.isNotBlank(callback)) {
  220. OrderDonPostDataDto orderDonPostDataDto = new OrderDonPostDataDto().setCallback(callback)
  221. .setCallbackType(callbackType);
  222. redisService.set(RedisService.key.ORDER_DON_POST_DATA_USER_KEY.getName() + user.getId(), Jsons.toJson(orderDonPostDataDto));
  223. }
  224. final String login_email = user.getEmail();
  225. final String login_phone = user.getLoginPhone();
  226. UserBindDetail bindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  227. .and(ub -> ub.eq(StrUtil.isNotBlank(login_email), UserBindDetail::getEmail, login_email)
  228. .or().eq(StrUtil.isNotBlank(login_phone), UserBindDetail::getPhone, login_phone))
  229. .last("limit 1"));
  230. if (bindDetail != null) {
  231. if (StrUtil.isNotBlank(login_phone)) {
  232. bindDetail.setPhoneUserId(user.getId());
  233. }
  234. if (StrUtil.isNotBlank(login_email)) {
  235. bindDetail.setEmailUserId(user.getId());
  236. }
  237. userBindDetailMapper.updateById(bindDetail);
  238. }
  239. }
  240. if (StrUtil.isEmpty(user.getShowId())) {
  241. user.setShowId(getUserShowId(user.getId()));
  242. }
  243. Optional.ofNullable(user.getIsBlack())
  244. .ifPresent(black -> {
  245. if (black) {
  246. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("user_black_error"));
  247. }
  248. });
  249. return user;
  250. }
  251. @Override
  252. public UserSharedDto getUserShredDtoByDsCode(String dsCode) {
  253. Long sharedId = 0l;
  254. Long dsPopularizeId = 0l;
  255. dsCode = dsCode.trim();
  256. User sharedUser = getOne(Wrappers.lambdaQuery(User.class)
  257. .eq(User::getShowId, dsCode).last("limit 1"));
  258. if (sharedUser != null) {
  259. sharedId = sharedUser.getId();
  260. }
  261. UserSharedDto sharedDto = new UserSharedDto();
  262. sharedDto.setSharedId(sharedId);
  263. sharedDto.setDsPopularizeId(dsPopularizeId);
  264. return sharedDto;
  265. }
  266. @Override
  267. public Long getSharedIdByUserId(Long userId) {
  268. UserDistributeShared distributeShared = userDistributeSharedMapper.selectOne(Wrappers.lambdaQuery(UserDistributeShared.class)
  269. .eq(UserDistributeShared::getUserId, userId)
  270. .last("limit 1")
  271. .select(UserDistributeShared::getSharedId));
  272. if (distributeShared == null) {
  273. return 0l;
  274. }
  275. return distributeShared.getSharedId();
  276. }
  277. @Override
  278. public void bindUserOrder(OrderBindCommonReq orderBindCommonReq) {
  279. String orderNo = orderBindCommonReq.getOrderNo();
  280. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  281. .eq(OrderDon::getOrderNo, orderNo)
  282. .eq(OrderDon::getUserId, 0)
  283. .last("limit 1"));
  284. if (orderDon == null) {
  285. return;
  286. }
  287. orderDon.setUserId(orderBindCommonReq.getUserId());
  288. GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
  289. .eq(GroupsRelation::getId, orderDon.getRelationId())
  290. .eq(GroupsRelation::getStatus, GroupsRelation.Status.validity)
  291. .last("limit 1"));
  292. //免登录兑换流程未成功 继续兑换
  293. if (relation == null) {
  294. String exCode = orderDon.getExCode();
  295. ExchangeCodeTicket exchangeCodeTicket = new ExchangeCodeTicket();
  296. exchangeCodeTicket.setCode(exCode);
  297. exchangeCodeService.getTicketByExchangeCode(exchangeCodeTicket, orderBindCommonReq.getUserId());
  298. orderDonMapper.updateById(orderDon);
  299. }
  300. }
  301. }