|
@@ -2,24 +2,23 @@ package com.xy.third.service;
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
import cn.binarywang.wx.miniapp.api.WxMaUserService;
|
|
import cn.binarywang.wx.miniapp.api.WxMaUserService;
|
|
-import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
|
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
|
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
|
|
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
|
|
-import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
|
|
|
-import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
|
|
|
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
|
|
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
-import com.xy.collections.map.JHashMap;
|
|
|
|
import com.xy.dto.ThirdLoginDto;
|
|
import com.xy.dto.ThirdLoginDto;
|
|
import com.xy.dto.WxMaDto;
|
|
import com.xy.dto.WxMaDto;
|
|
|
|
+import com.xy.error.CommRuntimeException;
|
|
import com.xy.service.ThirdLoginService;
|
|
import com.xy.service.ThirdLoginService;
|
|
-import com.xy.third.config.ThirdLoginConfig;
|
|
|
|
|
|
+import com.xy.utils.FunctionUtils;
|
|
import com.xy.utils.R;
|
|
import com.xy.utils.R;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
|
@@ -30,7 +29,7 @@ import static com.xy.utils.Beans.copy;
|
|
@Api(tags = "第三方登录")
|
|
@Api(tags = "第三方登录")
|
|
public class ThirdLoginServiceImpl implements ThirdLoginService {
|
|
public class ThirdLoginServiceImpl implements ThirdLoginService {
|
|
|
|
|
|
- private ThirdLoginConfig thirdLoginConfig;
|
|
|
|
|
|
+ private WxMaService wxMaService;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@ApiOperation("登录")
|
|
@ApiOperation("登录")
|
|
@@ -42,30 +41,36 @@ public class ThirdLoginServiceImpl implements ThirdLoginService {
|
|
@Override
|
|
@Override
|
|
@ApiOperation("获取用户信息-微信小程序")
|
|
@ApiOperation("获取用户信息-微信小程序")
|
|
public R<WxMaDto.Vo> getUserInfoWxMa(@RequestBody @Validated WxMaDto.GetUserInfo getUserInfo) {
|
|
public R<WxMaDto.Vo> getUserInfoWxMa(@RequestBody @Validated WxMaDto.GetUserInfo getUserInfo) {
|
|
- ThirdLoginConfig.Wx wx = thirdLoginConfig.getWx();
|
|
|
|
- WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
|
|
|
|
- config.setAppid(wx.getAppid());
|
|
|
|
- config.setSecret(wx.getSecret());
|
|
|
|
- WxMaService wxMaService = new WxMaServiceImpl();
|
|
|
|
- wxMaService.setMultiConfigs(new JHashMap<String, WxMaConfig>().set("config", config));
|
|
|
|
- try {
|
|
|
|
- WxMaUserService userService = wxMaService.getUserService();
|
|
|
|
- //获取session
|
|
|
|
- WxMaJscode2SessionResult session = userService.getSessionInfo(getUserInfo.getCode());
|
|
|
|
- String sessionKey = session.getSessionKey();
|
|
|
|
- //获取用户信息
|
|
|
|
- WxMaUserInfo userInfo = userService.getUserInfo(sessionKey, getUserInfo.getEncryptedData(), getUserInfo.getIv());
|
|
|
|
- //获取手机信息
|
|
|
|
|
|
+ WxMaDto.Vo vo = new WxMaDto.Vo();
|
|
|
|
+ WxMaUserService userService = wxMaService.getUserService();
|
|
|
|
+ //获取用户信息
|
|
|
|
+ FunctionUtils.NoParamsNoResult getInfo = () -> {
|
|
|
|
+ String code = getUserInfo.getCode();
|
|
|
|
+ if (StringUtils.isEmpty(code)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ WxMaJscode2SessionResult session;
|
|
|
|
+ try {
|
|
|
|
+ session = userService.getSessionInfo(code);
|
|
|
|
+ } catch (WxErrorException e) {
|
|
|
|
+ throw new CommRuntimeException("登录失败");
|
|
|
|
+ }
|
|
|
|
+ vo.setOpenId(session.getOpenid());
|
|
|
|
+ WxMaUserInfo userInfo = userService.getUserInfo(session.getSessionKey(), getUserInfo.getEncryptedData(), getUserInfo.getIv());
|
|
|
|
+ copy(vo, userInfo);
|
|
|
|
+ };
|
|
|
|
+ //获取手机信息
|
|
|
|
+ FunctionUtils.NoParamsNoResult getTel = () -> {
|
|
|
|
+ String sessionKey = getUserInfo.getSessionKey();
|
|
|
|
+ if (StringUtils.isEmpty(sessionKey)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
WxMaPhoneNumberInfo phoneNoInfo = userService.getPhoneNoInfo(sessionKey, getUserInfo.getEncryptedData(), getUserInfo.getIv());
|
|
WxMaPhoneNumberInfo phoneNoInfo = userService.getPhoneNoInfo(sessionKey, getUserInfo.getEncryptedData(), getUserInfo.getIv());
|
|
- //封装返回值
|
|
|
|
- WxMaDto.Vo vo = copy(WxMaDto.Vo.class, userInfo)
|
|
|
|
- .setOpenId(session.getOpenid())
|
|
|
|
- .setTel(phoneNoInfo.getPhoneNumber());
|
|
|
|
- return R.ok(vo);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- return R.fail("code无效");
|
|
|
|
- } finally {
|
|
|
|
- WxMaConfigHolder.remove();
|
|
|
|
- }
|
|
|
|
|
|
+ vo.setTel(phoneNoInfo.getPhoneNumber());
|
|
|
|
+ };
|
|
|
|
+ getInfo.run();
|
|
|
|
+ getTel.run();
|
|
|
|
+ WxMaConfigHolder.remove();
|
|
|
|
+ return R.ok(vo);
|
|
}
|
|
}
|
|
}
|
|
}
|