Commit b25af5cf authored by muhong's avatar muhong

修改 小程序登录接口

parent 4a8f66da
......@@ -3,7 +3,15 @@ package com.wwdz.ch.wx.constant;
import java.util.HashMap;
import java.util.Map;
public class CacheCodeEnum {
public class CacheCodeConstants {
/**
* 微信接口调用凭证access_token
*/
public static final String WX_ACCESS_TOKEN = "quanku:wx:access_token";
/**
* 短信验证码类型枚举
*/
public enum SmsTypeEnum {
LOGIN("LOGIN:", "登录"),
UPDATE("UPDATE:", "修改手机号"),
......
......@@ -20,4 +20,5 @@ public class UserRequestDto implements Entity {
private String smsType;
private String encryptedData;
private String iv;
private Integer type;
}
......@@ -13,7 +13,7 @@ import com.wwdz.ch.core.util.UUID;
import com.wwdz.ch.core.util.bcrypt.BCryptPasswordEncoder;
import com.wwdz.ch.db.dao.UserDao;
import com.wwdz.ch.db.domain.User;
import com.wwdz.ch.wx.constant.CacheCodeEnum;
import com.wwdz.ch.wx.constant.CacheCodeConstants;
import com.wwdz.ch.wx.dao.UserInfo;
import com.wwdz.ch.wx.dao.UserToken;
import com.wwdz.ch.wx.entity.request.FavoritesRequestDto;
......@@ -68,10 +68,16 @@ public class UserServiceImpl implements UserService {
try {
String mobile = "";
try {
mobile = wxLoginService.getPhone(dto.getCode());
mobile = wxLoginService.getPhone(dto);
if (StringUtils.equals("false", mobile)) {
return Result.failed("获取access_token失败");
}
if (StringUtils.equals("sessionFalse", mobile)) {
return Result.failed("获取sessionKey失败");
}
if (StringUtils.equals("phoneFalse", mobile)) {
return Result.failed("获取手机号失败");
}
} catch (Exception e) {
logger.error("获取微信用户手机号失败", e);
return Result.failed("获取微信用户手机号失败");
......@@ -197,7 +203,7 @@ public class UserServiceImpl implements UserService {
@Override
public Result loginByMobile(UserRequestDto dto, HttpServletRequest request) {
try {
String mobileCode = CaptchaCodeManager.getCachedCaptcha(CacheCodeEnum.SmsTypeEnum.LOGIN.getCode() + dto.getMobile());
String mobileCode = CaptchaCodeManager.getCachedCaptcha(CacheCodeConstants.SmsTypeEnum.LOGIN.getCode() + dto.getMobile());
if (!StringUtils.equals(dto.getSmsCode(), mobileCode)) {
return Result.failed("验证码不正确");
}
......@@ -371,7 +377,7 @@ public class UserServiceImpl implements UserService {
if (!StringUtils.equals(user.getMobile(), dto.getBeforeMobile())) {
return Result.failed("原手机号码不正确");
}
String mobileCode = CaptchaCodeManager.getCachedCaptcha(CacheCodeEnum.SmsTypeEnum.UPDATE.getCode() + dto.getAfterMobile());
String mobileCode = CaptchaCodeManager.getCachedCaptcha(CacheCodeConstants.SmsTypeEnum.UPDATE.getCode() + dto.getAfterMobile());
if (!StringUtils.equals(dto.getSmsCode(), mobileCode)) {
return Result.failed("验证码不正确");
}
......
......@@ -2,13 +2,19 @@ package com.wwdz.ch.wx.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import com.wwdz.ch.core.util.HttpUtil;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.alibaba.fastjson.JSON;
import com.wwdz.ch.core.util.OkHttpUtil;
import com.wwdz.ch.wx.constant.CacheCodeConstants;
import com.wwdz.ch.wx.entity.request.UserRequestDto;
import com.wwdz.ch.wx.service.WxLoginService;
import com.wwdz.ch.wx.util.RedisUtil;
import com.xxdxxs.utils.JsonUtils;
import com.xxdxxs.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
......@@ -18,42 +24,86 @@ import java.util.Map;
public class WxLoginServiceImpl implements WxLoginService {
private static final Logger logger = LoggerFactory.getLogger(WxMaServiceImpl.class);
private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/stable_token";
private static final String GET_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s";
private static final String appid = "wx5b4409448bf2c72b";
private static final String secret = "6fee0d7b2197845fed0a3fe8f5a9bd66";
@Value("${dts.wx.app-id}")
private String appid;
@Value("${dts.wx.app-secret}")
private String secret;
// private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/stable_token";
private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
private static final String GET_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s";
@Autowired
private WxMaService wxMaService;
@Autowired
private RedisUtil redisUtil;
@Override
public String getPhone(String code) {
logger.info("开始获取access_token----------------");
// Map<String, String> tokenParam = new HashMap<>();
// tokenParam.put("grant_type", "client_credential");
// tokenParam.put("appid", "wx3618eab6c8b144af");
// tokenParam.put("secret", "6fee0d7b2197845fed0a3fe8f5a9bd66");
// String tokenResult = HttpUtil.sendPost(GET_ACCESS_TOKEN_URL, tokenParam);
// logger.info("获取access_token的结果是:" + tokenResult);
// String accessToken = JsonUtils.getValueByPath(tokenResult, "access_token");
String accessToken = "";
try {
accessToken = wxMaService.getAccessToken();
} catch (Exception e) {
logger.error("获取accessToken出错");
return "false";
}
public String getPhone(UserRequestDto dto) {
String phone = "";
if (dto.getType() == 1) {
logger.info("开始获取access_token----------------");
String accessToken = "";
if (redisUtil.hasKey(CacheCodeConstants.WX_ACCESS_TOKEN)) {
accessToken = redisUtil.get(CacheCodeConstants.WX_ACCESS_TOKEN).toString();
} else {
try {
String accessTokenUrl = String.format(GET_ACCESS_TOKEN_URL, appid, secret);
String res = OkHttpUtil.builder().url(accessTokenUrl)
.get().sync();
logger.info("获取access_token结果:" + res);
if (!res.contains("access_token")) {
logger.error("获取accessToken出错,返回结果是:" + res);
return "false";
}
accessToken = JsonUtils.getValueByPath(res, "access_token");
String expiresIn = JsonUtils.getValueByPath(res, "expires_in");
redisUtil.set(CacheCodeConstants.WX_ACCESS_TOKEN, accessToken, Long.parseLong(expiresIn));
} catch (Exception e) {
logger.error("获取accessToken出错", e);
return "false";
}
}
logger.info("开始获取phone-----------------------");
String url = String.format(GET_PHONE_URL, accessToken);
Map<String, String> phoneParam = new HashMap<>();
phoneParam.put("code", code);
String phoneResult = HttpUtil.sendPost(url, phoneParam);
logger.info("获取phone的结果是:" + phoneResult);
return JsonUtils.getValueByPath(phoneResult, "phone_info/phoneNumber");
logger.info("开始获取phone-----------------------");
String url = String.format(GET_PHONE_URL, accessToken);
Map<String, Object> phoneParam = new HashMap<>();
phoneParam.put("code", dto.getCode());
String phoneResult = OkHttpUtil.builder().url(url)
.addParams(phoneParam)
.post(true).sync();
logger.info("获取phone的结果是:" + phoneResult);
if (!phoneResult.contains("phoneNumber")) {
logger.error("获取手机号出错,返回结果是:" + phoneResult);
return "phoneFalse";
}
} else {
logger.info("开始获取sessionKey-----------------------");
String sessionKey = "";
try {
WxMaJscode2SessionResult result = this.wxMaService.getUserService().getSessionInfo(dto.getCode());
sessionKey = result.getSessionKey();
logger.info("获取sessionKey结果:" + JSON.toJSONString(result));
} catch (Exception e) {
logger.error("获取用户登录信息失败", e);
return "sessionFalse";
}
if (StringUtils.isEmpty(sessionKey)) {
logger.error("微信登录,调用官方接口失败:{}", dto.getCode());
return "sessionFalse";
}
logger.info("解析手机号加密信息-----------------------");
try {
WxMaPhoneNumberInfo wxMaPhoneNumberInfo = this.wxMaService.getUserService().getPhoneNoInfo(sessionKey, dto.getEncryptedData(), dto.getIv());
logger.info("解析手机号加密信息结果:" + JSON.toJSONString(wxMaPhoneNumberInfo));
phone = wxMaPhoneNumberInfo.getPhoneNumber();
} catch (Exception e) {
logger.error("解析手机号加密信息失败", e);
return "phoneFalse";
}
}
return phone;
}
}
package com.wwdz.ch.wx.service;
import com.wwdz.ch.wx.entity.request.UserRequestDto;
public interface WxLoginService {
String getPhone(String code);
String getPhone(UserRequestDto dto);
}
......@@ -7,7 +7,7 @@ import com.wwdz.ch.core.util.ResponseUtil;
import com.wwdz.ch.db.domain.UserAccountObsolete;
import com.wwdz.ch.db.service.DtsAccountService;
import com.wwdz.ch.wx.annotation.LoginUser;
import com.wwdz.ch.wx.constant.CacheCodeEnum;
import com.wwdz.ch.wx.constant.CacheCodeConstants;
import com.wwdz.ch.wx.entity.request.KlDataRequestDto;
import com.wwdz.ch.wx.entity.request.UserRequestDto;
import com.wwdz.ch.wx.service.UserService;
......@@ -99,9 +99,21 @@ public class WxUserController {
if (StringUtils.isBlank(code)) {
return Result.failed("授权码不能为空");
}
if (!json.contains("type")) {
return Result.failed("版本类型不能为空");
}
Integer type = Integer.parseInt(JsonUtils.getValueByPath(json, "type"));
UserRequestDto dto = new UserRequestDto();
dto.setCode(code);
dto.setType(type);
if (type == 2) {
if (!json.contains("encryptedData") || !json.contains("iv")) {
return Result.failed("encryptedData或iv不能为空");
}
dto.setEncryptedData(JsonUtils.getValueByPath(json, "encryptedData"));
dto.setIv(JsonUtils.getValueByPath(json, "iv"));
}
return userService.loginByWx(dto, request);
}
......@@ -117,7 +129,7 @@ public class WxUserController {
UserRequestDto dto = new UserRequestDto();
dto.setMobile(mobile);
dto.setSmsType(CacheCodeEnum.SmsTypeEnum.LOGIN.getCode());
dto.setSmsType(CacheCodeConstants.SmsTypeEnum.LOGIN.getCode());
return userService.regCaptcha(dto);
}
......@@ -233,7 +245,7 @@ public class WxUserController {
UserRequestDto dto = new UserRequestDto();
dto.setMobile(mobile);
dto.setSmsType(CacheCodeEnum.SmsTypeEnum.UPDATE.getCode());
dto.setSmsType(CacheCodeConstants.SmsTypeEnum.UPDATE.getCode());
return userService.regCaptcha(dto);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment