<?php
/**
 *
 * ============================================================================
 * * 版权所有 蜘蛛出行 * *
 * 网站地址: http://www.zhizhuchuxing.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author By: 娄梦宁
 * PhpStorm FxUserController.php
 * Create By 2017/3/1 17:36 $
 */

namespace fx\controllers\user;

use common\Controller\FxWebController;
use common\util\AliPay;
use common\util\PayService;
use common\util\Util;
use common\util\WeChatPay;
use fx\service\user\FxUserService;
use fx\util\FxUtil;
use Yii;

class FxUserController extends FxWebController
{

    public function service()
    {
        if ($this->service == '') {
            $this->service = new FxUserService();
        }
        return $this->service;
    }

    public function actionDoLogin()
    {
        return '分销页面';
    }

    //我的佣金
    public function actionMyCommission()
    {
        $result = $this->service()->myCommission();
        return json_encode($result);
    }

    //提现申请
    public function actionCashCommission()
    {
        $apply_commission = Yii::$app->request->post('apply_commission', '');
        $result = $this->service()->CashCommission($apply_commission);
        return json_encode($result);
    }

    //订单列表
    public function actionOrderList()
    {
        $params = [
            'is_refund' => $this->_post('is_refund', ''),//是否返佣  1待返佣 2返佣 3已取消
            'status' => $this->_post('status', ''),//订单状态 1 待支付 2：已支付 3：已完成 4：已退款5:已取消
            'current_page' => $this->_post('current_page', 1),//当前页
            'page_size' => $this->_post('page_size', 10),//每页展示条数
        ];
        $result = $this->service()->FxOrderList($params);
        return json_encode($result);
    }

    /**
     * Des:获取订单详细信息进行展示在订单详情页
     * Name: actionGetOrderInfo
     * @return string
     * @author 倪宗锋
     */
    public function actionGetOrderInfo()
    {
        $order_id = $this->_post('order_id', '');//订单ID
        $getOrderInfo = $this->service()->getOrderInfo($order_id);
        return json_encode($getOrderInfo);
    }

    /**
     * Des:变更订单价格
     * Name: actionChangeOrderMoney
     * @return string
     * @author 倪宗锋
     */
    public function actionChangeOrderMoney()
    {
        $params = [
            'money' => $this->_post('money', ''),//金额
            'order_id' => $this->_post('order_id', ''),//订单ID
        ];
        $result = $this->service()->changeOrderMoney($params);
        return json_encode($result);
    }

    //提现明细
    public function actionApplyCommissionDetail()
    {
        $result = $this->service()->ApplyCommissionDetail();
        return json_encode($result);
    }

    //账户充值
    public function actionRecharge()
    {
        $params = [
            'amount' => $this->_post('amount', ''),
            'payType' => $this->_post('payType', 1),
            'fx_uid' => FxUtil::$uid
        ];
        $result = $this->service()->recharge($params);
        return json_encode($result);
    }

    //充值数据接收
    public function actionRechargeNotify()
    {
        $return['return_code'] = 'FAIL';
        $return['return_msg'] = '未知错误！';
        $appId = $this->_post('auth_app_id', '');
        $aliPay = AliPay::getAliPayConfig();
        //*******************支付宝********************//
        if ($appId == $aliPay['app_id']) {//如果相同 这是支付宝付款成功回调 否则是微信付款回调
            $post = $_POST;
            /***支付结果判定***/
            if ($post['trade_status'] == 'TRADE_SUCCESS') {
                $payFlag = $this->service()->payRecharge($post);
                if ($payFlag['flag'] == true) {
                    //设置返回值
                    $return = array(
                        'return_code' => 'SUCCESS',
                        'return_msg' => 'OK'
                    );
                    return Util::arrayToXml($return);//转换为xml返回
                }
                $return['return_msg'] = $payFlag['msg'];
            }
        } else {
            //***************微信************************//
            $getContent = file_get_contents("php://input");
            $params = Util::xmlToArray($getContent);
            if (empty($params)) {
                return Util::arrayToXml($return);//转换为xml返回
            }
            $checkSign = WeChatPay::getSign($params);

            /**签名校验*/
            if ($params['sign'] == $checkSign) {
                /**支付结果判定*/
                if ($params['result_code'] == 'SUCCESS' && $params['return_code'] == 'SUCCESS') {
                    $payFlag = $this->service()->payRecharge($params);
                    if ($payFlag['flag'] == true) {
                        //设置返回值
                        $return = array(
                            'return_code' => 'SUCCESS',
                            'return_msg' => 'OK'
                        );
                        return Util::arrayToXml($return);//转换为xml返回
                    }
                    $return['return_msg'] = $payFlag['msg'];
                }
            }
        }
        return Util::arrayToXml($return);//转换为xml返回
    }

    //获取账户余额列表
    public function actionGetAmountLog()
    {
        $params = [
            'current_page' => $this->_post('current_page', 1),//显示页编号
            'page_size' => $this->_post('page_size', 20),//每页显示数量
            'fx_uid' => FxUtil::$uid
        ];
        $return = $this->service()->getAmountLog($params);
        return json_encode($return);
    }

    //充值记录列表
    public function actionRechargeList()
    {
        $params = [
            'current_page' => $this->_post('current_page', 1),//显示页编号
            'page_size' => $this->_post('page_size', 20),//每页显示数量
            'fx_uid' => FxUtil::$uid
        ];
        $return = $this->service()->getRechargeList($params);
        return json_encode($return);
    }

    //pc端登录入口
    public function actionPc()
    {
        $config = Util::getSiteConfig();
        if (empty($config['pc_uid'])) {
            $msg = '非法请求！';
            include ROOT_PATH . '/web/fx/fx_disable.phtml';
            return '';
        }
        $cache = Yii::$app->cache;
        $tokenKey = 'FX_LOGIN_' . YII_ENV . '_' . $config['pc_uid'];
        $userToken = $cache->get($tokenKey);
        setcookie('fx_token', $userToken['fx_token'], time() + 3600000, "/");
        return $this->redirect('/web/fx/reserve_prod.html');
    }

}
