parent
f1d8da75b9
commit
9c94beecff
25
pom.xml
25
pom.xml
|
|
@ -30,6 +30,31 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.26</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-core</artifactId>
|
||||
<version>3.5.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.42.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
package com.gao.finalhw;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.gao.finalhw.mapper")
|
||||
public class FinalApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,29 @@
|
|||
package com.gao.finalhw.controller;
|
||||
|
||||
import com.gao.finalhw.model.User;
|
||||
import com.gao.finalhw.pojo.LoginRequest;
|
||||
import com.gao.finalhw.response.ServerResponseEntity;
|
||||
import com.gao.finalhw.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@PostMapping("/login")
|
||||
public ServerResponseEntity<User> login(@RequestBody LoginRequest loginRequest) {
|
||||
User user = userService.getUserByUsernameAndPassword(loginRequest.getUsername(), loginRequest.getPassword());
|
||||
if (user != null) {
|
||||
// 隐藏密码
|
||||
user.setPassword("null");
|
||||
// 如果验证成功
|
||||
return ServerResponseEntity.success("login success", user);
|
||||
} else {
|
||||
// 如果验证失败
|
||||
return ServerResponseEntity.fail("username or password error.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.gao.finalhw.mapper;
|
|||
|
||||
import com.gao.finalhw.model.Address;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author wangsiyuan
|
||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2024-01-18 15:21:27
|
||||
* @Entity com.gao.finalhw.model.Address
|
||||
*/
|
||||
@Mapper
|
||||
public interface AddressMapper extends BaseMapper<Address> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.gao.finalhw.mapper;
|
|||
|
||||
import com.gao.finalhw.model.Orders;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author wangsiyuan
|
||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2024-01-18 15:21:27
|
||||
* @Entity com.gao.finalhw.model.Orders
|
||||
*/
|
||||
@Mapper
|
||||
public interface OrdersMapper extends BaseMapper<Orders> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.gao.finalhw.mapper;
|
|||
|
||||
import com.gao.finalhw.model.Ring;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author wangsiyuan
|
||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2024-01-18 15:21:27
|
||||
* @Entity com.gao.finalhw.model.Ring
|
||||
*/
|
||||
@Mapper
|
||||
public interface RingMapper extends BaseMapper<Ring> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.gao.finalhw.mapper;
|
|||
|
||||
import com.gao.finalhw.model.ShoppingCart;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author wangsiyuan
|
||||
|
|
@ -9,6 +10,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2024-01-18 15:21:27
|
||||
* @Entity com.gao.finalhw.model.ShoppingCart
|
||||
*/
|
||||
|
||||
@Mapper
|
||||
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.gao.finalhw.mapper;
|
|||
|
||||
import com.gao.finalhw.model.UserInfo;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author wangsiyuan
|
||||
|
|
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2024-01-18 15:21:27
|
||||
* @Entity com.gao.finalhw.model.UserInfo
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserInfoMapper extends BaseMapper<UserInfo> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ package com.gao.finalhw.mapper;
|
|||
|
||||
import com.gao.finalhw.model.User;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
/**
|
||||
* @author wangsiyuan
|
||||
|
|
@ -9,8 +11,11 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
* @createDate 2024-01-18 15:21:27
|
||||
* @Entity com.gao.finalhw.model.User
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
|
||||
@Select("SELECT * FROM user WHERE user_name = #{username} AND password = #{password}")
|
||||
User findByUsernameAndPassword(String username, String password);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
package com.gao.finalhw.pojo;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class LoginRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package com.gao.finalhw.response;
|
||||
|
||||
public enum ResponseEnum {
|
||||
OK("0","ok"),
|
||||
SHOW_FAIL("-1", "fail");
|
||||
private final String code;
|
||||
private final String msg;
|
||||
|
||||
public String getMsg() {return msg;}
|
||||
|
||||
public String value() {return code;}
|
||||
|
||||
public String getCode(){return code;}
|
||||
|
||||
ResponseEnum(String code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ResponseEnum{" + "code='" + code + '\'' + ", msg='" + msg + '\'' + "} " + super.toString();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,142 @@
|
|||
package com.gao.finalhw.response;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import java.io.Serializable;
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ServerResponseEntity <T> implements Serializable {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ServerResponseEntity.class);
|
||||
/** Response status code. */
|
||||
private String code;
|
||||
|
||||
/** Response message. */
|
||||
private String msg;
|
||||
|
||||
/** Response timestamp. */
|
||||
private Long timestamp;
|
||||
|
||||
/** Response data. */
|
||||
private T data;
|
||||
|
||||
public String getMsg(){return msg;}
|
||||
public void setMsg(String msg){this.msg = msg;}
|
||||
public String getCode(){return code;}
|
||||
public void setCode(String code){this.code = code;}
|
||||
public T getData() {return data;}
|
||||
|
||||
public Long getTimestamp() {return timestamp;}
|
||||
public void setTimestamp(Long timestamp) {this.timestamp = timestamp;
|
||||
}
|
||||
public ServerResponseEntity<T> setData(T data) {
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
/** Timestamp when the response is created. Defaults to current time. */
|
||||
@JsonIgnore
|
||||
public boolean isSuccess(){return Objects.equals(ResponseEnum.OK.value(),this.code);}
|
||||
@JsonIgnore
|
||||
public boolean isFail(){return Objects.equals(ResponseEnum.SHOW_FAIL.value(),this.code);}
|
||||
/**
|
||||
* Creates a success response with data.
|
||||
*
|
||||
* @param data the response data
|
||||
* @return the API response entity
|
||||
*/
|
||||
public static <T> ServerResponseEntity<T> success(T data){
|
||||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
serverResponseEntity.setData(data);
|
||||
serverResponseEntity.setCode(ResponseEnum.OK.value());
|
||||
serverResponseEntity.setMsg(ResponseEnum.OK.getMsg());
|
||||
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
|
||||
return serverResponseEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a success response without data.
|
||||
*
|
||||
* @return the API response entity
|
||||
*/
|
||||
public static <T> ServerResponseEntity<T> success(){
|
||||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
serverResponseEntity.setCode(ResponseEnum.OK.value());
|
||||
serverResponseEntity.setMsg(ResponseEnum.OK.getMsg());
|
||||
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
|
||||
serverResponseEntity.setData(null);
|
||||
return serverResponseEntity;
|
||||
}
|
||||
|
||||
public static <T> ServerResponseEntity<T> success(Integer code, T data) {
|
||||
return success(String.valueOf(code), data);
|
||||
}
|
||||
|
||||
public static <T> ServerResponseEntity<T> success(String code, T data) {
|
||||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
serverResponseEntity.setCode(code);
|
||||
serverResponseEntity.setData(data);
|
||||
serverResponseEntity.getMsg();
|
||||
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
|
||||
return serverResponseEntity;
|
||||
}
|
||||
|
||||
public static <T> ServerResponseEntity<T> showFailMsg(String msg) {
|
||||
logger.error(msg);
|
||||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
serverResponseEntity.setMsg(msg);
|
||||
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
|
||||
serverResponseEntity.setCode(ResponseEnum.SHOW_FAIL.value());
|
||||
return serverResponseEntity;
|
||||
}
|
||||
|
||||
public static <T> ServerResponseEntity<T> fail(ResponseEnum responseEnum) {
|
||||
logger.error(responseEnum.toString());
|
||||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
serverResponseEntity.setMsg(responseEnum.getMsg());
|
||||
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
|
||||
serverResponseEntity.setCode(responseEnum.value());
|
||||
return serverResponseEntity;
|
||||
}
|
||||
|
||||
public static <T> ServerResponseEntity<T> fail(ResponseEnum responseEnum, T data) {
|
||||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
serverResponseEntity.setMsg(responseEnum.getMsg());
|
||||
serverResponseEntity.setCode(responseEnum.value());
|
||||
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
|
||||
serverResponseEntity.setData(data);
|
||||
return serverResponseEntity;
|
||||
}
|
||||
|
||||
public static <T> ServerResponseEntity<T> fail(Integer code, String msg, T data) {
|
||||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
serverResponseEntity.setMsg(msg);
|
||||
serverResponseEntity.setCode(String.valueOf(code));
|
||||
serverResponseEntity.setData(data);
|
||||
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
|
||||
return serverResponseEntity;
|
||||
}
|
||||
public static <T> ServerResponseEntity<T> fail(Integer code, String msg) {
|
||||
return fail(code, msg, null);
|
||||
}
|
||||
public static <T> ServerResponseEntity<T> fail(String msg) {
|
||||
return fail(-1,msg,null);
|
||||
}
|
||||
public static <T> ServerResponseEntity<T> fail(Integer code, T data) {
|
||||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
serverResponseEntity.setCode(String.valueOf(code));
|
||||
serverResponseEntity.setData(data);
|
||||
serverResponseEntity.setMsg("error");
|
||||
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
|
||||
return serverResponseEntity;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ApiResponseEntity{" +
|
||||
"code='" + code + '\'' +
|
||||
", msg='" + msg + '\'' +
|
||||
", timestamp=" + timestamp +
|
||||
", data=" + data +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package com.gao.finalhw.service;
|
||||
|
||||
import com.gao.finalhw.mapper.UserMapper;
|
||||
import com.gao.finalhw.model.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
public User getUserByUsernameAndPassword(String username, String password) {
|
||||
return userMapper.findByUsernameAndPassword(username, password);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
server.address=0.0.0.0
|
||||
server.port=8090
|
||||
debug=true
|
||||
debug=false
|
||||
|
|
@ -110,30 +110,41 @@
|
|||
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
$('#login span').eq(0).click(function() {
|
||||
location.href = "/reg"
|
||||
});
|
||||
|
||||
$('#login span').eq(1).click(function() {
|
||||
$('.ts_wrong').remove();
|
||||
$('.the_input').removeClass('error');
|
||||
if($('#txtName').val() == '') {
|
||||
var email = $('#txtName').val();
|
||||
var password = $('#txtPwd').val();
|
||||
|
||||
if(email == '') {
|
||||
var notice = '<div class="ts_wrong"><span id="wrong">请输入邮箱/手机号码!</span></div>';
|
||||
$('#txtName').parent().after(notice);
|
||||
$('#txtName').parent().addClass('error');
|
||||
return false;
|
||||
}
|
||||
|
||||
if($('#txtPwd').val() == '') {
|
||||
if(password == '') {
|
||||
var notice = '<div class="ts_wrong"><span id="wrong">请输入密码!</span></div>';
|
||||
$('#txtPwd').parent().after(notice);
|
||||
$('#txtPwd').parent().addClass('error');
|
||||
return false;
|
||||
}
|
||||
|
||||
$('form').submit();
|
||||
// 使用 AJAX 发送 JSON 数据
|
||||
$.ajax({
|
||||
url: '/login',
|
||||
type: 'POST',
|
||||
contentType: 'application/json',
|
||||
data: JSON.stringify({email: email, password: password}),
|
||||
success: function(response) {
|
||||
// 处理响应
|
||||
console.log(response);
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
// 处理错误
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
$(document).keyup(function(event) {
|
||||
|
|
@ -141,7 +152,9 @@
|
|||
$('#login span').eq(1).click();
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue