在数据库中建立t_user表
在idea中建立jianyue-api项目
- 添加pom依赖
com.soft1721
jianyue-api
0.0.1-SNAPSHOT
jianyue-api
JianYue API project
UTF-8
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
5.1.39
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
org.projectlombok
lombok
1.18.6
true
com.spring4all
swagger-spring-boot-starter
1.8.0.RELEASE
org.springframework.boot
spring-boot-maven-plugin
- 配置文件
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_jianyue?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#指定实体类映射的包
mybatis.type-aliases-package=com.spring.jianyueapi.entity
#swagger配置
swagger.enabled=true
swagger.title=jianyue api project
swagger.description=Starter for swagger 2.x
swagger.license=Apache License, Version 2.0
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
swagger.contact.name=wjl
swagger.contact.url=https://www.jianshu.com/u/a32873d23481
[email protected]
swagger.base-package=com.spring.jianyueapi.controller
swagger.base-path=/**
swagger.exclude-path=/error, /ops/**
- User.java实体类
@Data
public class User {
private Integer id;
private String mobile;
private String password;
private String nickname;
private String token;
private Short status;
private Date regtime;
private String avatar;
}
- UserDTO数据传输对象类
@Data
public class UserDTO {
private String mobile;
private String password;
}
-
Util工具包
public class MsgConst {
public static final String SUCCESS="请求成功";
public static final String USER_ID_NOT_FOUND="id不存在";
public static final String USER_MOBILE_NOT_FOUND="手机号不存在";
public static final String PASSWORD_ERROR="密码错误";
public static final String USER_STATUS_ERROR="账号错误";
public static final String MOBILE_EXIST = "手机号已被注册";
public static final String VERIFYCODE_ERROR = "验证码错误";
}
@Data
public class ResponseResult {
private int code;
private String msg;
private Object data;
public static ResponseResult error(int code,String msg){
ResponseResult responseResult=new ResponseResult();
responseResult.setCode(code);
responseResult.setMsg(msg);
return responseResult;
}
public static ResponseResult success(){
ResponseResult responseResult=new ResponseResult();
responseResult.setCode(StatusConst.SUCCESS);
responseResult.setMsg(MsgConst.SUCCESS);
return responseResult;
}
public static ResponseResult success(Object data){
ResponseResult responseResult=new ResponseResult();
responseResult.setCode(StatusConst.SUCCESS);
responseResult.setMsg(MsgConst.SUCCESS);
responseResult.setData(data);
return responseResult;
}
}
public class StatusConst {
public static final int SUCCESS=0;
public static final int USER_ID_NOT_FOUND=1;
public static final int USER_MOBILE_NOT_FOUND=2;
public static final int PASSWORD_ERROR=3;
public static final int USER_STATUS_ERROR=4;
public static final int MOBILE_EXIST = 5;
public static final int VERIFYCODE_ERROR = 6;
}
public class StringUtil {
public static String getDateString(Date date){
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
public static String getBase64Encoder(String srcString){
String resultstr="";
try {
resultstr= Base64.getEncoder().encodeToString(srcString.getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return resultstr;
}
public static String getBase64Decoder(String srcString){
byte[] bytes=Base64.getDecoder().decode(srcString);
return new String(bytes);
}
public static String getUUIDString(){
return UUID.randomUUID().toString();
}
public static String getVerifyCode(){
Random random=new Random();
StringBuilder stringBuilder=new StringBuilder();
for(int i=0;i<6;i++){
stringBuilder.append(String.valueOf(random.nextInt(10)));
}
return stringBuilder.toString();
}
}
- UserMapper接口
public interface UserMapper {
@Results({
@Result(property = "id",column = "id"),
@Result(property = "mobile",column = "mobile"),
@Result(property = "password",column = "password"),
@Result(property = "nickname",column = "nickname"),
@Result(property = "avatar",column = "avatar"),
@Result(property = "regtime",column = "regtime"),
@Result(property = "token",column = "token")
})
@Select("SELECT * FROM t_user WHERE mobile = #{mobile} ")
User getUserByMobile(String mobile);
@Results({
@Result(property = "id",column = "id"),
@Result(property = "mobile",column = "mobile"),
@Result(property = "password",column = "password"),
@Result(property = "nickname",column = "nickname"),
@Result(property = "avatar",column = "avatar"),
@Result(property = "regtime",column = "regtime"),
@Result(property = "token",column = "token")
})
@Select("SELECT * FROM t_user WHERE id = #{id} ")
User getUserById(Integer id);
@Update("UPDATE t_user SET password= #{password},nickname= #{nickname},avatar= #{avatar},status= #{status},token= #{token} WHERE id= #{id} ")
void update(User user);
@Insert("INSERT INTO t_user(mobile,password,nickname,avatar,regtime,status) VALUES(#{mobile},#{password},#{nickname},#{avatar},#{regtime},#{status})")
void insert(User user);
}
- UserService接口
public interface UserService {
User getUserByMobile(String mobile);
int signIn(UserDTO userDTO);
User getUserById(Integer id);
void update(User user);
/**
* 用户注册方法
* @param userDTO
* @return int
*/
void signUp(UserDTO userDTO);
}
- Service实现类
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public User getUserByMobile(String mobile) {
return userMapper.getUserByMobile(mobile);
}
@Override
public int signIn(UserDTO userDTO) {
User user=userMapper.getUserByMobile(userDTO.getMobile());
//手机号存在
if(user !=null){
//密码正确
if(userDTO.getPassword().equals(user.getPassword())){
//账号正常
if(user.getStatus()==1){
user.setToken(StringUtil.getUUIDString());
userMapper.update(user);
return StatusConst.SUCCESS;
}else{
//账号异常
return StatusConst.USER_STATUS_ERROR;
}
}else{
//密码错误
return StatusConst.PASSWORD_ERROR;
}
}else{
//手机号不存在
return StatusConst.USER_MOBILE_NOT_FOUND;
}
}
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
@Override
public void update(User user) {
userMapper.update(user);
}
@Override
public void signUp(UserDTO userDTO) {
User user1=new User();
user1.setMobile(userDTO.getMobile());
user1.setPassword(StringUtil.getBase64Encoder(userDTO.getPassword()));
user1.setNickname("新用户");
user1.setAvatar("http://ppeto2k90.bkt.clouddn.com/avatar/default.png");
user1.setRegtime(new Date());
user1.setStatus((short)1);
userMapper.insert(user1);
}
}
- 单元测试类
package com.spring.jianyueapi.service.impl;
import com.spring.jianyueapi.entity.User;
import com.spring.jianyueapi.entity.dto.UserDTO;
import com.spring.jianyueapi.service.UserService;
import com.spring.jianyueapi.util.StatusConst;
import com.spring.jianyueapi.util.StringUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import javax.swing.*;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceImplTest {
@Resource
private UserService userService;
@Test
public void getUserByMobile() {
User user=userService.getUserByMobile("18852009609");
System.out.println(user);
}
@Test
public void getUSerById(){
User user=userService.getUserById(1);
System.out.println(user);
}
@Test
public void signIn() {
UserDTO loginUser=new UserDTO();
loginUser.setMobile("18852009609");
String base64pass= StringUtil.getBase64Encoder("1234");
loginUser.setPassword(base64pass);
int status=userService.signIn(loginUser);
assertEquals(StatusConst.SUCCESS,status);
}
@Test
public void signUp() {
UserDTO userDTO = new UserDTO();
userDTO.setMobile("139****1489");
userDTO.setPassword("1112");
userService.signUp(userDTO);
}
}
- UserController
@RestController
@RequestMapping(value = "/api/user")
public class UserController {
@Resource
private UserService userService;
@Resource
private RedisService redisService;
@PostMapping(value = "/sign_in")
public ResponseResult signIn(@RequestBody UserDTO userDTO){
System.out.println(userDTO);
User user=userService.getUserByMobile(userDTO.getMobile());
if (user==null){
return ResponseResult.error(StatusConst.USER_MOBILE_NOT_FOUND, MsgConst.USER_MOBILE_NOT_FOUND);
}else{
userDTO.setPassword(StringUtil.getBase64Encoder(userDTO.getPassword()));
int status=userService.signIn(userDTO);
if(status==StatusConst.SUCCESS){
return ResponseResult.success(user);
}else if(status==StatusConst.PASSWORD_ERROR){
return ResponseResult.error(status,MsgConst.PASSWORD_ERROR);
}else {
return ResponseResult.error(status,MsgConst.USER_STATUS_ERROR);
}
}
}
- config下跨域配置
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
//跨域配置
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
//重写父类提供的跨域请求处理的接口
public void addCorsMappings(CorsRegistry registry) {
//添加映射路径
registry.addMapping("/**")
//放行哪些原始域
.allowedOrigins("*")
//是否发送Cookie信息
.allowCredentials(true)
//放行哪些原始域(请求方式)
.allowedMethods("GET", "POST", "PUT", "DELETE")
//放行哪些原始域(头部信息)
.allowedHeaders("*")
//暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
.exposedHeaders("Header1", "Header2");
}
};
}
}
- 启动主类
@SpringBootApplication
@MapperScan("com.spring.jianyueapi.mapper")
@EnableSwagger2Doc
public class JianyueApiApplication {
public static void main(String[] args) {
SpringApplication.run(JianyueApiApplication.class, args);
}
}
- swagger测试(显示200通过)
前端
- signin.vue
账号密码登录
手机验证码登录
登录遇到问题?
社交账号直接登录
{{mean.name}}
登录代表已经同意用户协议 和隐私政策
手机验证码登录
账号密码登录
登录遇到问题?
社交账号直接登录
{{mean.name}}
未注册的手机号登录时将自动注册,且代表已经同意用户协议 和隐私政策
- setting.vue
消息推送
{{item.name}}
{{item.status}}
通用设置
编辑个人资料
{{item.name}}
{{item.status}}
移动网络下加载图片
其他
{{other.name}}
{{other.status}}