|
|
|
@ -1,18 +1,23 @@
|
|
|
|
|
package com.ruoyi.system.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
|
import com.ruoyi.common.utils.http.HttpUtils;
|
|
|
|
|
import com.ruoyi.common.utils.ip.IpUtils;
|
|
|
|
|
import com.ruoyi.common.utils.wechat.WechatKeys;
|
|
|
|
|
import com.ruoyi.system.domain.TDoctor;
|
|
|
|
|
import com.ruoyi.system.domain.TPatient;
|
|
|
|
|
import com.ruoyi.system.domain.TWechatUser;
|
|
|
|
|
import com.ruoyi.system.domain.req.GetOpenidReq;
|
|
|
|
|
import com.ruoyi.system.domain.req.LoginForAppReq;
|
|
|
|
|
import com.ruoyi.system.domain.req.LoginForMpReq;
|
|
|
|
|
import com.ruoyi.system.domain.resp.GetOpenidResp;
|
|
|
|
|
import com.ruoyi.system.mapper.TDoctorMapper;
|
|
|
|
|
import com.ruoyi.system.mapper.TPatientMapper;
|
|
|
|
|
import com.ruoyi.system.mapper.TWechatUserMapper;
|
|
|
|
|
import com.ruoyi.system.service.IWechatService;
|
|
|
|
@ -41,12 +46,23 @@ public class WechatServiceImpl implements IWechatService {
|
|
|
|
|
@Value("${wx.mpSecret}")
|
|
|
|
|
private String appSecret;
|
|
|
|
|
|
|
|
|
|
@Value("${wx.wechatAppId}")
|
|
|
|
|
private String wechatAppId;
|
|
|
|
|
/**
|
|
|
|
|
* 微信小程序AppSecret
|
|
|
|
|
*/
|
|
|
|
|
@Value("${wx.wechatSecret}")
|
|
|
|
|
private String wechatSecret;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private TWechatUserMapper tWechatUserMapper;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private TPatientMapper tPatientMapper;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private TDoctorMapper tDoctorMapper;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private UserTokenService userTokenService;
|
|
|
|
|
|
|
|
|
@ -54,7 +70,7 @@ public class WechatServiceImpl implements IWechatService {
|
|
|
|
|
public GetOpenidResp getOpenid(GetOpenidReq req, HttpServletRequest request) {
|
|
|
|
|
//查询openid
|
|
|
|
|
String openid = queryOpenid(req);
|
|
|
|
|
TWechatUser wechatUser = updateWechatUser(openid, req, request);
|
|
|
|
|
TWechatUser wechatUser = updateWechatUserByOpenid(openid, req, request);
|
|
|
|
|
GetOpenidResp resp = new GetOpenidResp();
|
|
|
|
|
if (Objects.isNull(wechatUser.getUserId())) {
|
|
|
|
|
resp.setBindFlag(false);
|
|
|
|
@ -69,6 +85,25 @@ public class WechatServiceImpl implements IWechatService {
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public GetOpenidResp getUnionid(GetOpenidReq req, HttpServletRequest request) {
|
|
|
|
|
//查询unionid
|
|
|
|
|
String unionid = queryUnionid(req);
|
|
|
|
|
TWechatUser wechatUser = updateWechatUserByUnionid(unionid, req, request);
|
|
|
|
|
GetOpenidResp resp = new GetOpenidResp();
|
|
|
|
|
if (Objects.isNull(wechatUser.getUserId())) {
|
|
|
|
|
resp.setBindFlag(false);
|
|
|
|
|
} else {
|
|
|
|
|
// 已绑定信息 直接登录
|
|
|
|
|
resp.setBindFlag(true);
|
|
|
|
|
TDoctor tDoctor = tDoctorMapper.selectTDoctorById(wechatUser.getId());
|
|
|
|
|
String token = buildTokenByDoctor(tDoctor);
|
|
|
|
|
resp.setToken(token);
|
|
|
|
|
}
|
|
|
|
|
resp.setUnionid(unionid);
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String loginForMp(LoginForMpReq req) {
|
|
|
|
|
// 查询手机号是否存在患者信息
|
|
|
|
@ -99,6 +134,36 @@ public class WechatServiceImpl implements IWechatService {
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String loginForApp(LoginForAppReq req) {
|
|
|
|
|
// 查询手机号是否存在医生信息
|
|
|
|
|
TDoctor tDoctor = new TDoctor();
|
|
|
|
|
BeanUtils.copyProperties(req, tDoctor);
|
|
|
|
|
TDoctor currentDoctor = tDoctorMapper.selectTDoctorByPhone(req.getPhone());
|
|
|
|
|
TWechatUser wechatUser = new TWechatUser();
|
|
|
|
|
if (Objects.nonNull(currentDoctor)) {
|
|
|
|
|
// 存在 需要把患者信息更新 并绑定openid
|
|
|
|
|
BeanUtils.copyProperties(req, currentDoctor);
|
|
|
|
|
currentDoctor.setDelFlag("0");
|
|
|
|
|
tDoctorMapper.updateTDoctor(currentDoctor);
|
|
|
|
|
wechatUser.setUserId(currentDoctor.getId());
|
|
|
|
|
tDoctor.setId(currentDoctor.getId());
|
|
|
|
|
} else {
|
|
|
|
|
// 不存在 患者信息入库
|
|
|
|
|
tDoctor.setDelFlag("0");
|
|
|
|
|
tDoctor.setCreateTime(new Date());
|
|
|
|
|
tDoctor.setUpdateTime(new Date());
|
|
|
|
|
tDoctorMapper.insertTDoctor(tDoctor);
|
|
|
|
|
wechatUser.setUserId(tDoctor.getId());
|
|
|
|
|
}
|
|
|
|
|
// 绑定openid
|
|
|
|
|
wechatUser.setUnionid(req.getUnionid());
|
|
|
|
|
tWechatUserMapper.updateWechatUserByUnionId(wechatUser);
|
|
|
|
|
// 登录获取token
|
|
|
|
|
String token = buildTokenByDoctor(tDoctor);
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String queryOpenid(GetOpenidReq req){
|
|
|
|
|
String url = String.format(WechatKeys.MP_CODE_TO_OPENID_URL, appId, appSecret, req.getCode());
|
|
|
|
|
String json = HttpUtils.sendGet(url);
|
|
|
|
@ -112,7 +177,19 @@ public class WechatServiceImpl implements IWechatService {
|
|
|
|
|
return openid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TWechatUser updateWechatUser(String openid, GetOpenidReq req, HttpServletRequest request){
|
|
|
|
|
private String queryUnionid(GetOpenidReq req){
|
|
|
|
|
String url = String.format(WechatKeys.WECHAT_CODE_TO_UNIONID_URL, wechatAppId, wechatSecret, req.getCode());
|
|
|
|
|
String json = HttpUtils.sendGet(url);
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
|
|
String unionid = (String) jsonObject.get("unionid");
|
|
|
|
|
logger.info("login openid:{}", unionid);
|
|
|
|
|
if (StringUtils.isEmpty(unionid)) {
|
|
|
|
|
throw new ServiceException("未获取到unionid");
|
|
|
|
|
}
|
|
|
|
|
return unionid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TWechatUser updateWechatUserByOpenid(String openid, GetOpenidReq req, HttpServletRequest request){
|
|
|
|
|
//查询当前微信用户是否存在
|
|
|
|
|
TWechatUser wechatUser = tWechatUserMapper.selectWechatUserByOpenid(openid);
|
|
|
|
|
if (wechatUser == null){
|
|
|
|
@ -132,6 +209,26 @@ public class WechatServiceImpl implements IWechatService {
|
|
|
|
|
return wechatUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TWechatUser updateWechatUserByUnionid(String unionid, GetOpenidReq req, HttpServletRequest request){
|
|
|
|
|
//查询当前微信用户是否存在
|
|
|
|
|
TWechatUser wechatUser = tWechatUserMapper.selectWechatUserByUnionid(unionid);
|
|
|
|
|
if (wechatUser == null){
|
|
|
|
|
wechatUser = new TWechatUser();
|
|
|
|
|
wechatUser.setUnionid(unionid);
|
|
|
|
|
wechatUser.setAvatar(req.getAvatarUrl());
|
|
|
|
|
wechatUser.setNickname(req.getNickName());
|
|
|
|
|
wechatUser.setLastLoginTime(new Date());
|
|
|
|
|
wechatUser.setLastLoginIp(IpUtils.getIpAddr(request));
|
|
|
|
|
wechatUser.setStatus((byte) 0);
|
|
|
|
|
tWechatUserMapper.insert(wechatUser);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
wechatUser.setLastLoginTime(new Date());
|
|
|
|
|
wechatUser.setLastLoginIp(IpUtils.getIpAddr(request));
|
|
|
|
|
tWechatUserMapper.updateByPrimaryKeySelective(wechatUser);
|
|
|
|
|
return wechatUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String buildTokenByPatient(TPatient tPatient) {
|
|
|
|
|
if (Objects.isNull(tPatient)) {
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
@ -148,4 +245,23 @@ public class WechatServiceImpl implements IWechatService {
|
|
|
|
|
String token = userTokenService.createToken(loginUser);
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String buildTokenByDoctor(TDoctor tDoctor) {
|
|
|
|
|
if (Objects.isNull(tDoctor)) {
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
}
|
|
|
|
|
LoginUser loginUser = new LoginUser();
|
|
|
|
|
loginUser.setUserId(tDoctor.getUserId());
|
|
|
|
|
SysUser user = new SysUser();
|
|
|
|
|
user.setUserId(tDoctor.getUserId());
|
|
|
|
|
user.setUserName(tDoctor.getName());
|
|
|
|
|
user.setDelFlag(tDoctor.getDelFlag());
|
|
|
|
|
user.setSex(tDoctor.getSex());
|
|
|
|
|
user.setPhonenumber(tDoctor.getPhone());
|
|
|
|
|
user.setDoctorId(tDoctor.getId());
|
|
|
|
|
user.setHospitalId(tDoctor.getHospitalId());
|
|
|
|
|
loginUser.setUser(user);
|
|
|
|
|
String token = userTokenService.createToken(loginUser);
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|