QrcodeController.php 3.12 KB
<?php
namespace app\wx\controllers;

use common\helpers\CheckActiveHelper;
use common\helpers\Log;
use domain\smart\ScanRecords;
use domain\smart\SellerInputRecordRepository;
use Yii;
use stdClass;

/**
 * qrcode-控制器
 * Class QrcodeController
 * @package app\wx\controllers
 */
class QrcodeController extends BaseController
{
    /**
     * @throws yii\web\BadRequestHttpException
     */
    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->checkClientStatus();
        $this->handleMallAccessCtrl();
    }
    /**
     * @return string
     * @return string HTML
     */
    public function actionIndex()
    {
        $uuid = Yii::$app->request->get("uuid");
        // 判断路径中是否存在UUID
        /*if (empty($uuid)) {
            $this->jumpError();
        }
        if (strstr($uuid, "@") === false) {
            $this->jumpError();
        }
        $explodArrar = explode("@", $uuid);
        if (empty($explodArrar)) {
            $this->jumpError();
        }
        $uuid = $explodArrar[0];*/
        // @todo 校验UUID是否有效 -2 无效  0 有效未激活 2 有效已激活
        $startTime = microtime(true);
        $result = CheckActiveHelper::getAppCheck($uuid, "");
        $etime=microtime(true);//获取程序执行结束的时间
        $total=$etime-$startTime;   //计算差值
        Log::DEBUG("调用检查接口结果[uuid=$uuid]:" . json_encode($result) . '执行时间:' . $total . "秒");
        if (isset($result["code"])) {
            if ($result["code"] == 2) {
                $hasRecord = SellerInputRecordRepository::findOne(["uuid" => $uuid]);
                if (empty($hasRecord)) {
                    Log::DEBUG("执行跳转code={2},未存记录");
                    $this->jumpActive($uuid);
                } else {
                    Log::DEBUG("执行跳转code={2},已存记录");
                    // 记录扫码次数
                    $scanCount = ScanRecords::updateCount($uuid);
                    $scanCount = $scanCount ? $scanCount : 1;
                    $this->jumpScanCount($uuid, $scanCount);
                }
            } else if ($result["code"] == 0) {
                Log::DEBUG("执行跳转code={0},跳转到激活页");
                $this->jumpActive($uuid);
            } else {
                $this->jumpError();
            }
        } else {
            $this->jumpError();
        }
    }

    /**
     * 跳转到错误页面
     */
    private function jumpError()
    {
        $jumpUrl = Yii::$app->params["baseUrl"]."/check#error/0";
        header("Location: $jumpUrl");exit;
    }

    /**
     * 跳转到扫码次数页面
     */
    private function jumpScanCount($uuid, $scanCount)
    {
        $jumpUrl = Yii::$app->params["baseUrl"] . "?jumpmodel=check&jumppath=scan-count/" . $uuid . "/" . $scanCount;
        header("Location: $jumpUrl");exit;
    }

    /**
     * 跳转到激活页面
     */
    private function jumpActive($uuid)
    {
        $jumpUrl = Yii::$app->params["baseUrl"] . "?jumpmodel=smart&jumppath=enter/" . $uuid ;
        header("Location: $jumpUrl");exit;
    }
}