Orders.php 9.88 KB
<?php

use Action\WebAction;
use WebPlugin\Helpers;
use Home\UserModel;
use Home\OrderModel;
use LibModels\Web\Home\OrderData;
use Configs\ChannelConfig;
use WebPlugin\Paging;
use Shopping\CartModel;

/**
 * 订单
 */
class OrdersController extends WebAction
{

    /**
     * 我的订单
     */
    public function indexAction()
    {
        //判断是否登录
        $uid = $this->auditJumpLogin(false, Helpers::url('/home/orders'));

        //头部导航
        $channel = Helpers::getChannelNameByCookie();
        $this->setWebNavHeader($channel);
        //面包屑-左侧导航
        $path = UserModel::getCenterCrumb('订单中心');
        $leftNav = UserModel::getCenterLeftNav('我的订单', $uid);
        //分页
        $page = $this->get('page', 1);
        $type = $this->get('type', 1);
        $limit = 10;
        $paging = new Paging('yoho');

        //订单 type-8-历史
        if ($type == ChannelConfig::$historyType) {
            $orders = OrderModel::getHistoryOrders($uid, $page, $limit);
        }
        else {
            $orders = OrderModel::getOrders($uid, $page, $limit, $type, TRUE);
        }

        $total = isset($orders['pager']['total']) ? $orders['pager']['total'] : 0;
        $pageTotal = isset($orders['pager']['pageTotal']) ? $orders['pager']['pageTotal'] : 0;
        $page = isset($orders['pager']['page']) ? $orders['pager']['page'] : 0;
        unset($orders['pager']);
        $paging->setTotal($total)->setSize($limit)->setQuery(array('page' => $page, 'type' => $type));
        //取消订单原因列表
        $resons = OrderData::closeReasons();
        $cancelReason = isset($resons['data']) ? $resons['data'] : '';

        //数据整合
        $data = array(
            'path' => $path,
            'userThumb' => UserModel::getUserHeadImg($uid),
            'homeNav' => $leftNav,
            'tabs' => OrderModel::getOrderTabs($type),
            'cancelReason' => $cancelReason,
            'pager' => array(
                'count' => $total,
                'curPage' => $page,
                'totalPages' => $pageTotal,
                'pagerHtml' => $paging->view(false)
            )
        );

        //历史订单/普通订单
        $orderKey = ($type == ChannelConfig::$historyType) ? 'historyOrders' : 'orders';
        $data[$orderKey] = $orders;
        $this->_view->display('orders', array('meOrdersPage' => true, 'meOrders' => $data));
    }

    /**
     * 订单详情
     */
    public function detailAction()
    {
        //判断是否登录
        $uid = $this->auditJumpLogin();

        //头部导航
        $channel = Helpers::getChannelNameByCookie();
        $this->setWebNavHeader($channel);
        //面包屑-左侧导航
        $path = UserModel::getCenterCrumb('订单详细', array('href' => Helpers::url('/home/orders'), 'name' => '订单中心'));
        $leftNav = UserModel::getCenterLeftNav('我的订单', $uid);

        $orderCode = $this->get('orderCode', '');
        //兼容老网站传参方式
        if (!$orderCode) {
            $orderCode = $this->get('order_code', '');
        }
        $detail = OrderModel::getOrderDetail($uid, $orderCode);
//        print_r($detail);
        //取消订单原因列表
        $resons = OrderData::closeReasons();
        $cancelReason = isset($resons['data']) ? $resons['data'] : '';
        //非当前用户的订单,跳转个首
        if (isset($detail['code']) && $detail['code'] == 400) {
            $this->go(Helpers::url('/home'));
        }
        $data = array(
            'path' => $path,
            'userThumb' => UserModel::getUserHeadImg($uid),
            'homeNav' => $leftNav
        );
        $packages = false;
        if ($detail) {
            $data += array(
                'orderNum' => $detail['orderNum'],
                'curStatus' => $detail['statusStr'],
                'complete' => $detail['complete'],
                'changeable' => $detail['changeable'],
                'progress' => $detail['progress'],
                'remark' => $detail['remark'],
                'goods' => $detail['goods'],
                'yohoCoinUrl' => $detail['yohoCoinUrl'],
                'orderBalance' => isset($detail['orderBalance']) ? $detail['orderBalance'] : array(),
                'operation' => $detail['operation'],
                'totalYoho' => $detail['yoho_give_coin'],
            );
            if (isset($detail['noramlPayMode'])) {
                $data['orderInfo'] = $detail['orderInfo'];
                $data['traceOrder'] = $detail['traceOrder'];
                $data['noramlPayMode'] = $detail['noramlPayMode'];
            }
            else if (isset($detail['virtualPayMode'])) {
                $data['virtualPayMode'] = $detail['virtualPayMode'];
            }
            //JIT拆单
            $packages = $detail['packages'];
        }

        $this->_view->display('order-detail', array('meOrdersPage' => true, 'meOrderDetail' => $data, 'packages' => $packages, 'cancelReason' => $cancelReason));
    }

    /**
     * 二维码票
     */
    public function ticketsAction()
    {
        //判断是否登录
        $uid = $this->auditJumpLogin();

        //头部导航
        $channel = Helpers::getChannelNameByCookie();
        $this->setWebNavHeader($channel);
        //面包屑-左侧导航
        $path = UserModel::getCenterCrumb('查看二维码', array('href' => Helpers::url('/home/orders'), 'name' => '订单中心'));
        $leftNav = UserModel::getCenterLeftNav('我的订单', $uid);

        $orderCode = $this->get('orderCode', '');
        //兼容老网站传参方式
        if (!$orderCode) {
            $orderCode = $this->get('order_code', '');
        }
        $detail = OrderModel::getOrderDetail($uid, $orderCode);
        //非当前用户的订单,跳转个首
        if (isset($detail['code']) && $detail['code'] == 400) {
            $this->go(Helpers::url('/home'));
        }

        if (isset($detail) && $detail) {
            $tickets = OrderModel::getTicketsInfo($orderCode);
            $data = array(
                'path' => $path,
                'userThumb' => UserModel::getUserHeadImg($uid),
                'homeNav' => $leftNav,
                'name' => $detail['goods'][0]['name'],
                'num' => $detail['goods'][0]['num'],
                'orderNum' => $detail['orderNum'],
                'myOrdersUrl' => Helpers::url('/home/orders'),
                'tickets' => $tickets
            );
        }

        $this->_view->display('ticket', array('meOrderTicket' => $data));
    }

    /**
     * 取消订单-ajax
     */
    public function cancelOrderAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {

            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            $uid = $this->auditJumpLogin();
            $orderCode = trim($this->post('orderCode', '')); //订单编号
            $reason = $this->post('reason', ''); //取消订单原因
            $reasonid = $this->post('reasonId', ''); //取消原因id
            $data = OrderData::cancelUserOrder($uid, $orderCode, $reason, $reasonid);
            if (!isset($data['code'])) {
                break;
            }
        }
        while (false);

        $this->echoJson($data);
    }

    /**
     * 确认订单-ajax
     */
    public function confirmOrderAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            $uid = $this->auditJumpLogin();
            $orderCode = trim($this->post('orderCode', ''));
            $data = OrderData::confirmUserOrder($uid, $orderCode);
            if (!isset($data['code'])) {
                break;
            }
        }
        while (false);

        $this->echoJson($data);
    }

    /**
     * 获取省市区县信息列表
     *
     * @param int id
     * @return json
     */
//    public function getAreaListAction()
//    {
//        $result = array('code' => 200, 'message' => '地区信息', 'data' => array());
//
//        if ($this->isAjax()) {
//            $id = $this->get('id', 0);
//            $result['data'] = CartModel::getAreaList($id);
//        }
//
//        $this->echoJson($result);
//    }

    /**
     * 订单详情页地址修改
     */
    public function modifyAddressAction()
    {
        $result = array('code' => 400, 'message' => '', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
//            获取相关参数
            $order_code = $this->get('orderCode', ''); //订单号
            $user_name = $this->get('userName', ''); //收货人
            $area_code = $this->get('areaCode', ''); //区号
            $address = $this->get('address', ''); //地址
            $mobile = $this->get('mobile', ''); //手机号码
            $phoneNum = $this->get('phoneNum', ''); //固定电话
            $phoneCode = $this->get('phoneCode', ''); //电话编号
            if(!empty($phoneNum) && !empty($phoneCode)){
               $phone = $phoneCode.'-'.$phoneNum;
            }          
//            $order_code = 1616956289; //订单号
//            $user_name = '接口测试'; //收货人
//            $area_code = 321023; //区号
//            $address = '测试地址'; //地址
//            $mobile = 18021408798; //手机号码
            if (!$order_code || !$user_name || !$area_code || !$address) {
                $result = array('code' => 400, 'message' => '缺失必填项', 'data' => '');
                break;
            }
            $result = OrderData::updateDeliveryAddress($order_code, $address_id, $user_name, $area_code, $address, $mobile, $phone);
            if (!isset($result['code'])) {
                break;
            }
        }
        while (false);

        $this->echoJson($result);
    }

}