微信小程序登录接口

master
heminjian502 2 years ago
parent a7cee65d5e
commit 1b5b46a7e0

@ -1,5 +1,6 @@
package com.ruoyi.web.controller.system; package com.ruoyi.web.controller.system;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.req.GetOpenidReq; import com.ruoyi.system.domain.req.GetOpenidReq;
import com.ruoyi.system.domain.req.LoginForMpReq; import com.ruoyi.system.domain.req.LoginForMpReq;
@ -15,6 +16,8 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/** /**
* *
@ -43,6 +46,8 @@ public class WechatLoginController {
@PostMapping("/for/mp") @PostMapping("/for/mp")
public AjaxResult loginForMp(@RequestBody @Validated LoginForMpReq req) { public AjaxResult loginForMp(@RequestBody @Validated LoginForMpReq req) {
String token = iWechatService.loginForMp(req); String token = iWechatService.loginForMp(req);
return AjaxResult.success(token); Map map = new HashMap<>();
map.put(Constants.TOKEN, token);
return AjaxResult.success(map);
} }
} }

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

@ -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<? extends Payload>[] payload() default {};
}

@ -113,6 +113,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
.antMatchers("/*/api-docs").anonymous() .antMatchers("/*/api-docs").anonymous()
.antMatchers("/druid/**").anonymous() .antMatchers("/druid/**").anonymous()
.antMatchers("/wechat/login/**").anonymous() .antMatchers("/wechat/login/**").anonymous()
.antMatchers("/wechat/register/**").anonymous()
// 除上面外的所有请求全部需要鉴权认证 // 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated() .anyRequest().authenticated()
.and() .and()

@ -1,5 +1,10 @@
package com.ruoyi.system.domain.req; 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.NotBlank;
import java.io.Serializable; import java.io.Serializable;
@ -10,25 +15,29 @@ public class LoginForMpReq implements Serializable {
private String openId; private String openId;
@NotBlank @NotBlank
@Length(max = 10)
private String name; private String name;
@NotBlank @NotBlank
@Phone
private String phone; private String phone;
@NotBlank @NotBlank
private String sex; private String sex;
/** @Max(100)
* yyyy-MM-dd @Min(1)
*/ private Integer age;
private String birth;
@Max(300)
private Integer height; private Integer height;
@Max(500)
private Integer weight; private Integer weight;
private String marriage; private String marriage;
@Length(max = 30)
private String disease; private String disease;
public String getName() { public String getName() {
@ -55,12 +64,12 @@ public class LoginForMpReq implements Serializable {
this.sex = sex; this.sex = sex;
} }
public String getBirth() { public Integer getAge() {
return birth; return age;
} }
public void setBirth(String birth) { public void setAge(Integer age) {
this.birth = birth; this.age = age;
} }
public Integer getHeight() { public Integer getHeight() {

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

@ -10,7 +10,7 @@ import tk.mybatis.mapper.common.Mapper;
* @author ruoyi * @author ruoyi
* @date 2022-08-03 * @date 2022-08-03
*/ */
public interface TPatientMapper extends Mapper<TPatient> public interface TPatientMapper
{ {
/** /**
* *
@ -59,4 +59,6 @@ public interface TPatientMapper extends Mapper<TPatient>
* @return * @return
*/ */
public int deleteTPatientByIds(Long[] ids); public int deleteTPatientByIds(Long[] ids);
public TPatient queryTPatient(TPatient tPatient);
} }

@ -15,4 +15,6 @@ public interface TWechatUserMapper extends Mapper<TWechatUser> {
int updateByPrimaryKeySelective(TWechatUser record); int updateByPrimaryKeySelective(TWechatUser record);
int updateByPrimaryKey(TWechatUser record); int updateByPrimaryKey(TWechatUser record);
int updateWechatUserByOpenId(TWechatUser tWechatUser);
} }

@ -21,13 +21,10 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Objects; import java.util.Objects;
@Service @Service
@ -77,16 +74,12 @@ public class WechatServiceImpl implements IWechatService {
// 查询手机号是否存在患者信息 // 查询手机号是否存在患者信息
TPatient tPatient = new TPatient(); TPatient tPatient = new TPatient();
BeanUtils.copyProperties(req, tPatient); BeanUtils.copyProperties(req, tPatient);
Example examplePatient = new Example(TWechatUser.class); TPatient currentPatient = tPatientMapper.queryTPatient(tPatient);
Example.Criteria criteriaPatient = examplePatient.createCriteria();
criteriaPatient.andEqualTo("phone", tPatient.getPhone());
List<TPatient> tPatients = tPatientMapper.selectByExample(examplePatient);
TWechatUser wechatUser = new TWechatUser(); TWechatUser wechatUser = new TWechatUser();
if (!CollectionUtils.isEmpty(tPatients)) { if (Objects.nonNull(currentPatient)) {
// 存在 需要把患者信息更新 并绑定openid // 存在 需要把患者信息更新 并绑定openid
TPatient currentPatient = tPatients.get(0);
BeanUtils.copyProperties(req, currentPatient); BeanUtils.copyProperties(req, currentPatient);
tPatientMapper.updateByPrimaryKeySelective(currentPatient); tPatientMapper.updateTPatient(currentPatient);
wechatUser.setUserId(currentPatient.getId()); wechatUser.setUserId(currentPatient.getId());
tPatient.setId(currentPatient.getId()); tPatient.setId(currentPatient.getId());
} else { } else {
@ -99,10 +92,7 @@ public class WechatServiceImpl implements IWechatService {
} }
// 绑定openid // 绑定openid
wechatUser.setOpenid(req.getOpenId()); wechatUser.setOpenid(req.getOpenId());
Example example = new Example(TWechatUser.class); tWechatUserMapper.updateWechatUserByOpenId(wechatUser);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("openid", wechatUser.getOpenid());
tWechatUserMapper.updateByExampleSelective(wechatUser, example);
// 登录获取token // 登录获取token
String token = buildTokenByPatient(tPatient); String token = buildTokenByPatient(tPatient);
return token; return token;

@ -110,4 +110,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{id} #{id}
</foreach> </foreach>
</delete> </delete>
<select id="queryTPatient" parameterType="com.ruoyi.system.domain.TPatient" resultMap="TPatientResult">
<include refid="selectTPatientVo"/>
<where>
<if test="phone != null and phone != ''">
phone = #{phone}
</if>
</where>
</select>
</mapper> </mapper>

@ -177,4 +177,10 @@
from t_wechat_user from t_wechat_user
where openid = #{openid,jdbcType=VARCHAR} where openid = #{openid,jdbcType=VARCHAR}
</select> </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>
</mapper> </mapper>
Loading…
Cancel
Save