diff --git a/src/main/java/com/qzx/xdupartner/controller/WechatController.java b/src/main/java/com/qzx/xdupartner/controller/WechatController.java index 4d20f28..09fd788 100644 --- a/src/main/java/com/qzx/xdupartner/controller/WechatController.java +++ b/src/main/java/com/qzx/xdupartner/controller/WechatController.java @@ -1,9 +1,9 @@ package com.qzx.xdupartner.controller; import cn.binarywang.wx.miniapp.api.WxMaService; -import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; import cn.hutool.core.util.ObjectUtil; +import com.qzx.xdupartner.entity.Result; import com.qzx.xdupartner.entity.dto.WxUserInfo; import com.qzx.xdupartner.entity.vo.R; import com.qzx.xdupartner.entity.vo.ResultCode; @@ -16,6 +16,7 @@ import me.chanjar.weixin.common.error.WxErrorException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; + @Api @Slf4j @RestController @@ -31,12 +32,12 @@ public class WechatController { */ @ApiOperation("") @GetMapping("/register") - public R registor( + public R registor( @RequestParam("stuId") String stuId, @RequestParam("code") String code, @RequestParam("verCode") String verCode) { - WxMaJscode2SessionResult register = null; + Result register = null; try { register = userInfoService.register(stuId, code, verCode); } catch (WxErrorException e) { @@ -44,6 +45,7 @@ public R registor( } catch (MailCodeWrongException e) { return new R<>(ResultCode.MAIL_CODE_ERROR, null); } + return new R<>(ResultCode.SUCCESS, register); } @@ -52,8 +54,8 @@ public R registor( */ @ApiOperation("") @GetMapping("/login") - public R login(@RequestParam("code") String code) { - WxMaJscode2SessionResult login = userInfoService.login(code); + public R login(@RequestParam("code") String code) { + Result login = userInfoService.login(code); if (ObjectUtil.isNull(login)) { return new R<>(ResultCode.FAILED, null); } @@ -71,3 +73,4 @@ public R getUserInfo(@RequestBody WxUserInfo userInfo) { return new R<>(ResultCode.SUCCESS, userInfoService.getUserInfo(userInfo)); } } + diff --git a/src/main/java/com/qzx/xdupartner/entity/Result.java b/src/main/java/com/qzx/xdupartner/entity/Result.java new file mode 100644 index 0000000..1e4a710 --- /dev/null +++ b/src/main/java/com/qzx/xdupartner/entity/Result.java @@ -0,0 +1,10 @@ +package com.qzx.xdupartner.entity; + +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class Result { + WxMaJscode2SessionResult result; + Long userId; +} diff --git a/src/main/java/com/qzx/xdupartner/service/UserInfoService.java b/src/main/java/com/qzx/xdupartner/service/UserInfoService.java index 008cf7f..ff61283 100644 --- a/src/main/java/com/qzx/xdupartner/service/UserInfoService.java +++ b/src/main/java/com/qzx/xdupartner/service/UserInfoService.java @@ -1,17 +1,17 @@ package com.qzx.xdupartner.service; -import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; +import com.qzx.xdupartner.entity.Result; import com.qzx.xdupartner.entity.dto.WxUserInfo; import com.qzx.xdupartner.exception.MailCodeWrongException; import me.chanjar.weixin.common.error.WxErrorException; public interface UserInfoService { - WxMaJscode2SessionResult register(String stuId, String code, String verCode) throws WxErrorException, + Result register(String stuId, String code, String verCode) throws WxErrorException, MailCodeWrongException; - WxMaJscode2SessionResult login(String code); + Result login(String code); WxMaUserInfo getUserInfo(WxUserInfo userInfo); } diff --git a/src/main/java/com/qzx/xdupartner/service/impl/UserInfoServiceImpl.java b/src/main/java/com/qzx/xdupartner/service/impl/UserInfoServiceImpl.java index 5492994..0e464d0 100644 --- a/src/main/java/com/qzx/xdupartner/service/impl/UserInfoServiceImpl.java +++ b/src/main/java/com/qzx/xdupartner/service/impl/UserInfoServiceImpl.java @@ -7,6 +7,7 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.json.JSONUtil; import com.qzx.xdupartner.constant.RedisConstant; +import com.qzx.xdupartner.entity.Result; import com.qzx.xdupartner.entity.User; import com.qzx.xdupartner.entity.dto.WxUserInfo; import com.qzx.xdupartner.exception.MailCodeWrongException; @@ -38,7 +39,7 @@ public class UserInfoServiceImpl implements UserInfoService { private final StringRedisTemplate stringRedisTemplate; @Override - public WxMaJscode2SessionResult register(String stuId, String code, String verCode) throws WxErrorException, + public Result register(String stuId, String code, String verCode) throws WxErrorException, MailCodeWrongException { //验证邮箱 String realCode = stringRedisTemplate.opsForValue().get(RedisConstant.MAIL_CODE_PREFIX + stuId); @@ -58,11 +59,11 @@ public WxMaJscode2SessionResult register(String stuId, String code, String verCo user.setSessionKey(session.getSessionKey()); stringRedisTemplate.opsForValue().set(RedisConstant.LOGIN_PREFIX + session.getSessionKey(), JSONUtil.toJsonStr(user), RedisConstant.LOGIN_VALID_TTL, TimeUnit.DAYS); - return session; + return new Result(session, user.getId()); } @Override - public WxMaJscode2SessionResult login(String code) { + public Result login(String code) { try { WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code); log.info(session.getSessionKey()); @@ -77,7 +78,7 @@ public WxMaJscode2SessionResult login(String code) { JSONUtil.toJsonStr(user), RedisConstant.LOGIN_VALID_TTL, TimeUnit.DAYS); - return session; + return new Result(session, user.getId()); } catch (WxErrorException e) { log.error(e.getMessage(), e); return null;