Topicdongtai.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Keyworld;
  6. use think\Exception;
  7. /**
  8. * 圈子动态
  9. */
  10. class Topicdongtai extends Api
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = ['*'];
  14. //发布动态
  15. public function addone(){
  16. $content = input('content','');
  17. $images = input('images','');
  18. $audio_file = input('audio_file','');
  19. $topic_ids = input('topic_ids','');
  20. $aite = input('aite','','htmlspecialchars_decode');
  21. $type = input('type',1);
  22. $audio_second = input('audio_second',0);
  23. if(!$content && !$images && !$audio_file){
  24. $this->error(__('Invalid parameters'));
  25. }
  26. if(!$this->user_auth_limit()){
  27. $this->error('请先完成实名认证');
  28. }
  29. //关键字替换
  30. $content = Keyworld::sensitive($content);
  31. //只保留一个
  32. if($type == 1){
  33. $audio_file = '';
  34. $audio_second = 0;
  35. }else{
  36. $images = '';
  37. }
  38. $data = [
  39. 'topic_ids' => $topic_ids,
  40. 'user_id' => $this->auth->id,
  41. 'content' => $content,
  42. 'images' => $images,
  43. 'audio_file' => $audio_file,
  44. 'type' => $type,
  45. 'cityname' => input('cityname',''),
  46. 'provincename' => input('provincename',''),
  47. 'aite' => $aite,
  48. 'is_public' => input('is_public',1),
  49. 'audio_second' => $audio_second,
  50. 'longitude' => input('longitude',0),
  51. 'latitude' => input('latitude',0),
  52. 'createtime' => time(),
  53. 'updatetime' => time(),
  54. 'auditstatus' => 1, //默认通过
  55. ];
  56. //如果强制审核,默认不通过
  57. $dongtai_audit_switch = config('site.dongtai_audit_switch');
  58. if($dongtai_audit_switch == 1){
  59. $data['auditstatus'] = 0;
  60. }
  61. Db::startTrans();
  62. $id = Db::name('topic_dongtai')->insertGetId($data);
  63. //圈子新增一个贴
  64. $rs = Db::name('topic_hub')->where('id','IN',$topic_ids)->setInc('t_number');
  65. //task任务
  66. //发第一条动态
  67. $task_count = Db::name('topic_dongtai')->where('user_id',$this->auth->id)->count();
  68. if($task_count == 1){
  69. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,16);
  70. if($task_rs === false){
  71. Db::rollback();
  72. $this->error('完成任务失败');
  73. }
  74. }
  75. //通知艾特的人
  76. if(!empty($aite)){
  77. $aite = json_decode($aite,true);
  78. if(!empty($aite) && is_array($aite)){
  79. foreach($aite as $key => $uid){
  80. //系统消息
  81. if(is_array($uid) && isset($uid['id'])){
  82. $msg_id = \app\common\model\Message::addMessage($uid['id'],'动态通知',$this->auth->nickname.'发布动态并@了你,快去看看吧','dongtai_aite',$id);
  83. }
  84. }
  85. }
  86. }
  87. Db::commit();
  88. $this->success('发布成功',$id);
  89. }
  90. //自己看列表
  91. //某用户的帖子列表
  92. public function my_lists(){
  93. $page = input('page',1);
  94. if($page > 1){
  95. $this->success('success',[]);
  96. }
  97. $uid = input('uid',$this->auth->id);
  98. if (empty($uid)) {
  99. $uid = $this->auth->id;
  100. }
  101. $where = [
  102. 'dt.user_id'=>$uid,
  103. ];
  104. if($uid != $this->auth->id){
  105. $where['dt.is_public'] = 1; //不是自己的,就只能看公开的。公开:1=公开,2=私密
  106. $where['dt.auditstatus'] = 1; //不是自己的,就只能看审核通过的
  107. }
  108. $list = Db::name('topic_dongtai')->alias('dt')
  109. ->join('user','dt.user_id = user.id','LEFT')
  110. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  111. ->field('dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status,uw.vip_endtime')
  112. ->where($where)
  113. ->order('dt.id desc')->select();
  114. $list = list_domain_image($list,['images','audio_file','avatar']);
  115. if(!empty($list)){
  116. foreach($list as $key => &$val){
  117. $val['aite'] = json_decode($val['aite'],true);
  118. //用户年龄
  119. $val['age'] = birthtime_to_age($val['birthday']);
  120. unset($val['birthday']);
  121. //用户vip
  122. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  123. unset($val['vip_endtime']);
  124. //追加点赞
  125. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  126. //时间
  127. $val['createtime_text'] = get_last_time($val['createtime']);
  128. //关注
  129. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  130. //收藏
  131. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  132. //拉黑
  133. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  134. //层主评论数量
  135. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  136. //话题
  137. $ids_str = $val['topic_ids'];
  138. if($ids_str){
  139. $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->orderRaw('field(id,'.$ids_str.')')->column('name');
  140. }else{
  141. $val['topic_text'] = [];
  142. }
  143. }
  144. }
  145. $this->success('success',$list);
  146. }
  147. //动态删除
  148. public function delete(){
  149. $id = input('id',0);
  150. $where['id'] = $id;
  151. $where['user_id'] = $this->auth->id;
  152. $dongtai = Db::name('topic_dongtai')->field('id,topic_ids')->where($where)->find();
  153. if (empty($dongtai)) {
  154. $this->error('未找到动态信息');
  155. }
  156. Db::startTrans();
  157. $delRes = Db::name('topic_dongtai')->where('id',$id)->where('user_id',$this->auth->id)->delete();
  158. if (!$delRes) {
  159. Db::rollback();
  160. $this->error('动态删除失败');
  161. }
  162. //话题少一个贴
  163. if (!empty($dongtai['topic_ids'])) {
  164. $res = Db::name('topic_hub')->where('id','IN',$dongtai['topic_ids'])->setDec('t_number');
  165. /*话题可能被删掉了,这里不判断了
  166. if (!$res) {
  167. Db::rollback();
  168. $this->error('更新话题数量失败');
  169. }*/
  170. }
  171. //删除对应的评论,
  172. Db::name('topic_dongtai_answer')->where('dt_id',$id)->delete();
  173. //点赞,
  174. Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
  175. //评论点赞
  176. Db::name('topic_answer_good')->where('dt_id',$id)->delete();
  177. Db::commit();
  178. $this->success('删除成功');
  179. }
  180. //某个圈子里的动态列表,关注,最新,附近
  181. public function topic_list(){
  182. $where = [
  183. 'dt.is_public' => 1,
  184. 'dt.auditstatus' => 1,
  185. ];
  186. //话题
  187. $topic_id = input('topic_id',0);
  188. $where_exp = [];
  189. if($topic_id){
  190. $where_exp[] = ['exp',Db::raw("FIND_IN_SET('".$topic_id."',dt.topic_ids)")];
  191. }
  192. //最新
  193. $order = input('orderby','new');
  194. $orderby = 'dt.toptime desc,dt.id desc';
  195. //关注
  196. $where_follow = '';
  197. if($order == 'follow'){
  198. //关注的人
  199. $follow_user_ids = Db::name('user_follow')->where(['uid'=>$this->auth->id])->column('follow_uid');
  200. if(!empty($follow_user_ids)){
  201. $where_follow .= '(dt.user_id IN ('.implode(',',$follow_user_ids).'))';
  202. }
  203. //关注的话题
  204. $where_topic = "";
  205. $follow_topic_ids= Db::name('user_follow_topic')->where(['uid'=>$this->auth->id])->column('topic_id');
  206. if(!empty($follow_topic_ids)){
  207. if(!empty($follow_user_ids)){
  208. $where_follow .= ' or ';
  209. }
  210. $where_topic .= "(";
  211. foreach($follow_topic_ids as $ck => $cv){
  212. $where_topic .= "FIND_IN_SET('".$cv."',dt.topic_ids)";
  213. if($ck+1 < count($follow_topic_ids)){
  214. $where_topic .= " or ";
  215. }
  216. }
  217. $where_topic .= ")";
  218. $where_follow .= $where_topic;
  219. }
  220. //默认
  221. if($where_follow == ''){
  222. $where_follow = 'dt.id = 0';
  223. }
  224. }
  225. //附近,根据距离排序
  226. $having_dis = '';
  227. if($order == 'near'){
  228. // $where['dt.provincename'] = $this->auth->provincename;
  229. $having_dis = 'distance < ' . 100000; //100公里范围
  230. $orderby = 'dt.toptime desc,distance asc';
  231. }
  232. //性别
  233. $gender = input('gender','all');
  234. if($gender != 'all'){
  235. $where['user.gender'] = $gender;
  236. }
  237. //属性
  238. $attribute = input('attribute','all');
  239. if($attribute != 'all'){
  240. if($attribute == 'EE'){
  241. $where['user.attribute'] = ['IN',['EE','BOTH']];
  242. }
  243. if($attribute == 'ER'){
  244. $where['user.attribute'] = ['IN',['ER','BOTH']];
  245. }
  246. if($attribute == 'BOTH'){
  247. //$where['user.attribute'] = 'BOTH';
  248. }
  249. }
  250. //排除黑名单的
  251. $where_black = [];
  252. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  253. if(!empty($black_ids)){
  254. $where_black['dt.user_id'] = ['NOTIN',$black_ids];
  255. }
  256. //列表
  257. $field = 'dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status,uw.vip_endtime';
  258. if($order == 'near'){
  259. $field .= ',(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(dt.longitude,dt.latitude))*111195) as distance';
  260. }
  261. $list = Db::name('topic_dongtai')->alias('dt')
  262. ->join('user','dt.user_id = user.id','LEFT')
  263. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  264. ->field($field)
  265. ->where($where)
  266. ->where($where_exp)
  267. ->where($where_follow)
  268. ->where($where_black);
  269. if($order == 'near'){
  270. $list = $list->having($having_dis);
  271. }
  272. $list = $list->order($orderby)
  273. ->autopage()->select();
  274. $list = list_domain_image($list,['images','audio_file','avatar']);
  275. if(!empty($list)){
  276. foreach($list as $key => &$val){
  277. //艾特
  278. $val['aite'] = json_decode($val['aite'],true);
  279. //距离
  280. $val['distance'] = isset($val['distance']) ? $val['distance'] : 0;
  281. $val['distance'] = bcdiv(intval($val['distance']),1000,1).'km';
  282. if($this->auth->longitude == 0 || $val['longitude'] == 0){
  283. $val['distance'] = '未知';
  284. }
  285. //vip如果开了隐私保护,需要隐藏距离
  286. $yinsi = $this->user_power($val['user_id'],'yinsi');
  287. if($yinsi == 1){
  288. $val['distance'] = '';
  289. }
  290. //用户年龄
  291. $val['age'] = birthtime_to_age($val['birthday']);
  292. unset($val['birthday']);
  293. //用户vip
  294. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  295. unset($val['vip_endtime']);
  296. //追加点赞
  297. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  298. //时间
  299. $val['createtime_text'] = get_last_time($val['createtime']);
  300. //关注
  301. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  302. //收藏
  303. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  304. //拉黑
  305. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  306. //层主评论数量
  307. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  308. //话题
  309. $ids_str = $val['topic_ids'];
  310. if($ids_str){
  311. $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->orderRaw('field(id,'.$ids_str.')')->column('name');
  312. }else{
  313. $val['topic_text'] = [];
  314. }
  315. //艾特了谁
  316. // $val['aite_user'] = Db::name('user')->where('id','IN',$val['aite'])->column('nickname');
  317. }
  318. }
  319. $this->success('success',$list);
  320. }
  321. //详情
  322. public function info(){
  323. $id = input('id');
  324. $info = Db::name('topic_dongtai')->alias('dt')
  325. ->join('user','dt.user_id = user.id','LEFT')
  326. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  327. ->field('dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status,uw.vip_endtime')
  328. ->where('dt.id',$id)->find();
  329. $info = info_domain_image($info,['images','audio_file','avatar']);
  330. if($info){
  331. $info['aite'] = json_decode($info['aite'],true);
  332. //用户年龄
  333. $info['age'] = birthtime_to_age($info['birthday']);
  334. unset($info['birthday']);
  335. //用户vip
  336. $info['is_vip'] = $info['vip_endtime'] > time() ? 1 : 0;
  337. unset($info['vip_endtime']);
  338. //是否点赞过
  339. $info['isgood'] = $this->is_good($id,$this->auth->id);
  340. //时间
  341. $info['createtime_text'] = get_last_time($info['createtime']);
  342. //关注
  343. $info['is_follow'] = $this->is_follow($this->auth->id,$info['user_id']);
  344. //收藏
  345. $info['is_collect'] = $this->is_collect($id,$this->auth->id);
  346. //拉黑
  347. $info['is_black'] = $this->is_black($this->auth->id,$info['user_id']);
  348. //层主评论数量
  349. $info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->count();
  350. //话题
  351. $ids_str = $info['topic_ids'];
  352. if($ids_str){
  353. $info['topic_text'] = Db::name('topic_hub')->where('id','IN',$ids_str)->orderRaw('field(id,'.$ids_str.')')->column('name');
  354. }else{
  355. $info['topic_text'] = [];
  356. }
  357. }else{
  358. $this->error('此动态已被删除');
  359. }
  360. $this->success('success',$info);
  361. }
  362. //点赞,取消点赞
  363. public function good(){
  364. $id = input('id');
  365. $where = [
  366. 'dt_id' => $id,
  367. 'user_id' => $this->auth->id,
  368. ];
  369. $check = Db::name('topic_dongtai_good')->where($where)->find();
  370. $dt_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  371. if($check){
  372. Db::name('topic_dongtai_good')->where($where)->delete();
  373. $down = Db::name('topic_dongtai')->where('id',$id)->setDec('goodnum');
  374. $this->success('已取消点赞');
  375. }else{
  376. Db::startTrans();
  377. $where['createtime'] = time();
  378. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  379. if(!$rs){
  380. Db::rollback();
  381. $this->error('点赞失败');
  382. }
  383. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  384. if($up === false){
  385. Db::rollback();
  386. $this->error('点赞失败');
  387. }
  388. //系统消息
  389. if($dt_user_id != $this->auth->id){
  390. $msg_id = \app\common\model\Message::addMessage($dt_user_id,'动态点赞',$this->auth->nickname.'('.$this->auth->username.')赞了你的动态','dongtai_good',$id);
  391. }
  392. Db::commit();
  393. $this->success('点赞成功');
  394. }
  395. }
  396. //评论
  397. public function answer(){
  398. if($this->apiLimit(1,5000) == false){
  399. $this->error('抱歉,您在5秒内只能进行一次回复');
  400. }
  401. $id = input('id',0);
  402. $content = input('content','');
  403. $to_user_id = input('to_user_id',0);
  404. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  405. $floor = input('floor',0);
  406. if(empty($content) || empty($id)){
  407. $this->error();
  408. }
  409. //判断
  410. if($level == 2 && $floor == 0){
  411. $this->error('楼层错误');
  412. }
  413. //黑名单判断
  414. //是否被动态发布者拉黑
  415. $dongtai_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  416. $black_check = $this->is_black($dongtai_user_id,$this->auth->id);
  417. if($black_check){
  418. $this->error('您已被对方拉黑,禁止评论此动态');
  419. }
  420. //是否被层主拉黑
  421. if($level == 2){
  422. $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();
  423. $black_check = $this->is_black($answer_info['user_id'],$this->auth->id);
  424. if($black_check){
  425. $this->error('您已被对方拉黑,禁止点评此评论');
  426. }
  427. }
  428. //关键字替换
  429. $content = Keyworld::sensitive($content);
  430. //回复楼主,最新楼层
  431. if($level == 1 || $floor == 0){
  432. $to_user_id = 0;
  433. $floor = 1; //默认一楼
  434. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  435. if($last_floor){
  436. $floor = $last_floor + 1;
  437. }
  438. }
  439. //判断user_id
  440. if($to_user_id){
  441. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  442. if(empty($to_user)){
  443. $this->error('被回复的用户不存在');
  444. }
  445. }
  446. //data
  447. $data = [
  448. 'dt_id' => $id,
  449. 'floor' => $floor,
  450. 'user_id' => $this->auth->id,
  451. 'content' => $content,
  452. 'to_user_id' => $to_user_id,
  453. 'level' => $level,
  454. 'createtime' => time(),
  455. 'updatetime' => time(),
  456. ];
  457. Db::startTrans();
  458. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  459. Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
  460. //系统消息
  461. if($level == 1){
  462. //发给动态用户
  463. $msg_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  464. $msg_title = '动态评论';
  465. $msg_content = $this->auth->nickname.'评论了你的动态';
  466. $infotype_id = $rs;
  467. }else{
  468. //发给层主
  469. $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();
  470. $msg_user_id = $answer_info['user_id'];
  471. $msg_title = '动态评论点评';
  472. $msg_content = $this->auth->nickname.'点评了你的动态评论';
  473. $infotype_id = $answer_info['id'];
  474. }
  475. $msg_id = \app\common\model\Message::addMessage($msg_user_id,$msg_title,$msg_content,'dongtai_answer',$infotype_id);
  476. Db::commit();
  477. $this->success('评论成功');
  478. }
  479. //对评论点赞
  480. public function answer_good(){
  481. $dt_id = input('dt_id',0);
  482. $answer_id = input('answer_id',0);
  483. $where = [
  484. 'dt_id' => $dt_id,
  485. 'answer_id' => $answer_id,
  486. 'user_id' => $this->auth->id,
  487. ];
  488. $check = Db::name('topic_answer_good')->where($where)->find();
  489. if($check){
  490. Db::name('topic_answer_good')->where($where)->delete();
  491. Db::name('topic_dongtai_answer')->where('id',$answer_id)->setDec('goodnum');
  492. $this->success('已取消点赞');
  493. }else{
  494. Db::startTrans();
  495. $where['createtime'] = time();
  496. $rs = Db::name('topic_answer_good')->insertGetId($where);
  497. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  498. if($rs && $up !== false){
  499. Db::commit();
  500. $this->success('点赞成功');
  501. }
  502. Db::rollback();
  503. $this->error('点赞失败');
  504. }
  505. }
  506. //举报枚举
  507. /*public function report_enum(){
  508. $arr = [
  509. '侮辱谩骂',
  510. '色情低俗',
  511. '政治敏感',
  512. '违法违规',
  513. '其他',
  514. ];
  515. $this->success(1,$arr);
  516. }*/
  517. //举报
  518. public function report(){
  519. $dt_id = input('dt_id',0);
  520. $data['dt_id'] = $dt_id;
  521. $check = Db::name('topic_dongtai')->where('id',$data['dt_id'])->find();
  522. if(empty($check)){
  523. $this->error('不存在的动态');
  524. }
  525. $data['user_id'] = $this->auth->id;
  526. $data['to_user_id'] = $check['user_id'];
  527. $data['createtime'] = time();
  528. Db::name('topic_dongtai_report')->insertGetId($data);
  529. $this->success('举报成功');
  530. }
  531. //收藏,取消收藏
  532. public function collect(){
  533. $where = [
  534. 'user_id' => $this->auth->id,
  535. 'table' => 'topic_dongtai',
  536. 'table_id' => input('id',0),
  537. ];
  538. $check = Db::name('user_collect')->where($where)->find();
  539. if($check){
  540. Db::name('user_collect')->where($where)->delete();
  541. $this->success('已取消收藏');
  542. }else{
  543. Db::name('user_collect')->insertGetId($where);
  544. $this->success('收藏成功');
  545. }
  546. }
  547. //我的收藏
  548. public function my_collect(){
  549. $collect_id = Db::name('user_collect')->where(['table'=>'topic_dongtai','user_id'=>$this->auth->id])->column('table_id');
  550. $where = ['dt.id'=>['IN',$collect_id]];
  551. $list = Db::name('topic_dongtai')->alias('dt')
  552. ->join('user','dt.user_id = user.id','LEFT')
  553. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  554. ->field('dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status,uw.vip_endtime')
  555. ->where($where)
  556. ->order('dt.id desc')->autopage()->select();
  557. $list = list_domain_image($list,['images','audio_file','avatar']);
  558. if(!empty($list)){
  559. foreach($list as $key => &$val){
  560. $val['aite'] = json_decode($val['aite'],true);
  561. //用户年龄
  562. $val['age'] = birthtime_to_age($val['birthday']);
  563. unset($val['birthday']);
  564. //用户vip
  565. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  566. unset($val['vip_endtime']);
  567. //追加点赞
  568. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  569. //时间
  570. $val['createtime_text'] = get_last_time($val['createtime']);
  571. //关注
  572. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  573. //收藏
  574. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  575. //拉黑
  576. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  577. //层主评论数量
  578. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  579. //话题
  580. $ids_str = $val['topic_ids'];
  581. if($ids_str){
  582. $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->orderRaw('field(id,'.$ids_str.')')->column('name');
  583. }else{
  584. $val['topic_text'] = [];
  585. }
  586. }
  587. }
  588. $this->success('success',$list);
  589. }
  590. //不感兴趣,屏蔽某条
  591. /*public function screen(){
  592. $data = [
  593. 'user_id' => $this->auth->id,
  594. 'dt_id' => input('dt_id',0),
  595. ];
  596. $check = Db::name('topic_dongtai_screen')->where($data)->find();
  597. if($check){
  598. $this->success('操作成功');
  599. }
  600. Db::name('topic_dongtai_screen')->insertGetId($data);
  601. $this->success('操作成功');
  602. }*/
  603. //评论列表
  604. public function answer_list(){
  605. $dt_id = input('dt_id',0);
  606. //楼
  607. $floor_list = Db::name('topic_dongtai_answer')
  608. ->alias('a')
  609. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status')
  610. ->join('user','a.user_id = user.id','LEFT')
  611. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('a.id desc')->autopage()->select();
  612. $floor_list = list_domain_image($floor_list,['avatar']);
  613. //追加子评论
  614. if(!empty($floor_list)){
  615. foreach($floor_list as $key => &$val){
  616. //下面几条子回复,字符串
  617. $val['childremark'] = '';
  618. $map = [
  619. 'a.dt_id' => $dt_id,
  620. 'a.floor' => $val['floor'],
  621. 'a.level' => 2,
  622. ];
  623. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  624. if($number > 0){
  625. $answer_info = Db::name('topic_dongtai_answer')
  626. ->alias('a')
  627. ->field('user.nickname')
  628. ->join('user','a.user_id = user.id','LEFT')
  629. ->where($map)->order('a.id desc')->find();
  630. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  631. }
  632. //时间处理
  633. $val['createtime_text'] = get_last_time($val['createtime']);
  634. //回复是否已赞
  635. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  636. //用户年龄
  637. $val['age'] = birthtime_to_age($val['birthday']);
  638. unset($val['birthday']);
  639. }
  640. }
  641. $this->success(1,$floor_list);
  642. }
  643. //单独某一层的详细
  644. public function answer_info(){
  645. $answer_id = input('answer_id');
  646. //楼
  647. $floor_info = Db::name('topic_dongtai_answer')
  648. ->alias('a')
  649. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status')
  650. ->join('user','a.user_id = user.id','LEFT')
  651. ->where(['a.id'=>$answer_id])->find();
  652. if(empty($floor_info)){
  653. $this->success(1,[]);
  654. }
  655. $floor_info = info_domain_image($floor_info,['avatar']);
  656. $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);
  657. //用户年龄
  658. $floor_info['age'] = birthtime_to_age($floor_info['birthday']);
  659. unset($floor_info['birthday']);
  660. //回复是否已赞
  661. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  662. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  663. //层
  664. $floors = $floor_info['floor'];
  665. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  666. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status')
  667. ->join('user','a.user_id = user.id','LEFT')
  668. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();
  669. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  670. if(!empty($child_lists)){
  671. foreach($child_lists as $key => &$answer){
  672. //用户年龄
  673. $answer['age'] = birthtime_to_age($answer['birthday']);
  674. unset($answer['birthday']);
  675. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  676. $answer['createtime_text'] = get_last_time($answer['createtime']);
  677. }
  678. }
  679. //合并
  680. $floor_info['child'] = $child_lists;
  681. $this->success('success',$floor_info);
  682. }
  683. //是否点赞
  684. private function is_good($dt_id,$uid){
  685. $where = [
  686. 'dt_id' => $dt_id,
  687. 'user_id' => $uid,
  688. ];
  689. $check = Db::name('topic_dongtai_good')->where($where)->find();
  690. if($check){
  691. return 1;
  692. }else{
  693. return 0;
  694. }
  695. }
  696. //回复是否点赞
  697. private function answer_is_good($answer_id,$uid){
  698. $where = [
  699. 'answer_id' => $answer_id,
  700. 'user_id' => $uid,
  701. ];
  702. $check = Db::name('topic_answer_good')->where($where)->find();
  703. if($check){
  704. return 1;
  705. }else{
  706. return 0;
  707. }
  708. }
  709. //动态是否收藏
  710. private function is_collect($dt_id,$uid){
  711. $where = [
  712. 'user_id' => $uid,
  713. 'table' => 'topic_dongtai',
  714. 'table_id' => $dt_id,
  715. ];
  716. $check = Db::name('user_collect')->where($where)->find();
  717. if($check){
  718. return 1;
  719. }else{
  720. return 0;
  721. }
  722. }
  723. //用户是否拉黑
  724. private function is_black($uid,$black_uid){
  725. $where = [
  726. 'uid' => $uid,
  727. 'black_uid' => $black_uid,
  728. ];
  729. $check = Db::name('user_black')->where($where)->find();
  730. if($check){
  731. return 1;
  732. }else{
  733. return 0;
  734. }
  735. }
  736. //我的评论
  737. public function my_answer(){
  738. $map = [
  739. 'a.user_id' => $this->auth->id,
  740. 'a.level' => 1,
  741. ];
  742. $list = Db::name('topic_dongtai_answer')->alias('a')
  743. ->field('a.id,a.createtime,a.content,a.dt_id,
  744. dt.images,dt.content as dt_content,dt.type as dt_type,dtuser.nickname as dtuser_nickname,dtuser.username as dtuser_username,user.username,
  745. user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status,uw.vip_endtime')
  746. ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')
  747. ->join('user dtuser','dt.user_id = dtuser.id','LEFT')
  748. ->join('user user','a.user_id = user.id','LEFT')
  749. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  750. ->where($map)->order('a.id desc')->autopage()->select();
  751. $list = list_domain_image($list,['avatar']);
  752. if(!empty($list)){
  753. foreach($list as $key => &$val){
  754. //用户年龄
  755. $val['age'] = birthtime_to_age($val['birthday']);
  756. unset($val['birthday']);
  757. //用户vip
  758. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  759. unset($val['vip_endtime']);
  760. }
  761. }
  762. $this->success(1,$list);
  763. }
  764. //删除我的某个评论
  765. public function delete_answer(){
  766. $id = input('id',0);
  767. if(!$id){
  768. $this->error();
  769. }
  770. Db::startTrans();
  771. $info = Db::name('topic_dongtai_answer')->where('id',$id)->where('user_id',$this->auth->id)->find();
  772. if(!$info){
  773. $this->error('不存在的动态评论');
  774. }
  775. if($info['level'] == 1){
  776. //楼层内都删
  777. $louceng_id = Db::name('topic_dongtai_answer')->where('dt_id',$info['dt_id'])->where('level',2)->where('floor',$info['floor'])->column('id');
  778. if(!empty($louceng_id)){
  779. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum',count($louceng_id));//回复数减1
  780. Db::name('topic_dongtai_answer')->where('id','IN',$louceng_id)->delete();//评论删掉
  781. Db::name('topic_answer_good')->where('answer_id','IN',$louceng_id)->delete();//评论点赞删掉
  782. }
  783. }
  784. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum');//回复数减1
  785. Db::name('topic_dongtai_answer')->where('id',$id)->delete(); //评论删掉
  786. Db::name('topic_answer_good')->where('answer_id',$id)->delete();//评论点赞删掉
  787. Db::commit();
  788. $this->success();
  789. }
  790. //我点赞的动态
  791. public function my_good(){
  792. $where = ['good.user_id'=>$this->auth->id];
  793. $list = Db::name('topic_dongtai')->alias('dt')
  794. ->join('user','dt.user_id = user.id','LEFT')
  795. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  796. ->join('topic_dongtai_good good','dt.id = good.dt_id','LEFT')
  797. ->field('dt.*,dt.id as dt_id,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,user.idcard_status,uw.vip_endtime')
  798. ->where($where)
  799. ->order('dt.id desc')->autopage()->select();
  800. $list = list_domain_image($list,['images','audio_file','avatar']);
  801. if(!empty($list)){
  802. foreach($list as $key => &$val){
  803. $val['aite'] = json_decode($val['aite'],true);
  804. //用户年龄
  805. $val['age'] = birthtime_to_age($val['birthday']);
  806. unset($val['birthday']);
  807. //用户vip
  808. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  809. unset($val['vip_endtime']);
  810. //追加点赞
  811. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  812. //时间
  813. $val['createtime_text'] = get_last_time($val['createtime']);
  814. //关注
  815. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  816. //收藏
  817. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  818. //拉黑
  819. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  820. //层主评论数量
  821. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  822. //话题
  823. $ids_str = $val['topic_ids'];
  824. if($ids_str){
  825. $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->orderRaw('field(id,'.$ids_str.')')->column('name');
  826. }else{
  827. $val['topic_text'] = [];
  828. }
  829. }
  830. }
  831. $this->success('success',$list);
  832. }
  833. }