Merge remote-tracking branch 'origin/master'

master
chenfei 2 years ago
commit 8172a37a3c

@ -4,6 +4,7 @@ import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
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.service.IWechatService;
@ -53,6 +54,16 @@ public class WechatLoginController extends BaseController {
@PostMapping("/get/unionid")
public AjaxResult getWechatUnionInfo(@RequestBody @Validated GetOpenidReq getOpenidReq, HttpServletRequest request) {
return AjaxResult.success();
logger.info("wechat获取unionid, req = [{}]", getOpenidReq);
GetOpenidResp resp = iWechatService.getUnionid(getOpenidReq, request);
return AjaxResult.success(resp);
}
@PostMapping("/for/app")
public AjaxResult loginForApp(@RequestBody @Validated LoginForAppReq req) {
String token = iWechatService.loginForApp(req);
Map map = new HashMap<>();
map.put(Constants.TOKEN, token);
return AjaxResult.success(map);
}
}

@ -6,4 +6,6 @@ public class WechatKeys {
public static String MP_ACCESS_TOKEN_REDIS_KEY = "redis:mp:access:token:key";
public static String MP_CODE_TO_OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
public static String WECHAT_CODE_TO_UNIONID_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
}

@ -0,0 +1,87 @@
package com.ruoyi.system.domain.req;
import com.ruoyi.common.utils.Phone;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
public class LoginForAppReq implements Serializable {
private static final long serialVersionUID = 3175019113468466769L;
@NotBlank
private String unionid;
@NotBlank
@Length(max = 10)
private String name;
@NotBlank
@Phone
private String phone;
@NotNull
private Long hospitalId;
/**
*
*/
@Length(max = 30)
private String speciality;
/**
*
*/
private String medicalLicense;
public String getUnionid() {
return unionid;
}
public void setUnionid(String unionid) {
this.unionid = unionid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Long getHospitalId() {
return hospitalId;
}
public void setHospitalId(Long hospitalId) {
this.hospitalId = hospitalId;
}
public String getSpeciality() {
return speciality;
}
public void setSpeciality(String speciality) {
this.speciality = speciality;
}
public String getMedicalLicense() {
return medicalLicense;
}
public void setMedicalLicense(String medicalLicense) {
this.medicalLicense = medicalLicense;
}
}

@ -11,6 +11,8 @@ public class GetOpenidResp implements Serializable {
private String token;
private String unionid;
public String getOpenid() {
return openid;
}
@ -34,4 +36,12 @@ public class GetOpenidResp implements Serializable {
public void setToken(String token) {
this.token = token;
}
public String getUnionid() {
return unionid;
}
public void setUnionid(String unionid) {
this.unionid = unionid;
}
}

@ -12,9 +12,13 @@ public interface TWechatUserMapper extends Mapper<TWechatUser> {
TWechatUser selectWechatUserByOpenid(String openid);
TWechatUser selectWechatUserByUnionid(String unionid);
int updateByPrimaryKeySelective(TWechatUser record);
int updateByPrimaryKey(TWechatUser record);
int updateWechatUserByOpenId(TWechatUser tWechatUser);
int updateWechatUserByUnionId(TWechatUser tWechatUser);
}

@ -1,6 +1,7 @@
package com.ruoyi.system.service;
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;
@ -10,5 +11,9 @@ public interface IWechatService {
GetOpenidResp getOpenid(GetOpenidReq req, HttpServletRequest request);
GetOpenidResp getUnionid(GetOpenidReq req, HttpServletRequest request);
String loginForMp(LoginForMpReq req);
String loginForApp(LoginForAppReq req);
}

@ -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;
}
}

@ -178,9 +178,22 @@
where openid = #{openid,jdbcType=VARCHAR}
</select>
<select id="selectWechatUserByUnionid" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from t_wechat_user
where unionid = #{unionid,jdbcType=VARCHAR}
</select>
<update id="updateWechatUserByOpenId" parameterType="com.ruoyi.system.domain.TWechatUser">
update t_wechat_user
set user_id = #{userId,jdbcType=BIGINT}
where openid = #{openid,jdbcType=VARCHAR}
</update>
<update id="updateWechatUserByUnionId" parameterType="com.ruoyi.system.domain.TWechatUser">
update t_wechat_user
set user_id = #{userId,jdbcType=BIGINT}
where unionid = #{unionid,jdbcType=VARCHAR}
</update>
</mapper>
Loading…
Cancel
Save