Topicdongtai.php 34 KB

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