xxxrrrdddd 3 years ago
parent
commit
68947eba92

+ 3 - 0
application/common/model/User.php

@@ -180,6 +180,9 @@ class User extends Model
             Db::rollback();
         }
     }
+    /**
+     *@return HasMany|UserAddress
+     */
     public function address(){
         return $this->hasMany(UserAddress::class);
     }

+ 27 - 1
application/common/model/UserAddress.php

@@ -2,11 +2,12 @@
 
 namespace app\common\model;
 
-use think\Db;
+use think\db\Query;
 use think\Model;
 
 /**
  * 会员模型
+ * @method Query|self exists($data)
  */
 class UserAddress extends Model
 {
@@ -28,5 +29,30 @@ class UserAddress extends Model
                 self::where('user_id',$address['user_id'])->where('id','<>',$address['id'])->update(['is_default'=>0]);
             }
         });
+        self::beforeInsert(function (self $address){
+            if(empty($address['location'])){
+                $address['location']=tm()->getLocation($address['longitude'],$address['latitude'])['address'];
+            }
+        });
+    }
+    public function scopeExists(Query $query,$data){
+        if(!empty($data['name'])){
+            $query->where('name',$data['name']);
+        }
+        if(!empty($data['mobile'])){
+            $query->where('mobile',$data['mobile']);
+        }
+        if(!empty($data['address'])){
+            $query->where('address',$data['address']);
+        }
+        if(!empty($data['city'])){
+            $query->where('city',$data['city']);
+        }
+        if(!empty($data['longitude'])){
+            $query->where('longitude',$data['longitude']);
+        }
+        if(!empty($data['latitude'])){
+            $query->where('latitude',$data['latitude']);
+        }
     }
 }

+ 5 - 3
application/common/model/UserOrder.php

@@ -18,7 +18,7 @@ use traits\model\SoftDelete;
  *@property array settle_info
  *@property float profit
  *@property boolean is_same_city
- *@property boolean is_same_user
+ *@property User user
  *@method Query|self expired()
  */
 class UserOrder extends Model
@@ -463,8 +463,10 @@ class UserOrder extends Model
             $order['expired_at']=strtotime("+10minutes");
         });
         self::afterInsert(function (self $order){
-            if(!$order->user->address()->where($order['from_addr'])->find()) {
-                $order->user->address()->save($order['from_addr']);
+            if(!$order->user->address()->exists($order['from_addr'])->find()) {
+                $addr=$order['from_addr'];
+                $addr['user_id']=$order->user_id;
+                (new UserAddress())->allowField(true)->save($addr);
             }
         });
         self::afterUpdate(function (self $order){