123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- <?php
-
-
- namespace app\api\model\v1;
-
- use think\Db;
- use think\Cache;
- use app\api\logic\v1\AskLogic;
-
- load_trait('controller/Jump');
-
- class Ask extends Base
- {
- use \traits\controller\Jump;
- public $weapp_ask_db;
- public $weapp_ask_answer_db;
- public $weapp_ask_answer_like_db;
- public $weapp_ask_users_level_db;
- public $weapp_ask_type_db;
- public $users = [];
- public $users_id = 0;
- public $AskLogic;
-
-
- protected function initialize()
- {
-
- parent::initialize();
- $this->weapp_ask_db = Db::name('weapp_ask');
- $this->weapp_ask_answer_db = Db::name('weapp_ask_answer');
- $this->weapp_ask_answer_like_db = Db::name('weapp_ask_answer_like');
- $this->weapp_ask_users_level_db = Db::name('weapp_ask_users_level');
- $this->weapp_ask_type_db = Db::name('weapp_ask_type');
- $this->AskLogic = new AskLogic;
- }
-
- public function getTypeList(){
- $TypeData = $this->weapp_ask_type_db->order('sort_order asc, type_id asc')->select();
- $TypeData = array_merge([["type_id"=>0,"type_name"=>"全部"]],$TypeData);
- return $TypeData;
- }
- public function getAskList()
- {
- $param = input('param.');
- $limit = !empty($param['limit']) ? $param['limit'] : 15;
- $page = !empty($param['page']) ? $param['page'] : 1;
- $field = !empty($param['field']) ? $param['field'] : "";
-
- $where = [
- 'a.status' => ['IN', [0, 1]],
-
- 'a.is_review' => 1,
- ];
-
- if (1 == $param['replies']) {
- $where['a.replies'] = 0;
- }
- if (!empty($param['type_id'])) {
- $where['a.type_id'] = $param['type_id'];
- }
-
-
- $result = [];
-
- if (empty($field)) {
- $field = 'a.*, b.username, b.nickname, b.head_pic, b.sex, b.level, b.scores, c.type_name';
- }
-
- $paginate = ['page' => $page];
- $pages = $this->weapp_ask_db->field($field)
- ->alias('a')
- ->join('users b', 'a.users_id = b.users_id', 'LEFT')
- ->join('__WEAPP_ASK_TYPE__ c', 'a.type_id = c.type_id', 'LEFT')
- ->where($where)
- ->order('a.is_top desc, a.add_time desc')
- ->paginate($limit, false, $paginate);
- $res = $pages->toArray();
-
- $askData = $res['data'];
-
- $ask_ids = get_arr_column($askData, 'ask_id');
-
- $RepliesData = $this->weapp_ask_answer_db->field('a.ask_id, a.users_id, b.head_pic, b.nickname, b.username, b.sex')
- ->alias('a')
- ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
- ->where('ask_id', 'IN', $ask_ids)
- ->select();
-
- $RepliesDataNew = [];
- foreach ($RepliesData as $key => $value) {
-
- $RepliesDataNew[$value['ask_id']][] = $value;
- }
- foreach ($askData as $key => $value) {
-
- $value['head_pic'] = handle_subdir_pic(get_head_pic($value['head_pic'], false, $value['sex']));
- $askData[$key]['head_pic'] = get_absolute_url($value['head_pic'],'default',true);
-
-
- $askData[$key]['add_time'] = friend_date($value['add_time']);
- $content = htmlspecialchars_decode($value['content']);
- $askData[$key]['no_format_content'] = checkStrHtml($content);
-
- }
- $res['data'] = $askData;
- $typeList = $this->getTypeList();
- $res['typeList'] = $typeList;
-
- return $res;
- }
-
-
- public function GetAskDetails($users = [])
- {
- $param = input('param.');
- $users_id = !empty($users['users_id']) ? intval($users['users_id']) : 0;
-
- $info = $this->weapp_ask_db
- ->field('a.*,b.username, b.nickname, b.head_pic, b.sex, b.level, b.scores, c.type_name')
- ->alias('a')
- ->join('__WEAPP_ASK_TYPE__ c', 'c.type_id = a.type_id', 'LEFT')
- ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
- ->where('ask_id', $param['ask_id'])
- ->find();
- if (empty($info)) $this->error('浏览的问题不存在!');
-
- if (0 == $info['is_review'] && $info['users_id'] !== $users_id) $this->error('问题未审核通过,暂时不可浏览!');
-
- if (!empty($info['is_minipro'])) {
- $img_url = Db::name('weapp_ask_img')->where('ask_id', $param['ask_id'])->column('url');
- if (!empty($img_url)) {
- foreach ($img_url as $k => $v) {
- $img_url[$k] = get_absolute_url($v,"url",true);
- }
- $info['img_url'] = $img_url;
- }
- }
-
-
- $info['head_pic'] = get_absolute_url(get_head_pic($info['head_pic'], false, $info['sex']),'default',true);
-
-
- $info['add_time'] = friend_date($info['add_time']);
-
-
- $info['content'] = stripslashes($info['content']);
- $info['content'] = htmlspecialchars_decode(handle_subdir_pic($info['content'], 'html'));
-
-
-
- $info['seo_title'] = $info['ask_title'] . ' - ' . $info['type_name'];
- $info['seo_keywords'] = $info['ask_title'];
- $info['seo_description'] = @msubstr(checkStrHtml($info['content']), 0, config('global.arc_seo_description_length'), false);
-
- $info['answer'] = $this->GetAskReplyData($param);
- return $info;
- }
-
- public function GetAskReplyData($param = array())
- {
-
- $bestanswer_id = $this->weapp_ask_db->where('ask_id', $param['ask_id'])->getField('bestanswer_id');
- $RepliesWhere = ['ask_id' => $param['ask_id'], 'is_review' => 1];
- $WhereOr = [];
- if (!empty($param['answer_id'])) {
- $RepliesWhere = ['answer_id' => $param['answer_id'], 'is_review' => 1];
- $WhereOr = ['answer_pid' => $param['answer_id']];
- }
-
-
- $firstRow = !empty($param['firstRow']) ? $param['firstRow'] : 0;
- $listRows = !empty($param['listRows']) ? $param['listRows'] : 5;
- $result['firstRow'] = $firstRow;
- $result['listRows'] = $listRows;
-
-
-
-
- if (!empty($param['click_like'])) {
- $OrderBy = "a.click_like {$param['click_like']}, a.answer_id asc";
- $result['likeSortOrder'] = 'desc' == $param['click_like'] ? 'asc' : 'desc';
- } else if (!empty($param['new_answer'])) {
- $OrderBy = "a.answer_id {$param['new_answer']}";
- $result['newSortOrder'] = 'desc' == $param['new_answer'] ? 'asc' : 'desc';
- } else {
- $OrderBy = 'a.answer_id asc';
- }
-
-
-
- $RepliesData = $this->weapp_ask_answer_db->field('a.*, b.head_pic, b.nickname, b.username as u_username, b.sex,c.nickname as `at_usersname`')
- ->alias('a')
- ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
- ->join('__USERS__ c', 'a.at_users_id = c.users_id', 'LEFT')
- ->order($OrderBy)
- ->where($RepliesWhere)
- ->WhereOr($WhereOr)
- ->select();
- if (empty($RepliesData)) return [];
-
-
-
- $AnswerIds = get_arr_column($RepliesData, 'answer_id');
- $AnswerLikeData = $this->weapp_ask_answer_like_db->field('a.*, b.nickname, b.username')
- ->alias('a')
- ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
- ->order('like_id desc')
- ->where('answer_id', 'IN', $AnswerIds)
- ->select();
- $AnswerLikeData = group_same_key($AnswerLikeData, 'answer_id');
-
-
-
- $PidData = $AnswerData = [];
- foreach ($RepliesData as $key => $value) {
-
- $value['add_time'] = friend_date($value['add_time']);
-
- $value['content'] = stripslashes($value['content']);
- $value['content'] = htmlspecialchars_decode(handle_subdir_pic($value['content'], 'html'));
-
- $value['head_pic'] = get_absolute_url(get_head_pic($value['head_pic'], false, $value['sex']),'default',true);
- if (!is_http_url($value['head_pic'])) $value['head_pic'] = get_absolute_url(handle_subdir_pic($value['head_pic'], 'img', true),'default',true);
-
- $value['nickname'] = !empty($value['nickname']) ? $value['nickname'] : $value['u_username'];
-
-
- if ($value['answer_pid'] == 0) {
- $PidData[] = $value;
- } else {
- $AnswerData[] = $value;
- }
- }
-
-
-
- foreach ($PidData as $key => $PidValue) {
- $result['AnswerData'][] = $PidValue;
-
- $result['AnswerData'][$key]['AnswerSubData'] = [];
-
- $result['AnswerData'][$key]['AnswerLike'] = [];
-
-
- foreach ($AnswerData as $AnswerValue) {
- if ($AnswerValue['answer_pid'] == $PidValue['answer_id']) {
- array_push($result['AnswerData'][$key]['AnswerSubData'], $AnswerValue);
- }
- }
-
-
-
-
- array_multisort(get_arr_column($result['AnswerData'][$key]['AnswerSubData'], 'is_review'), SORT_DESC, $result['AnswerData'][$key]['AnswerSubData']);
-
- $result['AnswerData'][$key]['AnswerSubData'] = array_slice($result['AnswerData'][$key]['AnswerSubData'], $firstRow, $listRows);
-
-
- $result['AnswerData'][$key]['AnswerLike']['LikeNum'] = null;
- $result['AnswerData'][$key]['AnswerLike']['LikeName'] = null;
-
- foreach ($AnswerLikeData as $LikeKey => $LikeValue) {
- if ($PidValue['answer_id'] == $LikeKey) {
-
- $LikeNum = count($LikeValue);
- $result['AnswerData'][$key]['AnswerLike']['LikeNum'] = $LikeNum;
- for ($i = 0; $i < $LikeNum; $i++) {
-
- if ($i > 2) break;
-
- $LikeName = $LikeValue[$i]['nickname'];
-
-
-
-
-
-
- $result['AnswerData'][$key]['AnswerLike']['LikeName'] .= $LikeName;
- }
- }
- }
-
- }
-
-
-
- foreach ($result['AnswerData'] as $key => $value) {
- if ($bestanswer_id == $value['answer_id']) {
- $result['BestAnswer'][$key] = $value;
- unset($result['AnswerData'][$key]);
- }
- }
- $result['AnswerData'] = array_merge($result['AnswerData']);
-
-
-
- $result['AnswerCount'] = count($RepliesData);
- return $result;
- }
-
-
-
-
- private function clear_htmlcache()
- {
- delFile(HTML_PATH . 'plugins/ask/');
- }
-
-
- public function askLike($users = [])
- {
- if (IS_AJAX_POST) {
- $ask_id = input('param.ask_id/d');
- $answer_id = input('param.answer_id/d');
- if (empty($answer_id) || empty($ask_id)) $this->error('请选择点赞信息!');
-
- $Where = [
- 'ask_id' => $ask_id,
- 'users_id' => $users['users_id'],
- 'answer_id' => $answer_id,
- ];
- $IsCount = $this->weapp_ask_answer_like_db->where($Where)->count();
- if (!empty($IsCount)) {
- $this->error('您已赞过!');
- }else{
-
- $AddData = [
- 'click_like' => 1,
- 'users_ip' => clientIP(),
- 'add_time' => getTime(),
- 'update_time' => getTime(),
- ];
- $AddData = array_merge($Where, $AddData);
- $ResultId = $this->weapp_ask_answer_like_db->add($AddData);
-
- if (!empty($ResultId)) {
- unset($Where['users_id']);
-
- $LikeCount = $this->weapp_ask_answer_like_db->where($Where)->count();
-
- $UpdataNew = [
- 'click_like' => $LikeCount,
- 'update_time' => getTime(),
- ];
- $this->weapp_ask_answer_db->where($Where)->update($UpdataNew);
- $this->success('点赞成功!', null, $LikeCount);
- }
- }
- }
-
- $this->error('请求失败,请刷新重试!');
- }
-
-
- public function addAnswer($users = [])
- {
- if (IS_POST) {
- $param = input('param.');
- $this->users = $users;
- $this->users_id = $users['users_id'];
-
- $content = $this->AnswerDealWith($param, true);
-
-
- $AddAnswer = [
- 'ask_id' => $param['ask_id'],
-
- 'is_review' => 1 == $this->users['ask_is_review'] ? 0 : 1,
- 'type_id' => $param['type_id'],
- 'users_id' => $this->users_id,
- 'username' => $this->users['username'],
- 'users_ip' => clientIP(),
- 'content' => $content,
-
- 'answer_pid' => !empty($param['answer_id']) ? $param['answer_id'] : 0,
-
- 'at_users_id' => !empty($param['at_users_id']) ? $param['at_users_id'] : 0,
- 'at_answer_id'=> !empty($param['at_answer_id']) ? $param['at_answer_id'] : 0,
- 'add_time' => getTime(),
- 'update_time' => getTime(),
- ];
- $ResultId = $this->weapp_ask_answer_db->add($AddAnswer);
-
-
- if (!empty($ResultId)) {
-
- $this->UpdateAskReplies($param['ask_id'], true);
- if (1 == $this->users['ask_is_review']) {
- $this->success('回答成功,但你的回答需要管理员审核!', null, ['review' => true]);
- }else{
- $AddAnswer['answer_id'] = $ResultId;
- $AddAnswer['head_pic'] = $this->users['head_pic'];
- $AddAnswer['at_usersname'] = '';
- if (!empty($AddAnswer['at_users_id'])) {
- $FindData = Db::name('users')->field('nickname, username')->where('users_id', $AddAnswer['at_users_id'])->find();
- $AddAnswer['at_usersname'] = empty($FindData['nickname']) ? $FindData['username'] : $FindData['nickname'];
- }
- $ResultData = $this->AskLogic->GetReplyHtml($AddAnswer);
- $this->success('回答成功!', null, $ResultData);
- }
- }else{
- $this->error('提交信息有误,请刷新重试!');
- }
- }
- $this->error('请求失败,请刷新重试!');
- }
-
-
- private function AnswerDealWith($param = [], $is_add = true)
- {
-
- $this->IsAnswer($is_add);
-
- $rate = $this->AskLogic->GetRateData();
- if (!empty($rate['is_open']) && !empty($rate['duration'])){
- $map = [
- 'users_id' => $this->users_id,
- 'update_time' => array('gt', getTime() - $rate['duration']),
- ];
- $have = $this->weapp_ask_answer_db->where($map)->find();
- if($have){
- $this->error('您提交得太频繁了,请歇会再发布!');
- }
- }
- if (!empty($is_add)) {
-
- $count = Db::name('weapp_ask_answer')->where(['users_id'=>$this->users_id])->whereTime('add_time','today')->count();
- if (!empty($this->users['answer_count']) && $count >= $this->users['answer_count']) {
- $this->error("【{$this->users['level_name']}】每天最多回答{$this->users['answer_count']}次");
- }
-
- if (empty($param) || empty($param['ask_id']) ) $this->error('提交信息有误,请刷新重试!');
- }else{
-
- if (empty($is_add) && empty($param['ask_id'])) $this->error('请确认编辑问题!');
- }
-
- $content = '';
- $content = $this->AskLogic->ContentDealWith($param);
- if (empty($content)) $this->error('请写下你的回答!');
-
- $content = $this->check_sensitive($content);
-
- return $content;
- }
-
-
- private function check_sensitive($content){
- $sensitive = $this->AskLogic->GetSensitiveData();
- if (!empty($sensitive['sensitive_switch']) && !empty($sensitive['sensitive_data'])){
- list($count,$sensitiveWord,$content) = $this->AskLogic->sensitive($sensitive['sensitive_data'],$content);
- if ($count > 0 && !empty($sensitiveWord)){
- switch ($sensitive['sensitive_switch']){
- case "1":
- break;
- case "2":
- $this->users['ask_is_review'] = 1;
- break;
- case "3":
- $this->users['is_lock'] = -1;
- session('users',$this->users);
- Db::name("users")->where(['users_id'=>$this->users_id])->update(["is_lock"=>-1]);
- $this->error('您发布的内容存在敏感词,暂停发布信息权限!');
- break;
- case "4":
- $this->users['is_lock'] = -99;
- session('users',$this->users);
- Db::name("users")->where(['users_id'=>$this->users_id])->update(["is_lock"=>-99]);
- $this->error('您发布的内容存在敏感词!');
- break;
- }
- }
- }
-
- return $content;
- }
-
- private function IsAnswer($is_add = true)
- {
- if (empty($this->users['ask_is_release'])) {
- if (!empty($is_add)) {
- $this->error($this->users['level_name'].'不允许回复答案!');
- }else{
- $this->error($this->users['level_name'].'不允许编辑答案!');
- }
- }
- }
-
- public function UpdateAskReplies($ask_id = null, $IsAdd = true, $DelNum = 0)
- {
- if (empty($ask_id)) return false;
- if (!empty($IsAdd)) {
- $Updata = [
- 'replies' => Db::raw('replies+1'),
- 'update_time' => getTime(),
- ];
- }else{
- $Updata = [
- 'replies' => Db::raw('replies-1'),
- 'update_time' => getTime(),
- ];
- if ($DelNum > 0) $Updata['replies'] = Db::raw('replies-'.$DelNum);
- }
- $this->weapp_ask_db->where('ask_id', $ask_id)->update($Updata);
- }
-
-
- public function addAsk($users = [])
- {
- if (IS_POST) {
- $param = input('param.');
- $this->users = $users;
- $this->users_id = $users['users_id'];
-
- $content = $this->ParamDealWith($param, $this->users);
- $param['title'] = $this->check_sensitive($param['title']);
-
- $AddAsk = [
- 'type_id' => $param['ask_type_id'],
- 'users_id' => $this->users_id,
- 'ask_title' => $param['title'],
- 'content' => $content,
- 'users_ip' => clientIP(),
- 'add_time' => getTime(),
- 'update_time' => getTime(),
- 'money' => !empty($param['money']) ? $param['money'] : 0,
- ];
-
- if (1 == $this->users['ask_is_review']) $AddAsk['is_review'] = 0;
-
-
- $ResultId = $this->weapp_ask_db->add($AddAsk);
- if (!empty($ResultId)) {
- if (0 < $param['money'] && $this->reward_switch == 1){
-
- $this->AskModel->setUb($this->reward_type,$this->users_id,$ResultId,$param['money']);
- }
-
- $url = $this->AskLogic->GetUrlData($param, 'NewDateUrl');
- if (1 == $this->users['ask_is_review']) {
- $this->success('回答成功,但你的回答需要管理员审核!', $url, ['review' => true]);
- }else{
- $this->success('发布成功!', $url);
- }
- }else{
- $this->error('发布的信息有误,请检查!');
- }
- }
- $this->error('请求错误');
-
- }
-
-
- private function ParamDealWith($param = [], $users = [], $is_add = true)
- {
-
- $this->IsRelease($users, $is_add);
-
- $rate = $this->AskLogic->GetRateData();
- if (!empty($rate['is_open']) && !empty($rate['duration'])){
- $map = [
- 'users_id' => $users['users_id'],
- 'update_time' => array('gt', getTime() - $rate['duration']),
- ];
- $have = $this->weapp_ask_db->where($map)->find();
- if($have){
- $this->error('您提交得太频繁了,请歇会再发布!');
- }
- }
-
- if($is_add){
-
- $count = $this->weapp_ask_db->where(['users_id'=>$this->users_id])->whereTime('add_time','today')->count();
- if (!empty($users['ask_count']) && $count >= $users['ask_count']) {
- $this->error("【{$users['level_name']}】每天最多提问{$users['ask_count']}次");
- }
- }
-
- $content = '';
- if (empty($param)) $this->error('请提交完整信息!');
- if (empty($param['title'])) $this->error('请填写问题标题!');
- if (empty($param['ask_type_id'])) $this->error('请选择问题分类!');
- $content = $this->AskLogic->ContentDealWith($param);
- if (empty($content)) $this->error('请填写问题描述!');
-
- if (empty($is_add) && empty($param['ask_id'])) $this->error('请确认编辑问题!');
-
-
- $content = $this->check_sensitive($content);
-
- return $content;
- }
-
-
- private function IsRelease($users = [], $is_add = true)
- {
- if (empty($users['ask_is_release'])) {
- if (!empty($is_add)) {
- $this->error($users['level_name'].'不允许发布问题!');
- }else{
- $this->error($users['level_name'].'不允许编辑问题!');
- }
- }
- }
-
- }
|