微信登录流程优化

master
382696293@qq.com 2 years ago
parent 33afe29706
commit 67da87e6d3

@ -137,7 +137,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<include refid="selectWxScriptMessageVo"/>
where
message_type = #{messageType}
and status = #{status}
and status = 0
order by create_time desc
LIMIT 1
</select>

@ -3,6 +3,7 @@ package com.flossom.miniProgram.controller;
import com.flossom.common.core.constant.Constants;
import com.flossom.common.core.domain.R;
import com.flossom.common.core.domain.SysFile;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.flossom.common.core.exception.ServiceException;
import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.core.web.controller.BaseController;
@ -41,7 +42,12 @@ public class UserMemberController extends BaseController {
logger.error("登录code不能为空");
return AjaxResult.error("登录失败");
}
return AjaxResult.success(wxUserMemberService.login(code));
// 判断注册
WxUserMember wxUserMember = wxUserMemberService.wxRegister(code);
if (wxUserMember == null) {
return AjaxResult.error("登录失败");
}
return AjaxResult.success(wxUserMemberService.login(wxUserMember));
}
/**

@ -1,11 +1,14 @@
package com.flossom.miniProgram.service;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.flossom.miniProgram.domain.vo.LoginUserVo;
import com.flossom.miniProgram.domain.vo.UserMemberUpdateVo;
public interface IWxUserMemberService {
LoginUserVo login(String code) throws Exception;
WxUserMember wxRegister(String code) throws Exception;
LoginUserVo login(WxUserMember wxUserMember);
String upgradeMember(String code) throws Exception;

@ -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
* 2security120session_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();

Loading…
Cancel
Save