diff --git a/Others/sql/flashsale.sql b/Others/sql/flashsale.sql
index 54c9eff..93e7641 100644
--- a/Others/sql/flashsale.sql
+++ b/Others/sql/flashsale.sql
@@ -11,7 +11,7 @@
Target Server Version : 50726
File Encoding : 65001
- Date: 03/03/2022 23:08:26
+ Date: 04/03/2022 17:15:21
*/
SET NAMES utf8mb4;
@@ -90,7 +90,7 @@ CREATE TABLE `order_info` (
`id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`user_id` int(11) NOT NULL DEFAULT 0,
`item_id` int(11) NOT NULL,
- `item_pirce` decimal(10, 2) NOT NULL,
+ `item_price` decimal(10, 2) NOT NULL,
`amount` int(255) NOT NULL,
`order_price` decimal(10, 2) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
diff --git a/frontend/getitem.html b/frontend/getitem.html
index 8dcebd0..45397bd 100644
--- a/frontend/getitem.html
+++ b/frontend/getitem.html
@@ -90,6 +90,7 @@
$("#createorder").on("click", function () {
$.ajax({
type: "POST",
+ contentType: "application/x-www-form-urlencoded",
url: "http://localhost:8090/order/createorder",
data: {
"itemId": g_itemVO.id,
@@ -98,9 +99,12 @@
xhrFields: {withCredentials: true},
success: function (data) {
if (data.status == "success") {
- // TODO
+ alert("下单成功");
} else {
alert("下单失败,原因为" + data.data.errMsg);
+ if (data.data.errCode == "20003") {
+ window.location.href = "login.html";
+ }
}
},
error: function (data) {
diff --git a/frontend/login.html b/frontend/login.html
index 6a8535b..b1e48df 100644
--- a/frontend/login.html
+++ b/frontend/login.html
@@ -58,6 +58,7 @@
success: function (data) {
if (data.status == "success") {
alert("登录成功");
+ window.location.href = "listitem.html";
} else {
alert("登录失败,原因为" + data.data.errMsg);
}
diff --git a/src/main/java/com/cxyxiaomo/flashsale/controller/OrderController.java b/src/main/java/com/cxyxiaomo/flashsale/controller/OrderController.java
index a883d23..6b6b529 100644
--- a/src/main/java/com/cxyxiaomo/flashsale/controller/OrderController.java
+++ b/src/main/java/com/cxyxiaomo/flashsale/controller/OrderController.java
@@ -1,14 +1,16 @@
package com.cxyxiaomo.flashsale.controller;
import com.cxyxiaomo.flashsale.error.BusinessException;
+import com.cxyxiaomo.flashsale.error.EmBusinessError;
import com.cxyxiaomo.flashsale.response.CommonReturnType;
import com.cxyxiaomo.flashsale.service.OrderService;
import com.cxyxiaomo.flashsale.service.model.OrderModel;
+import com.cxyxiaomo.flashsale.service.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletRequest;
@Controller("/order")
@RequestMapping("/order")
@@ -18,10 +20,23 @@ public class OrderController extends BaseController {
@Autowired
private OrderService orderService;
+ @Autowired
+ private HttpServletRequest httpServletRequest;
+
// 封装下单请求
+ @RequestMapping(value = "/createorder", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
+ @ResponseBody
public CommonReturnType createOrder(@RequestParam(name = "itemId") Integer itemId,
@RequestParam(name = "amount") Integer amount) throws BusinessException {
- OrderModel orderModel = orderService.createOrder(null, itemId, amount);
+ // 获取用户登陆状态
+ Boolean isLogin = (Boolean) httpServletRequest.getSession().getAttribute("IS_LOGIN");
+ if (isLogin == null || !isLogin.booleanValue()) {
+ throw new BusinessException(EmBusinessError.USER_NOT_LOGIN, "用户还未登录,不能下单");
+ }
+ // 获取用户的登录信息
+ UserModel userModel = (UserModel) httpServletRequest.getSession().getAttribute("LOGIN_USER");
+
+ OrderModel orderModel = orderService.createOrder(userModel.getId(), itemId, amount);
return CommonReturnType.create(null);
}
diff --git a/src/main/java/com/cxyxiaomo/flashsale/dataobject/OrderDO.java b/src/main/java/com/cxyxiaomo/flashsale/dataobject/OrderDO.java
index b5bdd9d..f85f7e9 100644
--- a/src/main/java/com/cxyxiaomo/flashsale/dataobject/OrderDO.java
+++ b/src/main/java/com/cxyxiaomo/flashsale/dataobject/OrderDO.java
@@ -33,11 +33,11 @@ public class OrderDO {
/**
*
* This field was generated by MyBatis Generator.
- * This field corresponds to the database column order_info.item_pirce
+ * This field corresponds to the database column order_info.item_price
*
* @mbg.generated Thu Mar 03 22:04:45 CST 2022
*/
- private BigDecimal itemPirce;
+ private BigDecimal itemPrice;
/**
*
@@ -131,26 +131,26 @@ public class OrderDO {
/**
* This method was generated by MyBatis Generator.
- * This method returns the value of the database column order_info.item_pirce
+ * This method returns the value of the database column order_info.item_price
*
- * @return the value of order_info.item_pirce
+ * @return the value of order_info.item_price
*
* @mbg.generated Thu Mar 03 22:04:45 CST 2022
*/
- public BigDecimal getItemPirce() {
- return itemPirce;
+ public BigDecimal getItemPrice() {
+ return itemPrice;
}
/**
* This method was generated by MyBatis Generator.
- * This method sets the value of the database column order_info.item_pirce
+ * This method sets the value of the database column order_info.item_price
*
- * @param itemPirce the value for order_info.item_pirce
+ * @param itemPrice the value for order_info.item_price
*
* @mbg.generated Thu Mar 03 22:04:45 CST 2022
*/
- public void setItemPirce(BigDecimal itemPirce) {
- this.itemPirce = itemPirce;
+ public void setItemPrice(BigDecimal itemPrice) {
+ this.itemPrice = itemPrice;
}
/**
diff --git a/src/main/java/com/cxyxiaomo/flashsale/error/EmBusinessError.java b/src/main/java/com/cxyxiaomo/flashsale/error/EmBusinessError.java
index 644d06c..a4fbdef 100644
--- a/src/main/java/com/cxyxiaomo/flashsale/error/EmBusinessError.java
+++ b/src/main/java/com/cxyxiaomo/flashsale/error/EmBusinessError.java
@@ -8,6 +8,7 @@ public enum EmBusinessError implements CommonError {
// 20000开头为用户信息相关错误定义
USER_NOT_EXIST(20001, "用户不存在"),
USER_LOGIN_FAILED(20002, "用户手机号或密码不正确"),
+ USER_NOT_LOGIN(20003, "用户还未登录"),
// 30000开头为交易信息错误定义
STOCK_NOT_ENOUGH(30001, "库存不足");
diff --git a/src/main/resources/mapping/OrderDOMapper.xml b/src/main/resources/mapping/OrderDOMapper.xml
index c607386..8a48298 100644
--- a/src/main/resources/mapping/OrderDOMapper.xml
+++ b/src/main/resources/mapping/OrderDOMapper.xml
@@ -10,7 +10,7 @@
-
+
@@ -20,7 +20,7 @@
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Mar 03 22:04:45 CST 2022.
-->
- id, user_id, item_id, item_pirce, amount, order_price
+ id, user_id, item_id, item_price, amount, order_price