xieruidong 2 years ago
parent
commit
fa17ed8f94

+ 43 - 1
application/api/controller/mall/Orders.php

@@ -4,6 +4,7 @@ namespace app\api\controller\mall;
 
 use app\common\controller\Api;
 use app\common\model\OrderInfo;
+use app\common\model\Refund;
 use app\common\service\OrderService;
 use think\Db;
 use app\common\model\Orders as Order;
@@ -207,8 +208,49 @@ class Orders extends Api
 
     /**
      * 退货退款
+     * @ApiParams (name=refund_type,description=1退款2退款退货3仅退货)
+     * @ApiParams (name=num,description=退货数量)
+     * @ApiParams (name=order_info_id,description=订单ID)
+     * @ApiParams (name=refund_by,description=退货方式)
+     * @ApiParams (name=reason1,description=原因1)
+     * @ApiParams (name=reason2,description=原因2)
+     * @ApiParams (name=amount,description=金额)
      */
     public function refund(){
-
+        $data=$this->_validate([
+            'order_info_id'=>['require','integer','gt:0'],
+        ]);
+        $user=$this->auth->getUser();
+        $orderInfo=$user->orderInfo()->findOrFail($data['order_info_id']);
+        $order=$orderInfo->orders;
+        if(!$order){
+            $this->error('订单不存在');
+        }
+        if(!$order->allowRefund()){
+            $this->error('该订单不允许退款');
+        }
+        $refund=$orderInfo->refund;
+        if($refund){
+            $this->error('您已提交,不能再次提交');
+        }
+        $this->_validate([
+            'refund_type|售后方式'=>['require','in:'.array_filter(array_keys(Refund::getRefundTypes()))],
+            'num|数量'=>['require','integer','gt:0',"elt:{$orderInfo['num']}"],
+            'amount|金额'=>['require','integer','gt:0',"elt:{$orderInfo['amount_pay']}"],
+            'reason1|原因'=>['require','max:50'],
+            'reason2|原因'=>['require','max:100'],
+            'refund_by|方式'=>['require','max:50'],
+        ]);
+        $orderInfo->refund()->save([
+            'user_id'=>$user->id,
+            'order_id'=>$data['order_id'],
+            'refund_type'=>$data['refund_type'],
+            'refund_by'=>$data['refund_by'],
+            'num'=>$data['num'],
+            'amount'=>$data['amount'],
+            'reason1'=>$data['reason1'],
+            'reason2'=>$data['reason2'],
+        ]);
+        $this->success();
     }
 }

+ 4 - 33
application/common/model/OrderInfo.php

@@ -5,42 +5,10 @@ namespace app\common\model;
 use think\Model;
 
 /**
+ * @property Orders orders
  */
 class OrderInfo extends Model
 {
-    const REFUND_DEF=0;
-    const REFUND_ING=10;
-    const REFUND_PASS=20;
-    const REFUND_REJECT=30;
-    public static $refundStatus=[
-        self::REFUND_DEF=>'未提交',
-        self::REFUND_ING=>'申请处理中',
-        self::REFUND_PASS=>'申请通过',
-        self::REFUND_REJECT=>'申请驳回',
-    ];
-    /**
-     * @return string[]
-     */
-    public static function getRefundStatus(): array
-    {
-        return self::$refundStatus;
-    }
-    const REFUND_TYPE_DEF=0;
-    const REFUND_TYPE_MONEY=1;
-    const REFUND_TYPE_ALL=2;
-    public static $refundTypes=[
-        self::REFUND_TYPE_DEF=>'未提交',
-        self::REFUND_TYPE_MONEY=>'退款',
-        self::REFUND_TYPE_ALL=>'退款退货',
-    ];
-
-    /**
-     * @return string[]
-     */
-    public static function getRefundTypes(): array
-    {
-        return self::$refundTypes;
-    }
     protected $hidden=[
         'goods_bak'
     ];
@@ -50,6 +18,9 @@ class OrderInfo extends Model
     public function goodsBak(){
         return $this->belongsTo(OrderInfoGoods::class,'goods_bak_id');
     }
+    public function refund(){
+        return $this->belongsTo(Refund::class,'refund_id');
+    }
     public static function saveInfo(Orders $orders,$goods){
         $orderInfo=new self();
         $orderInfoGoods=OrderInfoGoods::create([

+ 17 - 1
application/common/model/Orders.php

@@ -39,7 +39,7 @@ class Orders extends Model
     const S_WAIT_REC=10;
     const S_OVER=20;
     const S_CANCEL=30;
-    const S_REFUND=40;
+    //const S_REFUND=40;
     public static $status=[
         self::S_WAIT_PAY=>'待支付',
         self::S_WAIT_REC=>'待收货',
@@ -125,9 +125,25 @@ class Orders extends Model
         $order->save();
         return true;
     }
+
+
+
+    /*
+     * 是否未支付
+     */
     public function isNotPay(){
         return $this['status']===self::S_WAIT_PAY;
     }
+    /**
+     * 是否允许退款
+     */
+    public function allowRefund(){
+        return !in_array($this['status'],[
+            self::S_WAIT_PAY,
+            self::S_CANCEL,
+        ]);
+    }
+
 
 
 

+ 49 - 0
application/common/model/Refund.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * 短信验证码
+ */
+class Refund Extends Model
+{
+    protected $name='order_info_refund';
+    const REFUND_DEF=0;
+    const REFUND_ING=10;
+    const REFUND_PASS=20;
+    const REFUND_REJECT=30;
+    public static $refundStatus=[
+        self::REFUND_DEF=>'未提交',
+        self::REFUND_ING=>'申请处理中',
+        self::REFUND_PASS=>'申请通过',
+        self::REFUND_REJECT=>'申请驳回',
+    ];
+    /**
+     * @return string[]
+     */
+    public static function getRefundStatus(): array
+    {
+        return self::$refundStatus;
+    }
+    const REFUND_TYPE_DEF=0;
+    const REFUND_TYPE_MONEY=1;
+    const REFUND_TYPE_ALL=2;
+    const REFUND_TYPE_GOODS=3;
+    public static $refundTypes=[
+        self::REFUND_TYPE_DEF=>'未提交',
+        self::REFUND_TYPE_MONEY=>'退款',
+        self::REFUND_TYPE_ALL=>'退款退货',
+        self::REFUND_TYPE_GOODS=>'仅退货',
+    ];
+
+    /**
+     * @return string[]
+     */
+    public static function getRefundTypes(): array
+    {
+        return self::$refundTypes;
+    }
+    protected $autoWriteTimestamp=true;
+}

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

@@ -214,6 +214,12 @@ class User extends Model
         return $this->hasMany(Orders::class);
     }
     /**
+     * @return OrderInfo|HasMany
+     */
+    public function orderInfo(){
+        return $this->hasMany(OrderInfo::class);
+    }
+    /**
      *@return ScoreLog|HasMany
      */
     public function scorelog(){

File diff suppressed because it is too large
+ 334 - 149
public/api.html


Some files were not shown because too many files changed in this diff