xxxrrrdddd 3 jaren geleden
bovenliggende
commit
58d2827404

+ 4 - 0
application/UserException.php

@@ -0,0 +1,4 @@
+<?php
+namespace app;
+
+class UserException extends \Exception{}

+ 32 - 4
application/api/controller/UserOrder.php

@@ -5,7 +5,9 @@ namespace app\api\controller;
 use app\common\controller\UserApi;
 use app\service\Pet;
 use app\service\UserOrderService;
+use app\UserException;
 use app\validate\UserOrderValidate;
+use think\Exception;
 
 /**
  * 用户端订单接口
@@ -56,6 +58,7 @@ class UserOrder extends UserApi
     }
     /**
      * 预下单
+     * @ApiMethod (POST)
      * @param array from_addr 必须,取宠地址信息
      * @param string from_addr[longitude] 必须,取宠地址经度
      * @param string from_addr[latitude] 必须,取宠地址纬度
@@ -82,14 +85,39 @@ class UserOrder extends UserApi
      * @param string images 照片
      * @param string protect_id 安心保ID
      * @param string coupon_id 优惠券ID
+     * @param string freight fast快车air空运special专车
+     *
+     * @ApiReturnParams (name=total_amount,type=string,description=总价)
+     * @ApiReturnParams (name=coupon_amount,type=string,description=优惠券优惠金额)
+     * @ApiReturnParams (name=real_amount,type=string,description=实付总价)
+     * @ApiReturnParams (name=freights[0][price],type=string,description=实际价格)
+     * @ApiReturnParams (name=freights[0][total_price],type=string,description=总价)
+     * @ApiReturnParams (name=freights[0][type],type=string,description=类型:fast快车air空运special专车)
+     * @ApiReturnParams (name=freights[0][coupon][0][amount],type=string,description=优惠券优惠额度)
+     * @ApiReturnParams (name=freights[0][coupon][0][end_time],type=string,description=优惠券过期时间)
+     * @ApiReturnParams (name=freights[0][coupon][0][id],type=string,description=优惠券id)
+     * @ApiReturnParams (name=freights[0][coupon][0][is_used],type=string,description=优惠券实付已使用)
+     * @ApiReturnParams (name=freights[0][coupon][0][max],type=string,description=优惠券满多少可以减)
+     * @ApiReturnParams (name=freights[0][coupon][0][selected],type=string,description=实付是选中的优惠券)
+     * @ApiReturnParams (name=freights[0][coupon][0][start_time],type=string,description=优惠券开始时间)
+     * @ApiReturnParams (name=freights[0][coupon][0][type],type=string,description="优惠券类型,1代金券2满减券3折扣券4新用户下单券")
+     * @ApiReturnParams (name=freights[0][coupon][0][type_desc],type=string,description=优惠券类型)
      */
     public function prepare(UserOrderService $userOrder){
         $data=input();
         $this->validate($data,UserOrderValidate::class);
         $user=$this->auth->getUser();
-        $data=$userOrder->setUser($user)
-            ->import($data)
-            ->prepare();
-        $this->success('',$data);
+        if(!empty($data['freight'])){
+            $this->validate($data,UserOrderValidate::$freight);
+        }
+        try {
+            $data=$userOrder->setUser($user)
+                ->import($data)
+                ->setCoupon($data['coupon_id']??null)
+                ->prepare();
+            $this->success('',$data);
+        }catch (UserException $e){
+            $this->error($e->getMessage());
+        }
     }
 }

+ 4 - 0
application/common.php

@@ -503,4 +503,8 @@ function fullUrl($url){
     }
     $url=request()->root(true).$url;
     return $url;
+}
+
+function throw_user($msg){
+    throw new \app\UserException($msg);
 }

+ 12 - 0
application/common/model/Coupon.php

@@ -9,9 +9,21 @@ use think\Model;
  */
 class Coupon extends Model
 {
+    protected $append=[
+        'type_desc',
+    ];
     const C1=1;
     const C2=2;
     const C3=3;
     const C4=4;
+    public static $types=[
+        self::C1=>'代金券',
+        self::C2=>'满减券',
+        self::C3=>'折扣券',
+        self::C4=>'新用户下单券',
+    ];
 
+    public function getTypeDescAttr($v,$d){
+        return self::$types[$d['type']]??'-';
+    }
 }

+ 13 - 0
application/common/model/UserCoupon.php

@@ -18,6 +18,9 @@ class UserCoupon extends Model
         Coupon::C3=>'discount',
         Coupon::C4=>'newUser',
     ];
+    protected $append=[
+        'type_desc',
+    ];
     public function user(){
         return $this->belongsTo(User::class);
     }
@@ -45,4 +48,14 @@ class UserCoupon extends Model
     public function newUser($amount){
         return $this->can_use;
     }
+    public function amount($amount){
+        if($this['type']==Coupon::C3){
+            return bcsub($amount,bcmul($amount,bcdiv($this['amount'],10,2),2),2);
+        }
+        return $this['amount'];
+    }
+
+    public function getTypeDescAttr($v,$d){
+        return Coupon::$types[$d['type']]??'-';
+    }
 }

+ 68 - 21
application/service/UserOrderService.php

@@ -4,13 +4,37 @@ namespace app\service;
 use app\common\model\User;
 use app\common\model\UserCoupon;
 use app\common\model\UserOrder;
+use think\Exception;
 
 class UserOrderService{
-    /** @var UserOrder */
     protected $order;
     protected $user;
     /** @var UserCoupon[] */
     protected $coupon;
+    protected $user_coupon=[];
+
+    /**
+     * @return UserCoupon[]
+     */
+    public function getCoupon(): array
+    {
+        return $this->coupon;
+    }
+
+    /**
+     * @param UserCoupon[] $coupon
+     */
+    public function setCoupon($coupon)
+    {
+        if($coupon) {
+            $c = $this->getUser()->coupon()->use()->find($coupon);
+            if (!$c) {
+                throw_user('优惠券不存在');
+            }
+            $this->user_coupon = $c;
+        }
+        return $this;
+    }
 
     public function __construct()
     {
@@ -18,7 +42,7 @@ class UserOrderService{
     }
 
     /**
-     * @return mixed
+     * @return User
      */
     public function getUser()
     {
@@ -38,7 +62,7 @@ class UserOrderService{
     /**
      * @return mixed
      */
-    public function getOrder(): UserOrder
+    public function getOrder()
     {
         return $this->order;
     }
@@ -46,60 +70,83 @@ class UserOrderService{
     /**
      * @param mixed $order
      */
-    public function setOrder(UserOrder $order): void
+    public function setOrder(UserOrder $order)
     {
-        $this->order = $order;
+        $data=$order->toArray();
+        $this->order = $data;
+        return $this;
     }
 
-    public function import($data){
-        $order=new UserOrder($data);
-        list($order->agree_date,$order->agree_time)=explode(' ',$data['agree_time']);
-        $order->from_city=$data['from_addr']['city'];
-        $order->to_city=$data['to_addr']['city'];
-        $order->pet_category=Pet::category()[$data['pet_category']]['name'];
-        $order->spec=Pet::spec()[$data['spec']]['name'];
+    public function import(&$data){
+        list($data['agree_date'],$data['agree_time'])=explode(' ',$data['agree_time']);
+        $data['from_city']=$data['from_addr']['city'];
+        $data['to_city']=$data['to_addr']['city'];
+        $data['pet_category']=Pet::category()[$data['pet_category']]['name'];
+        $data['spec']=Pet::spec()[$data['spec']]['name'];
         if(!empty($data['protect_id'])) {
             $protect=Pet::protect()[$data['protect_id']];
-            $order->protect_amount = $protect['price'];
-            $order->protect_max = $protect['max'];
+            $data['protect_amount'] = $protect['price'];
+            $data['protect_max'] = $protect['max'];
         }
-        $this->order=$order;
+        $data['coupon_amount']=0;
+        $data['real_amount']=0;
+        $this->order=$data;
         return $this;
     }
 
     function prepare(){
         $items=[];
+        $total_amount=0;
         foreach (UserOrder::$freights as $type=>$_){
             $item=[
-                'type'=>$type,
+                'type'=>$type,//类型
+                'price'=>0,#实付
+                'total_price'=>0,#总价
+                'coupon'=>[],
             ];
-            $item['price']=$this->order[$type]=$this->{$type}();
+            $item['total_price']=$this->{$type}();
+            $item['price']=$item['total_price'];
             $coupon=[];
             foreach ($this->coupon as $_coupon){
-                if($_coupon->fit($item['price'])){
+                $_coupon['selected']=false;
+                if($type==$this->order['freight'] && $this->user_coupon && $_coupon['id']==$this->user_coupon['id'] && $_coupon->fit($item['total_price'])){
+                    $_coupon['selected']=true;
+                    $this->order['coupon_amount']=$_coupon->amount($item['total_price']);
+                    $item['price']=bcsub($item['price'],$this->order['coupon_amount']);
+                }
+                if($_coupon->fit($item['total_price'])){
                     $coupon[]=$_coupon;
                 }
             }
             $item['coupon']=$coupon;
             $items[]=$item;
+
+            if($type==$this->order['freight']){
+                $total_amount=bcadd($total_amount,$item['total_price']);
+            }
         }
 
+        $protect=Pet::protect()[$this->order['protect_id']]??[];
+        $total_amount=bcadd($total_amount,$protect['price']??0);
+
         $this->order['freights']=$items;
+        $this->order['total_amount']=$total_amount;
+        $this->order['real_amount']=bcsub($this->order['total_amount'],$this->order['coupon_amount']);
 
         return $this->order;
     }
 
     protected function fast(){
-        $price=bcmul(bcmul(bcmul(2,$this->order->num),$this->order->weight),$this->order->piece);
+        $price=bcmul(bcmul(bcmul(2,$this->order['num']),$this->order['weight']),$this->order['piece']);
         return $price;
     }
     protected function air(){
-        $price=bcmul(bcmul(bcmul(2,$this->order->num),$this->order->weight),$this->order->piece);
+        $price=bcmul(bcmul(bcmul(2,$this->order['num']),$this->order['weight']),$this->order['piece']);
         $price=bcmul($price,5);
         return $price;
     }
     protected function special(){
-        $price=bcmul(bcmul(bcmul(2,$this->order->num),$this->order->weight),$this->order->piece);
+        $price=bcmul(bcmul(bcmul(2,$this->order['num']),$this->order['weight']),$this->order['piece']);
         $price=bcmul($price,2);
         return $price;
     }

+ 5 - 2
application/validate/UserOrderValidate.php

@@ -1,6 +1,7 @@
 <?php
 namespace app\validate;
 
+use app\common\model\UserOrder;
 use think\Validate;
 
 class UserOrderValidate extends Validate{
@@ -30,7 +31,9 @@ class UserOrderValidate extends Validate{
         'remark'=>['max:100'],
         'images'=>['array'],
         'protect_id'=>['integer','egt:0'],
+        'coupon_id'=>['integer','egt:0'],
+    ];
+    public static $freight=[
+        'freight'=>['require','in:'.UserOrder::FREIGHT_AIR.','.UserOrder::FREIGHT_FAST.','.UserOrder::FREIGHT_SPECIAL],
     ];
-    protected $order;
-
 }

+ 109 - 4
public/api.html

@@ -3555,7 +3555,7 @@
                                 <div class="panel panel-default">
                     <div class="panel-heading" id="heading-23">
                         <h4 class="panel-title">
-                            <span class="label label-success">GET</span>
+                            <span class="label label-primary">POST</span>
                             <a data-toggle="collapse" data-parent="#accordion23" href="#collapseOne23"> 预下单 <span class="text-muted">/api/user_order/prepare</span></a>
                         </h4>
                     </div>
@@ -3761,6 +3761,18 @@
                                                         <td>是</td>
                                                         <td>安心保ID</td>
                                                     </tr>
+                                                                                                        <tr>
+                                                        <td>coupon_id</td>
+                                                        <td>string</td>
+                                                        <td>是</td>
+                                                        <td>优惠券ID</td>
+                                                    </tr>
+                                                                                                        <tr>
+                                                        <td>freight</td>
+                                                        <td>string</td>
+                                                        <td>是</td>
+                                                        <td>fast快车air空运special专车</td>
+                                                    </tr>
                                                                                                     </tbody>
                                             </table>
                                                                                     </div>
@@ -3782,7 +3794,7 @@
                                                 </div>
                                                 </div>
                                                 <div class="panel-body">
-                                                    <form enctype="application/x-www-form-urlencoded" role="form" action="/api/user_order/prepare" method="get" name="form23" id="form23">
+                                                    <form enctype="application/x-www-form-urlencoded" role="form" action="/api/user_order/prepare" method="POST" name="form23" id="form23">
                                                                                                                 <div class="form-group">
                                                             <label class="control-label" for="from_addr">from_addr</label>
                                                             <input type="array" class="form-control input-sm" id="from_addr" required placeholder="必须,取宠地址信息" name="from_addr">
@@ -3883,6 +3895,14 @@
                                                             <label class="control-label" for="protect_id">protect_id</label>
                                                             <input type="string" class="form-control input-sm" id="protect_id" required placeholder="安心保ID" name="protect_id">
                                                         </div>
+                                                                                                                <div class="form-group">
+                                                            <label class="control-label" for="coupon_id">coupon_id</label>
+                                                            <input type="string" class="form-control input-sm" id="coupon_id" required placeholder="优惠券ID" name="coupon_id">
+                                                        </div>
+                                                                                                                <div class="form-group">
+                                                            <label class="control-label" for="freight">freight</label>
+                                                            <input type="string" class="form-control input-sm" id="freight" required placeholder="fast快车air空运special专车" name="freight">
+                                                        </div>
                                                                                                                 <div class="form-group form-group-submit">
                                                             <button type="submit" class="btn btn-success send" rel="23">提交</button>
                                                             <button type="reset" class="btn btn-info" rel="23">重置</button>
@@ -3904,7 +3924,92 @@
                                             <div class="panel panel-default">
                                                 <div class="panel-heading"><strong>返回参数</strong></div>
                                                 <div class="panel-body">
-                                                                                                        无
+                                                                                                        <table class="table table-hover">
+                                                        <thead>
+                                                            <tr>
+                                                                <th>名称</th>
+                                                                <th>类型</th>
+                                                                <th>描述</th>
+                                                            </tr>
+                                                        </thead>
+                                                        <tbody>
+                                                                                                                        <tr>
+                                                                <td>total_amount</td>
+                                                                <td>string</td>
+                                                                <td>总价</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>coupon_amount</td>
+                                                                <td>string</td>
+                                                                <td>优惠券优惠金额</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>real_amount</td>
+                                                                <td>string</td>
+                                                                <td>实付总价</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][price]</td>
+                                                                <td>string</td>
+                                                                <td>实际价格</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][total_price]</td>
+                                                                <td>string</td>
+                                                                <td>总价</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][type]</td>
+                                                                <td>string</td>
+                                                                <td>类型:fast快车air空运special专车</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][coupon][0][amount]</td>
+                                                                <td>string</td>
+                                                                <td>优惠券优惠额度</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][coupon][0][end_time]</td>
+                                                                <td>string</td>
+                                                                <td>优惠券过期时间</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][coupon][0][id]</td>
+                                                                <td>string</td>
+                                                                <td>优惠券id</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][coupon][0][is_used]</td>
+                                                                <td>string</td>
+                                                                <td>优惠券实付已使用</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][coupon][0][max]</td>
+                                                                <td>string</td>
+                                                                <td>优惠券满多少可以减</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][coupon][0][selected]</td>
+                                                                <td>string</td>
+                                                                <td>实付是选中的优惠券</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][coupon][0][start_time]</td>
+                                                                <td>string</td>
+                                                                <td>优惠券开始时间</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][coupon][0][type]</td>
+                                                                <td>string</td>
+                                                                <td>优惠券类型,1代金券2满减券3折扣券4新用户下单券</td>
+                                                            </tr>
+                                                                                                                        <tr>
+                                                                <td>freights[0][coupon][0][type_desc]</td>
+                                                                <td>string</td>
+                                                                <td>优惠券类型</td>
+                                                            </tr>
+                                                                                                                    </tbody>
+                                                    </table>
                                                                                                     </div>
                                             </div>
                                         </div>
@@ -5062,7 +5167,7 @@
 
                 </div>
                 <div class="col-md-6" align="right">
-                    Generated on 2021-09-23 10:50:07 <a href="./" target="_blank">宠365</a>
+                    Generated on 2021-09-23 17:18:29 <a href="./" target="_blank">宠365</a>
                 </div>
             </div>