/** * 评论写入接口 */ public function actionIndex() { $mComment = new Comment(); // print_r(Yii::$app->request->post());exit; //xxx 方便测试用 // $newsId = Yii::$app->request->get('id', false); if (Yii::$app->request->getIsPost()) { $params = Yii::$app->request->post(); // print_r($params);exit; // $params['cmt_name'] ? '' : Yii::$app->util->msg('评论内容不能为空'); //获取用户id $user = Yii::$app->util->isLogin(); //匿名用户验证码检测 if (empty($user)) { if (isset($params['vcode']) && $params['vcode']) { //获取验证码 $session = Yii::$app->session; $session->isActive ? '' : $session->open(); $cmtcode = $session->get('__captcha/comment/cmtcode'); if ($cmtcode !== $params['vcode']) { Yii::$app->util->msg('验证码错误!'); } } else { Yii::$app->util->msg('请输入验证码!'); } } $params['owner_id'] = $user['uid'] ? $user['uid'] : ''; $params['cmt_type_id'] = 1; //评论类型为资讯评论 // print_r($params);exit; //xxx 方便测试用 // $params['news_id'] = $newsId ? $newsId : 0; if (isset($params['news_id']) && $params['news_id'] > 0) { // print_r($params);exit; $mComment->saveStore($params); $mPostAttrVal = new PostAttrVal(); $options = ['news_id' => $params['news_id'], 'attr_id' => PostAttribute::COMMENT, 'attr_value_text' => $mComment->cmt_id]; $mPostAttrVal->saveStore($options); $mPostAttrStat = new PostAttrStat(); $mPostAttrStat->addNum($params['news_id'], 'attr_stat_comment_num', 1); $this->redirect(Url::toRoute(['news/detail', 'id' => $params['news_id'], '#' => 'comments'])); // echo $re; } else { echo "params news_id is missing"; } // print_r($params);exit; // $mComment = new Comment(); // $re = $mComment->saveStore($params); // echo "<pre>"; // echo $re;exit; } }
/** * 资讯统计表的like数 * * @return void * @author niancode **/ protected function _store_stat_with_news($news_id, $type = 'follow', $sum = true) { $num = $sum ? 1 : -1; $follow_num = 0; $attr_stat_model = PostAttrStat::findOne(['news_id' => $news_id]); if ($attr_stat_model) { // 关注 if ($type == 'follow') { $attr_stat_model->updateCounters(['attr_stat_follow_num' => $num]); $follow_num = $attr_stat_model->attr_stat_follow_num; } // like if ($type == 'like') { $attr_stat_model->updateCounters(['attr_stat_like_num' => $num]); $follow_num = $attr_stat_model->attr_stat_like_num; } } return $follow_num; }
/** * 保存/修改资讯数据 * * @params array $params 保存数据 * @return boolean 是否保存成功 */ public function saveNewsData($params = []) { // $params = Yii::$app->request->post('Post',false); $isNewRecord = $this->isNewRecord; if (!$params) { return false; } else { $news_id = $this->storeSave($params); $conditions = []; if (!$news_id) { return false; } else { if ($isNewRecord) { $model = new PostAttrStat(); $model->news_id = $news_id; $model->save(false); } $conditions['news_id'] = $news_id; //相关用户 if (isset($params['rel_user'])) { //数据处理 $params['rel_user'] = $this->getDataId($params['rel_user']); $conditions['attr_id'] = PostAttribute::USER; $this->saveAttrVals($conditions, $params['rel_user']); } //相关产品 if (isset($params['rel_product'])) { //数据处理 $params['rel_product'] = $this->getDataId($params['rel_product']); $conditions['attr_id'] = PostAttribute::PRODUCT; $this->saveAttrVals($conditions, $params['rel_product']); } //相关资讯 if (isset($params['rel_news'])) { $conditions['attr_id'] = PostAttribute::SIMILAR_NEWS; $this->saveAttrVals($conditions, $params['rel_news']); } //相关标签 if (isset($params['rel_tag'])) { //获取标签对应的tag_id组成的字符串 $mTag = new Tag(); $tagStr = $mTag->rebackTagIds($params['rel_tag']); $conditions['attr_id'] = PostAttribute::TAG; $this->saveAttrVals($conditions, $tagStr); } return true; } } }