fix wxchat login

main
wangqing 10 months ago
parent 330d19d924
commit bc1cdc4963

@ -23,7 +23,7 @@ public enum StatusCode implements IErrorCode {
WECHAT_lOGIN_FAILED(10011, "微信 id登录失败"), WECHAT_lOGIN_FAILED(10011, "微信 id登录失败"),
APPLEID_MOBILE_UNBINDED(10010,"请先绑定appleid和手机号"), WECHAT_MOBILE_UNBINDED(10010,"请先绑定微信登录手机号"),
// music 模块错误码以20XXX不足5位补0; // music 模块错误码以20XXX不足5位补0;

@ -12,7 +12,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject; 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 com.luoo.user.util.IOSTokenUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -22,9 +23,6 @@ import org.springframework.web.bind.annotation.*;
import constants.Constants; import constants.Constants;
import controller.BaseController; 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.CountryCodeDTO;
import com.luoo.user.dto.response.CreateImageCode; import com.luoo.user.dto.response.CreateImageCode;
import com.luoo.user.pojo.UserInfo; import com.luoo.user.pojo.UserInfo;
@ -64,6 +62,9 @@ public class LoginController extends BaseController {
@Autowired @Autowired
private RedisTemplate redisTemplate; 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<String> 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") // @ApiOperation(value = "2.1 appleId登录/注册", notes = "成功后返回authorization")
// @PostMapping("/appleIdLogin") // @PostMapping("/appleIdLogin")

@ -39,27 +39,27 @@ public class WeChatAouth2Controller extends BaseController {
return weChatAouth2Service.getShareToken(weChatShareReq); return weChatAouth2Service.getShareToken(weChatShareReq);
} }
@ApiOperation(value = "微信登录/注册",notes = "微信登录/注册,成功后返回authorization") // @ApiOperation(value = "微信登录/注册",notes = "微信登录/注册,成功后返回authorization")
@PostMapping("/login") // @PostMapping("/login")
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) // @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12)
public Result<String> getLoginAccessToken(HttpServletRequest request, // public Result<String> getLoginAccessToken(HttpServletRequest request,
@RequestBody @VerifyParam WeChatLoginReq loginReq){ // @RequestBody @VerifyParam WeChatLoginReq loginReq){
return weChatAouth2Service.loginOrRegister(loginReq,getIpAddr(request)); // return weChatAouth2Service.loginOrRegister(loginReq,getIpAddr(request));
} // }
//
//
@ApiOperation(value = "绑定手机号",notes = "绑定手机号") // @ApiOperation(value = "绑定手机号",notes = "绑定手机号")
@PostMapping("/bindPhoneNumber") // @PostMapping("/bindPhoneNumber")
@GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12) // @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.HOUR, requestFrequencyThreshold = 12)
public Result bindPhoneNumber(@RequestHeader(value = "Authorization", required = true) String authorization, // public Result bindPhoneNumber(@RequestHeader(value = "Authorization", required = true) String authorization,
@RequestBody @VerifyParam WeChatBindReq weChatBindReq){ // @RequestBody @VerifyParam WeChatBindReq weChatBindReq){
//
UserLoginDto user = new JwtUtil().getUserLoginDto(authorization); // UserLoginDto user = new JwtUtil().getUserLoginDto(authorization);
if(user == null) // if(user == null)
return Result.failed(StatusCode.UNAUTHORIZED); // return Result.failed(StatusCode.UNAUTHORIZED);
//
return weChatAouth2Service.bindPhoneNumber(user,weChatBindReq); // return weChatAouth2Service.bindPhoneNumber(user,weChatBindReq);
} // }
} }

@ -20,6 +20,8 @@ public interface UserInfoDao extends JpaRepository<UserInfo, String>, JpaSpecifi
public UserInfo findUserInfoByWxId(String wxId); public UserInfo findUserInfoByWxId(String wxId);
public UserInfo findByAppleId(String appleId); public UserInfo findByAppleId(String appleId);
@Modifying @Modifying
@Query(value = "update tb_user_info set follow_count=follow_count+? where id = ?", nativeQuery = true) @Query(value = "update tb_user_info set follow_count=follow_count+? where id = ?", nativeQuery = true)
void updatefollowcount(int x, String appUserInfoId); void updatefollowcount(int x, String appUserInfoId);

@ -22,6 +22,13 @@ public class WeChatLoginReq implements Serializable {
private static final long serialVersionUID = 1L; 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) @ApiModelProperty(name = "code", value = "code码", required = true)
@VerifyParam(required = true) @VerifyParam(required = true)
String code; String code;

@ -253,9 +253,51 @@ public class UserInfoService {
+ jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); + 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) { if (null == userInfo) {
userInfo = loginUserInfo; userInfo = loginUserInfo;
userInfo.setId(String.valueOf(idWorker.nextId())); userInfo.setId(String.valueOf(idWorker.nextId()));
@ -283,7 +325,6 @@ public class UserInfoService {
} }
public List<UserInfo> orderByField(List<String> idList) { public List<UserInfo> orderByField(List<String> idList) {
return userInfoDao.orderByField(idList); return userInfoDao.orderByField(idList);
} }

@ -3,6 +3,7 @@ package com.luoo.user.service;
import api.Result; import api.Result;
import api.StatusCode; import api.StatusCode;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.luoo.user.dao.UserInfoDao;
import com.luoo.user.dto.response.Aouth2WeChatJSAPIResponse; import com.luoo.user.dto.response.Aouth2WeChatJSAPIResponse;
import com.luoo.user.dto.request.WeChatBindReq; import com.luoo.user.dto.request.WeChatBindReq;
import com.luoo.user.dto.request.WeChatLoginReq; import com.luoo.user.dto.request.WeChatLoginReq;
@ -57,6 +58,9 @@ public class WeChatAouth2Service {
@Autowired @Autowired
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
@Autowired
private UserInfoDao userInfoDao;
public Result getShareToken(WeChatShareReq weChatShareReq){ public Result getShareToken(WeChatShareReq weChatShareReq){
Aouth2WeChatTokenResponse accessToken = getAccessToken(weChatShareReq.getCode()); Aouth2WeChatTokenResponse accessToken = getAccessToken(weChatShareReq.getCode());
if(accessToken == null || accessToken.getAccess_token() == null ) if(accessToken == null || accessToken.getAccess_token() == null )
@ -69,19 +73,70 @@ public class WeChatAouth2Service {
return Result.success(aouth2WeChatJSAPIResponse.getTicket()); 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()); Aouth2WeChatTokenResponse accessToken = getAccessToken(loginReq.getCode());
if(accessToken == null || accessToken.getAccess_token() == null){ if(accessToken == null || accessToken.getAccess_token() == null){
return Result.failed(StatusCode.WECHAT_lOGIN_FAILED); 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(); UserInfo loginUserInfo = new UserInfo();
loginUserInfo.setWxId(accessToken.getOpenid()); loginUserInfo.setWxId(accessToken.getOpenid());
loginUserInfo.setMobile(loginReq.getMobile());
loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId());
loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand());
loginUserInfo.setLastLoginIp(ipAddress); loginUserInfo.setLastLoginIp(ipAddress);
String token = userInfoService.wechatloginOrRegister(loginUserInfo); String token = userInfoService.wechatRegister(loginUserInfo);
return Result.success(token); return Result.success(token);
} }

Loading…
Cancel
Save