/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = AccessToken::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['validity' => $this->validity, 'createtime' => $this->createtime]);
     $query->andFilterWhere(['like', 'tokenid', $this->tokenid])->andFilterWhere(['like', 'clientid', $this->clientid])->andFilterWhere(['like', 'appkey', $this->appkey])->andFilterWhere(['like', 'orgid', $this->orgid])->andFilterWhere(['like', 'uid', $this->uid]);
     return $dataProvider;
 }
 /**
  * Finds the AccessToken model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return AccessToken the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = AccessToken::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 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;
 }