SubmitOrderController.class.php 2.84 KB
<?php
/**
 * Created by PhpStorm.
 * User: yingcai
 * Date: 2018/2/1
 * Time: 下午4:47
 */

namespace Apicp\Controller\LiveOrder;

use Common\Common\Order;

class SubmitOrderController extends AbstractController
{
    /**
     * SubmitOrder
     * @author houyingcai
     * @desc 提交购买并发人数订单接口
     * @param Int nums:true:100 购买数量
     * @param Int is_read:true:1 是否阅读并同意签署服务协议(1=是,2=否)
     * @return array|bool 商品信息
                array(
                    'order_id' => 123, // 订单ID
                    'order_desc' => '员圈直播', // 订单内容
                    'total_price' => 24518, // 订单总金额(单位:元)
                    'order_sn' => '20180117170840644', // 订单编号
                    'created' => 1517486261770, // 创建时间
                    'buy_content' => array( // 购买内容
                        'nums' => 100, // 购买人数
                        'years' => 1, // 购买年数
                    ),
                )
     */
    public function Index_post()
    {
        $params = I('post.');

        $nums = intval($params['nums']);

        // 购买数量不能为0
        if (!$nums) {

            E('_ERR_LIVE_BUY_CONCURRENT_NUMS');
        }
        // 未阅读并同意签署服务协议
        if (intval($params['is_read']) != 1) {

            E('_ERR_READ_AGREEMENT_NOT');
        }

        $orderServ = &Order::instance();
        // 获取购买并发人数规则
        $result = $orderServ->fetchProductRule(['prCostKey' => 'live_cast_rule']);
        $rule = json_decode($result['prCostValue'], true);

        // 计算所需金额(单位:分)
        $amount = $rule['price'] * $nums;

        $orderParams = [
            'num' => $nums,
            'ordTotalPrice' => $amount,
            'eaId' => $this->_login->user['eaId'],
            'prCostKey' => 'live_cast_rule',
        ];
        if (!empty($this->_login->user['eaMobile'])) {
            $orderParams['eaMobile'] = $this->_login->user['eaMobile'];
        }
        if (!empty($this->_login->user['eaRealname'])) {
            $orderParams['eaRealname'] = $this->_login->user['eaRealname'];
        }


        $orderDetail = $orderServ->addOrder($orderParams);

        // 购买内容
        $buy_content = json_decode($orderDetail['ordContent'], true);

        // 返回参数
        $this->_result = [
            'order_id' => $orderDetail['ordId'],
            'order_desc' => $orderDetail['ordTitle'],
            'total_price' => $orderDetail['ordTotalPrice'],
            'order_sn' => $orderDetail['ordCode'],
            'created' => $orderDetail['ordCreated'],
            'buy_content' => [
                'nums' => $buy_content['personAmount'],
                'years' => $buy_content['year'],
            ],
        ];

        return true;
    }
}