Browse Source

fix:首次添加默认地址时 强制默认

super-yimizi 1 month ago
parent
commit
9168975823
2 changed files with 18 additions and 4 deletions
  1. 5 3
      application/api/controller/Address.php
  2. 13 1
      application/common/model/Address.php

+ 5 - 3
application/api/controller/Address.php

@@ -39,10 +39,12 @@ class Address extends Base
     public function def_address()
     public function def_address()
     {
     {
         $row = AddressModel::where('user_id', $this->auth->id)->where('is_default', 1)->find();
         $row = AddressModel::where('user_id', $this->auth->id)->where('is_default', 1)->find();
-        if (!$row) {
-            $this->error('未找到记录');
+        $arrReturn = [];
+        if ($row) {
+            $arrReturn = $row->toArray();
         }
         }
-        $this->success('获取成功', $row);
+       
+        $this->success('获取成功', $arrReturn);
     }
     }
 
 
     //添加(编辑)地址
     //添加(编辑)地址

+ 13 - 1
application/common/model/Address.php

@@ -41,7 +41,19 @@ class Address extends Model
                     $info->save();
                     $info->save();
                 }
                 }
             }
             }
-            //$row['address'] = $row->address_full;
+            //新增时 第一个地址 默认地址
+            if (empty($row['id'])) {
+                $count = self::where('user_id', $row['user_id'])->count();
+                if ($count == 0) {
+                    $row->is_default = 1;
+                }
+            } else {
+                // 编辑时,如果只有一个地址,强制设为默认
+                $count = self::where('user_id', $row['user_id'])->count();
+                if ($count == 1) {
+                    $row->is_default = 1;
+                }
+            }
         });
         });
     }
     }
     public function getCityAttr($value, $data)
     public function getCityAttr($value, $data)