From 1567bffb81f2af0a3bfa504c6d44125e607abb69 Mon Sep 17 00:00:00 2001 From: heminjian502 Date: Wed, 10 Aug 2022 15:23:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=BB=E7=94=9F=E5=BE=AE=E4=BF=A1=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/WechatLoginController.java | 13 +- .../ruoyi/common/utils/wechat/WechatKeys.java | 2 + .../system/domain/req/LoginForAppReq.java | 87 +++++++++++++ .../system/domain/resp/GetOpenidResp.java | 10 ++ .../system/mapper/TWechatUserMapper.java | 4 + .../ruoyi/system/service/IWechatService.java | 5 + .../service/impl/WechatServiceImpl.java | 120 +++++++++++++++++- .../mapper/system/TWechatUserMapper.xml | 13 ++ 8 files changed, 251 insertions(+), 3 deletions(-) create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/req/LoginForAppReq.java diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/WechatLoginController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/WechatLoginController.java index 01c5fee..17caa19 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/WechatLoginController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/WechatLoginController.java @@ -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); } } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/wechat/WechatKeys.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/wechat/WechatKeys.java index c87541d..616943b 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/wechat/WechatKeys.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/wechat/WechatKeys.java @@ -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"; } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/LoginForAppReq.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/LoginForAppReq.java new file mode 100644 index 0000000..fbe7a3d --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/LoginForAppReq.java @@ -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; + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/resp/GetOpenidResp.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/resp/GetOpenidResp.java index f21eb35..697e5ab 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/resp/GetOpenidResp.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/resp/GetOpenidResp.java @@ -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; + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TWechatUserMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TWechatUserMapper.java index 3864636..7778d30 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TWechatUserMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TWechatUserMapper.java @@ -12,9 +12,13 @@ public interface TWechatUserMapper extends Mapper { TWechatUser selectWechatUserByOpenid(String openid); + TWechatUser selectWechatUserByUnionid(String unionid); + int updateByPrimaryKeySelective(TWechatUser record); int updateByPrimaryKey(TWechatUser record); int updateWechatUserByOpenId(TWechatUser tWechatUser); + + int updateWechatUserByUnionId(TWechatUser tWechatUser); } \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IWechatService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IWechatService.java index 966ddd2..3d7fd89 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/IWechatService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IWechatService.java @@ -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); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java index 0a3b626..b0e8674 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java @@ -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; + } } diff --git a/ruoyi-system/src/main/resources/mapper/system/TWechatUserMapper.xml b/ruoyi-system/src/main/resources/mapper/system/TWechatUserMapper.xml index 9c741c1..82fea85 100644 --- a/ruoyi-system/src/main/resources/mapper/system/TWechatUserMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/TWechatUserMapper.xml @@ -178,9 +178,22 @@ where openid = #{openid,jdbcType=VARCHAR} + + update t_wechat_user set user_id = #{userId,jdbcType=BIGINT} where openid = #{openid,jdbcType=VARCHAR} + + + update t_wechat_user + set user_id = #{userId,jdbcType=BIGINT} + where unionid = #{unionid,jdbcType=VARCHAR} + \ No newline at end of file