Order.php 17.3 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
<?php

namespace app\api\controller;

use addons\shopro\model\Config;

/**
 * 订单接口
 */
class Order extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * @ApiWeigh    (99)
     * @ApiTitle    (订单列表)
     * @ApiSummary  (订单列表)
     * @ApiMethod   (GET)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=type, type=string, required=true, description="类型:all=全部,nopay=待支付,nosend=待发货,noget=待收货,nocomment=待评价,aftersale=售后,finish=已完成")
     *
     * @ApiReturn()
     */
    public function index()
    {
        $params = $this->request->get();

        $this->success('订单列表', \addons\shopro\model\Order::getList($params));
    }

    /**
     * @ApiWeigh    (97)
     * @ApiTitle    (订单详情)
     * @ApiSummary  (订单详情)
     * @ApiMethod   (GET)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=id, type=string, required=true, description="订单ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "订单详情",
        "time": "1609149282",
        "data": {
            "id": 17,
            "type": "goods",
            "order_sn": "202004332686004378001000",
            "user_id": 1,
            "activity_type": "groupon",
            "goods_amount": "20.00",
            "dispatch_amount": "8.00",
            "phone": "15133120361",
            "consignee": "he",
            "province_name": "北京市",
            "city_name": "北京市",
            "area_name": "东城区",
            "address": "123",
            "province_id": 110000,
            "city_id": 110100,
            "area_id": 110101,
            "status": 1,
            "memo": null,
            "remark": "",
            "total_amount": "28.00",
            "score_amount": 0,
            "total_fee": "28.00",
            "discount_fee": "0.00",
            "coupon_fee": "0.00",
            "pay_fee": "28.00",
            "score_fee": 0,
            "goods_original_amount": "200.00",
            "coupons_id": 0,
            "transaction_id": "",
            "payment_json": "{\"order_sn\":\"202004332686004378001000\",\"transaction_id\":\"\",\"notify_time\":\"2020-12-26 16:33:30\",\"buyer_email\":1,\"pay_fee\":\"28.00\",\"pay_type\":\"wallet\"}",
            "pay_type": "wallet",
            "paytime": "2020-12-26 16:33:30", //支付时间
            "ext": "{\"buy_type\":\"groupon\",\"groupon_id\":0,\"expired_time\":1608972506,\"send_time\":1609149254}",
            "platform": "H5",
            "createtime": "2020-12-26 16:33:26" //下单时间
            "item": [{ //订单商品
                "id": 19,
                "user_id": 1,
                "order_id": 17,
                "goods_id": 6, //商品ID
                "goods_type": "normal",
                "goods_sku_price_id": 9,
                "activity_id": 15,
                "activity_type": "groupon",
                "item_goods_sku_price_id": 18,
                "goods_sku_text": null,
                "goods_title": "测试商品6", //商品名称
                "goods_image": "https://yixiaoxian.qiniu.broing.cn/uploads/20201222/FgNsN1wvKd465nWhgQ3_wM2YCALd.png", //商品主图
                "goods_original_price": "200.00", //商品原始价格
                "discount_fee": "0.00", //折扣价格
                "goods_price": "20.00", //商品原价
                "goods_num": 1, //购买数量
                "dispatch_status": 1, //发货状态:0=未发货,1=已发货,2=已收货
                "dispatch_fee": "8.00", //邮费
                "dispatch_type": "express",
                "dispatch_id": 1,
                "store_id": 0,
                "aftersale_status": 0,
                "comment_status": 0, //评价状态:0=未评价,1=已评价
                "refund_status": 0,
                "refund_fee": null,
                "refund_msg": null,
                "express_name": "百世快递", //快递公司
                "express_code": "HTKY", //快递公司编号
                "express_no": "123456", //快递单号
                "ext": "[]",
                "status_code": "noget", //状态码
                "status_name": "待收货", //状态名称
                "status_desc": "等待买家收货", //状态注释
                "btns": [
                    "get",
                    "aftersale"
                ],
                "ext_arr": []
            }],
            "status_code": "noget", //状态码
            "status_name": "待收货", //状态名称
            "status_desc": "等待买家收货", //状态注释
            "btns": [
                "express"
            ],
            "ext_arr": { //拓展数据
                "buy_type": "groupon",
                "groupon_id": 0,
                "expired_time": 1608972506,
                "send_time": "2020-12-28 17:54:14", //发货时间
                "confirm_time": "2020-12-28 18:16:00" //收货时间
            }
        }
    })
     */
    public function detail()
    {
        $params = $this->request->get();
        $detail = \addons\shopro\model\Order::detail($params);
        $detail['createtime'] = date('Y-m-d H:i:s',$detail['createtime']);
        $detail['paytime'] = !empty($detail['paytime']) ? date('Y-m-d H:i:s',$detail['paytime']) : '';
        $ext_arr = $detail['ext_arr'];
        $detail['ext_arr']['send_time'] = !empty($ext_arr['send_time']) ? date('Y-m-d H:i:s',$ext_arr['send_time']) : '';
        $detail['ext_arr']['confirm_time'] = !empty($ext_arr['confirm_time']) ? date('Y-m-d H:i:s',$ext_arr['confirm_time']) : '';
        $this->success('订单详情', $detail);
    }

    /**
     * @ApiWeigh    (95)
     * @ApiTitle    (订单商品)
     * @ApiSummary  (订单商品)
     * @ApiMethod   (GET)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=order_item_id, type=string, required=true, description="订单商品ID")
     * @ApiParams  (name=order_sn, type=string, required=false, description="订单号")
     * @ApiParams  (name=id, type=string, required=false, description="订单ID")
     * @ApiParams  (name=type, type=string, required=false, description="类型:default=默认")
     *
     * @ApiReturn()
     */
    public function itemDetail()
    {
        $params = $this->request->get();
        $this->success('订单商品', \addons\shopro\model\Order::itemDetail($params));
    }


    /**
     * @ApiWeigh    (93)
     * @ApiTitle    (订单数量)
     * @ApiSummary  (订单数量)
     * @ApiMethod   (GET)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "订单数量",
        "time": "1609138878",
        "data": {
            "nopay": 2, //待支付(数量)
            "nosend": 3, //待发货(数量)
            "noget": 0, //待收货(数量)
            "nocomment": 0, //待评价(数量)
            "aftersale": 0, //售后(数量)
            "finish": 0 //已完成(数量)
        }
    })
     */
    public function statusNum () {
        $this->success('订单数量', \addons\shopro\model\Order::statusNum());
    }


    /**
     * @ApiWeigh    (91)
     * @ApiTitle    (取消订单)
     * @ApiSummary  (取消订单)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=id, type=string, required=false, description="订单ID")
     *
     * @ApiReturn()
     */
    public function cancel()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'cancel');

        $this->success('取消成功', \addons\shopro\model\Order::operCancel($params));
    }

    /**
     * @ApiWeigh    (89)
     * @ApiTitle    (删除订单)
     * @ApiSummary  (删除订单)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=id, type=string, required=false, description="订单ID")
     *
     * @ApiReturn()
     */
    public function delete()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'delete');

        $this->success('删除成功', \addons\shopro\model\Order::operDelete($params));
    }

    /**
     * @ApiWeigh    (87)
     * @ApiTitle    (确认收货)
     * @ApiSummary  (确认收货)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=id, type=string, required=false, description="订单ID")
     * @ApiParams  (name=order_item_id, type=string, required=true, description="订单商品ID")
     *
     * @ApiReturn()
     */
    public function confirm()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'confirm');

        $this->success('收货成功', \addons\shopro\model\Order::operConfirm($params));
    }


    // 申请售后 (已废弃)
    // public function aftersale()
    // {
    //     $params = $this->request->post();

    //     // 表单验证
    //     $this->shoproValidate($params, get_class(), 'aftersale');

    //     $this->success('申请成功', \addons\shopro\model\Order::operAftersale($params));
    // }


    // 申请退款 (已废弃)
    // public function refund()
    // {
    //     $params = $this->request->post();

    //     // 表单验证
    //     $this->shoproValidate($params, get_class(), 'refund');

    //     $this->success('申请成功', \addons\shopro\model\Order::operRefund($params));
    // }

    /**
     * @ApiWeigh    (85)
     * @ApiTitle    (订单评价)
     * @ApiSummary  (订单评价)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=id, type=string, required=false, description="订单ID")
     * @ApiParams  (name=order_item_id, type=string, required=true, description="订单商品ID")
     * @ApiParams  (name=content, type=string, required=true, description="评价内容")
     * @ApiParams  (name=images, type=string, required=false, description="评价图片,数组")
     * @ApiParams  (name=video, type=string, required=false, description="评价视频")
     *
     * @ApiReturn()
     */
    public function comment () 
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'comment');

        $this->success('评价成功', \addons\shopro\model\Order::operComment($params));
    }

    /**
     * @ApiWeigh    (83)
     * @ApiTitle    (计算订单)
     * @ApiSummary  (计算订单)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=goods_list, type=object, required=false, description="购买的商品:[{goods_id: 3, goods_num: 1, sku_price_id: 3, goods_price: 0.10}]")
     * @ApiParams  (name=from, type=string, required=false, description="来源:goods")
     * @ApiParams  (name=address_id, type=inter, required=false, description="收货地址ID")
     * @ApiParams  (name=coupons_id, type=inter, required=false, description="优惠券ID")
     * @ApiParams  (name=order_type, type=inter, required=false, description="订单类型:goods")
     * @ApiParams  (name=buy_type, type=string, required=true, description="拼团的 购买方式: alone=单独购买,groupon=开团")
     * @ApiParams  (name=groupon_id, type=inter, required=false, description="拼团的 团 id")
     *
     * @ApiReturn()
     */
    public function pre()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'pre');

        $result = \addons\shopro\model\Order::pre($params);

        $this->success('计算成功', $result);
    }

    /**
     * @ApiWeigh    (81)
     * @ApiTitle    (创建订单)
     * @ApiSummary  (创建订单)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=goods_list, type=object, required=false, description="购买的商品:[{goods_id: 3, goods_num: 1, sku_price_id: 3, goods_price: 0.10}]")
     * @ApiParams  (name=from, type=string, required=false, description="来源:goods=商品详情,cart=购物车")
     * @ApiParams  (name=address_id, type=inter, required=false, description="收货地址ID")
     * @ApiParams  (name=coupons_id, type=inter, required=false, description="优惠券ID")
     * @ApiParams  (name=order_type, type=inter, required=false, description="订单类型:goods")
     * @ApiParams  (name=buy_type, type=string, required=true, description="拼团的 购买方式: alone=单独购买,groupon=开团")
     * @ApiParams  (name=groupon_id, type=inter, required=false, description="拼团的 团 id")
     * @ApiParams  (name=remark, type=string, required=false, description="买家留言")
     *
     * @ApiReturn()
     */
    public function createOrder() 
    {
        $params = $this->request->post();
        $params['goods_list'] = is_string($params['goods_list']) ? json_decode(htmlspecialchars_decode($params['goods_list']),true) : $params['goods_list'];
        // 表单验证
        $this->shoproValidate($params, get_class(), 'createOrder');

        $order = \addons\shopro\model\Order::createOrder($params);

        $this->success('订单添加成功', $order);
    }

    /**
     * @ApiWeigh    (79)
     * @ApiTitle    (获取可用优惠券列表)
     * @ApiSummary  (获取可用优惠券列表)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams  (name=goods_list, type=object, required=false, description="购买的商品:[{goods_id: 3, goods_num: 1, sku_price_id: 3, goods_price: 0.10}]")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "获取成功",
        "time": "1609222988",
        "data": [{
            "id": 3, //优惠券ID
            "name": "无门槛优惠券", //优惠券名称
            "type": "cash",
            "goods_ids": "0",
            "amount": "0.01", //券面额
            "enough": "0.00", //消费门槛
            "stock": 99,
            "limit": 1,
            "gettime": {
                "start": 1609171200,
                "end": 1609344000
            },
            "usetime": { //有效期
                "start": "2020.12.29", //开始时间
                "end": "2020.12.31" //结束时间
            },
            "description": "", //优惠券描述
            "usetimestart": 1609171200,
            "usetimeend": 1609344000,
            "gettimestart": 1609171200,
            "gettimeend": 1609344000,
            "user_coupons_id": 2, //领取ID
            "status_code": "no_use", //状态码:no_use=未使用,used=已使用,expired=已过期
            "status_name": "未使用" //状态名称
        }]
    })
     */
    public function coupons () {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'coupons');

        $coupons = \addons\shopro\model\Order::coupons($params);

        $this->success('获取成功', $coupons);
    }

    /**
     * @ApiWeigh    (77)
     * @ApiTitle    (支付倒计时)
     * @ApiSummary  (支付倒计时)
     * @ApiMethod   (GET)
     *
     * @ApiParams  (name=id, type=inter, required=true, description="订单ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "获取成功",
        "time": "1609132387",
        "data": {
            "minute": 0, //分钟
            "second": 0 //秒
        }
    })
     */
    public function paycountdown () {
        $params = $this->request->get();
        $order = \addons\shopro\model\Order::detail($params);

        // 本场倒计时
            // 默认取第一个商品
            $goods_one = $order['item'][0];
            $activity = (new \addons\shopro\model\Goods)->getActivity($goods_one['goods_id']);
            $activity_one = $activity ?? null;

            // 获取第一个商品的活动规则,活动不存在,自动会使用全局自动关闭
            $rules = $activity_one['rules'] ?? [];
            // 优先使用活动的订单关闭时间
            if (isset($rules['order_auto_close']) && $rules['order_auto_close'] > 0) {
                $close_minue = $rules['order_auto_close'];
            } else {
                // 添加自动关闭队列
                $config = Config::where('name', 'order')->cache(300)->find();        // 读取配置自动缓存 5 分钟
                $config = isset($config) ? json_decode($config['value'], true) : [];

                $close_minue = (isset($config['order_auto_close']) && $config['order_auto_close'] > 0)
                    ? $config['order_auto_close'] : 15;
            }
        $lefttime = 60 * $close_minue - (time() - $order['createtime']);
        $lefttime = $lefttime > 0 ? $lefttime : 0;
        $paycountdown = [
            'minute' => $lefttime / 60 % 60,//  倒计时剩余的分钟数
            'second' => $lefttime % 60,//  倒计时剩余的秒数
        ];
        $this->success('获取成功', $paycountdown);
    }
    
}