绑定手机号修改为登陆时缓存session_key(用户在小程序活跃,则session_key一直有效),获取手机号直接取值

This commit is contained in:
LIONCITYS\lioncity
2020-11-03 22:14:40 +08:00
parent b32feff0c3
commit 7eda6161ea
3 changed files with 10 additions and 9 deletions

View File

@ -97,6 +97,7 @@ public class AuthService {
}
WxMaService wxMaService = WxMaConfiguration.getWxMaService();
WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
WxMaUserInfo wxMpUser = wxMaService.getUserService()
.getUserInfo(session.getSessionKey(), encryptedData, iv);
String openid = wxMpUser.getOpenId();
@ -157,6 +158,7 @@ public class AuthService {
userService.updateById(yxUser);
}
userService.setSpread(spread, returnUser.getUid());
redisUtils.set(ShopConstants.YSHOP_MINI_SESSION_KET + yxUser.getUid(), session.getSessionKey());
return returnUser;
} catch (WxErrorException e) {
e.printStackTrace();

View File

@ -14,6 +14,7 @@ import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.hutool.core.util.StrUtil;
import co.yixiang.api.ApiResult;
import co.yixiang.api.YshopException;
import co.yixiang.constant.ShopConstants;
import co.yixiang.logging.aop.log.AppLog;
import co.yixiang.common.bean.LocalUser;
import co.yixiang.common.interceptor.AuthCheck;
@ -22,7 +23,9 @@ import co.yixiang.modules.user.service.YxUserService;
import co.yixiang.modules.wechat.rest.param.BindPhoneParam;
import co.yixiang.modules.wechat.rest.param.WxPhoneParam;
import co.yixiang.modules.mp.config.WxMaConfiguration;
import co.yixiang.utils.RedisUtil;
import co.yixiang.utils.RedisUtils;
import co.yixiang.utils.SecurityUtils;
import co.yixiang.utils.ShopKeyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -94,17 +97,13 @@ public class WxMaUserController {
WxMaService wxMaService = WxMaConfiguration.getWxMaService();
String phone = "";
try {
WxMaJscode2SessionResult session = wxMaService.getUserService()
.getSessionInfo(param.getCode());
// 解密
WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService()
.getPhoneNoInfo(session.getSessionKey(), param.getEncryptedData(), param.getIv());
String sessionKey = RedisUtil.get(ShopConstants.YSHOP_MINI_SESSION_KET+ user.getUid()).toString();
WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService()
.getPhoneNoInfo(sessionKey, param.getEncryptedData(), param.getIv());
phone = phoneNoInfo.getPhoneNumber();
user.setPhone(phone);
userService.updateById(user);
} catch (WxErrorException e) {
} catch (Exception e) {
e.printStackTrace();
throw new YshopException("绑定失败");
}

View File

@ -173,5 +173,5 @@ public interface ShopConstants {
String DEFAULT_UNI_H5_URL = "https://h5.yixiang.co";
String YSHOP_MINI_SESSION_KET = "yshop:session_key:";
}