WxUserServiceImpl.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.fdkankan.tk.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.fdkankan.tk.common.ResultCode;
  6. import com.fdkankan.tk.common.util.JwtUtil;
  7. import com.fdkankan.tk.common.util.RoomUtil;
  8. import com.fdkankan.tk.entity.WxConfig;
  9. import com.fdkankan.tk.entity.WxUser;
  10. import com.fdkankan.tk.exception.BusinessException;
  11. import com.fdkankan.tk.httpClient.client.WxClient;
  12. import com.fdkankan.tk.httpClient.request.WxGetPhoneParam;
  13. import com.fdkankan.tk.httpClient.response.WxOpenIdVo;
  14. import com.fdkankan.tk.mapper.IWxUserMapper;
  15. import com.fdkankan.tk.response.UserMsgVo;
  16. import com.fdkankan.tk.response.WxUserVo;
  17. import com.fdkankan.tk.service.IWxConfigService;
  18. import com.fdkankan.tk.service.IWxService;
  19. import com.fdkankan.tk.service.IWxUserService;
  20. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  21. import com.fdkankan.tk.util.MD5Utils;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.springframework.beans.BeanUtils;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Service;
  27. import javax.annotation.Resource;
  28. import java.security.spec.ECField;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. /**
  32. * <p>
  33. * 服务实现类
  34. * </p>
  35. *
  36. * @author
  37. * @since 2022-12-19
  38. */
  39. @Service
  40. @Slf4j
  41. public class WxUserServiceImpl extends ServiceImpl<IWxUserMapper, WxUser> implements IWxUserService {
  42. @Resource
  43. WxClient wxClient;
  44. @Autowired
  45. IWxService wxService;
  46. @Autowired
  47. IWxConfigService wxConfigService;
  48. @Override
  49. public Object wxLogin(String code) {
  50. WxConfig wxConfig = wxConfigService.getWxConfig();
  51. if(wxConfig == null){
  52. throw new BusinessException(ResultCode.WX_CONFIG_ERROR);
  53. }
  54. WxOpenIdVo wxOpenIdVo = wxClient.getOpenIdUrl(wxConfig.getAppId(),wxConfig.getAppSecret(), code);
  55. if(wxOpenIdVo.getErrcode() == null && StringUtils.isNotBlank(wxOpenIdVo.getOpenId())){
  56. WxUser wxUser = this.addUser(wxOpenIdVo);
  57. WxUserVo vo = new WxUserVo();
  58. BeanUtils.copyProperties(wxUser,vo);
  59. String token = JwtUtil.createJWT(-1, wxUser.getWxUserId(), "wx");
  60. vo.setToken(token);
  61. return vo;
  62. }
  63. throw new BusinessException(ResultCode.WX_LOGIN_ERROR);
  64. }
  65. @Override
  66. public WxUser addUser(WxOpenIdVo wxOpenIdVo) {
  67. WxUser wxUser = this.getByOpenId(wxOpenIdVo.getOpenId());
  68. if(wxUser != null){
  69. return wxUser;
  70. }
  71. wxUser = new WxUser();
  72. wxUser.setWxUserId(MD5Utils.getPWD(RoomUtil.ev + wxOpenIdVo.getOpenId()));
  73. wxUser.setOpenid(wxOpenIdVo.getOpenId());
  74. wxUser.setUnionid(wxOpenIdVo.getUnionid());
  75. wxUser.setSessionKey(wxOpenIdVo.getSession_key());
  76. this.save(wxUser);
  77. return wxUser;
  78. }
  79. @Override
  80. public WxUser getByOpenId(String openId) {
  81. try {
  82. LambdaQueryWrapper<WxUser> wrapper = new LambdaQueryWrapper<>();
  83. wrapper.eq(WxUser::getOpenid,openId);
  84. return this.getOne(wrapper);
  85. }catch (Exception e){
  86. e.printStackTrace();
  87. }
  88. throw new BusinessException(ResultCode.WX_OPENID_ERROr);
  89. }
  90. @Override
  91. public void updateByVo(WxUserVo param) {
  92. if(StringUtils.isBlank(param.getWxUserId())){
  93. throw new BusinessException(ResultCode.PARAM_MISS);
  94. }
  95. WxUser wxUser = new WxUser();
  96. BeanUtils.copyProperties(param,wxUser);
  97. wxUser.setUpdateTime(new Date());
  98. log.info("wxUser:{}",wxUser);
  99. if(StringUtils.isBlank(wxUser.getPhoneNumber())){
  100. wxUser.setPhoneNumber(null);
  101. }
  102. this.updateById(wxUser);
  103. }
  104. @Override
  105. public Object getPhone(String code,Integer reCount) {
  106. try {
  107. String token = wxService.getToken();
  108. if(StringUtils.isBlank(token)){
  109. throw new BusinessException(ResultCode.WX_PHONE_ERROR);
  110. }
  111. JSONObject jsonObject = wxClient.getPhone(token, new WxGetPhoneParam(code));
  112. if(jsonObject.getInteger("errcode") != null && jsonObject.getInteger("errcode") == 40001 && reCount < 2){
  113. log.info("wx-getPhone-error:{},当前重试次数:{}",code,reCount);
  114. reCount ++;
  115. wxService.delToken();
  116. return getPhone(code,reCount);
  117. }
  118. JSONObject phoneObj = (JSONObject) jsonObject.get("phone_info");
  119. if(phoneObj==null){
  120. throw new BusinessException(ResultCode.WX_PHONE_ERROR);
  121. }
  122. //return phoneObj.getString("phoneNumber");
  123. return phoneObj;
  124. } catch (Exception e){
  125. e.printStackTrace();
  126. }
  127. throw new BusinessException(ResultCode.WX_PHONE_ERROR);
  128. }
  129. @Override
  130. public Object userInfo(String token) {
  131. if(StringUtils.isBlank(token)){
  132. throw new BusinessException(ResultCode.USER_NOT_LOGIN);
  133. }
  134. String wxUserId = JwtUtil.getUserName(token);
  135. if(StringUtils.isBlank(wxUserId)){
  136. throw new BusinessException(ResultCode.USER_NOT_LOGIN);
  137. }
  138. WxUser wxUser = this.getById(wxUserId);
  139. if(wxUser == null){
  140. return null;
  141. }
  142. WxUserVo vo = new WxUserVo();
  143. BeanUtils.copyProperties(wxUser,vo);
  144. return vo;
  145. }
  146. }