<?php
/**
 * 微信接口类 提供给微信调用的接口
 * ============================================================================
 * * 版权所有 蜘蛛出行 * *
 * 网站地址: http://www.zhizhuchuxing.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author By: 倪宗锋
 * PhpStorm WeChatController.php
 * Create By 2017/3/2 11:06 $
 */
namespace shop\controllers\weChat;


use common\Controller\ShopWebController;
use common\models\OrderMain;
use common\util\PayService;
use common\util\Util;

use common\util\WeChatService;
use shop\util\ShopUtil;

class WeChatController extends ShopWebController
{
    public $service = null;

    /**
     * Des: 二维码跳转首页入口
     * Name: actionHome
     * @author 倪宗锋
     */
    public function actionQRCodeHome()
    {
        $id = $this->_get('id', '0');//0默认为非分销商销售
        if (Util::checkPattern('intVal', $id)) {
            setcookie('fx_uid', $id, time() + 3600000, "/");
        }
        $url = '/web/shop/reserve_prod.html?fx_uid=' . $id;
        $this->redirect($url, 302);
    }

    /**
     * Des:授权登录
     * Name: actionAuthLogin
     * @author 倪宗锋
     */
    public function actionAuthLogin()
    {
        $redirect = $this->_get('redirect');
        $url = ShopUtil::getAuthLoginUrl($redirect);
        return $this->redirect($url);
    }

    /**
     * Des:页面跳转
     * Name: setLoginAction
     * @return string
     * @author 倪宗锋
     */
    public function actionSetLogin()
    {
        $code = $this->_get('code', '');
        $token = WeChatService::getToken($code);
        if ($token['flag'] == true && empty($token['data']['openid']) == false) {
            $openid = $token['data']['openid'];
            $setLogin = ShopUtil::setLoginByOpenid($openid);//设置用户登录
            if ($setLogin['flag'] == false) {
                return $setLogin['msg'];
            }
        } else {
            $checkLogin = ShopUtil::checkLogin();
            if ($checkLogin['flag'] == false) {
                $msg = '授权登录失败！请稍候再试！';
                include ROOT_PATH . '/web/shop/shop_disable.phtml';
                return '';
            }
        }

        $url = $this->_get('redirect', '');
        $siteConfig = ShopUtil::getSiteConfig();
        $url = empty($url) ? $siteConfig['host_name'] : $url;
        return $this->redirect($url);
    }

    /**
     * Des:校验用户登录
     * Name: actionCheckLogin
     * @return string
     * @author 倪宗锋
     */
    public function actionCheckLogin()
    {
        $checkLogin = ShopUtil::checkLogin();
        if ($checkLogin['flag'] == false) {
            return json_encode($checkLogin);
        }
        $userInfo = ShopUtil::getUserInfo();
        return $userInfo['data']['openid'] . PHP_EOL . $userInfo['data']['nickname'];
    }

    public function actionTest()
    {
        $orderId = $this->_get('id', '');
        $getAppType = Util::checkWeChatOrAliPay();
        if($getAppType == 1) {
            $checkLogin = ShopUtil::checkLogin();
            if($checkLogin['flag'] == false) {
                $redirect ='/shop/?r=weChat/we-chat/test&id='.$orderId;
                $url = ShopUtil::getAuthLoginUrl($redirect);
                return $this->redirect($url);
            }
        }
        $getOrderInfo = $this->checkOrderCanPay($orderId);
        if($getOrderInfo['flag'] == false) {
            $msg = $getOrderInfo['msg'];
            include ROOT_PATH . '/web/fx/fx_disable.phtml';
            return '';
        }
        $orderInfo = $getOrderInfo['data'];
        $params = array(
            'order_id' => $orderId .'_'. rand(1000, 9999),
            'total_fee' => $orderInfo['total_money'],
            'name' => $orderInfo['pro_cate_name'],
            'openid' => ''
        );
        if ($getAppType == 1) {
            $userInfo = ShopUtil::getUserInfo();
            $params['openid'] = $userInfo['data']['openid'];
            $info = PayService::pay($params, 1);
            include ROOT_PATH . '/common/util/WeChatHtml.php';
        } elseif ($getAppType == 2) {
            $info = PayService::pay($params, 2);
            include ROOT_PATH . '/common/util/AlipayHtml.php';
        } else {
            $ali_info = PayService::pay($params, 2);
            include ROOT_PATH . '/common/util/AllPayHtml.php';
        }
        return '';
    }

    /**
     * 校验订单是否可以支付并返回订单信息
     * @param $orderId
     * @return array
     */
    public function checkOrderCanPay($orderId)
    {
        if (!Util::checkPattern('intVal', $orderId)) {
            return Util::returnArrEr('异常请求！');
        }
        $orderMain = new OrderMain();
        $orderInfo = $orderMain->getOrderPayInfo($orderId);
        if (empty($orderInfo['order_id'])) {
            return Util::returnArrEr('不存在的订单号！');
        }
        if(in_array($orderInfo['order_status'],[2,3,4])) {
            return Util::returnArrEr('订单已经支付，不必重复支付！');
        }
        if($orderInfo['order_status'] == 5) {
            return Util::returnArrEr('该订单已经过期！');
        }
        return Util::returnArrSu('',$orderInfo);
    }
}