订单列表接口,分页

// 我的购买订单列表,分页
public function my_order_list() {
        // 待发货
        // 待确认
        // 全部(待发货,待确认,已完成,已取消【5,6】)
        $uid = (int)trim($_POST['uid']);
        if (!$uid){
            $this->json->setErr(10001,'缺少参数');
            $this->json->Send();
        }

        $type = (int)trim($_POST['type']); // 1 待发货 2 待收货 3 全部
        if (!$type){
            $this->json->setErr(10002,'缺少参数');
            $this->json->Send();
        }

        if (!isset($_POST['page']) || $_POST['page'] < 1) {
            $page = 1;
        } else {
            $page = $_POST['page'];
        }

        switch ($type) {
            case 1: // 待发货
                $where['status'] = 2;
                break;
            case 2: // 待收货
                $where['status'] = 3;
                break;
            case 3: // 全部
                $where['status'] = array('in','2,3,4,5,6');
                break;
        }
        $where['uid'] = $uid;
        $where['user_del'] = 1;

        $order_model = M('order');
        $count = $order_model->where($where)->count();
        $total_page = ceil($count / C('PAGE_NORMAL'));
        $total_page = $total_page ? $total_page : 1;

        if ($page > $total_page) {
            $this->json->setErr(10003,'没有更多了');
            $this->json->Send();
        }
        $limit = (($page - 1) * C('PAGE_NORMAL')) . "," .C('PAGE_NORMAL');
        $order_list  = $order_model->where($where)->limit($limit)->order('id desc')->field('id as order_id,order_num,total_payed_price,status')->select();
        if ($order_list) {
            $product_model = M('product');
            $order_product_model = M('order_product');
            foreach ($order_list as $k=>&$v) {
                $v['total_payed_price'] = $v['total_payed_price']/100;
                // 获取订单商品信息
                $order_product_list = $order_product_model->where(['order_id'=>$v['order_id']])->field('pid,pnum,p_name,p_price')->select();
                foreach ($order_product_list as $opk=>&$opv) {
                    $opv['p_price'] = $opv['p_price'] / 100;
                    $product_info = $product_model->where(['id'=>$opv['pid']])->find();
                    $opv['img']     = $product_info['title_img2'];
                }
                $v['product_list'] = $order_product_list;
            }
        }

        if ($order_list) {
            $returnData = ['total_page' => $total_page, 'order' => $order_list,'current_page'=>$page];
        } else {
            $returnData = ['total_page' => 0, 'order' => [],'current_page'=>$page];
        }

        $this->json->setErr(0, '获取成功');
        $this->json->setAttr('data',$returnData);
        $this->json->Send();
    }
// 订单列表
public function lists()
{
        $this->checkLogin();
        $ordersModel = D('Orders');
        $ordersGoodsModel = D('OrdersGoods');
        $goodsModel  = D('Goods');
        $goodsImagesModel = D('GoodsImages');
        $ordersRefundModel = D('OrdersRefund');

        if (!isset($_POST['page']) || $_POST['page'] < 1) {
            $_POST['page'] = 1;
        }

        $where = [
            'uid' => $_POST['uid'],
            'status' => 1,
        ];

        $orderType = $_POST['type'] ?: 0;

        switch ($orderType) {
            // is_pay = 0
        case 1:
            $where['is_pay'] = 0;
            $where['create_time'] = ['gt', time() - C('ORDER_EXPIRED')];
            break;
        case 2:
            $where['is_deliver'] = 0;
            $where['is_pay'] = 1;
            $where['is_cancel'] = 0;
            break;
        case 3:
            $where['is_get'] = 0;
            $where['is_pay'] = 1;
            $where['is_deliver']  = 1;
            break;
        case 4:
            $where['is_get'] = 1;
            $where['is_evaluate'] = 0;
            $where['is_cancel'] = 0;
            break;
        case 5:
            $where['is_cancel'] = 1;
            break;
        default:
            break;
        }


        $count = $ordersModel->where($where)->count();

        $hasPage = ceil($count / C('PAGE.LIMIT'));
        $hasPage = $hasPage ? $hasPage : 1;
        $limit = (($_POST['page'] - 1) * C('PAGE.LIMIT')) . "," .C('PAGE.LIMIT');

        $orders = $ordersModel->where($where)->limit($limit)->order('id desc')->select();

        foreach ($orders as $k => $v) {
            $good = $ordersGoodsModel->field('id,price,count,good_id,is_evaluate,load_data')->where(['order_id' => $v['id']])->select();
            $isEvaluate = 1;

            foreach ($good as $key => $val) {
                $loadDataGood = json_decode($val['load_data'], true);
                $orderGoodInfo = [
                    'id'       => $loadDataGood['id'],
                    'name'     => $loadDataGood['name'],
                    'img_list' => $loadDataGood['images'],
                ];

                $good[$key]['info'] = $orderGoodInfo; 
                if (!$val['is_evaluate']) {
                    $isEvaluate = $val['is_evaluate'];
                }
            }

            $orders[$k]['goods'] = $good;

            if ($v['is_cancel']) {
                $orders[$k]['refund_status'] = $ordersRefundModel->where(['id' => $v['order_refund_id']])->getField('status'); 
            } else {
                $orders[$k]['refund_status'] = 0; 
            }

            $orders[$k]['process_status'] = $ordersModel->status($v['id']);
            $orders[$k]['is_evaluate'] = $isEvaluate;
        }

        if ($orders) {
            $returnData = ['all_page' => $hasPage, 'order' => $orders];
        } else {
            $returnData = ['all_page' => 0, 'order' => []];
        }


        $this->json->setAttr("data", $returnData);
        $this->json->Send();

}
posted @ 2018-05-25 20:01  TBHacker  阅读(1008)  评论(0编辑  收藏  举报