Orders.php 23.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
<?php

namespace app\api\controller;

use app\admin\model\Order;
use app\common\controller\Api;
use think\Db;
use think\Validate;
/**
 * 订单接口
 */
class Orders extends Api
{
    protected  $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $uid = '';//token存贮user_id
    protected $order_status = [];//订单状态
    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
        $this->order_status = config('verify.status');
    }

    /**
     * @ApiTitle    (我的订单状态数量)
     * @ApiSummary  (我的订单状态数量)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/myOrderTotal)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575700114",
        "data": {
            "total": 1,//待付款
            "total1": 0,//待发货
            "total2": 0,//待收货
            "total3": 1,//已完成
            "total4": 1//退换货
        }
    })
     */
    public function myOrderTotal(){
        if($this->request->isGet()){
            $arr = [];
            $status = $this->order_status;
            //待付款
            $arr['total'] = Common::countSoftTotal('order',['uid'=>$this->uid,'status'=>$status[0]]);
            //待发货
            $arr['total1'] = Common::countSoftTotal('order',['uid'=>$this->uid,'status'=>$status[2]]);
            //待收货
            $arr['total2'] = Common::countSoftTotal('order',['uid'=>$this->uid,'status'=>$status[5]]);
            //已完成
            $arr['total3'] = Common::countSoftTotal('order',['uid'=>$this->uid,'status'=>$status[8]]);
            //退换货
            $arr['total4'] = Common::countSoftTotal('order',['uid'=>$this->uid,'status'=>['in',[6,7]]]);
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (我的订单)
     * @ApiSummary  (我的订单)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/myOrderList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="status", type="inter", required=true, description="订单状态(-1:全部,0: 待付款, 1: 已取消,2: 待发货, 3: 退款申请中,4: 已退款, 5: 待收货,6: 退换货申请中, 7: 已换货,8: 已完成)")
     * @ApiParams   (name="page", type="inter", required=true, description="分页页码")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575443560",
        "data": {
            "data": [
                {
                    "id": 2,//订单id
                    "order_sn": 1000002,//订单号
                    "total_price": 390,//支付金额
                    "status": 0,//订单状态(0: 待付款, 1: 已取消,2: 待发货, 3: 退款申请中,4: 已退款, 5: 待收货,6: 退换货申请中, 7: 已退换货,8: 已完成)
                    "goods_list": [
                        {
                            "id": 3,//附加表id
                            "o_id": 2,//订单id
                            "g_id": 1,//商品id
                            "style": "款式1",//商品款式
                            "goods_number": 1,//商品数量
                            "price": 200,//商品价格
                            "name": "MONENT 动感系列动感系列动感系列1",//商品名称
                            "introduce": "轻波款,为客厅缀上霞光淡雾"//商品简介
                        },
                        {
                            "id": 4,
                            "o_id": 2,
                            "g_id": 2,
                            "style": "款式2",
                            "goods_number": 1,
                            "price": 200,
                            "name": "MONENT 动感系列动感系列2",
                            "introduce": "轻波款,为客厅缀上霞光淡雾"
                        }
                    ]
                },
                {
                    "id": 1,
                    "order_sn": 1000001,
                    "total_price": 200,
                    "goods_list": [
                        {
                            "id": 1,
                            "o_id": 1,
                            "g_id": 1,
                            "style": "款式1",
                            "goods_number": 2,
                            "price": 100,
                            "image": "商品图片路径",
                            "name": "MONENT 动感系列动感系列动感系列1",
                            "introduce": "轻波款,为客厅缀上霞光淡雾"
                        },
                        {
                            "id": 2,
                            "o_id": 1,
                            "g_id": 2,
                            "style": "款式2",
                            "goods_number": 3,
                            "price": 200,
                            "name": "MONENT 动感系列动感系列2",
                            "introduce": "轻波款,为客厅缀上霞光淡雾"
                        }
                    ]
                }
            ],
           "total_page": 1//总页码
        }
    })
     */
    public function myOrderList(){
        if($this->request->isGet()){
            $status = $this->request->get('status');
            $page = $this->request->get('page');

            $rule = config('verify.my_order');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['status'=>$status,'page'=>$page])) {
                $this->error($validate->getError());
            }

            $arr = [];
            if($status == -1){
                $where = ['uid'=>$this->uid];
            }else if($status == 6){
                //退换货申请中,显示所有申请中的和已退换货
                $where = ['uid'=>$this->uid,'status'=>['in',[6,7]]];
            }else{
                $where = ['uid'=>$this->uid,'status'=>$status];
            }
            $res = Common::selectSoftWherePageData('order',$where,'id,order_sn,status,total_price',$page);
            $id_s = array_column($res,'id');
            //订单附加表
            $res_o_goods = Db::name('ogoods')
                ->alias('og')
                ->join('goods g','og.g_id = g.id','LEFT')
                ->where(['og.o_id'=>['in',$id_s]])
                ->field('og.id,og.o_id,og.g_id,og.style,og.goods_number,og.price,g.image,g.name,g.introduce')
                ->useSoftDelete('og.deletetime')
                ->select();
            foreach ($res as &$value){
                $k=0;
                foreach ($res_o_goods as $o_value){
                    $k+=0;
                    if($value['id'] == $o_value['o_id']){
                        $value['goods_list'][$k] = $o_value;
                        $value['goods_list'][$k]['image'] = $this->auth->absolutionUrlOne($o_value['image']);
                        $k++;
                    }
                }
            }
            $arr['data'] = $res;
            $arr['total_page'] = Common::countSoft('order',$where);
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (订单详情)
     * @ApiSummary  (订单详情)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/myOrderDetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575444520",
        "data": {
            "id": 1,//订单id
            "uid": 2,
            "status": 0,//订单状态(0: 待付款, 1: 已取消,2: 待发货, 3: 退款申请中,4: 已退款, 5: 待收货,6: 退换货申请中, 7: 已退换货,8: 已完成)
            "order_sn": 1000001,//订单号
            "receive_name": "景龙",//收货人
            "receive_mobile": "13752011725",//收货电话
            "receive_address": "天津市南开区",//收货地址
            "leave_message": "",//留言
            "discount_price": 0,//优惠价格
            "total_goods_price": 200,//商品总价格
            "total_expense_price": 0,//运费
            "total_price": 200,//支付总金额
            "coupon_id": 0,//(0:未选择优惠券)
            "coupon_price": "9.5折"//为空则未选择优惠券
            "createtime": "2019-12-04 14:06:52",//下单时间
            "goods_list": [
                {
                    "id": 3,//附加表id
                    "o_id": 2,//订单id
                    "g_id": 1,//商品id
                    "style": "款式1",//商品款式
                    "goods_number": 1,//商品数量
                    "price": 200,//商品价格
                    "image": "",//商品图片路径
                    "name": "MONENT 动感系列动感系列动感系列1",//商品名称
                    "introduce": "轻波款,为客厅缀上霞光淡雾"//商品简介
                },
                {
                    "id": 2,
                    "o_id": 1,
                    "g_id": 2,
                    "style": "款式2",
                    "goods_number": 3,
                    "price": 200,
                    "name": "MONENT 动感系列动感系列2",
                    "introduce": "轻波款,为客厅缀上霞光淡雾"
                }
            ]
        }
    })
     */
    public function myOrderDetail(){
        if($this->request->isGet()){
            $order_id = $this->request->get('order_id');

            $rule = config('verify.order_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_id'=>$order_id])) {
                $this->error($validate->getError());
            }

            $res = Common::findSoftWhereData('order',['id'=>$order_id,'uid'=>$this->uid],'pay_order_sn,updatetime,deletetime',true);
            if($res){
                $res['createtime'] = date('Y-m-d H:i:s',$res['createtime']);
                //订单附加表
                $res_o_goods = Db::name('ogoods')
                    ->alias('og')
                    ->join('goods g','og.g_id = g.id','LEFT')
                    ->where(['og.o_id'=>$res['id']])
                    ->field('og.o_id,og.g_id id,og.style,og.goods_number,og.price,g.image,g.name,g.introduce')
                    ->useSoftDelete('og.deletetime')
                    ->select();
                foreach($res_o_goods  as &$o_g_value){
                    $o_g_value['image'] = $this->auth->absolutionUrlOne($o_g_value['image']);
                }
                $res['goods_list'] = $res_o_goods;

                //查询优惠券
                $res['coupon_price'] = '';
                if($res['coupon_id']){
                    $res_coupon = Common::findWhereData('coupon',['id'=>$res['coupon_id']],'*');
                    if($res_coupon['c_type'] == 0){
                        //无门槛
                        if($res_coupon['coupon_type'] == 0){
                            //减少券,去掉满减金额,折扣字段,
                            $res['coupon_price'] = '减:¥'.$res_coupon['reduce'];
                        }else{
                            //折扣券,去掉满减金额,减少金额字段,
                            $res['coupon_price'] = $res_coupon['discount'].'折';
                        }
                    }else{
                        //有门槛
                        if($res_coupon['coupon_type'] == 0){
                            //减少券,去掉折扣字段,
                            $res['coupon_price'] = '减:¥'.$res_coupon['reduce'];
                        }else{
                            //折扣券,去掉减少金额字段,
                            $res['coupon_price'] = $res_coupon['discount'].'折';
                        }
                    }
                }
            }

            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (退款,退换货原因)
     * @ApiSummary  (退款,退换货原因)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/returnReason)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575511885",
        "data": {
            "id": 2,
            "status": 3,//订单状态(0: 待付款, 1: 已取消,2: 待发货, 3: 退款申请中,4: 已退款, 5: 待收货,6: 退换货申请中, 7: 已退换货,8: 已完成)
            "refund_reason": "",//退款原因
            "return_reason": ""//退换货原因
        }
    })
     */
    public function returnReason(){
        if($this->request->isGet()){
            $order_id = $this->request->get('order_id');

            $rule = config('verify.order_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_id'=>$order_id])) {
                $this->error($validate->getError());
            }

            $res = Common::findSoftWhereData('order',['id'=>$order_id,'uid'=>$this->uid],'id,status,refund_reason,return_reason');
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (取消订单)
     * @ApiSummary  (取消订单)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/cancelOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571730709",
        "data": null
    })
     */
    public function cancelOrder(){
        if($this->request->isGet()){
            $order_id = $this->request->get('order_id');

            $rule = config('verify.order_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_id'=>$order_id])) {
                $this->error($validate->getError());
            }

            $status = $this->order_status;
            $orderModel = new Order();
            $res = $orderModel->where(['id'=>$order_id,'uid'=>$this->uid,'status'=>$status[0]])->update(['status'=>$status[1]]);
            if($res){
                $flag = config('verify.flag');
                $order = Common::findSoftWhereData('order',['id'=>$order_id,'uid'=>$this->uid,'status'=>$status[1]],'id,coupon_id');
                if($order['coupon_id']){
                    Common::updateRCoupon($this->uid,$order['coupon_id'],$flag[0]);
                }
                $this->success('成功');
            }else{
                $this->error('失败');
            }

        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (申请退款)
     * @ApiSummary  (申请退款)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/applyRefund)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     * @ApiParams   (name="refund_reason", type="string", required=true, description="退款原因")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571730709",
        "data": null
    })
     */
    public function applyRefund(){
        if($this->request->isGet()){
            $order_id = $this->request->get('order_id');
            $refund_reason = $this->request->get('refund_reason');

            $rule = config('verify.apply_refund');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_id'=>$order_id,'refund_reason'=>$refund_reason])) {
                $this->error($validate->getError());
            }

            //查询订单
            $status = $this->order_status;
            $where = ['id'=>$order_id,'uid'=>$this->uid,'status'=>$status[2]];//待发货
            $order = Common::findSoftWhereData('order',$where,'id');
            if($order){
                $orderModel = new Order();
                $res = $orderModel->where($where)->update(['status'=>$status[3],'return_refund_flag'=>1,'refund_reason'=>$refund_reason]);//退款申请中
                if($res){
                    $this->success('申请成功');
                }else{
                    $this->error('申请失败');
                }
            }
            $this->error('未找到订单');

        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (退款撤销)
     * @ApiSummary  (退款撤销)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/cancelApplyRefund)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571730709",
        "data": null
    })
     */
    public function cancelApplyRefund(){
        if($this->request->isGet()){
            $order_id = $this->request->get('order_id');

            $rule = config('verify.order_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_id'=>$order_id])) {
                $this->error($validate->getError());
            }

            //查询订单
            $status = $this->order_status;
            $where = ['id'=>$order_id,'uid'=>$this->uid,'status'=>$status[3]];//退款申请中
            $order = Common::findSoftWhereData('order',$where,'id');
            if($order){
                $orderModel = new Order();
                $res = $orderModel->where($where)->update(['status'=>$status[2],'return_refund_flag'=>0,'refund_reason'=>'']);//待发货
                if($res){
                    $this->success('撤销成功');
                }else{
                    $this->error('撤销失败');
                }
            }
            $this->error('未找到订单');

        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (申请退换货)
     * @ApiSummary  (申请退换货)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/applyReturnGoods)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     * @ApiParams   (name="return_flag", type="inter", required=true, description="退换货标识(0:退货,1:换货)")
     * @ApiParams   (name="return_reason", type="string", required=true, description="退换货原因")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571730709",
        "data": null
    })
     */
    public function applyReturnGoods(){
        if($this->request->isGet()){
            $order_id = $this->request->get('order_id');
            $return_flag = $this->request->get('return_flag');
            $return_reason = $this->request->get('return_reason');

            $rule = config('verify.apply_return');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_id'=>$order_id,'return_flag'=>$return_flag,'return_reason'=>$return_reason])) {
                $this->error($validate->getError());
            }

            //查询订单
            $status = $this->order_status;
            $where = ['id'=>$order_id,'uid'=>$this->uid,'status'=>$status[5]];//待收货
            $order = Common::findSoftWhereData('order',$where,'id');
            if($order){
                $orderModel = new Order();
                if($return_flag == 0){
                    $returnFlag = 2;
                }else{
                    $returnFlag = 3;
                }
                $res = $orderModel->where($where)->update(['status'=>$status[6],'return_refund_flag'=>$returnFlag,'return_reason'=>$return_reason]);//退换货申请中
                if($res){
                    $this->success('申请成功');
                }else{
                    $this->error('申请失败');
                }
            }
            $this->error('未找到订单');

        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (退换货撤销)
     * @ApiSummary  (退换货撤销)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/cancelApplyReturnGoods)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571730709",
        "data": null
    })
     */
    public function cancelApplyReturnGoods(){
        if($this->request->isGet()){
            $order_id = $this->request->get('order_id');

            $rule = config('verify.order_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_id'=>$order_id])) {
                $this->error($validate->getError());
            }

            //查询订单
            $status = $this->order_status;
            $where = ['id'=>$order_id,'uid'=>$this->uid,'status'=>$status[6]];//退换货申请中
            $order = Common::findSoftWhereData('order',$where,'id');
            if($order){
                $orderModel = new Order();
                $res = $orderModel->where($where)->update(['status'=>$status[5],'return_refund_flag'=>0,'return_reason'=>'']);//待收货
                if($res){
                    $this->success('撤销成功');
                }else{
                    $this->error('撤销失败');
                }
            }
            $this->error('未找到订单');

        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (确认收货)
     * @ApiSummary  (确认收货)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/orders/confirmGoods)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571730709",
        "data": null
    })
     */
    public function confirmGoods(){
        if($this->request->isGet()){
            $order_id = $this->request->get('order_id');

            $rule = config('verify.order_detail');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_id'=>$order_id])) {
                $this->error($validate->getError());
            }

            //待收货
            $status = $this->order_status;
            $orderModel = new Order();
            $res = $orderModel->where(['id'=>$order_id,'uid'=>$this->uid,'status'=>$status[5]])->update(['status'=>$status[8]]);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

}