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 987c582..7a37238 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,5 +1,6 @@ package com.ruoyi.web.controller.system; +import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.system.domain.req.GetOpenidReq; import com.ruoyi.system.domain.req.LoginForMpReq; @@ -15,6 +16,8 @@ import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; /** * 微信登录相关接口 @@ -43,6 +46,8 @@ public class WechatLoginController { @PostMapping("/for/mp") public AjaxResult loginForMp(@RequestBody @Validated LoginForMpReq req) { String token = iWechatService.loginForMp(req); - return AjaxResult.success(token); + Map map = new HashMap<>(); + map.put(Constants.TOKEN, token); + return AjaxResult.success(map); } } 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 new file mode 100644 index 0000000..cfed1c2 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/WechatRegisterController.java @@ -0,0 +1,37 @@ +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-common/src/main/java/com/ruoyi/common/utils/Phone.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/Phone.java new file mode 100644 index 0000000..647ca80 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/Phone.java @@ -0,0 +1,23 @@ +package com.ruoyi.common.utils; + +import org.hibernate.validator.constraints.CompositionType; +import org.hibernate.validator.constraints.ConstraintComposition; + +import javax.validation.Constraint; +import javax.validation.Payload; +import javax.validation.ReportAsSingleViolation; +import javax.validation.constraints.Pattern; +import java.lang.annotation.*; + +@ConstraintComposition(CompositionType.OR) +@Pattern(regexp = "1[3|4|5|7|8][0-9]\\d{8}") +@Documented +@Constraint(validatedBy = {}) +@Target({ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR,ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +@ReportAsSingleViolation +public @interface Phone { + String message() default "手机号码校验错误"; + Class[] groups() default {}; + Class[] payload() default {}; +} 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 6706dd8..a340924 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 @@ -113,6 +113,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter .antMatchers("/*/api-docs").anonymous() .antMatchers("/druid/**").anonymous() .antMatchers("/wechat/login/**").anonymous() + .antMatchers("/wechat/register/**").anonymous() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated() .and() diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/LoginForMpReq.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/LoginForMpReq.java index f35a20e..3f9fda0 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/LoginForMpReq.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/LoginForMpReq.java @@ -1,5 +1,10 @@ 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 java.io.Serializable; @@ -10,25 +15,29 @@ public class LoginForMpReq implements Serializable { private String openId; @NotBlank + @Length(max = 10) private String name; @NotBlank + @Phone private String phone; @NotBlank private String sex; - /** - * 出生年月日 yyyy-MM-dd - */ - private String birth; + @Max(100) + @Min(1) + private Integer age; + @Max(300) private Integer height; + @Max(500) private Integer weight; private String marriage; + @Length(max = 30) private String disease; public String getName() { @@ -55,12 +64,12 @@ public class LoginForMpReq implements Serializable { this.sex = sex; } - public String getBirth() { - return birth; + public Integer getAge() { + return age; } - public void setBirth(String birth) { - this.birth = birth; + public void setAge(Integer age) { + this.age = age; } public Integer getHeight() { 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 new file mode 100644 index 0000000..94e52ba --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/WechatRegisterReq.java @@ -0,0 +1,9 @@ +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/TPatientMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TPatientMapper.java index 23d86c2..22fa56f 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TPatientMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/TPatientMapper.java @@ -10,7 +10,7 @@ import tk.mybatis.mapper.common.Mapper; * @author ruoyi * @date 2022-08-03 */ -public interface TPatientMapper extends Mapper +public interface TPatientMapper { /** * 查询患者信息 @@ -59,4 +59,6 @@ public interface TPatientMapper extends Mapper * @return 结果 */ public int deleteTPatientByIds(Long[] ids); + + public TPatient queryTPatient(TPatient tPatient); } 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 751de76..3864636 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 @@ -15,4 +15,6 @@ public interface TWechatUserMapper extends Mapper { int updateByPrimaryKeySelective(TWechatUser record); int updateByPrimaryKey(TWechatUser record); + + int updateWechatUserByOpenId(TWechatUser tWechatUser); } \ No newline at end of file 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 0e08733..0013435 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 @@ -21,13 +21,10 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; -import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.Date; -import java.util.List; import java.util.Objects; @Service @@ -77,16 +74,12 @@ public class WechatServiceImpl implements IWechatService { // 查询手机号是否存在患者信息 TPatient tPatient = new TPatient(); BeanUtils.copyProperties(req, tPatient); - Example examplePatient = new Example(TWechatUser.class); - Example.Criteria criteriaPatient = examplePatient.createCriteria(); - criteriaPatient.andEqualTo("phone", tPatient.getPhone()); - List tPatients = tPatientMapper.selectByExample(examplePatient); + TPatient currentPatient = tPatientMapper.queryTPatient(tPatient); TWechatUser wechatUser = new TWechatUser(); - if (!CollectionUtils.isEmpty(tPatients)) { + if (Objects.nonNull(currentPatient)) { // 存在 需要把患者信息更新 并绑定openid - TPatient currentPatient = tPatients.get(0); BeanUtils.copyProperties(req, currentPatient); - tPatientMapper.updateByPrimaryKeySelective(currentPatient); + tPatientMapper.updateTPatient(currentPatient); wechatUser.setUserId(currentPatient.getId()); tPatient.setId(currentPatient.getId()); } else { @@ -99,10 +92,7 @@ public class WechatServiceImpl implements IWechatService { } // 绑定openid wechatUser.setOpenid(req.getOpenId()); - Example example = new Example(TWechatUser.class); - Example.Criteria criteria = example.createCriteria(); - criteria.andEqualTo("openid", wechatUser.getOpenid()); - tWechatUserMapper.updateByExampleSelective(wechatUser, example); + tWechatUserMapper.updateWechatUserByOpenId(wechatUser); // 登录获取token String token = buildTokenByPatient(tPatient); return token; diff --git a/ruoyi-system/src/main/resources/mapper/system/TPatientMapper.xml b/ruoyi-system/src/main/resources/mapper/system/TPatientMapper.xml index e36eaea..598cf21 100644 --- a/ruoyi-system/src/main/resources/mapper/system/TPatientMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/TPatientMapper.xml @@ -110,4 +110,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{id} + + \ No newline at end of file diff --git a/ruoyi-system/src/main/resources/mapper/system/TWechatUserMapper.xml b/ruoyi-system/src/main/resources/mapper/system/TWechatUserMapper.xml index 6a0a4c5..9c741c1 100644 --- a/ruoyi-system/src/main/resources/mapper/system/TWechatUserMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/TWechatUserMapper.xml @@ -177,4 +177,10 @@ from t_wechat_user where openid = #{openid,jdbcType=VARCHAR} + + + update t_wechat_user + set user_id = #{userId,jdbcType=BIGINT} + where openid = #{openid,jdbcType=VARCHAR} + \ No newline at end of file