|
|
|
|
@ -16,6 +16,7 @@ import com.flossom.common.core.mapper.WxScriptMessageMapper;
|
|
|
|
|
import com.flossom.common.core.mapper.WxUserMemberMapper;
|
|
|
|
|
import com.flossom.common.core.mapper.WxUserScriptLogMapper;
|
|
|
|
|
import com.flossom.common.core.utils.DateUtils;
|
|
|
|
|
import com.flossom.common.core.utils.StringUtils;
|
|
|
|
|
import com.flossom.common.redis.service.RedisService;
|
|
|
|
|
import com.flossom.common.security.utils.SecurityUtils;
|
|
|
|
|
import com.flossom.miniProgram.domain.vo.*;
|
|
|
|
|
@ -60,14 +61,14 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional
|
|
|
|
|
public LoginUserVo login(String code) throws Exception {
|
|
|
|
|
public WxUserMember wxRegister(String code) throws Exception {
|
|
|
|
|
// 1、使用临时凭证 code 获取 appi + appsecret + unionid
|
|
|
|
|
String result = MiniProgramUtils.getSessionKeyAndOpenId(code);
|
|
|
|
|
logger.info("请求微信服务器获取openid和unionid,返回结果:{}", result);
|
|
|
|
|
WxCode2SessionRet wxCode2SessionRet = JSON.parseObject(result, WxCode2SessionRet.class);
|
|
|
|
|
if (wxCode2SessionRet.getErrcode() != null) {
|
|
|
|
|
logger.error(wxCode2SessionRet.getErrmsg());
|
|
|
|
|
throw new ServiceException("校验失败");
|
|
|
|
|
throw new ServiceException("登录失败");
|
|
|
|
|
}
|
|
|
|
|
logger.info("openid = {}", wxCode2SessionRet.getOpenid());
|
|
|
|
|
logger.info("unionid = {}", wxCode2SessionRet.getUnionid());
|
|
|
|
|
@ -108,11 +109,21 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 临时将 sessionKey 放到 wxUserMember,调用登录时方便获取
|
|
|
|
|
*/
|
|
|
|
|
wxUserMember.getParams().put(CacheConstants.WX_SESSION_KEY_CACHE + wxUserMember.getOpenid(),
|
|
|
|
|
wxCode2SessionRet.getSession_key());
|
|
|
|
|
return wxUserMember;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public LoginUserVo login(WxUserMember wxUserMember) {
|
|
|
|
|
// 3、登录请求,获取 token
|
|
|
|
|
R<Map<String, String>> loginResult = remoteAuthService.wxLogin(wxUserMember);
|
|
|
|
|
if (loginResult.getCode() == R.FAIL) {
|
|
|
|
|
logger.error("获取token失败:{}", loginResult.getMsg());
|
|
|
|
|
throw new ServiceException(loginResult.getMsg());
|
|
|
|
|
throw new ServiceException("登录失败");
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* TODO: 将 session_key 缓存在 redis,并且由于官方没有规定 session_key 的有效期,所以使用时候要注意如下:
|
|
|
|
|
@ -120,8 +131,13 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
* 1、如果过期,重新调用登录接口获取新的 session_key
|
|
|
|
|
* 2、如果没过期,但由于security的拦截器会判断过期时间不足120分钟时会,会对session_key进行续期:{@link com.flossom.common.security.service.TokenService#refreshSessionKey}
|
|
|
|
|
*/
|
|
|
|
|
String sessionKey = (String) wxUserMember.getParams().get(CacheConstants.WX_SESSION_KEY_CACHE + wxUserMember.getOpenid());
|
|
|
|
|
if (StringUtils.isBlank(sessionKey)) {
|
|
|
|
|
logger.error("sessionKey 为空");
|
|
|
|
|
throw new ServiceException("登录失败");
|
|
|
|
|
}
|
|
|
|
|
redisService.setCacheObject(CacheConstants.WX_SESSION_KEY_CACHE + wxUserMember.getOpenid(),
|
|
|
|
|
wxCode2SessionRet.getSession_key(), CacheConstants.EXPIRATION, TimeUnit.MINUTES);
|
|
|
|
|
sessionKey, CacheConstants.EXPIRATION, TimeUnit.MINUTES);
|
|
|
|
|
|
|
|
|
|
// 4、返回登录信息
|
|
|
|
|
LoginUserVo loginUserVo = new LoginUserVo();
|
|
|
|
|
|