|
|
|
|
@ -9,6 +9,7 @@ import com.flossom.common.core.domain.R;
|
|
|
|
|
import com.flossom.common.core.domain.RegisterBody;
|
|
|
|
|
import com.flossom.common.core.domain.entity.WxUserMember;
|
|
|
|
|
import com.flossom.common.core.exception.ServiceException;
|
|
|
|
|
import com.flossom.common.core.utils.DateUtils;
|
|
|
|
|
import com.flossom.common.core.web.domain.AjaxResult;
|
|
|
|
|
import com.flossom.common.redis.service.RedisService;
|
|
|
|
|
import com.flossom.common.security.utils.SecurityUtils;
|
|
|
|
|
@ -50,18 +51,23 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
public LoginUserVo login(String code) throws Exception {
|
|
|
|
|
// 1、使用临时凭证 code 获取 appi + appsecret + unionid
|
|
|
|
|
WxCode2SessionRet wxCode2SessionRet = JSON.parseObject(MiniProgramUtils.getSessionKeyAndOpenId(code), WxCode2SessionRet.class);
|
|
|
|
|
if (200 != wxCode2SessionRet.getErrcode()) {
|
|
|
|
|
if (wxCode2SessionRet.getErrcode() != null) {
|
|
|
|
|
logger.error(wxCode2SessionRet.getErrmsg());
|
|
|
|
|
throw new ServiceException("校验失败");
|
|
|
|
|
}
|
|
|
|
|
logger.info("openid", wxCode2SessionRet.getOpenid());
|
|
|
|
|
logger.info("unionid", wxCode2SessionRet.getUnionid());
|
|
|
|
|
|
|
|
|
|
// 2、根据 openid 查询用户信息
|
|
|
|
|
WxUserMember wxUserMember = wxUserMemberMapper.selectWxUserMemberByOpenId(wxCode2SessionRet.getOpenid());
|
|
|
|
|
if (wxUserMember == null) {
|
|
|
|
|
// 未注册用户,就注册游客账户
|
|
|
|
|
// 2.1、封装微信用户信息
|
|
|
|
|
wxUserMember = new WxUserMember();
|
|
|
|
|
wxUserMember.setOpenid(wxCode2SessionRet.getOpenid());
|
|
|
|
|
wxUserMember.setUnionid(wxCode2SessionRet.getUnionid());
|
|
|
|
|
wxUserMember.setLoginTime(DateUtils.getNowDate());
|
|
|
|
|
wxUserMember.setCreateTime(DateUtils.getNowDate());
|
|
|
|
|
// 3.2、微信用户表注册
|
|
|
|
|
wxUserMemberMapper.insertWxUserMember(wxUserMember);
|
|
|
|
|
// 3.3、系统表注册
|
|
|
|
|
@ -70,6 +76,7 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
registerBody.setPassword(UserConstants.WX_SYSTEM_USER_PASSWORD);
|
|
|
|
|
R<?> registerResult = remoteAuthService.register(registerBody);
|
|
|
|
|
if (R.FAIL == registerResult.getCode()) {
|
|
|
|
|
logger.error("测试后台账号失败:{}", registerResult.getMsg());
|
|
|
|
|
throw new ServiceException(registerResult.getMsg());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -77,6 +84,7 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
// 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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -90,6 +98,7 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
LoginUserVo loginUserVo = new LoginUserVo();
|
|
|
|
|
BeanUtils.copyProperties(wxUserMember, loginUserVo);
|
|
|
|
|
loginUserVo.setToken(loginResult.getData().get("access_token"));
|
|
|
|
|
logger.info("登录用户信息:id = {}, username = {}", loginUserVo.getId(), loginUserVo.getUsername());
|
|
|
|
|
return loginUserVo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|