Browse Source

fix:地址规范

super-yimizi 2 months ago
parent
commit
c8055b7589

+ 24 - 6
application/api/controller/Address.php

@@ -4,7 +4,7 @@ namespace app\api\controller;
 
 use app\common\model\Address as AddressModel;
 use app\common\model\Area;
-
+use app\common\Enum\StatusEnum;
 /**
  * 地址
  */
@@ -38,7 +38,7 @@ class Address extends Base
      */
     public function def_address()
     {
-        $row = AddressModel::where('user_id', $this->auth->id)->where('isdefault', 1)->find();
+        $row = AddressModel::where('user_id', $this->auth->id)->where('is_default', 1)->find();
         if (!$row) {
             $this->error('未找到记录');
         }
@@ -51,9 +51,25 @@ class Address extends Base
         $id = $this->request->post('id');
         $address = $this->request->post('address');
         $area_id = $this->request->post('area_id');
-        $isdefault = $this->request->post('isdefault');
+        $isdefault = $this->request->post('is_default');
         $mobile = $this->request->post('mobile');
         $receiver = $this->request->post('receiver');
+        
+        // 增加 验证器
+        $validate = new \app\api\validate\Address();
+        $data_to_validate = [
+            'receiver'   => $receiver,
+            'mobile'     => $mobile, 
+            'address'    => $address,
+            'area_id'    => $area_id,
+            'is_default' => $isdefault,
+        ];
+        
+        // 验证数据
+        $scene = $id ? 'edit' : 'add';
+        if (!$validate->scene($scene)->check($data_to_validate)) {
+            $this->error($validate->getError());
+        }
         $area = Area::field('a.id,a.zipcode,c.id as city_id,p.id as province_id')
             ->alias('a')
             ->join('shop_area c', 'a.pid=c.id')
@@ -70,7 +86,7 @@ class Address extends Base
 
         $data = [
             'address'     => $address,
-            'isdefault'   => $isdefault,
+            'is_default'  => $isdefault,
             'mobile'      => $mobile,
             'receiver'    => $receiver,
             'area_id'     => $area_id,
@@ -78,10 +94,12 @@ class Address extends Base
             'city_id'     => $area['city_id'],
             'province_id' => $area['province_id'],
             'zipcode'     => $area['zipcode'],
-            'status'      => 'normal'
+            'status'      => StatusEnum::ENABLED
         ];
         if ($id) { //编辑
-            $row = AddressModel::where('id', $id)->where('user_id', $this->auth->id)->find();
+            $row = AddressModel::where('id', $id)
+            ->where('user_id', $this->auth->id)
+            ->find();
             if (!$row) {
                 $this->error('未找到记录');
             }

+ 43 - 0
application/api/validate/Address.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace app\api\validate;
+
+use think\Validate;
+
+class Address extends Validate
+{
+    /**
+     * 验证规则
+     */
+    protected $rule = [
+        'receiver'   => 'require|length:1,30',
+        'mobile'     => 'require|mobile',
+        'address'    => 'require|length:1,255',
+        'area_id'    => 'require|integer|gt:0',
+        'is_default' => 'in:0,1',
+    ];
+
+    /**
+     * 提示消息
+     */
+    protected $message = [
+        'receiver.require'   => '收货人姓名不能为空',
+        'receiver.length'    => '收货人姓名长度不能超过30个字符',
+        'mobile.require'     => '手机号不能为空',
+        'mobile.mobile'      => '手机号格式不正确',
+        'address.require'    => '详细地址不能为空',
+        'address.length'     => '详细地址长度不能超过255个字符',
+        'area_id.require'    => '请选择所在地区',
+        'area_id.integer'    => '地区ID必须为整数',
+        'area_id.gt'         => '地区ID必须大于0',
+        'is_default.in'      => '默认地址参数错误',
+    ];
+
+    /**
+     * 验证场景
+     */
+    protected $scene = [
+        'add'  => ['receiver', 'mobile', 'address', 'area_id', 'is_default'],
+        'edit' => ['receiver', 'mobile', 'address', 'area_id', 'is_default'],
+    ];
+} 

+ 6 - 6
application/common/model/Address.php

@@ -2,9 +2,9 @@
 
 namespace app\common\model;
 
+use app\common\Enum\StatusEnum;
 use think\Model;
 use traits\model\SoftDelete;
-
 /**
  * 模型
  */
@@ -14,7 +14,7 @@ class Address extends Model
     use SoftDelete;
 
     // 表名
-    protected $name = 'shop_address';
+    protected $name = 'shop_user_address';
     // 开启自动写入时间戳字段
     protected $autoWriteTimestamp = 'int';
     // 定义时间戳字段名
@@ -34,10 +34,10 @@ class Address extends Model
         parent::init();
         self::beforeWrite(function ($row) {
             $changed = $row->getChangedData();
-            if (isset($changed['isdefault']) && $changed['isdefault']) {
-                $info = \addons\shop\model\Address::where('isdefault', 1)->where('user_id', $row['user_id'])->find();
+            if (isset($changed['is_default']) && $changed['is_default']) {
+                $info = self::where('is_default', 1)->where('user_id', $row['user_id'])->find();
                 if ($info && (!isset($row['id']) || $info['id'] != $row['id'])) {
-                    $info->isdefault = 0;
+                    $info->is_default = 0;
                     $info->save();
                 }
             }
@@ -111,7 +111,7 @@ class Address extends Model
      */
     public static function getAddressList($user_id)
     {
-        $addressList = self::where('user_id', $user_id)->where('status', 'normal')->order('usednums desc,id desc')->select();
+        $addressList = self::where('user_id', $user_id)->where('status', StatusEnum::ENABLED)->order('used_nums desc,id desc')->select();
         return $addressList;
     }
 }