<?php

/**
 *------Create thems Controller------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-13 01:06:44------.
 */

namespace App\InApi\Controller;


use BaseController\Controller;

class BaseController extends Controller
{
    public function init()
    {
        // lm('user_logs', 'commons')->initVisitor();
        if (isCheckToken()) {
            $token = isset($_GET['token']) && !empty($_GET['token']) ? trim($_GET['token']) : '';
            if (empty($token)) {
                Render('5555', '-4010', lang('errors')->get('-4010'));
            }

            $re = C()->get('user')->refreshStatus($token);

            if ($re < 0) {
                Render('', $re, lang('errors')->get($re));
            }
        }
    }

    /**
     * 全局更新缓存.
     */
    private function updateCache()
    {
        $setTime = lm('Setinfo', 'Commons')->getType(1912);
        if (empty($setTime)) {
            return;
            // $setTime = date('Y-m-d H:i:s');
        }
        if (C()->get('cache')->has('systenLastUpdateTime')) {
            $lastTime = C()->get('cache')->get('systenLastUpdateTime');
        } else {
            // $lastTime = date('Y-md-d H:i:s');
            $lastTime = 0;
        }

        $setTimestamp = strtotime($setTime);
        $lastTimestamp = strtotime($lastTime);
        if ($setTimestamp > $lastTimestamp) {
            C()->get('cache')->clear();
            C()->get('cache')->set('systenLastUpdateTime', $setTime, 86400 * 365);
        }
    }

    public function beforeInit()
    {
        $this->updateCache();
    }
}
