コード例 #1
0
 /**
  * Displays a single Humour model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $this->layout = 'right_humour';
     $model = $this->findModel($id);
     /*view 查看次数存储*/
     /*判断用户是否已经点查看过,通过cookie判断*/
     $type = $this->getUniqueId();
     $ip = Yii::$app->request->getUserIP();
     $ip = str_replace('.', '_', $ip);
     $cookiename = $type . '_' . $ip . '_' . $id;
     $cookievalue = getDomainCookie($cookiename);
     if (!isset($cookievalue)) {
         $model->viewcount += 1;
         $model->save();
         setDomainCookie($cookiename, '1', 86400);
     }
     $comment = $this->newComment($model);
     /************评论分页开始****************/
     $dataProvider = $this->getComments($id);
     /*************评论分页结束****************/
     return $this->render('view_user', ['model' => $model, 'comment' => $comment, 'dataProvider' => $dataProvider]);
     /*非管理人员*/
     if (Yii::$app->user->role !== '1') {
         return $this->render('view_user', ['model' => $model, 'comment' => $comment]);
     }
     return $this->render('view', ['model' => $model, 'comment' => $comment]);
 }
コード例 #2
0
 public function actionIndex()
 {
     $params = Yii::$app->request->get();
     extract($params);
     define('TYPE', ucfirst($type));
     /*动态处理每一个type分类*/
     $TYPE = TYPE;
     $appClass = "\\app\\models\\{$TYPE}";
     if (isset($action) && in_array($action, array('up', 'down'))) {
         /*判断用户是否已经点击过,通过cookie判断*/
         $cookiename = $type . '_' . $action . '_' . $id;
         $cookievalue = getDomainCookie($cookiename);
         if (isset($cookievalue)) {
             exit(json_encode(array('stat' => '-1', 'retmsg' => '请勿重复点击')));
         }
         $model = new $appClass();
         if (($model = $model->findOne($id)) !== null) {
             if ($action === 'up') {
                 $model->up += 1;
                 $model->save();
                 /*cookie写入,防止多次提交*/
             } else {
                 $model->down += 1;
             }
             setDomainCookie($cookiename, 1, 7200);
             exit(json_encode(array('stat' => 'ok', 'up' => $model->up)));
         } else {
             throw new NotFoundHttpException('The requested page does not exist.');
         }
     } else {
         exit(json_encode(array('stat' => '-2', 'retmsg' => '处理失败!')));
     }
 }