add:实现功能

master
msb_89821 2 years ago
parent a8deefce83
commit 2349cfa8bf

@ -4,10 +4,12 @@ import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.mashibing.dto.PassengerRequestDto;
import com.mashibing.dto.ResponseResult;
import com.mashibing.exception.BizExecption;
import com.mashibing.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import mashibing.online.text.consonant.PassengerConsonant;
import mashibing.online.text.dto.UserInfo;
import mashibing.online.text.feign.PassengerUserService;
import mashibing.online.text.feign.SendSMSCodeService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@ -29,6 +31,8 @@ public class PassengerController {
@Resource
private SendSMSCodeService sendSMSCodeService;
@Resource
private PassengerUserService passengerUserService;
@Resource
private StringRedisTemplate stringRedisTemplate;
@ -38,7 +42,7 @@ public class PassengerController {
public ResponseResult getPhoneNumberCode(@RequestBody PassengerRequestDto passengerRequestDto){
// 校验参数
if(ObjectUtil.isNull(passengerRequestDto)||ObjectUtil.isEmpty(passengerRequestDto.getPhoneNumber())){
throw new RuntimeException("手机号码必填");
return ResponseResult.fail(1005,"手机号码必填");
}
@ -58,23 +62,25 @@ public class PassengerController {
public ResponseResult checkPhoneNumberCode(@RequestBody PassengerRequestDto passengerRequestDto){
// 校验参数
if(ObjectUtil.isNull(passengerRequestDto)||ObjectUtil.isEmpty(passengerRequestDto.getPhoneNumber())||ObjectUtil.isEmpty(passengerRequestDto.getPhoneNumberCode())){
throw new RuntimeException("手机号码 和短信验证码必填");
//throw new BizExecption("手机号码 和短信验证码必填",1005,null);
// 抛出异常 会被 自定义的拦截器 拦截,拦截后返回的信息不正确!
return ResponseResult.fail(1005,"手机号码 和短信验证码必填");
}
// 校验验证码
String phone=passengerRequestDto.getPhoneNumber();
String redisCode = stringRedisTemplate.opsForValue().get(PassengerConsonant.phoneNumberCodePreKey + phone);
if(StringUtils.isBlank(redisCode)){
throw new RuntimeException("短信验证码已过期!");
return ResponseResult.fail(1003,"短信验证码已过期!");
}
String reqCode=passengerRequestDto.getPhoneNumberCode();
if(!reqCode.equals(redisCode)){
throw new RuntimeException("短信验证码错误!");
return ResponseResult.fail(1004,"短信验证码错误!");
}
// 用户注册
log.info("phone {} 短信验证码正确!",phone);
// 用户注册
passengerUserService.reginsterUserbyPhone(passengerRequestDto);
log.info("手机号码={} 完成注册!",phone);
// 返回token
String token = JwtUtils.genaroteToken(phone, PassengerConsonant.PassengerIdendity);
UserInfo userInfo =new UserInfo();

@ -0,0 +1,27 @@
package mashibing.online.text.feign;
import com.mashibing.dto.PassengerRequestDto;
import com.mashibing.dto.ResponseResult;
import mashibing.online.text.feign.fallback.PassengerUserServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/**
* Copyright© 2020.10.20 by .All rights reserved.
* 1.
* 2.
* 3.使.
*
* @packageName: mashibing.online.text.feign.fallback
* @description:
* @author: nod
* @create: 2022-10-09
*/
@FeignClient(name = "service-passenger-user",fallback= PassengerUserServiceImpl.class)
public interface PassengerUserService {
@PostMapping("/user/reginsterUserbyPhone")
public ResponseResult reginsterUserbyPhone(@RequestBody PassengerRequestDto passengerRequestDto);
}

@ -0,0 +1,33 @@
package mashibing.online.text.feign.fallback;
import com.mashibing.dto.PassengerRequestDto;
import com.mashibing.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import mashibing.online.text.feign.PassengerUserService;
import mashibing.online.text.feign.SendSMSCodeService;
import org.springframework.stereotype.Service;
/**
* Copyright© 2020.10.20 by .All rights reserved.
* 1.
* 2.
* 3.使.
* <b>Description:</b><br>
*
* @author nod
* <b>ProjectName:</b> onelinetextplublic
* <br><b>PackageName:</b> mashibing.online.text.feign.fallback
* <br><b>ClassName:</b>
* <br><b>Date:</b> 2022/10/9 18:32
*/
@Service
@Slf4j
public class PassengerUserServiceImpl implements PassengerUserService {
@Override
public ResponseResult reginsterUserbyPhone(PassengerRequestDto passengerRequestDto) {
log.error("注册用户失败");
return ResponseResult.fail();
}
}

@ -29,6 +29,6 @@ public class SendSMSCodeServiceImpl implements SendSMSCodeService {
public String getSmsCode(int size, String phoneNumber) {
log.info("获取短信验证码失败");
return "";//ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), "获取短信验证码失败");
return null;
}
}

@ -0,0 +1,38 @@
package mashibing.online.text.intercept;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Copyright© 2020.10.20 by .All rights reserved.
* 1.
* 2.
* 3.使.
* <b>Description:</b><br>
*
* @author nod
* <b>ProjectName:</b> onelinetextplublic
* <br><b>PackageName:</b> mashibing.online.text.intercept
* <br><b>ClassName:</b>
* <br><b>Date:</b> 2022/10/19 14:18
*/
@Configuration
public class InterceptorConfinger implements WebMvcConfigurer {
@Bean
public JwtIntercept jwtIntercept(){
return new JwtIntercept();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtIntercept()).addPathPatterns("/**")
.excludePathPatterns("/passenger/checkPhoneNumberCode")
.excludePathPatterns("/passenger/getPhoneNumberCode")
.excludePathPatterns("/test/noTokenTest");
}
}

@ -1,8 +1,12 @@
package mashibing.online.text.intercept;
import com.alibaba.fastjson.JSON;
import com.mashibing.constant.CommonStatusEnum;
import com.mashibing.dto.PassengerRequestDto;
import com.mashibing.dto.ResponseResult;
import com.mashibing.dto.TokenResult;
import com.mashibing.util.JwtUtils;
import javafx.print.Printer;
import lombok.extern.slf4j.Slf4j;
import mashibing.online.text.consonant.PassengerConsonant;
import org.apache.commons.lang3.StringUtils;
@ -12,6 +16,7 @@ import org.springframework.web.servlet.HandlerInterceptor;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
@Slf4j
public class JwtIntercept implements HandlerInterceptor {
@ -21,26 +26,43 @@ public class JwtIntercept implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("Authorization");
String message=null;
boolean result =true;
TokenResult tokenResult =null;
if(StringUtils.isBlank(token)){
PrintWriter out = response.getWriter();
out.println(JSON.toJSONString(ResponseResult.fail(99, "token is empty !")));
return false;
}
try{
tokenResult = JwtUtils.parseToken(token);
}catch (Exception e){
log.error("解析token异常 e",e.getCause());
log.error("解析token异常 e ={}",e.getCause());
message="parse token exception";
result =false;
}
if(!result){
}else {
if(result){
String phone=tokenResult.getPhoneNum();
String idendity=tokenResult.getIdendity();
//根据手机号 和身份类型 查询
String s = stringRedisTemplate.opsForValue().get(PassengerConsonant.phoneNumberTokenPreKey + phone + idendity);
if(StringUtils.isBlank(s)){
//token 无效
message="token not exist";
result =false;
}else{
// 比对token
if(!token.equals(s)){
message="token is inval";
result =false;
}
}
}
if(!result){
PrintWriter out = response.getWriter();
out.println(JSON.toJSONString(ResponseResult.fail(99, message)));
}

@ -11,6 +11,6 @@ spring:
name: send-server
xsxx:
url: http://api.wxxsxx.com
url: http://api.wxxsxx.com/
account: bzxxhyhy
password: 3Vf8S9

@ -25,6 +25,36 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>
3.3.2
</version>
</dependency>
<dependency>
<groupId>com.mashibing</groupId>
<artifactId>untitled4</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

@ -0,0 +1,64 @@
package com.example.servicepassengeruser.controller.pojo;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.servicepassengeruser.mapper.PassengerUserMapper;
import com.example.servicepassengeruser.pojo.PassengerUser;
import com.mashibing.dto.PassengerRequestDto;
import com.mashibing.dto.ResponseResult;
import com.mashibing.exception.BizExecption;
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;
import java.util.List;
import java.util.UUID;
/**
* Copyright© 2020.10.20 by .All rights reserved.
* 1.
* 2.
* 3.使.
* <b>Description:</b><br>
*
* @author nod
* <b>ProjectName:</b> onelinetextplublic
* <br><b>PackageName:</b> com.example.servicepassengeruser.controller.pojo
* <br><b>ClassName:</b>
* <br><b>Date:</b> 2022/10/19 14:31
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private PassengerUserMapper passengerUserMapper;
@PostMapping("/reginsterUserbyPhone")
public ResponseResult reginsterUserbyPhone(@RequestBody PassengerRequestDto passengerRequestDto){
if(ObjectUtil.isNull(passengerRequestDto) || ObjectUtil.isEmpty(passengerRequestDto.getPhoneNumber())){
throw new BizExecption("手机号不能为空",1003,null);
}
List<PassengerUser> passengerUsers = passengerUserMapper.selectList(new QueryWrapper<PassengerUser>().lambda()
.eq(PassengerUser::getPassengerPhone, passengerRequestDto.getPhoneNumber()));
if(ObjectUtil.isEmpty(passengerUsers)){
//没有就新增
PassengerUser user=new PassengerUser();
user.setPassengerPhone(passengerRequestDto.getPhoneNumber());
user.setState(true);
//user.setId(UUID.randomUUID().toString());
//user.setPassengerGender();
passengerUserMapper.insert(user);
}
return ResponseResult.success("成功");
}
}

@ -0,0 +1,12 @@
package com.example.servicepassengeruser.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.servicepassengeruser.pojo.PassengerUser;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PassengerUserMapper extends BaseMapper<PassengerUser> {
}

@ -0,0 +1,34 @@
package com.example.servicepassengeruser.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import java.util.Date;
@Data
@TableName("passenger_user")
public class PassengerUser extends Model<PassengerUser> {
@TableId(type = IdType.AUTO)
private Integer id;
private Date gmtCreate;
private Date gmtModified;
private String passengerPhone;
private String passengerName;
private Boolean passengerGender;
private Boolean state;
private String profilePhoto;
}

@ -4,3 +4,14 @@ server:
spring:
application:
name: service-passenger-user
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.2.23.46:3306/oneline-txt?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
username: root
password: 1qaz@WSX

@ -0,0 +1,37 @@
package com.mashibing.exception;
import lombok.Data;
import java.awt.dnd.DropTarget;
/**
* Copyright© 2020.10.20 by .All rights reserved.
* 1.
* 2.
* 3.使.
* <b>Description:</b><br>
*
* @author nod
* <b>ProjectName:</b> onelinetextplublic
* <br><b>PackageName:</b> com.mashibing.exception
* <br><b>ClassName:</b>
* <br><b>Date:</b> 2022/10/19 14:01
*/
@Data
public class BizExecption extends RuntimeException {
private Integer code;
private Object data;
public BizExecption(String message,Throwable throwable){
super(message,throwable);
}
public BizExecption(String message,Integer code,Object data){
super(message);
this.code = code;
this.data= data;
}
}
Loading…
Cancel
Save