xxxrrrdddd 3 лет назад
Родитель
Сommit
1dc6517521
2 измененных файлов с 17 добавлено и 5 удалено
  1. 2 5
      application/api/controller/UserOrder.php
  2. 15 0
      application/common/model/UserOrder.php

+ 2 - 5
application/api/controller/UserOrder.php

@@ -171,11 +171,8 @@ class UserOrder extends UserApi
             ->setCoupon($data['coupon_id']??null)
             ->prepare();
         $data['user_id']=$user['id'];
-        $order=new \app\common\model\UserOrder();
-        $order->allowField(true)->save($data);
-        if($order['coupon_id']){
-            UserCoupon::where('id',$order['coupon_id'])->save(['is_used'=>1]);
-        }
+        $order=new UO();
+        $order->submit($data);
         $params=$order->pay();
         Db::commit();
         $this->success('',$params);

+ 15 - 0
application/common/model/UserOrder.php

@@ -161,6 +161,16 @@ class UserOrder extends Model
         }
         $this->delete();
     }
+    public function submit($data){
+        $this['status']=self::STATUS_WAIT_PAY;
+        $res=$this->allowField(true)->save($data);
+        if(!$res){
+            throw_user('保存失败');
+        }
+        if($this['coupon_id']){
+            UserCoupon::where('id',$this['coupon_id'])->save(['is_used'=>1]);
+        }
+    }
 
 
 
@@ -188,5 +198,10 @@ class UserOrder extends Model
             $order['no']=order_no();
             $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']);
+            }
+        });
     }
 }