<?php
namespace app\service\controller;

/**
 * 公共类
 */
class Common extends Base
{


    /**
     * 验证token
     *
     * @access public
     * @return string
     */
    protected function verifyToken()
    {
        $request      = \think\Request::instance();
        $getApiToken  = input('server.HTTP_apiToken');
        $getUserToken = input('server.HTTP_userToken');
        $action       = $request->action();
        $controller   = $request->controller();
        $module       = $request->module();
        $apiToken     = md5(strtolower($action.'Customer-Service'.$controller.strtotime(date('Y-m-d')).$module));
        //print_r([$apiToken,$getApiToken]);die;
        // 验证服务器token.
        if ($getApiToken === $apiToken && empty($getUserToken) === false) {
            $usersField          = [
                'id',
                'expire_time',
            ];
            $usersWhere['token'] = $getUserToken;
            // 验证用户token.
            $users         = model('users')->findUsers($usersField, $usersWhere);
            $effectiveTime = strtotime(date('Y-m-d H:i:s').'-1 hours');
            if (empty($users) === false && $effectiveTime <= $users['expire_time']) {
                $updateWhere['id']         = $users['id'];
                $updateData['expire_time'] = strtotime(date('Y-m-d H:i:s'));
                model('users')->updateUsers($updateWhere, $updateData);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }//end verifyToken()

    protected  function  doTokenCheck(){
        if (!$this->verifyToken()){
            echo  json_encode(['code' => -1, 'data' => [], 'msg' => 'check false!'],256);
            exit ;
        }
    }


    /**
     * 获取用户信息
     *
     * @access public
     * @return string
     */
    protected function getUserInfo()
    {
        $getUserToken        = input('server.HTTP_userToken');
        $usersField          = ['*'];
        $usersWhere['token'] = $getUserToken;
        // 获取用户信息.
        $usersInfo = model('users')->findUsers($usersField, $usersWhere);
        return $usersInfo;

    }//end getUserInfo()


}
