app登录、注册接口

master
heminjian502 3 years ago
parent 496e6f6968
commit 7697192403

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

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

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

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

@ -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
# 从库数据源

@ -62,7 +62,7 @@ spring:
# redis 配置
redis:
# 地址
host: 192.168.31.187
host: 192.168.20.89
# 端口默认为6379
port: 6379
# 数据库索引

@ -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()

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

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

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

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

@ -43,6 +43,8 @@ public interface SysUserMapper
*/
public SysUser selectUserByUserName(String userName);
public SysUser selectUserByPhone(String phonenumber);
/**
* ID
*

@ -22,6 +22,10 @@ public interface TDoctorMapper
*/
public TDoctor selectTDoctorById(Long id);
public TDoctor selectTDoctorByPhone(String phone);
public TDoctor selectTDoctorByUserId(Long userId);
/**
*
*

@ -42,6 +42,8 @@ public interface ISysUserService
*/
public SysUser selectUserByUserName(String userName);
public SysUser selectUserByPhone(String phone);
/**
* ID
*

@ -112,6 +112,11 @@ public class SysUserServiceImpl implements ISysUserService
return userMapper.selectUserByUserName(userName);
}
@Override
public SysUser selectUserByPhone(String phone) {
return userMapper.selectUserByPhone(phone);
}
/**
* ID
*

@ -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());

@ -124,6 +124,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<include refid="selectUserVo"/>
where u.user_name = #{userName}
</select>
<select id="selectUserByPhone" parameterType="String" resultMap="SysUserResult">
<include refid="selectUserVo"/>
where u.phonenumber = #{phonenumber}
</select>
<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
<include refid="selectUserVo"/>
@ -190,6 +195,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="loginDate != null">login_date = #{loginDate},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
update_time = sysdate()
</set>
where user_id = #{userId}

@ -53,6 +53,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<include refid="selectTDoctorVo"/>
where id = #{id}
</select>
<select id="selectTDoctorByPhone" parameterType="java.lang.String" resultMap="TDoctorResult">
<include refid="selectTDoctorVo"/>
where phone = #{phone}
</select>
<select id="selectTDoctorByUserId" parameterType="Long" resultMap="TDoctorResult">
<include refid="selectTDoctorVo"/>
where user_id = #{userId}
</select>
<insert id="insertTDoctor" parameterType="TDoctor" useGeneratedKeys="true" keyProperty="id">
insert into t_doctor

Loading…
Cancel
Save