|
|
|
|
@ -1,29 +1,21 @@
|
|
|
|
|
package com.flossom.miniProgram.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.flossom.common.core.constant.CacheConstants;
|
|
|
|
|
import com.flossom.common.core.constant.UserConstants;
|
|
|
|
|
import com.flossom.common.core.domain.LoginBody;
|
|
|
|
|
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.mapper.WxUserMemberMapper;
|
|
|
|
|
import com.flossom.common.core.utils.DateUtils;
|
|
|
|
|
import com.flossom.common.core.web.domain.AjaxResult;
|
|
|
|
|
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.LoginUserVo;
|
|
|
|
|
import com.flossom.miniProgram.domain.vo.UserMemberUpdateVo;
|
|
|
|
|
import com.flossom.miniProgram.domain.vo.WxCode2PhoneRet;
|
|
|
|
|
import com.flossom.miniProgram.domain.vo.WxCode2SessionRet;
|
|
|
|
|
import com.flossom.miniProgram.domain.vo.*;
|
|
|
|
|
import com.flossom.miniProgram.service.IWxUserMemberService;
|
|
|
|
|
import com.flossom.miniProgram.utils.MiniProgramUtils;
|
|
|
|
|
import com.flossom.system.api.RemoteAuthService;
|
|
|
|
|
import com.flossom.system.api.RemoteUserService;
|
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
@ -50,13 +42,15 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
@Override
|
|
|
|
|
public LoginUserVo login(String code) throws Exception {
|
|
|
|
|
// 1、使用临时凭证 code 获取 appi + appsecret + unionid
|
|
|
|
|
WxCode2SessionRet wxCode2SessionRet = JSON.parseObject(MiniProgramUtils.getSessionKeyAndOpenId(code), WxCode2SessionRet.class);
|
|
|
|
|
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("校验失败");
|
|
|
|
|
}
|
|
|
|
|
logger.info("openid", wxCode2SessionRet.getOpenid());
|
|
|
|
|
logger.info("unionid", wxCode2SessionRet.getUnionid());
|
|
|
|
|
logger.info("openid = {}", wxCode2SessionRet.getOpenid());
|
|
|
|
|
logger.info("unionid = {}", wxCode2SessionRet.getUnionid());
|
|
|
|
|
|
|
|
|
|
// 2、根据 openid 查询用户信息
|
|
|
|
|
WxUserMember wxUserMember = wxUserMemberMapper.selectWxUserMemberByOpenId(wxCode2SessionRet.getOpenid());
|
|
|
|
|
@ -68,9 +62,9 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
wxUserMember.setUnionid(wxCode2SessionRet.getUnionid());
|
|
|
|
|
wxUserMember.setLoginTime(DateUtils.getNowDate());
|
|
|
|
|
wxUserMember.setCreateTime(DateUtils.getNowDate());
|
|
|
|
|
// 3.2、微信用户表注册
|
|
|
|
|
// 2.2、微信用户表注册
|
|
|
|
|
wxUserMemberMapper.insertWxUserMember(wxUserMember);
|
|
|
|
|
// 3.3、系统表注册
|
|
|
|
|
// 2.3、系统表注册
|
|
|
|
|
RegisterBody registerBody = new RegisterBody();
|
|
|
|
|
registerBody.setUsername(wxUserMember.getOpenid());
|
|
|
|
|
registerBody.setPassword(UserConstants.WX_SYSTEM_USER_PASSWORD);
|
|
|
|
|
@ -87,26 +81,46 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
logger.error("获取token失败:{}", loginResult.getMsg());
|
|
|
|
|
throw new ServiceException(loginResult.getMsg());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO: 暂时将session_key保存在redis,后期根据业务修改
|
|
|
|
|
*/
|
|
|
|
|
redisService.setCacheObject(CacheConstants.wx_session_key_cache + wxUserMember.getOpenid(),
|
|
|
|
|
redisService.setCacheObject(CacheConstants.WX_SESSION_KEY_CACHE + wxUserMember.getOpenid(),
|
|
|
|
|
wxCode2SessionRet.getSession_key(), CacheConstants.EXPIRATION, TimeUnit.MINUTES);
|
|
|
|
|
|
|
|
|
|
// 4、返回登录信息
|
|
|
|
|
LoginUserVo loginUserVo = new LoginUserVo();
|
|
|
|
|
BeanUtils.copyProperties(wxUserMember, loginUserVo);
|
|
|
|
|
loginUserVo.setToken(loginResult.getData().get("access_token"));
|
|
|
|
|
logger.info("登录用户信息:id = {}, username = {}", loginUserVo.getId(), loginUserVo.getUsername());
|
|
|
|
|
logger.info("登录用户信息:id = {}, username = {}, token={}", loginUserVo.getId(), loginUserVo.getUsername(), loginUserVo.getToken());
|
|
|
|
|
return loginUserVo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void upgradeMember(String code) throws Exception {
|
|
|
|
|
WxCode2PhoneRet wxCode2PhoneRet = JSON.parseObject(MiniProgramUtils.getPhone(code), WxCode2PhoneRet.class);
|
|
|
|
|
/**
|
|
|
|
|
* TODO: 暂时将access_token保存在redis,后期根据业务修改
|
|
|
|
|
*/
|
|
|
|
|
String accessToken = redisService.getCacheObject(CacheConstants.WX_ACCESS_TOKEN_CACHE + SecurityUtils.getLoginUser().getWxUserMember().getOpenid());
|
|
|
|
|
if (StringUtils.isBlank(accessToken)) {
|
|
|
|
|
// 获取 access_token
|
|
|
|
|
String result = MiniProgramUtils.getAccessToken();
|
|
|
|
|
logger.info("请求微信服务器获取access_token返回结果:{}", result);
|
|
|
|
|
WxAccessTokenRet wxAccessTokenRet = JSON.parseObject(result, WxAccessTokenRet.class);
|
|
|
|
|
if (wxAccessTokenRet.getErrcode() != null) {
|
|
|
|
|
logger.error("获取access_token 失败: {}", wxAccessTokenRet.getErrmsg());
|
|
|
|
|
throw new ServiceException("获取用户手机号码失败");
|
|
|
|
|
}
|
|
|
|
|
// 保存缓存
|
|
|
|
|
redisService.setCacheObject(CacheConstants.WX_ACCESS_TOKEN_CACHE + SecurityUtils.getLoginUser().getWxUserMember().getOpenid(),
|
|
|
|
|
wxAccessTokenRet.getAccess_token(), CacheConstants.EXPIRATION, TimeUnit.MINUTES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取手机号
|
|
|
|
|
String result = MiniProgramUtils.getPhone(code, accessToken);
|
|
|
|
|
logger.info("请求微信服务器获取手机号码返回结果:{}", result);
|
|
|
|
|
WxCode2PhoneRet wxCode2PhoneRet = JSON.parseObject(result, WxCode2PhoneRet.class);
|
|
|
|
|
if (wxCode2PhoneRet.getErrcode() != null) {
|
|
|
|
|
logger.error("获取用户手机号码失败: ", wxCode2PhoneRet.getErrmsg());
|
|
|
|
|
logger.error("获取用户手机号码失败: {}", wxCode2PhoneRet.getErrmsg());
|
|
|
|
|
throw new ServiceException("获取用户手机号码失败");
|
|
|
|
|
}
|
|
|
|
|
wxUserMemberMapper.upgradeMember(wxCode2PhoneRet.getPhoneInfo().getPhoneNumber(), SecurityUtils.getWxUserId().intValue());
|
|
|
|
|
|