Skip to content

Commit

Permalink
feat : a
Browse files Browse the repository at this point in the history
  • Loading branch information
qizhenxin committed Apr 15, 2024
1 parent ff97d3e commit 3e6febf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -31,19 +32,20 @@ public class WechatController {
*/
@ApiOperation("")
@GetMapping("/register")
public R<WxMaJscode2SessionResult> registor(
public R<Result> 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) {
return new R<>(ResultCode.WECHAT_ERROR, null);
} catch (MailCodeWrongException e) {
return new R<>(ResultCode.MAIL_CODE_ERROR, null);
}

return new R<>(ResultCode.SUCCESS, register);
}

Expand All @@ -52,8 +54,8 @@ public R<WxMaJscode2SessionResult> registor(
*/
@ApiOperation("")
@GetMapping("/login")
public R<WxMaJscode2SessionResult> login(@RequestParam("code") String code) {
WxMaJscode2SessionResult login = userInfoService.login(code);
public R<Result> login(@RequestParam("code") String code) {
Result login = userInfoService.login(code);
if (ObjectUtil.isNull(login)) {
return new R<>(ResultCode.FAILED, null);
}
Expand All @@ -71,3 +73,4 @@ public R<WxMaUserInfo> getUserInfo(@RequestBody WxUserInfo userInfo) {
return new R<>(ResultCode.SUCCESS, userInfoService.getUserInfo(userInfo));
}
}

10 changes: 10 additions & 0 deletions src/main/java/com/qzx/xdupartner/entity/Result.java
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions src/main/java/com/qzx/xdupartner/service/UserInfoService.java
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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;
Expand Down

0 comments on commit 3e6febf

Please sign in to comment.