|
|
|
|
@ -1,27 +1,176 @@
|
|
|
|
|
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.R;
|
|
|
|
|
import com.flossom.common.core.exception.ServiceException;
|
|
|
|
|
import com.flossom.common.core.web.domain.AjaxResult;
|
|
|
|
|
import com.flossom.common.redis.service.RedisService;
|
|
|
|
|
import com.flossom.common.security.utils.SecurityUtils;
|
|
|
|
|
import com.flossom.hzMapper.domain.WxUserMember;
|
|
|
|
|
import com.flossom.hzMapper.mapper.WxUserMemberMapper;
|
|
|
|
|
import com.flossom.miniProgram.domain.vo.LoginUserVo;
|
|
|
|
|
import com.flossom.miniProgram.domain.vo.WxCode2PhoneRet;
|
|
|
|
|
import com.flossom.miniProgram.domain.vo.WxCode2SessionRet;
|
|
|
|
|
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 com.flossom.system.api.domain.LoginBody;
|
|
|
|
|
import com.flossom.system.api.domain.RegisterBody;
|
|
|
|
|
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;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class WxUserMemberServiceImpl implements IWxUserMemberService {
|
|
|
|
|
|
|
|
|
|
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private WxUserMemberMapper wxUserMemberMapper;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RemoteAuthService remoteAuthService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RemoteUserService remoteUserService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisService redisService;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public WxUserMember obtainUserMemberInfo(Long userNo) {
|
|
|
|
|
public LoginUserVo obtainUserMemberInfo(Long userNo) {
|
|
|
|
|
WxUserMember wxUserMember = new WxUserMember();
|
|
|
|
|
if (userNo != null) {
|
|
|
|
|
// 根据 userNo 查询用户信息
|
|
|
|
|
wxUserMember = wxUserMemberMapper.selectWxUserMemberById(userNo);
|
|
|
|
|
}
|
|
|
|
|
if (userNo == null || wxUserMember == null) {
|
|
|
|
|
// 未注册用户,就注册游客账户
|
|
|
|
|
// 微信用户表注册
|
|
|
|
|
wxUserMemberMapper.insertTouristUser(wxUserMember);
|
|
|
|
|
// 系统表注册
|
|
|
|
|
RegisterBody registerBody = new RegisterBody();
|
|
|
|
|
registerBody.setUsername(UserConstants.WX_SYSTEM_USER_USERNAME + wxUserMember.getId());
|
|
|
|
|
registerBody.setPassword(UserConstants.WX_SYSTEM_USER_PASSWORD);
|
|
|
|
|
R<?> registerResult = remoteAuthService.register(registerBody);
|
|
|
|
|
if (R.FAIL == registerResult.getCode()) {
|
|
|
|
|
throw new ServiceException(registerResult.getMsg());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取token
|
|
|
|
|
LoginBody loginBody = new LoginBody();
|
|
|
|
|
loginBody.setUsername(UserConstants.WX_SYSTEM_USER_USERNAME + wxUserMember.getId());
|
|
|
|
|
loginBody.setPassword(UserConstants.WX_SYSTEM_USER_PASSWORD);
|
|
|
|
|
R<Map<String, String>> loginResult = remoteAuthService.login(loginBody);
|
|
|
|
|
if (loginResult.getCode() == R.FAIL) {
|
|
|
|
|
throw new ServiceException(loginResult.getMsg());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoginUserVo loginUserVo = new LoginUserVo();
|
|
|
|
|
BeanUtils.copyProperties(wxUserMember, loginUserVo);
|
|
|
|
|
loginUserVo.setAccessToken(loginResult.getData().get("access_token"));
|
|
|
|
|
loginUserVo.setExpiresIn(loginResult.getData().get("expires_in"));
|
|
|
|
|
return loginUserVo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public LoginUserVo login(String code, String rawData, String signature) throws Exception {
|
|
|
|
|
// 1、使用临时凭证 code 获取 appi + appsecret + code
|
|
|
|
|
WxCode2SessionRet wxCode2SessionRet = JSON.parseObject(MiniProgramUtils.getSessionKeyAndOpenId(code), WxCode2SessionRet.class);
|
|
|
|
|
if (200 != wxCode2SessionRet.getErrcode()) {
|
|
|
|
|
logger.error(wxCode2SessionRet.getErrmsg());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// 2、校验签名 小程序发送的签名signature与服务器端生成的签名signature2 = sha1(rawData + sessionKey)
|
|
|
|
|
String signature2 = DigestUtils.sha1Hex(rawData + wxCode2SessionRet.getSession_key());
|
|
|
|
|
if (!signature.equals(signature2)) {
|
|
|
|
|
logger.error("签名校验失败:code = {}", code);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3、根据 openid 查询用户信息
|
|
|
|
|
WxUserMember wxUserMember = wxUserMemberMapper.selectWxUserMemberByOpenId(wxCode2SessionRet.getOpenid());
|
|
|
|
|
if (wxUserMember == null) {
|
|
|
|
|
// 未注册用户,就注册游客账户
|
|
|
|
|
|
|
|
|
|
// 2.1、封装微信用户信息
|
|
|
|
|
/**
|
|
|
|
|
* rawData:
|
|
|
|
|
* {
|
|
|
|
|
* "nickName": "Band",
|
|
|
|
|
* "avatarUrl": "http://wx.qlogo.cn/mmopen/vi_32/1vZvI39NWFQ9XM4LtQpFrQJ1xlgZxx3w7bQxKARol6503Iuswjjn6nIGBiaycAjAtpujxyzYsrztuuICqIM5ibXQ/0"
|
|
|
|
|
* "gender": 1,//不再返回
|
|
|
|
|
* "language": "zh_CN", //强制返回 “zh_CN”
|
|
|
|
|
* "city": "Guangzhou", //不再返回
|
|
|
|
|
* "province": "Guangdong", //不再返回
|
|
|
|
|
* "country": "CN", //不再返回
|
|
|
|
|
* }
|
|
|
|
|
*/
|
|
|
|
|
JSONObject rawDataJson = JSON.parseObject(rawData);
|
|
|
|
|
wxUserMember.setNickname(rawDataJson.getString("nickName"));
|
|
|
|
|
wxUserMember.setHeadimg(rawDataJson.getString("avatarUrl"));
|
|
|
|
|
wxUserMember.setOpenid(wxCode2SessionRet.getOpenid());
|
|
|
|
|
wxUserMember.setUnionid(wxCode2SessionRet.getUnionid());
|
|
|
|
|
/**
|
|
|
|
|
* TODO: 暂时将session_key保存在redis,无失效期,后期根据业务修改
|
|
|
|
|
*/
|
|
|
|
|
redisService.setCacheObject(CacheConstants.wx_session_key_cache + wxUserMember.getId(), wxCode2SessionRet.getSession_key());
|
|
|
|
|
// 3.2、微信用户表注册
|
|
|
|
|
wxUserMemberMapper.insertWxUserMember(wxUserMember);
|
|
|
|
|
// 3.3、系统表注册
|
|
|
|
|
RegisterBody registerBody = new RegisterBody();
|
|
|
|
|
registerBody.setUsername(UserConstants.WX_SYSTEM_USER_USERNAME + wxUserMember.getId());
|
|
|
|
|
registerBody.setPassword(UserConstants.WX_SYSTEM_USER_PASSWORD);
|
|
|
|
|
R<?> registerResult = remoteAuthService.register(registerBody);
|
|
|
|
|
if (R.FAIL == registerResult.getCode()) {
|
|
|
|
|
throw new ServiceException(registerResult.getMsg());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4、获取 token
|
|
|
|
|
LoginBody loginBody = new LoginBody();
|
|
|
|
|
loginBody.setUsername(UserConstants.WX_SYSTEM_USER_USERNAME + wxUserMember.getId());
|
|
|
|
|
loginBody.setPassword(UserConstants.WX_SYSTEM_USER_PASSWORD);
|
|
|
|
|
R<Map<String, String>> loginResult = remoteAuthService.login(loginBody);
|
|
|
|
|
if (loginResult.getCode() == R.FAIL) {
|
|
|
|
|
throw new ServiceException(loginResult.getMsg());
|
|
|
|
|
}
|
|
|
|
|
return wxUserMember;
|
|
|
|
|
|
|
|
|
|
// 5、返回登录信息
|
|
|
|
|
LoginUserVo loginUserVo = new LoginUserVo();
|
|
|
|
|
BeanUtils.copyProperties(wxUserMember, loginUserVo);
|
|
|
|
|
loginUserVo.setAccessToken(loginResult.getData().get("access_token"));
|
|
|
|
|
loginUserVo.setExpiresIn(loginResult.getData().get("expires_in"));
|
|
|
|
|
return loginUserVo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public AjaxResult upgradeMember(String code) throws Exception {
|
|
|
|
|
WxCode2PhoneRet wxCode2PhoneRet = JSON.parseObject(MiniProgramUtils.getPhone(code), WxCode2PhoneRet.class);
|
|
|
|
|
if (wxCode2PhoneRet.getErrcode() != 200) {
|
|
|
|
|
logger.error("用户手机号码获取失败");
|
|
|
|
|
return AjaxResult.error("手机号获取失败");
|
|
|
|
|
}
|
|
|
|
|
String username = SecurityUtils.getLoginUser().getUsername();
|
|
|
|
|
if (StringUtils.isBlank(username)) {
|
|
|
|
|
logger.error("获取用户信息失败");
|
|
|
|
|
return AjaxResult.error("获取用户信息失败");
|
|
|
|
|
}
|
|
|
|
|
String id = username.replace(UserConstants.WX_SYSTEM_USER_USERNAME, "");
|
|
|
|
|
wxUserMemberMapper.upgradeMember(wxCode2PhoneRet.getPhoneInfo().getPhoneNumber(), id);
|
|
|
|
|
return AjaxResult.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|