UserServiceImpl.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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.task.GlobalThreadPoolTaskExecutor;
  13. import com.cyksj.common.util.Codec;
  14. import com.cyksj.common.util.Jsons;
  15. import com.cyksj.common.util.StringUtil;
  16. import com.cyksj.config.wxlogin.WxOpenPlatYHLXLoginConfig;
  17. import com.cyksj.mapper.PhoneCodeMapper;
  18. import com.cyksj.mapper.UserMapper;
  19. import com.cyksj.mapper.channel.ChannelPopularizeMapper;
  20. import com.cyksj.mapper.manage.cps.UserDistributePopularizeMapper;
  21. import com.cyksj.mapper.market.task.UserBindDetailMapper;
  22. import com.cyksj.model.dto.*;
  23. import com.cyksj.model.entity.*;
  24. import com.cyksj.model.request.LoginReq;
  25. import com.cyksj.redis.RedisService;
  26. import com.cyksj.service.distribute.DistributeService;
  27. import com.cyksj.service.distribute.equipment.EquipmentDistributeService;
  28. import com.cyksj.service.mange.coupon.CouponCommonService;
  29. import com.cyksj.service.market.task.BindTaskService;
  30. import com.cyksj.service.shop.YhShopDistributeService;
  31. import com.cyksj.service.sys.SysConfigService;
  32. import com.cyksj.service.user.UserService;
  33. import com.cyksj.service.wechat.WeChatService;
  34. import lombok.RequiredArgsConstructor;
  35. import lombok.extern.slf4j.Slf4j;
  36. import org.springframework.dao.DuplicateKeyException;
  37. import org.springframework.stereotype.Service;
  38. import org.springframework.util.Assert;
  39. import java.util.ArrayList;
  40. import java.util.Date;
  41. import java.util.List;
  42. import java.util.Optional;
  43. /**
  44. * @author chan
  45. * @date 2021/10/10 下午3:27
  46. */
  47. @Slf4j
  48. @Service
  49. @RequiredArgsConstructor
  50. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  51. private final DistributeService distributeService;
  52. private final EquipmentDistributeService equipDistributeService;
  53. private final ChannelPopularizeMapper channelPopularizeMapper;
  54. private final CouponCommonService couponCommonService;
  55. private final RedisService redisService;
  56. private final SysConfigService sysConfigService;
  57. private final UserBindDetailMapper userBindDetailMapper;
  58. private final UserDistributePopularizeMapper distributePopularizeMapper;
  59. private final UserMapper userMapper;
  60. private final PhoneCodeMapper phoneCodeMapper;
  61. private final WeChatService weChatService;
  62. private final BindTaskService bindTaskService;
  63. private final WxOpenPlatYHLXLoginConfig wxOpenPlatYHLXLoginConfig;
  64. private final YhShopDistributeService yhShopDistributeService;
  65. private static final GlobalThreadPoolTaskExecutor TASK_POOL = GlobalThreadPoolTaskExecutor.getInstance();
  66. @Override
  67. public User saveAuth(GzhOAuth2UserInfo info, Long sharedId, Integer dsType, Long pid, User.RegisterEnv registerEnv, String dsCode, String callback, String callbackType, String customId) throws Exception {
  68. final String unionid = info.getUnionid();
  69. if (StrUtil.isEmpty(unionid)) {
  70. throw BusinessRuntimeException.getInstance("授权操作异常,请刷新页面");
  71. }
  72. User user = getSyncUser(info.getOpenid(), info.getUnionid(), null);
  73. user = Optional.ofNullable(user).orElseGet(() -> new User()
  74. .setUnionid(unionid));
  75. user.setCountry(info.getCountry())
  76. .setProvince(info.getProvince())
  77. .setCity(info.getCity());
  78. if (user.getSex() == null) {
  79. user.setSex(info.getSex());
  80. }
  81. if (StrUtil.isEmpty(user.getHeadimgurl())) {
  82. user.setHeadimgurl(info.getHeadimgurl());
  83. }
  84. if (StrUtil.isEmpty(user.getNickname())) {
  85. user.setNickname(info.getNickname());
  86. }
  87. user.setUnionid(unionid);
  88. user.setOpenId(info.getOpenid());
  89. Boolean isNewUser = false;
  90. if (user.getId() == null) {
  91. isNewUser = true;
  92. user.setPopularizeId(pid);
  93. user.setRegisterEnv(registerEnv.name());
  94. if (!redisService.setNx(RedisService.key.REPEAT_USER_KEY.getNameFormat(user.getUnionid()), user.getUnionid(), RedisService.key.REPEAT_USER_KEY.getTimeout())) {
  95. return this.getOne(Wrappers.lambdaQuery(User.class)
  96. .eq(User::getUnionid, unionid).last("limit 1"));
  97. }
  98. this.save(user);
  99. //缓存用户注册 回传信息
  100. if (StrUtil.isNotBlank(callback)) {
  101. OrderDonPostDataDto orderDonPostDataDto = new OrderDonPostDataDto().setCallback(callback)
  102. .setCallbackType(callbackType);
  103. redisService.set(RedisService.key.ORDER_DON_POST_DATA_USER_KEY.getName() + user.getId(), Jsons.toJson(orderDonPostDataDto));
  104. }
  105. //发放新用户福利
  106. final Long fUid = user.getId();
  107. TASK_POOL.execute(() -> {
  108. try {
  109. sendNewUserWelfare(fUid);
  110. } catch (Exception e) {
  111. }
  112. });
  113. } else {
  114. if (StrUtil.isEmpty(user.getRegisterEnv())) {
  115. user.setRegisterEnv(registerEnv.name());
  116. }
  117. this.updateById(user);
  118. }
  119. Long dsPopularizeId = null;
  120. if (StrUtil.isNotBlank(dsCode)) {
  121. dsCode = dsCode.trim();
  122. User sharedUser = getOne(Wrappers.lambdaQuery(User.class)
  123. .eq(User::getShowId, dsCode).last("limit 1"));
  124. if (sharedUser != null) {
  125. sharedId = sharedUser.getId();
  126. } else {
  127. UserDistributePopularize distributePopularize = distributePopularizeMapper.selectOne(Wrappers.lambdaQuery(UserDistributePopularize.class)
  128. .eq(UserDistributePopularize::getCode, dsCode)
  129. .select(UserDistributePopularize::getUserId, UserDistributePopularize::getId).last("limit 1"));
  130. if (distributePopularize != null) {
  131. log.info("渠道sharedId:{}设置固定推广链接用户分销", distributePopularize.getUserId());
  132. sharedId = distributePopularize.getUserId();
  133. dsPopularizeId = distributePopularize.getId();
  134. }
  135. }
  136. }
  137. if (sharedId != null && isNewUser) {
  138. if (dsType == null || dsType == 1) {
  139. distributeService.saveDistributionInvitation(sharedId, user.getId(), dsPopularizeId);
  140. }
  141. }
  142. //店铺注册用户 关联
  143. if (isNewUser && StrUtil.isNotBlank(customId)) {
  144. yhShopDistributeService.saveDistributionInvitation(customId, user.getId());
  145. }
  146. Long userId = user.getId();
  147. if (isNewUser && pid != null) {
  148. TASK_POOL.execute(() -> {
  149. sendFixedCustomerCoupon(pid, userId, unionid);
  150. });
  151. }
  152. //新旧用户都可实物分销
  153. if (sharedId != null && dsType != null && dsType == 2) {
  154. equipDistributeService.saveDistributionInvitation(sharedId, isNewUser, user.getId());
  155. }
  156. return user;
  157. }
  158. @Override
  159. public UserWhoami whoami(long userId) {
  160. User user = this.getById(userId);
  161. Assert.notNull(user, "业务处理失败,请联系客服");
  162. UserWhoami userWhoami = new UserWhoami();
  163. if (StrUtil.isEmpty(user.getShowId())) {
  164. user.setShowId(getUserShowId(userId));
  165. }
  166. BeanUtil.copyProperties(user, userWhoami);
  167. if (StrUtil.isEmpty(user.getLoginPhone())) {
  168. UserBindDetail bindPhone = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  169. .eq(StrUtil.isNotEmpty(user.getOpenId()), UserBindDetail::getUserId, userId)
  170. .eq(StrUtil.isNotEmpty(user.getEmail()), UserBindDetail::getEmailUserId, userId).last("limit 1"));
  171. //手机号
  172. if (bindPhone != null) {
  173. userWhoami.setBindPhone(bindPhone.getPhone());
  174. userWhoami.setBindEmail(bindPhone.getEmail());
  175. }
  176. }
  177. Integer type = 3;
  178. if (StrUtil.isNotBlank(user.getLoginPhone())) {
  179. type = 1;
  180. } else if (StrUtil.isNotBlank(user.getEmail())) {
  181. type = 2;
  182. }
  183. userWhoami.setType(type);
  184. return userWhoami;
  185. }
  186. @Override
  187. public User getSyncUser(String openId, String unionId, WxSyncLog wxSyncLog) {
  188. User user;
  189. //迁移公众号之后同步授权后信息
  190. //是否是迁移用户
  191. if (wxSyncLog != null) {
  192. user = this.getOne(Wrappers.lambdaQuery(User.class)
  193. .ne(User::getUnionid, unionId)
  194. .eq(User::getOpenId, wxSyncLog.getOriOpenid()).last("limit 1"));
  195. if (user != null) {
  196. user.setOpenId(wxSyncLog.getNewOpenid());
  197. } else {
  198. // 查询用户是否存在
  199. user = Optional.ofNullable(
  200. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  201. .orderByAsc(User::getId)
  202. .last("limit 1")))
  203. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  204. .orderByAsc(User::getId)
  205. .last("limit 1")));
  206. }
  207. } else {
  208. // 查询用户是否存在
  209. user = Optional.ofNullable(
  210. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  211. .orderByAsc(User::getId)
  212. .last("limit 1")))
  213. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  214. .orderByAsc(User::getId)
  215. .last("limit 1")));
  216. }
  217. return user;
  218. }
  219. @Override
  220. public void sendFixedCustomerCoupon(Long popularizeId, Long userId, String unionId) {
  221. //若是百度渠道 添加固定客服 注册后直接发放优惠券
  222. ChannelPopularize channelPopularize = channelPopularizeMapper.selectById(popularizeId);
  223. if (channelPopularize != null && channelPopularize.getChannelId() == 34) {
  224. // SysConfig corpConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  225. // .eq(SysConfig::getSysKey, Constant.CORP_FIXED_CUSTOMER)
  226. // .last("limit 1"));
  227. // if (corpConfig != null) {
  228. // Optional.ofNullable(corpConfig.getSysValue()).ifPresent(value -> {
  229. // try {
  230. // if (StrUtil.isNotBlank(value)) {
  231. // List<String> followIds = Jsons.parseList(value, String.class);
  232. // //添加固定客服发放优惠券
  233. // CorpUserFollowView corpUserFollowView = beanSearcher.searchFirst(CorpUserFollowView.class, MapUtils.builder()
  234. // .field(CorpUserFollowView::getUnionId, unionId)
  235. // .field(CorpUserFollowView::getFollowId, followIds).op(Operator.InList).build());
  236. // if (corpUserFollowView != null) {
  237. // Constant.FIXED_CUSTOMER_COUPON_IDS.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, 0l, CouponUser.Channel.wxCorp));
  238. // }
  239. // }
  240. // } catch (Exception e) {
  241. // }
  242. // });
  243. //
  244. // }
  245. SysConfig config = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  246. .eq(SysConfig::getSysKey, Constant.BAIDU_POPULARIZE_COUPON_IDS).last("limit 1"));
  247. if (config != null) {
  248. String sysValue = config.getSysValue();
  249. if (StrUtil.isNotBlank(sysValue)) {
  250. try {
  251. List<Long> couponIds = Jsons.parseList(sysValue, Long.class);
  252. couponIds.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, 0l, 0l, popularizeId, CouponUser.Channel.manual));
  253. } catch (Exception e) {
  254. log.error("百度渠道优惠券解析错误");
  255. }
  256. }
  257. }
  258. }
  259. }
  260. @Override
  261. public List<Long> getRelationUserIdList(Long userId, User selectUser) {
  262. List<Long> userIds = new ArrayList<>();
  263. userIds.add(userId);
  264. UserBindDetail userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  265. .eq(UserBindDetail::getUserId, userId).last("limit 1"));
  266. if (userBindDetail == null) {
  267. User user = Optional.ofNullable(selectUser).orElse(this.getById(userId));
  268. if (StrUtil.isEmpty(user.getOpenId()) && StrUtil.isNotEmpty(user.getLoginPhone())) {
  269. userBindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  270. .eq(UserBindDetail::getPhone, user.getLoginPhone()).last("limit 1"));
  271. if (userBindDetail != null) {
  272. userIds.add(userBindDetail.getUserId());
  273. }
  274. }
  275. } else {
  276. if (StrUtil.isNotBlank(userBindDetail.getPhone())) {
  277. User otherUser = this.getOne(Wrappers.lambdaQuery(User.class)
  278. .eq(User::getLoginPhone, userBindDetail.getPhone()).last("limit 1"));
  279. if (otherUser != null) {
  280. userIds.add(otherUser.getId());
  281. }
  282. }
  283. }
  284. return userIds;
  285. }
  286. @Override
  287. public List<Long> getUserIdsByNickname(String nickname) {
  288. return userMapper.selectListByNickname(nickname);
  289. }
  290. @Override
  291. public String getUserShowId(Long userId) {
  292. Integer update = 0;
  293. String showId = null;
  294. while (update != 1) {
  295. showId = StringUtil.randomString(5);
  296. try {
  297. update = userMapper.update(null, Wrappers.lambdaUpdate(User.class)
  298. .set(User::getShowId, showId)
  299. .eq(User::getId, userId));
  300. } catch (DuplicateKeyException e) {
  301. update = 0;
  302. }
  303. }
  304. return showId;
  305. }
  306. @Override
  307. public Long getUserIdByShowId(String showId) {
  308. return userMapper.getUserIdByShowId(showId);
  309. }
  310. @Override
  311. public User webLogin(LoginReq loginReq) throws Exception {
  312. LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class).last("limit 1");
  313. if (loginReq.getType() == null || loginReq.getType() < 1 && loginReq.getType() > 2) {
  314. throw BusinessRuntimeException.getInstance("系统异常,请刷新页面重新操作");
  315. }
  316. if (loginReq.getType() == 1) {
  317. if (StrUtil.hasEmpty(loginReq.getPhone(), loginReq.getCode())) {
  318. throw BusinessRuntimeException.getInstance("请输入对应信息");
  319. }
  320. if (!Constant.LOGIN_CODE.equals(loginReq.getCode())) {
  321. DateTime now = DateTime.now();
  322. PhoneCode phoneCode = phoneCodeMapper.selectOne(Wrappers.lambdaQuery(PhoneCode.class).eq(PhoneCode::getPhone, loginReq.getPhone()).last("limit 1"));
  323. if (phoneCode == null || StrUtil.isEmpty(phoneCode.getCode())) {
  324. throw BusinessRuntimeException.getInstance("请重新获取手机验证码");
  325. }
  326. //3分钟失效
  327. Date updateTime = phoneCode.getUpdateTime();
  328. DateTime afterFiveMinutes = DateUtil.offsetMinute(updateTime, 3);
  329. if (now.isAfter(afterFiveMinutes)) {
  330. throw BusinessRuntimeException.getInstance("手机验证码已失效,请重新获取");
  331. }
  332. if (!StrUtil.equals(loginReq.getCode(), phoneCode.getCode())) {
  333. throw BusinessRuntimeException.getInstance("手机验证码错误,请重新输入");
  334. }
  335. }
  336. wrapper.eq(User::getLoginPhone, loginReq.getPhone());
  337. }
  338. if (loginReq.getType() == 2) {
  339. if (StrUtil.isEmpty(loginReq.getEmail())) {
  340. throw BusinessRuntimeException.getInstance("邮箱不能为空");
  341. }
  342. if (!Constant.LOGIN_CODE.equals(loginReq.getCode())) {
  343. String code = redisService.getStr(RedisService.key.EMAIL_CODE_KEY.getNameFormat(loginReq.getEmail()));
  344. if (StrUtil.isEmpty(code)) {
  345. throw BusinessRuntimeException.getInstance("邮箱验证码已失效,请重新获取");
  346. }
  347. if (!StrUtil.equals(loginReq.getCode(), code)) {
  348. throw BusinessRuntimeException.getInstance("邮箱验证码错误,请重新输入");
  349. }
  350. redisService.del(RedisService.key.EMAIL_CODE_KEY.getNameFormat(loginReq.getEmail()));
  351. }
  352. wrapper.eq(User::getEmail, loginReq.getEmail());
  353. }
  354. if (loginReq.getType() == 3){
  355. wrapper.eq(User::getEmail, loginReq.getEmail());
  356. }
  357. //保存用户信息
  358. User user = this.getOne(wrapper);
  359. String dsCode = loginReq.getDsCode();
  360. Long sharedId = loginReq.getSharedId();
  361. Long dsPopularizeId = null;
  362. Integer dsType = loginReq.getDsType();
  363. String customId = loginReq.getCustomId();
  364. Boolean isNewUser = false;
  365. if (StrUtil.isNotBlank(dsCode)) {
  366. dsCode = dsCode.trim();
  367. User sharedUser = this.getOne(Wrappers.lambdaQuery(User.class)
  368. .eq(User::getShowId, dsCode).last("limit 1"));
  369. if (sharedUser != null) {
  370. sharedId = sharedUser.getId();
  371. } else {
  372. UserDistributePopularize distributePopularize = distributePopularizeMapper.selectOne(Wrappers.lambdaQuery(UserDistributePopularize.class)
  373. .eq(UserDistributePopularize::getCode, dsCode)
  374. .select(UserDistributePopularize::getUserId, UserDistributePopularize::getId).last("limit 1"));
  375. if (distributePopularize != null) {
  376. log.info("渠道sharedId:{}设置固定推广链接PC用户分销", distributePopularize.getUserId());
  377. sharedId = distributePopularize.getUserId();
  378. dsPopularizeId = distributePopularize.getId();
  379. }
  380. }
  381. }
  382. if (user != null && loginReq.getType() == 3 && !user.getIsGoogle()) {
  383. user.setIsGoogle(true);
  384. this.updateById(user);
  385. }
  386. if (user == null) {
  387. user = new User();
  388. isNewUser = true;
  389. StringBuilder name = new StringBuilder();
  390. name.append(Constant.DEFAULT_NAME);
  391. if (loginReq.getPopularizeId() != null) {
  392. user.setPopularizeId(loginReq.getPopularizeId());
  393. }
  394. if (loginReq.getType() == 1) {
  395. user.setLoginPhone(loginReq.getPhone());
  396. }
  397. if (loginReq.getType() == 2 || loginReq.getType() == 3) {
  398. user.setEmail(loginReq.getEmail());
  399. name.append("@");
  400. }
  401. if (loginReq.getType() == 3) {
  402. user.setIsGoogle(true);
  403. }
  404. user.setNickname(String.format("%s%s", name.toString(), RandomUtil.randomString(6)));
  405. //默认头像
  406. user.setHeadimgurl(Constant.DEFAULT_HEAD_IMG);
  407. String registerEnv = loginReq.getRegisterEnv();
  408. user.setRegisterEnv(StrUtil.isEmpty(registerEnv) ? User.RegisterEnv.pc.name() : registerEnv);
  409. this.save(user);
  410. //新用户分销
  411. if (sharedId != null) {
  412. distributeService.saveDistributionInvitation(sharedId, user.getId(), dsPopularizeId);
  413. }
  414. //店铺注册用户 关联
  415. if (StrUtil.isNotBlank(customId)) {
  416. yhShopDistributeService.saveDistributionInvitation(customId, user.getId());
  417. }
  418. //缓存用户注册 回传信息
  419. String callback = loginReq.getCallback();
  420. String callbackType = loginReq.getCallbackType();
  421. if (StrUtil.isNotBlank(callback)) {
  422. OrderDonPostDataDto orderDonPostDataDto = new OrderDonPostDataDto().setCallback(callback)
  423. .setCallbackType(callbackType);
  424. redisService.set(RedisService.key.ORDER_DON_POST_DATA_USER_KEY.getName() + user.getId(), Jsons.toJson(orderDonPostDataDto));
  425. }
  426. //发放新用户福利
  427. final Long fUid = user.getId();
  428. TASK_POOL.execute(() -> {
  429. try {
  430. sendNewUserWelfare(fUid);
  431. } catch (Exception e) {
  432. }
  433. });
  434. final String login_email = user.getEmail();
  435. final String login_phone = user.getLoginPhone();
  436. UserBindDetail bindDetail = userBindDetailMapper.selectOne(Wrappers.lambdaQuery(UserBindDetail.class)
  437. .and(ub -> ub.eq(StrUtil.isNotBlank(login_email), UserBindDetail::getEmail, login_email)
  438. .or().eq(StrUtil.isNotBlank(login_phone), UserBindDetail::getPhone, login_phone))
  439. .last("limit 1"));
  440. if (bindDetail != null) {
  441. if (StrUtil.isNotBlank(login_phone)) {
  442. bindDetail.setPhoneUserId(user.getId());
  443. }
  444. if (StrUtil.isNotBlank(login_email)) {
  445. bindDetail.setEmailUserId(user.getId());
  446. }
  447. userBindDetailMapper.updateById(bindDetail);
  448. }
  449. }
  450. if (StrUtil.isEmpty(user.getShowId())) {
  451. user.setShowId(getUserShowId(user.getId()));
  452. }
  453. //新旧用户都可实物分销
  454. if (sharedId != null && dsType != null && dsType == 2) {
  455. equipDistributeService.saveDistributionInvitation(sharedId, isNewUser, user.getId());
  456. }
  457. return user;
  458. }
  459. @Override
  460. public String bindWxAccountRedirect(String code, String state) throws Exception {
  461. AuthToken token = weChatService.getAuthToken(code, wxOpenPlatYHLXLoginConfig.getAppId());
  462. //重定向
  463. // 根据openid 与 access_token 获取用户信息
  464. GzhOAuth2UserInfo userinfo = weChatService.getUserInfo(token.getAccessToken(), token.getOpenid());
  465. // 转跳参数
  466. String json = Codec.DoBase64.custom().setData(state).decodeAsText();
  467. AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
  468. //保存用户信息
  469. User user = saveAuth(userinfo, re.getSharedId(), re.getDsType(), re.getPid(), User.RegisterEnv.pc, re.getDsCode(), re.getCk(), re.getCt(), re.getCd());
  470. if (user == null) {
  471. throw BusinessRuntimeException.getInstance("系统错误,请重新授权");
  472. }
  473. String loginPhone = re.getLp();
  474. String loginEmail = re.getLe();
  475. if (StrUtil.isNotBlank(loginPhone)) {
  476. log.info("手机号phone:{}登录用户,微信扫描网页授权二维码进行用户信息绑定", loginPhone);
  477. try {
  478. bindTaskService.bindPhone(user, loginPhone, null);
  479. } catch (Exception e) {
  480. log.info(StringUtil.getErrorText(e));
  481. }
  482. }
  483. if (StrUtil.isNotBlank(loginEmail)) {
  484. log.info("邮箱email:{}登录用户,微信扫描网页授权二维码进行用户信息绑定", loginEmail);
  485. try {
  486. bindTaskService.bindEmail(user, loginEmail, null);
  487. } catch (Exception e) {
  488. log.info(StringUtil.getErrorText(e));
  489. }
  490. }
  491. return redisService.getStr(re.getMappingId().toString());
  492. }
  493. /**
  494. * 发放新用户福利
  495. */
  496. public void sendNewUserWelfare(Long userId) throws Exception {
  497. SysConfig newUserWelfareConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  498. .eq(SysConfig::getSysKey, Constant.NEW_USER_WELFARE_KEY).last("limit 1"));
  499. if (StrUtil.isEmpty(newUserWelfareConfig.getSysValue())) {
  500. return;
  501. }
  502. NewUserWelfareDto newUserWelfareDto = Jsons.parseObject(newUserWelfareConfig.getSysValue(), NewUserWelfareDto.class);
  503. if (newUserWelfareDto != null) {
  504. List<Long> couponIds = newUserWelfareDto.getCouponIds();
  505. couponIds.forEach(couponId -> couponCommonService.sendCouponToUser(userId, couponId, null, null, null, CouponUser.Channel.new_welfare));
  506. }
  507. }
  508. }