Ver Fonte

数据校验

541469799@qq.com há 10 meses atrás
pai
commit
f31cdaaf33

+ 13 - 0
app/common.php

@@ -62,6 +62,19 @@ function throwError(string $message, ?int $status = null, array $data = [])
 }
 
 /**
+ * 输出错误信息
+ * @param string $message 报错信息
+ * @param int|null $status 状态码,默认为配置文件status.error
+ * @param array $data 附加数据
+ * @throws BaseException
+ */
+function throwErrorInfo(string $message, ?int $status = null, array $data = [])
+{
+    is_null($status) && $status = config('status.error');
+    return ['status' => $status, 'message' => $message];
+}
+
+/**
  * 下划线转驼峰
  * @param string $uncamelized_words
  * @param string $separator

+ 6 - 2
app/index/controller/Index.php

@@ -132,7 +132,7 @@ class Index extends Controller
      */
     public function productDetails()
     {
-        $goodsId = $this->request->param('goodsId', 0);
+        $goodsId = intval($this->request->param('goodsId', 0));
         if (empty($goodsId)) {
             return \redirect('index');
         }
@@ -140,7 +140,11 @@ class Index extends Controller
         $key = $this->request->param('key', '');
 
         $goodsModel = new GoodsModel();
-        $goods = $goodsModel->getDetails($goodsId)->toArray();
+        $goods = $goodsModel->getDetails($goodsId);
+        if (empty($goods) || $goods['status'] != 200){
+            return \view('/error');
+        }
+        $goods = $goods->toArray();
         $goods['content'] = html_entity_decode($goods['content']);
 
         $model = new CommentModel;

+ 5 - 2
app/index/model/Goods.php

@@ -112,6 +112,9 @@ class Goods extends GoodsModel
         $with = ['images.file', 'skuList.image', 'video', 'videoCover'];
         // 获取商品记录
         $goodsInfo = $this->getGoodsMain($goodsId, $with, $verifyStatus);
+        if(empty($goodsInfo['goods_id'])){
+            return $goodsInfo;
+        }
         // 商品规格列表
         $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
         return $goodsInfo->hidden(array_merge($this->hidden, ['images']));
@@ -164,11 +167,11 @@ class Goods extends GoodsModel
         $goodsInfo = static::detail($goodsId, $with);
         // 判断商品是否存在
         if (empty($goodsInfo) || $goodsInfo['is_delete']) {
-            throwError('很抱歉,商品信息不存在');
+            return throwErrorInfo('Non-existent goods.');
         }
         // 判断商品状态(上架)
         if ($verifyStatus && $goodsInfo['status'] == GoodsStatusEnum::OFF_SALE) {
-            throwError('很抱歉,当前商品已下架');
+            return throwErrorInfo('Products that have been removed from the shelves.');
         }
         // 整理商品数据并返回
         return $this->setGoodsDataFromApi($goodsInfo);

+ 3 - 3
app/index/model/Order.php

@@ -263,7 +263,7 @@ class Order extends OrderModel
     public function cancel()
     {
         if ($this['delivery_status'] == DeliveryStatusEnum::DELIVERED) {
-            $this->error = '已发货订单不可取消';
+            $this->error = 'Unsupported action.';
             return false;
         }
         // 订单是否已支付
@@ -289,7 +289,7 @@ class Order extends OrderModel
         // 条件1: 订单必须已发货
         // 条件2: 订单必须未收货
         if ($this['delivery_status'] != 20 || $this['receipt_status'] != 10) {
-            $this->error = '该订单不合法';
+            $this->error = 'Unsupported actions.';
             return false;
         }
         return $this->transaction(function () {
@@ -385,7 +385,7 @@ class Order extends OrderModel
             'order_id' => $orderId,
             'user_id' => UserService::getCurrentLoginUserId(),
         ], $with);
-        empty($order) && throwError('订单不存在');
+        empty($order) && throwError('The order does not exist');
         return $order;
     }
 

+ 1 - 1
app/index/view/error.html

@@ -5,7 +5,7 @@
 {/block}
 {block name="mainArea"}
 <section class="sectionContainer">
-   Sorry, no relevant content found
+      Sorry, no relevant content found.
 </section>
 {/block}
 {block name="scriptSrc"}