/**
  * Creates a new AccessToken model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new AccessToken();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->tokenid]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public static function validateAPIAuth($appkey, $clientid, $clientsecurity)
 {
     /**
      * @var AccessToken $accessToken
      * @var AccessApp $accessApp
      */
     //app
     $accessApp = AccessApp::find()->andWhere('appkey=:appkey', array(':appkey' => $appkey))->one();
     if (empty($accessApp)) {
         throw new Exception('传入appkey 错误');
     }
     if ($accessApp->client_id != $clientid) {
         throw new Exception('client_id 错误');
     }
     if ($accessApp->client_secret != $clientsecurity) {
         throw new Exception('client_secret 错误');
     }
     $accessToken = AccessToken::findOne(array('clientid' => $clientid, 'appkey' => $appkey));
     $usable = true;
     if (!empty($accessToken)) {
         $date1 = date_create(BaseDataHelper::getCurrentTime());
         $date2 = date_create($accessToken->createtime);
         $diff = date_diff($date1, $date2);
         if ($diff->format('%y') > 0 || $diff->format('%m') > 0 || $diff->format('%d') > 0 || $diff->format('%h') > 0 || $diff->format('%i') > $accessToken->validity / 60) {
             $accessToken->delete();
             $usable = false;
         }
     } else {
         $usable = false;
     }
     //token
     if (!$usable) {
         $accessToken = new AccessToken();
         $accessToken->tokenid = DataHelper::random(10);
         $accessToken->appkey = $appkey;
         $accessToken->clientid = $clientid;
         $accessToken->validity = 600;
         //60秒
         $accessToken->uid = $accessApp->uid;
         $accessToken->orgid = $accessApp->user->orgid;
         if (!$accessToken->save()) {
             var_dump($accessToken->errors);
             die;
         }
         //当前登录人信息
         $session = Yii::$app->session;
         $model = AccessToken::findOne(array('tokenid' => $accessToken->tokenid));
         $session->set('user', $model->user);
     }
     return $accessToken;
 }