Topicdongtai.php 33 KB

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