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
<?php

namespace Home;

use Configs\CacheConfig;
use Plugin\Cache;
use LibModels\Wap\Home\IndexData;
use LibModels\Wap\Home\OrderData;
use Plugin\Helpers;

/**
 * Description of Order
 *
 */
class OrderModel
{

    const CODE_PAYMENT = '04cf5abaa7c20178325a07c4a833782c'; //支付订单资源码
    const CODE_STROLL = 'a7989369aa86681c678bc40f171b8f1d'; //随便逛逛url地址资源码
    const CODE_LOGISTIC_BANNER = '1fc9b2484fcd559049f2f7e0db313f20'; // 物流详情banner资源码

    /**
     * 订单相关数据处理
     *
     * payment_type:
      '1' => array(//在线支付
      0 => '待付款',
      1 => '已付款',
      2 => '已付款',
      3 => '已付款',
      4 => '已发货',
      5 => '已发货',
      6 => '交易成功'
      ),
      '2' => array(//货到付款
      0 => '备货中',
      1 => '已付款',
      2 => '已付款',
      3 => '已付款',
      4 => '已发货',
      5 => '已发货',
      6 => '交易成功'
      ),
      '3' => array(//现金支付
      0 => '待付款',
      1 => '已付款',
      2 => '已付款',
      3 => '已付款',
      4 => '已发货',
      5 => '已发货',
      6 => '交易成功'
      ),
      '4' => array(//抵消支付
      0 => '待付款',
      1 => '已付款',
      2 => '已付款',
      3 => '已付款',
      4 => '已发货',
      5 => '已发货',
      6 => '交易成功'
      )
     */

    public static function getOrder($type, $page, $limit, $gender, $yh_channel, $uid)
    {
        $result = array();
        //调用接口获得数据
        $data = OrderData::getOrderData($type, $page, $limit, $gender, $yh_channel, $uid);

        // 判断是否还有数据, 没有数据则返回空
        if (isset($data['data']['page_total']) && $page > $data['data']['page_total']) {
            return $result;
        }
        //检查数据返回是否正常,正常则处理数据
        if (!empty($data['data']['order_list'])) {
            $count = 0;
            foreach ($data['data']['order_list'] as $key => $vo) {
                // 订单件数清零
                $count = 0;
                //根据订单status判断订单处于什么状态。
                $result[$key] = self::getOrderStatus($vo);
                //订单号,支付状态,订单商品数量,订单总价格
                $result[$key]['orderNum'] = $vo['order_code'];
                $result[$key]['orderStatus'] = $vo['status_str'];
                $result[$key]['sumCost'] = $vo['amount'];
                // 如果运费大于0,会显示运费
                if (floatval($vo['shipping_cost']) > 0) {
                    $result[$key]['shippingCost'] = $vo['shipping_cost'];
                }
                //类内调用格式化订单商品数据方法
                $tickets = false;
                if (isset($vo['virtual_type']) && $vo['virtual_type'] * 1 == 3) {
                    $tickets = true;
                    $result[$key]['isVirtual'] = true;//虚拟商品
                }
                $result[$key]['goods'] = Helpers::formatOrderGoods($vo['order_goods'], $count, false, $tickets);
                $result[$key]['detailUrl'] = Helpers::url('/home/orderdetail', array('order_code' => $vo['order_code']));
                $result[$key]['count'] = $count;
                $result[$key]['isDepositAdvance'] = $vo['attribute'] * 1 === 9;//定金预售
                //倒计时时间
                if (isset($vo['counter_flag']) && $vo['counter_flag'] == 'Y') {
                    $result[$key]['leftTime'] = $vo['pay_lefttime']*1000;
                }
            }
        }

        return $result;
    }

    /**
     * 查看物流
     *
     * @param int $orderCode 订单编号
     * @param int $uid 用户ID
     * @return array
     */
    public static function Logistics($orderCode, $uid)
    {
        $result = array();

        do {
            if (!isset($orderCode) || !is_numeric($uid)) {
                break;
            }

            // 获取物流详情页banner
            $banner = self::getLogisterBanner();
            if ($banner) {
                $result['banner'] = $banner;
            }

            $logistics = OrderData::LogisticsData($orderCode, $uid);
            if (empty($logistics['data'])) {
                break;
            }
            $result['logisticUrl'] = $logistics['data']['url'];
            $result['logisticImg'] = strtr($logistics['data']['logo'], array('http://' => '//'));
            $result['logisticCompany'] = $logistics['data']['caption'];
            $result['logisticNumber'] = $logistics['data']['express_number'];

            $build = array();
            foreach ($logistics['data']['express_detail'] as $value) {
                $build['status'] = $value['accept_address'];
                $build['date'] = $value['acceptTime'];
                $result['logisticDetail'][] = $build;
            }
        }
        while (false);

        return $result;
    }

    /**
     * 获取物流详情页banner
     *
     * @return array|bool
     */
    private static function getLogisterBanner()
    {
        $result = false;

        if (USE_CACHE) {
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get(CacheConfig::KEY_CODE_LOGISTIC_BANNER, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        // 调用接口获取数据
        $banner = IndexData::getBannerStart(self::CODE_LOGISTIC_BANNER);
        if (isset($banner['code']) && $banner['code'] == 200 && !empty($banner['data'])) {
            $result = array();
            // 处理数据
            foreach ($banner['data'] as $val) {
                foreach ($val['data'] as $single) {
                    $result['url'] = Helpers::getFilterUrl($single['url']);
                    $result['img'] = Helpers::getImageUrl($single['src'], 640, 200);
                }
            }
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get(CacheConfig::KEY_CODE_LOGISTIC_BANNER, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set(CacheConfig::KEY_CODE_LOGISTIC_BANNER, $result);
            }
        }

        return $result;
    }

    /**
     * 获取订单详情信息
     *
     * @param string $orderCode 订单号
     * @param int $uid 用户ID
     * @param string $sessionKey 用户的会话
     * @return array
     */
    public static function orderDetail($orderCode, $uid, $sessionKey)
    {
        $result = array();
        if (is_numeric($orderCode) && is_numeric($uid) && is_string($sessionKey)) {
            $orderDetail = OrderData::viewOrderData($orderCode, $uid, $sessionKey);
            if (isset($orderDetail['data']['order_code'])) {
                $count = 0;
                $orderDetail['data']['virtual_type'] = empty($orderDetail['data']['virtual_type']) ? '' : $orderDetail['data']['virtual_type'] * 1;
                $result = self::getOrderStatus($orderDetail['data'], true); // 订单状态
                //门票

                if (isset($orderDetail['data']['virtual_type']) && $orderDetail['data']['virtual_type'] == 3) {
                    //手机号
                    $result['isVirtual'] = true;
                    $result['mobile'] = isset($orderDetail['data']['mobile']) ? $orderDetail['data']['mobile'] : '';
                }
                $result['name'] = $orderDetail['data']['user_name'];
                $result['phoneNum'] = $orderDetail['data']['mobile'];
                $result['address'] = $orderDetail['data']['area'] . $orderDetail['data']['address'];
                $result['orderStatus'] = $orderDetail['data']['status_str'];
                $result['orderNum'] = $orderDetail['data']['order_code'];
                $result['orderTime'] = date('Y-m-d H:i:s', $orderDetail['data']['create_time']);
                //倒计时时间
                if (isset($orderDetail['data']['counter_flag']) && $orderDetail['data']['counter_flag'] == 'Y') {
                    $result['leftTime'] = $orderDetail['data']['pay_lefttime']*1000;
                }
                $result['goods'] = Helpers::formatOrderGoods($orderDetail['data']['order_goods'], $count, true, true);
                if (isset($orderDetail['data']['promo_code_amount'])) {
                    $result['promo_code_amount'] = self::filterOrderPrice($orderDetail['data']['promo_code_amount']); // 优惠码
                }
                $result['yohoCoin'] = $orderDetail['data']['yoho_give_coin']; // 有货币返回
                $result['price'] = $orderDetail['data']['amount']; // 实付金额
                $result['goodsAmount'] = $orderDetail['data']['payment_amount']; // 商品总金额没有人民币符号
                $result['orderCount'] = $count; // 订单总件数
                $result['isPay'] = $orderDetail['data']['payment_status'] === 'Y';
                //判断是否可以修改地址
                $result['changeable'] = $orderDetail['data']['can_update_delivery_address'] == 'Y' ? 'true' : 'false';
                //判断是否有关联订单
                $result['relation'] = $orderDetail['data']['relate_order_code'] == 'Y' ? 'true' : 'false';
                //修改地址跳转链接
                $result['url'] = Helpers::url('/home/addressModify', array('orderCode' => $orderCode, 'relation' => $result['relation']));
            }
            //相关金额(商品总金额,活动金额,运费,优惠券,有货币)
            if (isset($orderDetail['data']['promotion_formulas']) && $orderDetail['data']['promotion_formulas']) {
                foreach ($orderDetail['data']['promotion_formulas'] as $pricek => $pricev) {
                    $result['orderBalance'][$pricek] = array('promotion' => $pricev['promotion'], 'account' => $pricev['promotion_amount']);
                }
            }
            //为支付的拆单配送信息
            if (isset($orderDetail['data']['is_multi_package']) && $orderDetail['data']['is_multi_package'] == 'Y') {
                $result['isJit'] = true;
                $result['jitDetailUrl'] = Helpers::url('/cart/index/jitDetail', array('orderCode' => $orderCode, 'sessionKey' => $sessionKey));
            }

            $result['invoice'] = array();
            //电子发票
            do{
                if(empty($orderDetail['data']['invoice']) || !isset($orderDetail['data']['invoice'])){
                    break;
                }
                //判断是否显示电子发票相关信息
                if(isset($orderDetail['data']['invoice']['type']) == false && empty($orderDetail['data']['invoice']['type'])){
                    break;
                }
                $result['invoice']['type'] = $orderDetail['data']['invoice']['type'] * 1 === 2 ? true : false;//发票类型 纸质 1,电子 2
                $result['invoice']['title'] = $orderDetail['data']['invoice']['title'];//发票抬头
                $result['invoice']['contentValue'] = $orderDetail['data']['invoice']['contentValue'];//发票内容
                $result['invoice']['mobilePhone'] = $orderDetail['data']['invoice']['mobilePhone'];//联系电话
                //电子发票下载链接显示
                if($orderDetail['data']['invoice']['showInvoice'] == true && isset($orderDetail['data']['invoice']['pdfUrl'])){
                     $result['invoice']['pdfUrl'] = $orderDetail['data']['invoice']['pdfUrl'];
                }
            }
            while(false);
        }
        //取消订单原因列表
        $resons = OrderData::closeReasons();
        $result['cancelReason'] = isset($resons['data']) ? $resons['data'] : '';
        return $result;
    }

    /**
     * 根据type值设置nav属性
     */
    public static function getNavs($type)
    {
        $navType = array(1 => '全部', 2 => '待付款', 3 => '待发货', 4 => '待收货');
        $nav = array();
        foreach ($navType as $key => $value) {
            $act = false;
            if ($type == $key) {
                $act = true;
            }
            $nav[] = array(
                'name' => $value,
                'typeId' => $key,
                'active' => $act,
                'url' => Helpers::url('/home/orders', array('type' => $key))
            );
        }

        return $nav;
    }

    /**
     * 获取订单状态
     *
     * @param array $order 订单
     * @param bool $showLogistics 控制是否显示物流信息
     * @return array
     */
    private static function getOrderStatus($order, $showLogistics = false)
    {
        $result = array();

        //根据订单status判断订单处于什么状态。
        do {
            //订单取消状态 = Y 时,跳出判断订单状态循环,并设置订单状态为已取消。
            if ($order['is_cancel'] === 'Y') {
                $result['canceled'] = true;
                break;
            }

            //转数字
            $order['virtual_type'] = empty($order['virtual_type']) ? '' : $order['virtual_type'] * 1;

            // 先判断订单付款方式,根据不同的付款方式计算订单状态。(注:货到付款没有待付款状态)
            // 支付方式为非货到付款时,计算订单状态。
            if ($order['payment_type'] != 2) {
                switch ($order['status']) {
                    case 0: // 待付款
                        $result['unpaid'] = true;
                        $result['payUrl'] = Helpers::url('/home/orders/pay', array('order_code' => $order['order_code']));
                        break;
                    //未发货&未收货 状态,统一合并到待收货状态。
                    case 1:
                    case 2:
                    case 3:
                        //已付款状态不给查看物流URL
                        $result['unreceived'] = true;
                        break;
                    case 4:
                    case 5:
                        //已发货状态,给查看物流或二维码URL
                        $result['unreceived'] = true;
                        //门票
                        if (isset($order['virtual_type']) && $order['virtual_type'] === 3) {
                            $result['qrcode'] = Helpers::url('/home/QRcode/'.$order['order_code']);
                        }
                        //非门票
                        else {
                            self::assignExpressInfo($showLogistics, $order, $result);
                        }
                        break;

                    case 6:
                        $result['completed'] = true;
                        // 已成功订单,给查看物流或二维码URL
                        //门票
                        if (isset($order['virtual_type']) && $order['virtual_type'] === 3) {
                            $result['qrcode'] = Helpers::url('/home/QRcode/'.$order['order_code']);
                        }
                        //非门票
                        else {
                            self::assignExpressInfo($showLogistics, $order, $result);
                        }
                        break;
                    default:
                        break;
                }
                break;
            }

            // 订单为货到付款订单时,计算订单状态。(货到付款没有待付款状态)
            switch ($order['status']) {
                case 0: // 备货中
                    $result['unpaid'] = true;
                    break;
                case 1:
                case 2:
                case 3:
                    //备货中、已付款状态不给查看物流链接
                    $result['unreceived'] = true;
                    break;
                case 4:
                case 5:
                    //待收货状态,给查看物流url
                    $result['unreceived'] = true;
                    self::assignExpressInfo($showLogistics, $order, $result);
                    break;
                case 6:
                    $result['completed'] = true;
                    self::assignExpressInfo($showLogistics, $order, $result);
                    break;
                default:
                    break;
            }
        }
        while (false);

        return $result;
    }

    /**
     * 获取快递有关信息
     * @param boolean $showLogistics 是否显示
     * @param array $order 订单信息
     * @param array $result
     */
    private static function assignExpressInfo($showLogistics, $order, &$result)
    {
        if ($showLogistics && isset($order['express_company']['caption']) && isset($order['express_number'])) {
            $result['logisticsUrl'] = Helpers::url('/home/logistic', array('order_code' => $order['order_code']));
            $result['logisticsCompany'] = $order['express_company']['caption'];
            $result['logisticsNum'] =  $order['express_number'];
        }
    }

    /**
     * 过滤掉为0的价格数据
     *
     * @param $price
     * @return string 过滤之后的价格
     */
    private static function filterOrderPrice($price)
    {
        if (is_string($price) && strstr($price, '¥0.00') !== false) {
            return '';
        }
        else {
            return $price;
        }
    }

}