Example #1
0
 public function run()
 {
     $question_id = intval($this->getDataItem('question_id', 0));
     if ($question_id < 1) {
         return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, '参数错误!');
     }
     $QuestionTag = new QuestionTag();
     if ($tagList = $QuestionTag->query()->where('question_id = ' . $question_id)->execute()->toArray()) {
         $tag_id = '';
         foreach ($tagList as $val) {
             $tag_id .= $val['tag_id'] . ',';
         }
         $tag_id = '(' . trim($tag_id, ',') . ')';
         $question_id_list = $QuestionTag->query()->groupBy('question_id')->where("tag_id in{$tag_id} and question_id != {$question_id}")->orderBy('id DESC')->limit(50)->execute()->toArray();
         if ($question_id_list) {
             /*$where_question_id = '';
               foreach ($question_id_list as $val) {
                   $where_question_id .= $val['question_id'] . ',';
               }
               $where_question_id = '(' . trim($where_question_id, ',') . ')';*/
             // 随机取相关问答的id
             $count = count($question_id_list);
             if ($count >= 3) {
                 $start = mt_rand(0, $count - 3);
                 $where_question_id = '(' . $question_id_list[$start]['question_id'] . ',' . $question_id_list[$start + 1]['question_id'] . ',' . $question_id_list[$start + 2]['question_id'] . ')';
             } else {
                 $where_question_id = '';
                 foreach ($question_id_list as $val) {
                     $where_question_id .= $val['question_id'] . ',';
                 }
                 $where_question_id = '(' . trim($where_question_id, ',') . ')';
             }
             $list = Question::query()->columns(['question_id', 'question_content', 'question_pics', 'question_tag', 'answer_num', 'question_browsers', 'question_addtime', 'u.user_id', 'u.user_nickname', 'u.user_expert', 'u.user_cover', 'u.user_gender'])->leftJoin('\\Apps\\Common\\Models\\UserBase', 'u.user_id = \\Apps\\Common\\Models\\Question.user_id', 'u')->where("question_id in{$where_question_id} and question_state = 1")->execute()->toArray();
             foreach ($list as $k => $val) {
                 $list[$k]['question_tag'] = $val['question_tag'] ? json_decode($val['question_tag'], true) : [];
                 $list[$k]['user_cover'] = PicUrl::UserCover($val['user_cover'], $this->getDi());
                 $list[$k]['question_content'] = unserialize(base64_decode($val['question_content'])) ?: $val['question_content'];
                 if ($val['question_pics']) {
                     $question_pics = [];
                     foreach (json_decode($val['question_pics'], true) as $key => $value) {
                         $question_pics[] = PicUrl::Question($value, $this->getDi());
                     }
                     $list[$k]['question_pics'] = $question_pics;
                 }
             }
             $this->setResult($list);
         }
     }
 }
Example #2
0
 /**
  * 问题列表
  */
 public function listAction()
 {
     $this->setLeftNav('list');
     $req = $this->request;
     $page = intval($req->getQuery('page', null, 1));
     $page = $page > 0 ? $page : 1;
     $limit = $this->pageNavLimit;
     $offset = $limit * ($page - 1);
     $filterTag = intval($req->getQuery('filterTag', null, -10000));
     $filterOrder = intval($req->getQuery('filterOrder', null, 0));
     $columns = ['question_id', 'ub.user_id', 'user_nickname', 'user_cover', 'question_tag', 'tag_title', 'question_content', 'question_pics', 'answer_num', 'question_follows', 'question_browsers', 'question_state', 'question_addtime'];
     $where = [];
     $bind = [];
     if ($filterTag > 0) {
         $where[] = 'Apps\\Common\\Models\\Question.question_tag = :tag:';
         $bind['tag'] = $filterTag;
     }
     // 只查询正常的内容
     $where[] = 'Apps\\Common\\Models\\Question.question_state > 0';
     $whereStr = implode(' AND ', $where);
     $query = Question::query()->columns($columns)->leftJoin('Apps\\Common\\Models\\UserBase', 'ub.user_id = Apps\\Common\\Models\\Question.user_id', 'ub')->leftJoin('Apps\\Common\\Models\\QuestionTag', 'tag.tag_id = question_tag', 'tag');
     if (count($where) > 0) {
         $query = $query->where($whereStr)->bind($bind);
     }
     $data = $query->orderBy($filterOrder == 1 ? 'answer_num ASC, question_id DESC' : 'question_id DESC')->limit($limit, $offset)->execute();
     $total = Question::count(['conditions' => $whereStr, 'bind' => $bind]);
     $this->view->setVar('data', $data);
     $this->view->setVar('page', $page);
     $this->view->setVar('limit', $limit);
     $this->view->setVar('total', $total);
     $this->view->setVar('tagList', QuestionTag::query()->columns('*')->orderBy('tag_order DESC, tag_id ASC')->execute());
     $this->view->setVar('filterTag', $filterTag);
     $this->view->setVar('filterOrder', $filterOrder);
 }
Example #3
0
 public function run()
 {
     $limit = $this->getConfig()->limit;
     //统计问答标签出现的次数 倒序获取次数最多即热门标签
     $list = QuestionTag::query()->columns('b.tag_id id,tag_name name')->join('Apps\\Common\\Models\\BeautyParlorTagInfo', 'Apps\\Common\\Models\\QuestionTag.tag_id = b.tag_id', 'b')->where('tag_state = 1 AND b.parent_id IN (SELECT s.tag_id FROM Apps\\Common\\Models\\BeautyParlorTagInfo as s WHERE tag_state>0 AND parent_id=0 ) GROUP BY Apps\\Common\\Models\\QuestionTag.tag_id')->orderBy('COUNT(Apps\\Common\\Models\\QuestionTag.tag_id) desc')->limit($limit, 0)->execute()->toArray();
     //如果热门标签不足 获取 最新标签补足
     if (count($list) < $limit) {
         $limit -= count($list);
         $temp = BeautyParlorTagInfo::query()->columns(['tag_id id', 'tag_title name'])->where('tag_state = 1 AND  parent_id IN (SELECT tag_id FROM Apps\\Common\\Models\\BeautyParlorTagInfo WHERE tag_state>0 AND parent_id=0 )')->orderBy('tag_id DESC')->limit($limit, 0)->execute()->toArray();
         //合并数据
         $list = array_merge($list, $temp);
     }
     $this->setResult($list);
 }
 /**
  * 添加问题
  * @date: 2016-1-15 
  * @author: futao
  */
 public function addquestionAction()
 {
     $this->setLeftNav('addquestion');
     $req = $this->request;
     if (!$req->isPost()) {
         $userList = UserBase::find("is_manage = 1");
         $tag = BeautyParlorTagInfo::find("parent_id != 0 and tag_state = 1");
         $this->view->setVar('city_list', $this->openCityList());
         $this->view->setVar('userlist', $userList);
         $this->view->setVar('tag', $tag);
         $this->view->pick("question/add");
         return;
     }
     $this->view->disable();
     $echo = function ($msg, $state = false) {
         echo '<script type="text/javascript">';
         echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
         echo '</script>';
     };
     $questionContent = $req->getPost('question_content', null, '');
     $questionTag = $req->getPost('question_tag', null, '');
     $questionBrowsers = intval($req->getPost('question_browsers', null, 0));
     $userId = intval($req->getPost('user_id', null, 0));
     $cityCode = intval($req->getPost('city_id', null, 0));
     if ($userId < 0 || $questionBrowsers < 0) {
         $echo("添加失败!");
         return;
     }
     if (is_array($questionTag)) {
         foreach ($questionTag as $val) {
             $tagArr = explode(',', $val);
             $tagName = $tagArr[1];
             $tagNameArr[] = $tagName;
         }
     } else {
         $echo("标签必选!");
         return;
     }
     $questionModel = new Question();
     //保存图片
     if ($_FILES['q_photos']['name'][0] != '') {
         $uploadFile = new UploadPic();
         $uploadFile->request = $req;
         $questionPics = $uploadFile->questionPic();
         $questionModel->question_pics = json_encode($questionPics);
     } else {
         $questionModel->question_pics = json_encode([]);
     }
     $questionModel->question_content = $questionContent;
     $questionModel->city_code = $cityCode;
     $questionModel->question_browsers = $questionBrowsers;
     $questionModel->question_tag = json_encode($tagNameArr);
     $questionModel->user_id = $userId;
     if (!$questionModel->save()) {
         $echo("添加失败!");
         return;
     }
     if ($questionTag != '' && is_array($questionTag)) {
         unset($tagNameArr);
         unset($tagName);
         unset($tagArr);
         foreach ($questionTag as $val) {
             unset($tagId);
             unset($tagName);
             $tagArr = explode(',', $val);
             $tagId = $tagArr[0];
             $tagName = $tagArr[1];
             $tagNameArr[] = $tagName;
             $questionTagModel = new QuestionTag();
             $questionTagModel->question_id = $questionModel->question_id;
             $questionTagModel->tag_id = $tagId;
             $questionTagModel->tag_name = $tagName;
             if ($questionTagModel->save()) {
                 $echo("添加成功!", true);
             }
         }
     } else {
         $echo("添加失败!");
         return;
     }
 }
Example #5
0
 public function run()
 {
     $this->tags = QuestionTag::query()->columns(['tag_id id', 'tag_title title'])->where('tag_state = 1')->orderBy('tag_order DESC, tag_id ASC')->execute()->toArray();
     $this->tags = $this->tags ? $this->tags : [];
     $this->setResult($this->tags);
 }
 public function updateAction()
 {
     $req = $this->request;
     if (!$req->isPost()) {
         $id = intval($req->getQuery('id', null, 0));
         $tagTitle = '';
         $tagOrder = 0;
         if ($id > 0) {
             $info = QuestionTag::findFirst('tag_id=' . $id);
             if ($info) {
                 $tagTitle = $info->tag_title;
                 $tagOrder = $info->tag_order;
             }
         }
         $this->view->setVar('id', $id);
         $this->view->setVar('tagTitle', $tagTitle);
         $this->view->setVar('tagOrder', $tagOrder);
         return;
     }
     $response = new ResponseResult();
     $response->callback = $req->getPost('callback', null, 'parent.setFormResult');
     $response->callbackJavascriptTag = true;
     $id = intval($req->getPost('tag_id', null, 0));
     $title = $req->getPost('tag_title');
     $order = intval($req->getPost('tag_order', null, '-10000'));
     // 通过名称查找,是否存在
     $exist = function ($title, $id) {
         $existInfo = QuestionTag::query()->columns('*')->where('tag_title = :title:', ['title' => $title])->limit(1)->execute()->getFirst();
         if ($existInfo && $existInfo->tag_id != $id) {
             return true;
         }
         return false;
     };
     if ($id > 0) {
         // 修改
         $info = QuestionTag::findFirst('tag_id=' . $id);
         if (!$info) {
             $response->sendError(ResponseResultStatus::ERROR, '分类不存在!');
             return $response;
         }
         if ($exist($title, $id)) {
             $response->sendError(ResponseResultStatus::ERROR, '名称已经存在了!');
             return $response;
         }
         $data = [];
         if ($info->tag_title != $title) {
             $data['tag_title'] = $title;
         }
         if ($order > -1) {
             $data['tag_order'] = $order;
         }
         if (count($data) > 0) {
             $info->update($data);
         }
     } else {
         // 添加
         if ($exist($title, $id)) {
             $response->sendError(ResponseResultStatus::ERROR, '名称已经存在了!');
             return $response;
         }
         $tag = new QuestionTag();
         $tag->tag_title = $title;
         $tag->tag_order = $order;
         if (!$tag->save()) {
             $this->databaseErrorLog($tag);
             $response->sendError(ResponseResultStatus::DATABASE_ERROR, '保存数据异常!');
             return $response;
         }
     }
     $response->sendResult('ok');
     return $response;
 }