From bc1cdc49638291b2b3cf54e82a8c5f61788c1b57 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Wed, 27 Mar 2024 10:14:17 +0800 Subject: [PATCH] fix wxchat login --- luoo_common/src/main/java/api/StatusCode.java | 2 +- .../luoo/user/controller/LoginController.java | 40 +++++++++++-- .../controller/WeChatAouth2Controller.java | 42 ++++++------- .../java/com/luoo/user/dao/UserInfoDao.java | 2 + .../luoo/user/dto/request/WeChatLoginReq.java | 7 +++ .../luoo/user/service/UserInfoService.java | 47 ++++++++++++++- .../user/service/WeChatAouth2Service.java | 59 ++++++++++++++++++- 7 files changed, 168 insertions(+), 31 deletions(-) diff --git a/luoo_common/src/main/java/api/StatusCode.java b/luoo_common/src/main/java/api/StatusCode.java index 8583550..6c94dfa 100644 --- a/luoo_common/src/main/java/api/StatusCode.java +++ b/luoo_common/src/main/java/api/StatusCode.java @@ -23,7 +23,7 @@ public enum StatusCode implements IErrorCode { WECHAT_lOGIN_FAILED(10011, "微信 id登录失败"), - APPLEID_MOBILE_UNBINDED(10010,"请先绑定appleid和手机号"), + WECHAT_MOBILE_UNBINDED(10010,"请先绑定微信登录手机号"), // music 模块错误码以20XXX,不足5位补0; diff --git a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java index bcb3f1d..c8a1253 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java @@ -12,7 +12,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSONObject; -import com.luoo.user.dto.request.AppleLoginReq; +import com.luoo.user.dto.request.*; +import com.luoo.user.service.WeChatAouth2Service; import com.luoo.user.util.IOSTokenUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -22,9 +23,6 @@ import org.springframework.web.bind.annotation.*; import constants.Constants; import controller.BaseController; -import com.luoo.user.dto.request.LoginReq; -import com.luoo.user.dto.request.SendSmsReq; -import com.luoo.user.dto.request.TokenRefreshReq; import com.luoo.user.dto.response.CountryCodeDTO; import com.luoo.user.dto.response.CreateImageCode; import com.luoo.user.pojo.UserInfo; @@ -64,6 +62,9 @@ public class LoginController extends BaseController { @Autowired private RedisTemplate redisTemplate; + @Autowired + private WeChatAouth2Service weChatAouth2Service; + /** * 更新好友粉丝数和用户关注数 * @@ -173,6 +174,37 @@ public class LoginController extends BaseController { + } + + @ApiOperation(value = "2.2 微信wxId登录/注册", notes = "成功后返回authorization") + @PostMapping("/wxIdLogin") + @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) + public Result wxIdLogin(HttpServletRequest request,@VerifyParam WeChatLoginReq weChatLoginReq) { + + // 如果有手机号,是首次微信登录时绑定流程 + if(StringUtils.isNotEmpty(weChatLoginReq.getMobile())) { + //得到缓存中的验证码 + String redisMobileCheckCodeKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + weChatLoginReq.getDeviceId(); + String redisMobileCheckCode = (String) redisTemplate.opsForValue().get(redisMobileCheckCodeKey); + if (StringTools.isEmpty(redisMobileCheckCode)) { + return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_EXPIRED); + } + if (!redisMobileCheckCode.equals(weChatLoginReq.getMobileCheckCode())) { + return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH); + } + return weChatAouth2Service.register(weChatLoginReq,getIpAddr(request)); + } + + // 无手机号,非第一次微信登录场景,需要判断当前wxID在数据库中是否有记录,如果有记录登录正常执行,如果无记录,返回状态到上面绑定手机号流程 + weChatAouth2Service.login(weChatLoginReq,getIpAddr(request)); + + + + + return Result.success(""); + + + } // @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization") // @PostMapping("/appleIdLogin") diff --git a/luoo_user/src/main/java/com/luoo/user/controller/WeChatAouth2Controller.java b/luoo_user/src/main/java/com/luoo/user/controller/WeChatAouth2Controller.java index 929122b..2bcf89b 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/WeChatAouth2Controller.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/WeChatAouth2Controller.java @@ -39,27 +39,27 @@ public class WeChatAouth2Controller extends BaseController { return weChatAouth2Service.getShareToken(weChatShareReq); } - @ApiOperation(value = "微信登录/注册",notes = "微信登录/注册,成功后返回authorization") - @PostMapping("/login") - @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) - public Result getLoginAccessToken(HttpServletRequest request, - @RequestBody @VerifyParam WeChatLoginReq loginReq){ - return weChatAouth2Service.loginOrRegister(loginReq,getIpAddr(request)); - } - - - @ApiOperation(value = "绑定手机号",notes = "绑定手机号") - @PostMapping("/bindPhoneNumber") - @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) - public Result bindPhoneNumber(@RequestHeader(value = "Authorization", required = true) String authorization, - @RequestBody @VerifyParam WeChatBindReq weChatBindReq){ - - UserLoginDto user = new JwtUtil().getUserLoginDto(authorization); - if(user == null) - return Result.failed(StatusCode.UNAUTHORIZED); - - return weChatAouth2Service.bindPhoneNumber(user,weChatBindReq); - } +// @ApiOperation(value = "微信登录/注册",notes = "微信登录/注册,成功后返回authorization") +// @PostMapping("/login") +// @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) +// public Result getLoginAccessToken(HttpServletRequest request, +// @RequestBody @VerifyParam WeChatLoginReq loginReq){ +// return weChatAouth2Service.loginOrRegister(loginReq,getIpAddr(request)); +// } +// +// +// @ApiOperation(value = "绑定手机号",notes = "绑定手机号") +// @PostMapping("/bindPhoneNumber") +// @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) +// public Result bindPhoneNumber(@RequestHeader(value = "Authorization", required = true) String authorization, +// @RequestBody @VerifyParam WeChatBindReq weChatBindReq){ +// +// UserLoginDto user = new JwtUtil().getUserLoginDto(authorization); +// if(user == null) +// return Result.failed(StatusCode.UNAUTHORIZED); +// +// return weChatAouth2Service.bindPhoneNumber(user,weChatBindReq); +// } } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java index 1898a36..2b24bdb 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java @@ -20,6 +20,8 @@ public interface UserInfoDao extends JpaRepository, JpaSpecifi public UserInfo findUserInfoByWxId(String wxId); public UserInfo findByAppleId(String appleId); + + @Modifying @Query(value = "update tb_user_info set follow_count=follow_count+? where id = ?", nativeQuery = true) void updatefollowcount(int x, String appUserInfoId); diff --git a/luoo_user/src/main/java/com/luoo/user/dto/request/WeChatLoginReq.java b/luoo_user/src/main/java/com/luoo/user/dto/request/WeChatLoginReq.java index cf45201..3d8d947 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/request/WeChatLoginReq.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/request/WeChatLoginReq.java @@ -22,6 +22,13 @@ public class WeChatLoginReq implements Serializable { private static final long serialVersionUID = 1L; + + @ApiModelProperty(name = "mobile", value = "手机号", required = false) + private String mobile; + + @ApiModelProperty(name = "mobileCheckCode", value = "6位验证码", required = false) + String mobileCheckCode; + @ApiModelProperty(name = "code", value = "code码", required = true) @VerifyParam(required = true) String code; diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 51c24a0..2fdbdef 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -253,9 +253,51 @@ public class UserInfoService { + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); } +// +// public String wechatloginOrRegister(UserInfo loginUserInfo) { +// UserInfo userInfo = userInfoDao.findUserInfoByWxId(loginUserInfo.getWxId()); +// if (null == userInfo) { +// userInfo = loginUserInfo; +// userInfo.setId(String.valueOf(idWorker.nextId())); +// userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); +// Date curDate = new Date(); +// userInfo.setJoinTime(curDate); +// userInfo.setLastLoginTime(curDate); +// userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); +// userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); +// userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); +// userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); +// } else { +// userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); +// userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); +// userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); +// } +// +// if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { +// return null; +// } +// userInfo.setLastLoginTime(new Date()); +// userInfoDao.save(userInfo); +// return Constants.TOKEN_PREFIX +// + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); +// } + + + public String wechatlogin(UserInfo loginUserInfo) { + + + if (!UserStatusEnum.ENABLE.getStatus().equals(loginUserInfo.getStatus())) { + return null; + } + loginUserInfo.setLastLoginTime(new Date()); + userInfoDao.save(loginUserInfo); + return Constants.TOKEN_PREFIX + + jwtUtil.createJWT(loginUserInfo.getId(), loginUserInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, loginUserInfo.getAvatar()); + } - public String wechatloginOrRegister(UserInfo loginUserInfo) { - UserInfo userInfo = userInfoDao.findUserInfoByWxId(loginUserInfo.getWxId()); + + public String wechatRegister(UserInfo loginUserInfo) { + UserInfo userInfo = userInfoDao.findByMobile(loginUserInfo.getMobile()); if (null == userInfo) { userInfo = loginUserInfo; userInfo.setId(String.valueOf(idWorker.nextId())); @@ -283,7 +325,6 @@ public class UserInfoService { } - public List orderByField(List idList) { return userInfoDao.orderByField(idList); } diff --git a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java index adc5268..7a0f504 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java +++ b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java @@ -3,6 +3,7 @@ package com.luoo.user.service; import api.Result; import api.StatusCode; import com.alibaba.fastjson.JSON; +import com.luoo.user.dao.UserInfoDao; import com.luoo.user.dto.response.Aouth2WeChatJSAPIResponse; import com.luoo.user.dto.request.WeChatBindReq; import com.luoo.user.dto.request.WeChatLoginReq; @@ -57,6 +58,9 @@ public class WeChatAouth2Service { @Autowired private RedisTemplate redisTemplate; + @Autowired + private UserInfoDao userInfoDao; + public Result getShareToken(WeChatShareReq weChatShareReq){ Aouth2WeChatTokenResponse accessToken = getAccessToken(weChatShareReq.getCode()); if(accessToken == null || accessToken.getAccess_token() == null ) @@ -69,19 +73,70 @@ public class WeChatAouth2Service { return Result.success(aouth2WeChatJSAPIResponse.getTicket()); } - public Result loginOrRegister(WeChatLoginReq loginReq,String ipAddress){ +// public Result loginOrRegister(WeChatLoginReq loginReq,String ipAddress){ +// +// Aouth2WeChatTokenResponse accessToken = getAccessToken(loginReq.getCode()); +// if(accessToken == null || accessToken.getAccess_token() == null){ +// return Result.failed(StatusCode.WECHAT_lOGIN_FAILED); +// } +// +// UserInfo loginUserInfo = new UserInfo(); +// loginUserInfo.setWxId(accessToken.getOpenid()); +// loginUserInfo.setMobile(loginReq.getMobile()); +// loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); +// loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); +// loginUserInfo.setLastLoginIp(ipAddress); +// String token = userInfoService.wechatloginOrRegister(loginUserInfo); +// return Result.success(token); +// } + + public Result login(WeChatLoginReq loginReq,String ipAddress){ + + Aouth2WeChatTokenResponse accessToken = getAccessToken(loginReq.getCode()); + if(accessToken == null || accessToken.getAccess_token() == null){ + return Result.failed(StatusCode.WECHAT_lOGIN_FAILED); + } + + UserInfo userInfo = userInfoDao.findUserInfoByWxId(accessToken.getOpenid()); + if(null == userInfo){ + // 当前微信用户是新用户,需要先绑定手机号 + return Result.failed(StatusCode.WECHAT_MOBILE_UNBINDED); + } + UserInfo loginUserInfo = new UserInfo(); +// loginUserInfo.setWxId(accessToken.getOpenid()); +// loginUserInfo.setMobile(loginReq.getMobile()); + loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); + loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); + loginUserInfo.setLastLoginIp(ipAddress); + String token = userInfoService.wechatlogin(loginUserInfo); + return Result.success(token); + } + + + public Result register(WeChatLoginReq loginReq,String ipAddress){ Aouth2WeChatTokenResponse accessToken = getAccessToken(loginReq.getCode()); if(accessToken == null || accessToken.getAccess_token() == null){ return Result.failed(StatusCode.WECHAT_lOGIN_FAILED); } + // 如果当前手机号在数据库中存在,更新用户信息wxid,返回token + UserInfo userInfo = userInfoDao.findByMobile(loginReq.getMobile()); + if(null != userInfo) { + userInfo.setWxId(accessToken.getOpenid()); + String token = userInfoService.wechatRegister(userInfo); + return Result.success(token); + } + + // 如果当前手机号不存在,那就是新用户 UserInfo loginUserInfo = new UserInfo(); + loginUserInfo.setWxId(accessToken.getOpenid()); + loginUserInfo.setMobile(loginReq.getMobile()); loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); loginUserInfo.setLastLoginIp(ipAddress); - String token = userInfoService.wechatloginOrRegister(loginUserInfo); + String token = userInfoService.wechatRegister(loginUserInfo); return Result.success(token); }