From 769719240318b50901acb52a9d6a687eb63280ed Mon Sep 17 00:00:00 2001 From: heminjian502 Date: Mon, 8 Aug 2022 16:01:36 +0800 Subject: [PATCH] =?UTF-8?q?app=E7=99=BB=E5=BD=95=E3=80=81=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/SysLoginController.java | 8 ++ .../system/SysRegisterController.java | 8 ++ .../system/WechatLoginController.java | 3 +- .../system/WechatRegisterController.java | 37 -------- .../src/main/resources/application-druid.yml | 2 +- .../src/main/resources/application.yml | 2 +- .../framework/config/SecurityConfig.java | 5 +- .../web/service/SysLoginService.java | 63 +++++++++++++- .../web/service/SysRegisterService.java | 69 +++++++++++++++ .../system/domain/req/AppRegisterReq.java | 85 +++++++++++++++++++ .../system/domain/req/WechatRegisterReq.java | 9 -- .../ruoyi/system/mapper/SysUserMapper.java | 2 + .../ruoyi/system/mapper/TDoctorMapper.java | 4 + .../ruoyi/system/service/ISysUserService.java | 2 + .../service/impl/SysUserServiceImpl.java | 5 ++ .../service/impl/WechatServiceImpl.java | 2 + .../resources/mapper/system/SysUserMapper.xml | 6 ++ .../resources/mapper/system/TDoctorMapper.xml | 10 +++ 18 files changed, 266 insertions(+), 56 deletions(-) delete mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/WechatRegisterController.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/req/AppRegisterReq.java delete mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/req/WechatRegisterReq.java diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java index d959a17..3aa2ee9 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java @@ -51,6 +51,14 @@ public class SysLoginController return ajax; } + @PostMapping("/login/app") + public AjaxResult loginForApp(@RequestBody LoginBody loginBody) { + AjaxResult ajax = AjaxResult.success(); + String token = loginService.loginForApp(loginBody.getUsername(), loginBody.getPassword()); + ajax.put(Constants.TOKEN, token); + return ajax; + } + /** * 获取用户信息 * diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRegisterController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRegisterController.java index fe19249..5af8fa3 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRegisterController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRegisterController.java @@ -1,6 +1,8 @@ package com.ruoyi.web.controller.system; +import com.ruoyi.system.domain.req.AppRegisterReq; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @@ -35,4 +37,10 @@ public class SysRegisterController extends BaseController String msg = registerService.register(user); return StringUtils.isEmpty(msg) ? success() : error(msg); } + + @PostMapping("/register/app") + public AjaxResult registerForApp(@RequestBody @Validated AppRegisterReq req) { + registerService.registerForApp(req); + return AjaxResult.success(); + } } 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 7a37238..694e4b0 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 @@ -1,6 +1,7 @@ package com.ruoyi.web.controller.system; 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.LoginForMpReq; @@ -24,7 +25,7 @@ import java.util.Map; */ @RestController @RequestMapping("/wechat/login") -public class WechatLoginController { +public class WechatLoginController extends BaseController { private static final Logger logger = LoggerFactory.getLogger(WechatLoginController.class); diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/WechatRegisterController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/WechatRegisterController.java deleted file mode 100644 index cfed1c2..0000000 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/WechatRegisterController.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.ruoyi.web.controller.system; - -import com.ruoyi.common.core.domain.AjaxResult; -import com.ruoyi.system.domain.req.WechatRegisterReq; -import com.ruoyi.system.service.IWechatService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import javax.annotation.Resource; - -/** - * 微信登录相关接口 - */ -@RestController -@RequestMapping("/wechat/register") -public class WechatRegisterController { - - private static final Logger logger = LoggerFactory.getLogger(WechatRegisterController.class); - - @Resource - private IWechatService iWechatService; - - /** - * @param - * @return - */ - @PostMapping("/for/app") - public AjaxResult wechatRegister(@RequestBody WechatRegisterReq req) { - logger.info("wechat register, req = [{}]", req); - - return AjaxResult.success(); - } -} diff --git a/ruoyi-admin/src/main/resources/application-druid.yml b/ruoyi-admin/src/main/resources/application-druid.yml index b1bbcea..18b78db 100644 --- a/ruoyi-admin/src/main/resources/application-druid.yml +++ b/ruoyi-admin/src/main/resources/application-druid.yml @@ -6,7 +6,7 @@ spring: druid: # 主库数据源 master: - url: jdbc:mysql://192.168.31.187:3306/tongue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + url: jdbc:mysql://192.168.20.89:3306/tongue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: root # 从库数据源 diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index c4085c5..805e540 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -62,7 +62,7 @@ spring: # redis 配置 redis: # 地址 - host: 192.168.31.187 + host: 192.168.20.89 # 端口,默认为6379 port: 6379 # 数据库索引 diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index a340924..c8d2e4a 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -97,7 +97,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter // 过滤请求 .authorizeRequests() // 对于登录login 注册register 验证码captchaImage 允许匿名访问 - .antMatchers("/login", "/register", "/captchaImage").anonymous() + .antMatchers("/login", "/login/app", "/register", "/register/app", "/captchaImage").anonymous() .antMatchers( HttpMethod.GET, "/", @@ -112,8 +112,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter .antMatchers("/webjars/**").anonymous() .antMatchers("/*/api-docs").anonymous() .antMatchers("/druid/**").anonymous() - .antMatchers("/wechat/login/**").anonymous() - .antMatchers("/wechat/register/**").anonymous() + .antMatchers("/wechat/login/get/openid","/wechat/login/for/mp").anonymous() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated() .and() diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index e9e051b..7bcd82a 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -1,6 +1,14 @@ package com.ruoyi.framework.web.service; import javax.annotation.Resource; + +import com.ruoyi.common.enums.UserStatus; +import com.ruoyi.common.utils.*; +import com.ruoyi.system.domain.TDoctor; +import com.ruoyi.system.mapper.TDoctorMapper; +import com.ruoyi.system.service.impl.UserTokenService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; @@ -15,16 +23,14 @@ import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.exception.user.CaptchaException; import com.ruoyi.common.exception.user.CaptchaExpireException; import com.ruoyi.common.exception.user.UserPasswordNotMatchException; -import com.ruoyi.common.utils.DateUtils; -import com.ruoyi.common.utils.MessageUtils; -import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.ip.IpUtils; import com.ruoyi.framework.manager.AsyncManager; import com.ruoyi.framework.manager.factory.AsyncFactory; import com.ruoyi.system.service.ISysConfigService; import com.ruoyi.system.service.ISysUserService; +import java.util.Objects; + /** * 登录校验方法 * @@ -33,6 +39,7 @@ import com.ruoyi.system.service.ISysUserService; @Component public class SysLoginService { + private static final Logger log = LoggerFactory.getLogger(SysLoginService.class); @Autowired private TokenService tokenService; @@ -48,6 +55,12 @@ public class SysLoginService @Autowired private ISysConfigService configService; + @Resource + private TDoctorMapper tDoctorMapper; + + @Resource + private UserTokenService userTokenService; + /** * 登录验证 * @@ -93,6 +106,48 @@ public class SysLoginService return tokenService.createToken(loginUser); } + public String loginForApp(String username, String password) { + SysUser sysUser = checkLoginUser(username, password); + String token = buildTokenByDoctor(sysUser); + return token; + } + + private String buildTokenByDoctor(SysUser sysUser) { + TDoctor tDoctor = tDoctorMapper.selectTDoctorByUserId(sysUser.getUserId()); + if (Objects.isNull(tDoctor)) { + log.info("登录用户:{} 不存在.", sysUser.getUserName()); + throw new ServiceException("登录用户:" + sysUser.getUserName() + " 不存在,请先注册!"); + } + LoginUser loginUser = new LoginUser(); + loginUser.setUserId(sysUser.getUserId()); + loginUser.setUser(sysUser); + String token = userTokenService.createToken(loginUser); + return token; + } + + private SysUser checkLoginUser(String username, String password) { + SysUser user = userService.selectUserByUserName(username); + if (StringUtils.isNull(user)) + { + log.info("登录用户:{} 不存在.", username); + throw new ServiceException("登录用户:" + username + " 不存在,请先注册!"); + } + else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) + { + log.info("登录用户:{} 已被删除.", username); + throw new ServiceException("对不起,您的账号:" + username + " 已被删除"); + } + else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) + { + log.info("登录用户:{} 已被停用.", username); + throw new ServiceException("对不起,您的账号:" + username + " 已停用"); + } else if (!SecurityUtils.matchesPassword(user.getPassword(), password)) { + log.info("登录用户:{} 密码不正确.", username); + throw new ServiceException("登录失败!"); + } + return user; + } + /** * 校验验证码 * diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysRegisterService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysRegisterService.java index 8cc5ced..16a0e48 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysRegisterService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysRegisterService.java @@ -1,5 +1,10 @@ package com.ruoyi.framework.web.service; +import com.ruoyi.common.exception.ServiceException; +import com.ruoyi.system.domain.TDoctor; +import com.ruoyi.system.domain.req.AppRegisterReq; +import com.ruoyi.system.mapper.TDoctorMapper; +import com.ruoyi.system.service.ITDoctorService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.ruoyi.common.constant.Constants; @@ -17,6 +22,9 @@ import com.ruoyi.framework.manager.factory.AsyncFactory; import com.ruoyi.system.service.ISysConfigService; import com.ruoyi.system.service.ISysUserService; +import java.util.Date; +import java.util.Objects; + /** * 注册校验方法 * @@ -34,6 +42,9 @@ public class SysRegisterService @Autowired private RedisCache redisCache; + @Autowired + private TDoctorMapper tDoctorMapper; + /** * 注册 */ @@ -112,4 +123,62 @@ public class SysRegisterService throw new CaptchaException(); } } + + public void registerForApp(AppRegisterReq req) { + // 查询sys_user数据 + SysUser sysUser = userService.selectUserByPhone(req.getPhone()); + if (Objects.nonNull(sysUser)) { + if ("2".equals(sysUser.getDelFlag())) { + // 医生账号被删除 重新注册 + sysUser.setDelFlag("0"); + sysUser.setPassword(SecurityUtils.encryptPassword(req.getPassword())); + sysUser.setUserName(req.getPhone()); + sysUser.setNickName(req.getName()); + sysUser.setStatus("0"); + userService.updateUser(sysUser); + } else { + throw new ServiceException("该手机号已注册。"); + } + } else { + // 注册 + sysUser = new SysUser(); + sysUser.setPhonenumber(req.getPhone()); + sysUser.setUserName(req.getPhone()); + sysUser.setNickName(req.getName()); + sysUser.setStatus("0"); + sysUser.setDelFlag("0"); + sysUser.setPassword(SecurityUtils.encryptPassword(req.getPassword())); + userService.insertUser(sysUser); + } + // 查询医生表数据 + TDoctor tDoctor = tDoctorMapper.selectTDoctorByPhone(req.getPhone()); + if (Objects.nonNull(tDoctor)) { + if ("2".equals(tDoctor.getDelFlag())) { + // 重新注册 + tDoctor.setDelFlag("0"); + tDoctor.setUpdateTime(new Date()); + tDoctor.setHospitalId(req.getHospitalId()); + tDoctor.setName(req.getName()); + tDoctor.setSpeciality(req.getSpeciality()); + tDoctor.setMedicalLicense(req.getMedicalLicense()); + tDoctor.setUserId(sysUser.getUserId()); + tDoctorMapper.updateTDoctor(tDoctor); + } else { + throw new ServiceException("该手机号已注册。"); + } + } else { + // 注册 + tDoctor = new TDoctor(); + tDoctor.setDelFlag("0"); + tDoctor.setPhone(req.getPhone()); + tDoctor.setUpdateTime(new Date()); + tDoctor.setCreateTime(new Date()); + tDoctor.setHospitalId(req.getHospitalId()); + tDoctor.setName(req.getName()); + tDoctor.setSpeciality(req.getSpeciality()); + tDoctor.setMedicalLicense(req.getMedicalLicense()); + tDoctor.setUserId(sysUser.getUserId()); + tDoctorMapper.insertTDoctor(tDoctor); + } + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/AppRegisterReq.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/AppRegisterReq.java new file mode 100644 index 0000000..b8d8a03 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/AppRegisterReq.java @@ -0,0 +1,85 @@ +package com.ruoyi.system.domain.req; + +import com.ruoyi.common.utils.Phone; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +public class AppRegisterReq implements Serializable { + private static final long serialVersionUID = -2536864616757279758L; + + @NotBlank + @Length(max = 10) + private String name; + + @Phone + @NotBlank + private String phone; + + @NotBlank + private String password; + + @NotNull + private Long hospitalId; + + /** + * 专长 + */ + @Length(max = 30) + private String speciality; + + /** + * 医生执照 + */ + private String medicalLicense; + + 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 String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + 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/req/WechatRegisterReq.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/WechatRegisterReq.java deleted file mode 100644 index 94e52ba..0000000 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/WechatRegisterReq.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.ruoyi.system.domain.req; - -import java.io.Serializable; - -public class WechatRegisterReq implements Serializable { - private static final long serialVersionUID = -2536864616757279758L; - - -} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java index b42ac52..ed48e27 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java @@ -43,6 +43,8 @@ public interface SysUserMapper */ public SysUser selectUserByUserName(String userName); + public SysUser selectUserByPhone(String phonenumber); + /** * 通过用户ID查询用户 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TDoctorMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TDoctorMapper.java index 46204ca..6978432 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TDoctorMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TDoctorMapper.java @@ -22,6 +22,10 @@ public interface TDoctorMapper */ public TDoctor selectTDoctorById(Long id); + public TDoctor selectTDoctorByPhone(String phone); + + public TDoctor selectTDoctorByUserId(Long userId); + /** * 查询医生信息列表 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java index 59a41a1..7aa5cb4 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java @@ -42,6 +42,8 @@ public interface ISysUserService */ public SysUser selectUserByUserName(String userName); + public SysUser selectUserByPhone(String phone); + /** * 通过用户ID查询用户 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java index 52f9cf9..6b2b051 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java @@ -112,6 +112,11 @@ public class SysUserServiceImpl implements ISysUserService return userMapper.selectUserByUserName(userName); } + @Override + public SysUser selectUserByPhone(String phone) { + return userMapper.selectUserByPhone(phone); + } + /** * 通过用户ID查询用户 * 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 0013435..0a3b626 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 @@ -79,6 +79,7 @@ public class WechatServiceImpl implements IWechatService { if (Objects.nonNull(currentPatient)) { // 存在 需要把患者信息更新 并绑定openid BeanUtils.copyProperties(req, currentPatient); + currentPatient.setDelFlag("0"); tPatientMapper.updateTPatient(currentPatient); wechatUser.setUserId(currentPatient.getId()); tPatient.setId(currentPatient.getId()); @@ -139,6 +140,7 @@ public class WechatServiceImpl implements IWechatService { loginUser.setUserId(tPatient.getId()); SysUser user = new SysUser(); user.setUserId(tPatient.getId()); + user.setUserName(tPatient.getName()); user.setDelFlag(tPatient.getDelFlag()); user.setSex(tPatient.getSex()); user.setPhonenumber(tPatient.getPhone()); diff --git a/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml index 02f3a7b..8484189 100644 --- a/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml @@ -124,6 +124,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where u.user_name = #{userName} + + + + + + insert into t_doctor