Skip to content
关注公众号,获取新课通知

订单列表


控制器 app/controller/admin/Order.php

php
protected $excludeValidateCheck = ['index'];
// 后台订单列表
public function orderList(){
	$param = request()->param();
       $limit = intval(getValByKey('limit',$param,10));
       $tab = getValByKey('tab',$param,'all');
       $model = $this->M;
       // 订单类型
       switch ($tab) {
       	case 'nopay': // 待付款
       		$model = $this->M->where('closed',0)
       						->whereNull('payment_method');
       		break;
       	case 'noship': // 待发货
       		$model = $this->M->where('closed',0)
       						->whereNotNull('payment_method')
       						->where('ship_status','pending')
       						->where('refund_status','pending');
       		break;
       	case 'shiped': // 已发货
       		$model = $this->M->where('closed',0)
       						->whereNotNull('payment_method')
       						->where('ship_status','delivered')
       						->where('refund_status','pending');
       		break;
       	case 'received': // 已收货
       		$model = $this->M->where('closed',0)
       						->whereNotNull('payment_method')
       						->where('ship_status','received')
       						->where('refund_status','pending');
       		break;
       	case 'finish': // 已完成
       		$model = $this->M->where('closed',0)
       						->whereNotNull('payment_method')
       						->where('ship_status','received')
       						->where('refund_status','pending');
       		break;
       	case 'closed': // 已关闭
       		$model = $this->M->where('closed',1);
       		break;
       	case 'refunding': // 退款中
       		$model = $this->M->where('closed',0)
       						->where('refund_status','applied');
       		break;
       }
       // 搜索条件
       if (array_key_exists('starttime',$param) && array_key_exists('endtime',$param)) {
       	$model = $model->whereTime('create_time', 'between', [$param['starttime'], $param['endtime']]);
       }
       if (array_key_exists('no',$param)) {
       	$model = $model->where('no','like','%'.$param['no'].'%');
       }
       if (array_key_exists('name',$param)) {
       	$model = $model->where('address->name','like','%'.$param['name'].'%');
       }
       if (array_key_exists('phone',$param)) {
       	$model = $model->where('address->phone','like','%'.$param['phone'].'%');
       }
       
       $totalCount = $model->count();
       $list = $model->page($param['page'],$limit)
	        ->with(['orderItems.goodsItem','user'])
	        ->order([ 'id'=>'desc' ])
			->select();
       return showSuccess([
       	'list'=>$list,
       	'totalCount'=>$totalCount
       ]);
}

模型 app/model/Order.php

php
	// 只读字段
    protected $readonly = ['no'];

    // 定义json字段
    protected $json = ['address','ship_data','extra'];

    // 退款状态地图
    public static $refundStatusMap = [
        'pending'=>'未退款',
        'applied'=>'已申请退款',
        'processing'=>'退款中',
        'success'=>'退款成功',
        'failed'=>'退款失败',
    ];

    // 物流状态地图
    public static $shipStatusMap = [
        'pending'=>'未发货',
        'delivered'=>'已发货',
        'received'=>'已收货',
    ];

	// 关联用户
    public function user(){
        return $this->belongsTo(\app\model\common\User::class);
    }
	
	// 关联商品
    public function goodsItem(){
    	return $this->belongsTo('goods','goods_id');
    }

	// 全部
    public function scopeAll($query)
    {
        $query->where('closed',0);
    }
	// 待支付
    public function scopePaying($query)
    {
        $query->whereNull('paid_time')
              ->where('closed',0);
    }
    // 待收货
    public function scopeReceiving($query)
    {
        $query->whereNotNull('paid_time')
              ->where([
                ['ship_status','<>','received'],
                ['closed','=',0]
            ]);
    }
    // 待评价
    public function scopeReviewing($query)
    {
        $query->whereNotNull('paid_time')
              ->where([
                ['ship_status','=','received'],
                ['reviewed','=',0],
                ['closed','=',0]
            ]);
    }

    // 关联订单商品
    public function orderItems(){
        return $this->hasMany('OrderItem');
    }

验证器 app/validate/Order.php

php
protected $rule = [
    'page'=>'require|integer|>:0',
    'limit'=>"integer",
];
protected $scene = [
    // ...
    'orderList'=>['page','limit'],
    ...
];

路由 router/admin.php

php
Route::get('order/:page','admin.Order/orderList');