Topicdongtai.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Keyworld;
  6. /**
  7. * 圈子动态
  8. */
  9. class Topicdongtai extends Api
  10. {
  11. protected $noNeedLogin = [''];
  12. protected $noNeedRight = ['*'];
  13. //发布动态
  14. public function adddongtai(){
  15. $content = input('content','', 'trim');
  16. $images = input('images','', 'trim');
  17. $topic_id = input('topic_id', 0, 'intval'); //热门话题id
  18. $type = input('type', 0, 'intval'); //类型:0=文字,1=图片,2=视频
  19. if(!$content && !$images){
  20. $this->error(__('Invalid parameters'));
  21. }
  22. if ($topic_id) {
  23. $topic_info = Db::name('topic_hub')->where(['id' => $topic_id])->find();
  24. if (!$topic_info) {
  25. $this->error('话题已过时,请重新选择');
  26. }
  27. if ($topic_info['status'] != 1) {
  28. $this->error('话题已过时,请重新选择');
  29. }
  30. }
  31. if (!in_array($type, [0, 1, 2])) {
  32. $this->error('您的网络开小差啦~');
  33. }
  34. //关键字替换
  35. $content = Keyworld::sensitive($content);
  36. $data = [
  37. 'topic_id' => $topic_id,
  38. 'user_id' => $this->auth->id,
  39. 'content' => $content,
  40. 'images' => $images,
  41. 'createtime' => time(),
  42. 'updatetime' => time(),
  43. 'type' => $type
  44. ];
  45. Db::startTrans();
  46. $id = Db::name('topic_dongtai')->insertGetId($data);
  47. if (!$id) {
  48. Db::rollback();
  49. $this->error('您的网络开小差啦~');
  50. }
  51. //圈子新增一个贴
  52. if ($topic_id) {
  53. $rs = Db::name('topic_hub')->where('id', $topic_id)->setInc('t_number');
  54. if (!$rs) {
  55. Db::rollback();
  56. $this->error('您的网络开小差啦~');
  57. }
  58. }
  59. Db::commit();
  60. $this->success('发布成功',$id);
  61. }
  62. //动态列表
  63. public function dongtailist() {
  64. $type = input('type', 0, 'intval'); //类型:0热门 1关注
  65. $topic_id = input('topic_id', 0); //热门话题id
  66. if (!in_array($type, [0, 1])) {
  67. $this->error('您的网络开小差啦~');
  68. }
  69. //关注
  70. $where_follow = '';
  71. if ($type == 0) {
  72. $orderby = 'dt.id desc';
  73. } else {
  74. $orderby = 'dt.id desc';
  75. //关注的人
  76. $follow_user_ids = Db::name('user_follow')->where(['uid'=>$this->auth->id])->column('follow_uid');
  77. if(!empty($follow_user_ids)){
  78. $where_follow .= '(dt.user_id IN ('.implode(',',$follow_user_ids).'))';
  79. }
  80. //默认
  81. if($where_follow == ''){
  82. $where_follow = 'dt.id = 0';
  83. }
  84. }
  85. $where['dt.status'] = 0;
  86. $where['dt.auit_status'] = 1;
  87. $where['user.is_kefu'] = 0;
  88. if ($this->auth->gender == 1) {
  89. $where['user.gender'] = 0;
  90. } elseif ($this->auth->gender == 0) {
  91. $where['user.gender'] = 1;
  92. } else {
  93. $this->success('success',[]);
  94. }
  95. if ($topic_id) {
  96. $where['dt.topic_id'] = $topic_id;
  97. $orderby = 'dt.id desc';
  98. }
  99. $list = Db::name('topic_dongtai')->alias('dt')
  100. ->join('user','dt.user_id = user.id','LEFT')
  101. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  102. ->join('topic_hub th','dt.topic_id = th.id','LEFT')
  103. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  104. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status,user.is_hideaddress,th.name,uw.vip_endtime,ur.nickname_remark')
  105. ->where($where)
  106. ->where($where_follow)
  107. ->order($orderby)->autopage()->select();
  108. $list = list_domain_image($list,['images','avatar']);
  109. //追加是否点赞
  110. if(!empty($list)){
  111. $ids = array_column($list,'id');
  112. $map = [
  113. 'dt_id' => ['IN',$ids],
  114. 'user_id' => $this->auth->id,
  115. ];
  116. $good_list = Db::name('topic_dongtai_good')->where($map)->select();
  117. $mt_user_greet = Db::name('user_greet'); //是否打过招呼
  118. $mt_gift_user_dongtai = Db::name('gift_user_dongtai');
  119. foreach ($list as &$val) {
  120. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  121. $val['name'] = $val['name'] ? : '';
  122. $val['birthday'] = birthtime_to_age($val['birthday']);
  123. $val['createtime'] = get_last_time($val['createtime']);
  124. $val['cityname'] = $val['is_hideaddress'] ? '' : $val['address'] ;
  125. //用户vip
  126. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  127. unset($val['vip_endtime']);
  128. //是否点过赞:0否 1是
  129. $val['isgood'] = 0;
  130. foreach($good_list as $k => $v){
  131. if($val['id'] == $v['dt_id']){
  132. $val['isgood'] = 1;
  133. }
  134. }
  135. //礼物数量
  136. $val['gift_count'] = $mt_gift_user_dongtai->where(['dt_id' => $val['id']])->count('id');
  137. //查询是否打过招呼
  138. $count = $mt_user_greet->where(['user_id' => $this->auth->id, 'user_to_id' => $val['user_id']])->count('id');
  139. if ($count) {
  140. $val['is_chat'] = 1; //是否打过招呼: 1是 0否
  141. } else {
  142. $val['is_chat'] = 0; //是否打过招呼: 1是 0否
  143. }
  144. //创建视频缩略图
  145. $val['images_thumb'] = '';
  146. if ($val['type'] == 2) {
  147. $images_url = explode('.', $val['images']);
  148. unset($images_url[count($images_url) - 1]);
  149. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  150. }
  151. }
  152. }
  153. $this->success('success',$list);
  154. }
  155. //用户动态列表
  156. public function mydongtailist() {
  157. $user_id = input('uid', $this->auth->id);
  158. if (!$user_id) {
  159. $this->error('您的网络开小差啦~');
  160. }
  161. $where['dt.user_id'] = $user_id;
  162. $where['dt.status'] = 0;
  163. if($user_id != $this->auth->id){
  164. $where['dt.auit_status'] = 1;
  165. }
  166. $orderby = 'dt.id desc';
  167. $list = Db::name('topic_dongtai')->alias('dt')
  168. ->join('user','dt.user_id = user.id','LEFT')
  169. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  170. ->join('topic_hub th','dt.topic_id = th.id','LEFT')
  171. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  172. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status,user.is_hideaddress,th.name,uw.vip_endtime,ur.nickname_remark')
  173. ->where($where)
  174. ->order($orderby)->autopage()->select();
  175. $list = list_domain_image($list,['images','avatar']);
  176. //追加是否点赞
  177. if(!empty($list)){
  178. $ids = array_column($list,'id');
  179. $map = [
  180. 'dt_id' => ['IN',$ids],
  181. 'user_id' => $this->auth->id,
  182. ];
  183. $good_list = Db::name('topic_dongtai_good')->where($map)->select();
  184. $mt_user_greet = Db::name('user_greet'); //是否打过招呼
  185. $mt_gift_user_dongtai = Db::name('gift_user_dongtai');
  186. foreach ($list as &$val) {
  187. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  188. $val['name'] = $val['name'] ? : '';
  189. $val['birthday'] = birthtime_to_age($val['birthday']);
  190. $val['createtime'] = get_last_time($val['createtime']);
  191. $val['cityname'] = $val['is_hideaddress'] ? '' : $val['address'];
  192. //用户vip
  193. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  194. unset($val['vip_endtime']);
  195. //是否点过赞:0否 1是
  196. $val['isgood'] = 0;
  197. foreach($good_list as $k => $v){
  198. if($val['id'] == $v['dt_id']){
  199. $val['isgood'] = 1;
  200. }
  201. }
  202. //礼物数量
  203. $val['gift_count'] = $mt_gift_user_dongtai->where(['dt_id' => $val['id']])->count('id');
  204. //查询是否打过招呼
  205. $count = $mt_user_greet->where(['user_id' => $this->auth->id, 'user_to_id' => $val['user_id']])->count('id');
  206. if ($count) {
  207. $val['is_chat'] = 1; //是否打过招呼: 1是 0否
  208. } else {
  209. $val['is_chat'] = 0; //是否打过招呼: 1是 0否
  210. }
  211. //创建视频缩略图
  212. $val['images_thumb'] = '';
  213. if ($val['type'] == 2) {
  214. $images_url = explode('.', $val['images']);
  215. unset($images_url[count($images_url) - 1]);
  216. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  217. }
  218. }
  219. }
  220. $this->success('success',$list);
  221. }
  222. //动态详情
  223. public function dongtaiinfo(){
  224. $id = input('id', 0, 'intval');
  225. if (!$id) {
  226. $this->error('您的网络开小差啦~');
  227. }
  228. $info = Db::name('topic_dongtai')->alias('dt')
  229. ->join('user','dt.user_id = user.id','LEFT')
  230. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  231. ->join('topic_hub th','dt.topic_id = th.id','LEFT')
  232. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  233. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status,user.is_hideaddress,th.name,uw.vip_endtime,ur.nickname_remark')
  234. ->where('dt.id',$id)->find();
  235. if (!$info) {
  236. $this->error('您的网络开小差啦~');
  237. }
  238. if ($info['status'] != 0) {
  239. $this->error('您的网络开小差啦~');
  240. }
  241. $info = info_domain_image($info,['images','avatar']);
  242. $info['nickname'] = !empty($info['nickname_remark']) ? $info['nickname_remark'] : $info['nickname'];
  243. $info['name'] = $info['name'] ? : '';
  244. $info['birthday'] = birthtime_to_age($info['birthday']);
  245. $info['createtime'] = get_last_time($info['createtime']);
  246. $info['cityname'] = $info['is_hideaddress'] ? '' : $info['address'];
  247. //用户vip
  248. $info['is_vip'] = $info['vip_endtime'] > time() ? 1 : 0;
  249. unset($info['vip_endtime']);
  250. //是否点赞过
  251. $info['isgood'] = $this->is_good($id,$this->auth->id);
  252. //礼物数量
  253. $info['gift_count'] = Db::name('gift_user_dongtai')->where(['dt_id' => $info['id']])->count('id');
  254. //查询是否打过招呼
  255. $count = Db::name('user_greet')->where(['user_id' => $this->auth->id, 'user_to_id' => $info['user_id']])->count('id');
  256. if ($count) {
  257. $info['is_chat'] = 1; //是否打过招呼: 1是 0否
  258. } else {
  259. $info['is_chat'] = 0; //是否打过招呼: 1是 0否
  260. }
  261. //创建视频缩略图
  262. $info['images_thumb'] = '';
  263. if ($info['type'] == 2) {
  264. $images_url = explode('.', $info['images']);
  265. unset($images_url[count($images_url) - 1]);
  266. $info['images_thumb'] = join('.', $images_url) . '_0.jpg';
  267. }
  268. //是否收藏
  269. $info['is_collect'] = $this->is_collect($id,$this->auth->id);
  270. $this->success('success',$info);
  271. }
  272. //点赞
  273. public function good(){
  274. $id = input('id', 0, 'intval');
  275. if (!$id) {
  276. $this->error('您的网络开小差啦~');
  277. }
  278. $info = Db::name('topic_dongtai')->find($id);
  279. if (!$info) {
  280. $this->error('您的网络开小差啦~');
  281. }
  282. if ($info['status'] != 0) {
  283. $this->error('您的网络开小差啦~');
  284. }
  285. $where = [
  286. 'dt_id' => $id,
  287. 'user_id' => $this->auth->id,
  288. ];
  289. $check = Db::name('topic_dongtai_good')->where($where)->find();
  290. $dt_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  291. if($check){
  292. Db::name('topic_dongtai_good')->where($where)->delete();
  293. $down = Db::name('topic_dongtai')->where('id',$id)->setDec('goodnum');
  294. $this->success('已取消点赞');
  295. }else{
  296. Db::startTrans();
  297. $where['createtime'] = time();
  298. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  299. if(!$rs){
  300. Db::rollback();
  301. $this->error('点赞失败');
  302. }
  303. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  304. if($up === false){
  305. Db::rollback();
  306. $this->error('点赞失败');
  307. }
  308. //系统消息
  309. if($dt_user_id != $this->auth->id){
  310. //入库
  311. $data = [];
  312. $data['user_id'] = $dt_user_id;
  313. $data['dt_id'] = $id;
  314. $data['from_user_id'] = $this->auth->id;
  315. $data['title'] = '点赞了你的动态';
  316. $data['createtime'] = time();
  317. Db::name('topic_dongtai_message')->insertGetId($data);
  318. }
  319. Db::commit();
  320. $this->success('点赞成功');
  321. }
  322. }
  323. //是否点赞
  324. private function is_good($dt_id,$uid){
  325. $where = [
  326. 'dt_id' => $dt_id,
  327. 'user_id' => $uid,
  328. ];
  329. $check = Db::name('topic_dongtai_good')->where($where)->find();
  330. if($check){
  331. return 1;
  332. }else{
  333. return 0;
  334. }
  335. }
  336. //评论
  337. public function answer(){
  338. $id = input('id',0);
  339. $content = input('content','');
  340. $to_user_id = input('to_user_id',0);
  341. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  342. $floor = input('floor',0);
  343. if(empty($content) || empty($id)){
  344. $this->error();
  345. }
  346. //关键字替换
  347. $content = Keyworld::sensitive($content);
  348. //判断
  349. if($level == 2 && $floor == 0){
  350. $this->error('楼层错误');
  351. }
  352. //黑名单判断
  353. //是否被动态发布者拉黑
  354. $dongtai_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  355. $black_check = $this->is_black($dongtai_user_id,$this->auth->id);
  356. if($black_check){
  357. $this->error('您已被对方拉黑,禁止评论此动态');
  358. }
  359. //是否被层主拉黑
  360. if($level == 2){
  361. $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();
  362. $black_check = $this->is_black($answer_info['user_id'],$this->auth->id);
  363. if($black_check){
  364. $this->error('您已被对方拉黑,禁止点评此评论');
  365. }
  366. }
  367. //回复楼主,最新楼层
  368. if($level == 1 || $floor == 0){
  369. $to_user_id = 0;
  370. $floor = 1; //默认一楼
  371. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  372. if($last_floor){
  373. $floor = $last_floor + 1;
  374. }
  375. }
  376. //判断user_id
  377. if($to_user_id){
  378. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  379. if(empty($to_user)){
  380. $this->error('被回复的用户不存在');
  381. }
  382. }
  383. //data
  384. $data = [
  385. 'dt_id' => $id,
  386. 'floor' => $floor,
  387. 'user_id' => $this->auth->id,
  388. 'content' => $content,
  389. 'to_user_id' => $to_user_id,
  390. 'level' => $level,
  391. 'createtime' => time(),
  392. 'updatetime' => time(),
  393. ];
  394. Db::startTrans();
  395. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  396. if($level == 1){
  397. Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
  398. }
  399. //系统消息
  400. /*if($level == 1){
  401. //发给动态用户
  402. $msg_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  403. $msg_title = '动态评论';
  404. $msg_content = $this->auth->nickname.'评论了你的动态';
  405. $infotype_id = $rs;
  406. }else{
  407. //发给层主
  408. $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();
  409. $msg_user_id = $answer_info['user_id'];
  410. $msg_title = '动态评论点评';
  411. $msg_content = $this->auth->nickname.'点评了你的动态评论';
  412. $infotype_id = $answer_info['id'];
  413. }
  414. $msg_id = \app\common\model\Message::addMessage($msg_user_id,$msg_title,$msg_content,'dongtai_answer',$infotype_id);*/
  415. Db::commit();
  416. $this->success('评价成功');
  417. }
  418. //用户是否拉黑
  419. private function is_black($uid,$black_uid){
  420. $where = [
  421. 'uid' => $uid,
  422. 'black_uid' => $black_uid,
  423. ];
  424. $check = Db::name('user_black')->where($where)->find();
  425. if($check){
  426. return 1;
  427. }else{
  428. return 0;
  429. }
  430. }
  431. //某动态的评论列表
  432. public function answer_list(){
  433. $dt_id = input('id',0);
  434. //楼
  435. $floor_list = Db::name('topic_dongtai_answer')
  436. ->alias('a')
  437. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday,ur.nickname_remark')
  438. ->join('user','a.user_id = user.id','LEFT')
  439. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  440. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('a.id desc')->autopage()->select();
  441. $floor_list = list_domain_image($floor_list,['avatar']);
  442. //追加子评论
  443. if(!empty($floor_list)){
  444. foreach($floor_list as $key => &$val){
  445. //下面几条子回复,字符串
  446. $val['childremark'] = '';
  447. $map = [
  448. 'a.dt_id' => $dt_id,
  449. 'a.floor' => $val['floor'],
  450. 'a.level' => 2,
  451. ];
  452. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  453. if($number > 0){
  454. $answer_info = Db::name('topic_dongtai_answer')
  455. ->alias('a')
  456. ->field('user.nickname,ur.nickname_remark')
  457. ->join('user','a.user_id = user.id','LEFT')
  458. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  459. ->where($map)->order('a.id desc')->find();
  460. $answer_info['nickname'] = !empty($answer_info['nickname_remark']) ? $answer_info['nickname_remark'] : $answer_info['nickname'];
  461. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  462. }
  463. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  464. //时间处理
  465. $val['createtime'] = get_last_time($val['createtime']);
  466. //回复是否已赞
  467. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  468. //用户年龄
  469. $val['birthday'] = birthtime_to_age($val['birthday']);
  470. }
  471. }
  472. $this->success('success',$floor_list);
  473. }
  474. //单独某一层的详细
  475. public function answer_info(){
  476. $answer_id = input('answer_id');
  477. //楼
  478. $floor_info = Db::name('topic_dongtai_answer')
  479. ->alias('a')
  480. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday')
  481. ->join('user','a.user_id = user.id','LEFT')
  482. ->where(['a.id'=>$answer_id])->find();
  483. if(empty($floor_info)){
  484. $this->success('success',[]);
  485. }
  486. $floor_info = info_domain_image($floor_info,['avatar']);
  487. $floor_info['createtime'] = get_last_time($floor_info['createtime']);
  488. //用户年龄
  489. $floor_info['birthday'] = birthtime_to_age($floor_info['birthday']);
  490. //回复是否已赞
  491. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  492. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  493. //层
  494. $floors = $floor_info['floor'];
  495. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  496. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday')
  497. ->join('user','a.user_id = user.id','LEFT')
  498. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();
  499. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  500. if(!empty($child_lists)){
  501. foreach($child_lists as $key => &$answer){
  502. //用户年龄
  503. $answer['birthday'] = birthtime_to_age($answer['birthday']);
  504. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  505. $answer['createtime'] = get_last_time($answer['createtime']);
  506. }
  507. }
  508. //合并
  509. $floor_info['child'] = $child_lists;
  510. $this->success('success',$floor_info);
  511. }
  512. //对评论点赞
  513. public function answer_good(){
  514. $dt_id = input('dt_id',0);
  515. $answer_id = input('answer_id',0);
  516. $where = [
  517. 'dt_id' => $dt_id,
  518. 'answer_id' => $answer_id,
  519. 'user_id' => $this->auth->id,
  520. ];
  521. $check = Db::name('topic_answer_good')->where($where)->find();
  522. if($check){
  523. Db::name('topic_answer_good')->where($where)->delete();
  524. Db::name('topic_dongtai_answer')->where('id',$answer_id)->setDec('goodnum');
  525. $this->success('已取消点赞');
  526. }else{
  527. Db::startTrans();
  528. $where['createtime'] = time();
  529. $rs = Db::name('topic_answer_good')->insertGetId($where);
  530. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  531. if($rs && $up !== false){
  532. Db::commit();
  533. $this->success('点赞成功');
  534. }
  535. Db::rollback();
  536. $this->error('点赞失败');
  537. }
  538. }
  539. //回复是否点赞
  540. private function answer_is_good($answer_id,$uid){
  541. $where = [
  542. 'answer_id' => $answer_id,
  543. 'user_id' => $uid,
  544. ];
  545. $check = Db::name('topic_answer_good')->where($where)->find();
  546. if($check){
  547. return 1;
  548. }else{
  549. return 0;
  550. }
  551. }
  552. //删除我的某个评论
  553. public function delete_answer(){
  554. $id = input('id',0);
  555. if(!$id){
  556. $this->error();
  557. }
  558. Db::startTrans();
  559. $info = Db::name('topic_dongtai_answer')->where('id',$id)->where('user_id',$this->auth->id)->find();
  560. if(!$info){
  561. $this->error('不存在的动态评论');
  562. }
  563. if($info['level'] == 1){
  564. //楼层内都删
  565. $louceng_id = Db::name('topic_dongtai_answer')->where('dt_id',$info['dt_id'])->where('level',2)->where('floor',$info['floor'])->column('id');
  566. if(!empty($louceng_id)){
  567. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum',count($louceng_id));//回复数减1
  568. Db::name('topic_dongtai_answer')->where('id','IN',$louceng_id)->delete();//评论删掉
  569. Db::name('topic_answer_good')->where('answer_id','IN',$louceng_id)->delete();//评论点赞删掉
  570. }
  571. }
  572. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum');//回复数减1
  573. Db::name('topic_dongtai_answer')->where('id',$id)->delete(); //评论删掉
  574. Db::name('topic_answer_good')->where('answer_id',$id)->delete();//评论点赞删掉
  575. Db::commit();
  576. $this->success();
  577. }
  578. //动态赠送礼物
  579. public function givegiftdongtai() {
  580. // 接口防并发
  581. if (!$this->apiLimit(1, 1)) {
  582. $this->error(__('Operation frequently'));
  583. }
  584. // $user_id = input('user_id');// 赠送对象
  585. $dt_id = input('dt_id', 0, 'intval'); //动态id
  586. $gift_id = input('gift_id');// 礼物ID
  587. $number = input('number',1,'intval');//数量
  588. if (!$dt_id || !$gift_id || $number < 1) {
  589. $this->error();
  590. }
  591. //查询动态
  592. $dongtai_info = Db::name('topic_dongtai')->find($dt_id);
  593. if (!$dongtai_info) {
  594. $this->error('您的网络开小差啦~');
  595. }
  596. if ($dongtai_info['status'] != 0) {
  597. $this->error('您的网络开小差啦~');
  598. }
  599. $user_id = $dongtai_info['user_id'];
  600. // 不可以赠送给自己
  601. if($this->auth->id == $user_id) {
  602. $this->error("不可以赠送给自己");
  603. }
  604. // 获取礼物信息
  605. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  606. if (!$giftinfo)
  607. {
  608. $this->error("请选择礼物");
  609. }
  610. $giftvalue = bcmul($giftinfo['price'],$number);
  611. //被赠送人信息
  612. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  613. if (!$touserinfo) {
  614. $this->error("不存在的用户");
  615. }
  616. // 判断当前用户余额
  617. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  618. if($user_gold < $giftvalue) {
  619. $this->error("您的金币余额不足");
  620. }
  621. Db::startTrans();
  622. // 添加礼物赠送记录表
  623. $data = [
  624. 'user_id' => $this->auth->id,
  625. 'user_to_id' => $user_id,
  626. 'dt_id' => $dt_id,
  627. 'gift_id' => $giftinfo['id'],
  628. 'gift_name' => $giftinfo['name'],
  629. 'number' => $number,
  630. 'price' => $giftinfo['price'],
  631. 'total_price' => $giftvalue,
  632. 'createtime' => time(),
  633. ];
  634. //每个礼物都要计算平台抽成和房主抽成
  635. $gift_plat_scale = config('site.gift_plat_scale');
  636. $data['platvalue'] = bcmul($gift_plat_scale/100 ,$data['total_price'],1);//平台抽成
  637. $data['getvalue'] = bcsub($data['total_price'] ,$data['platvalue'],1);//减去抽成剩余价值
  638. $money_to_gold = config('site.money_to_gold');
  639. $data['getmoney'] = bcdiv($data['getvalue'] ,$money_to_gold,2);//收益
  640. $log_id = Db::name('gift_user_dongtai')->insertGetId($data);
  641. if(!$log_id){
  642. Db::rollback();
  643. $this->error('赠送失败');
  644. }
  645. if($giftvalue > 0){
  646. // 扣除当前用户余额
  647. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,59,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id);
  648. if($wallet_rs['status'] === false){
  649. Db::rollback();
  650. $this->error($wallet_rs['msg']);
  651. }
  652. }
  653. if($data['getmoney'] > 0){
  654. // 添加获赠用户收益
  655. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$data['getmoney'],60,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id,2);
  656. if($wallet_rs['status'] === false){
  657. Db::rollback();
  658. $this->error($wallet_rs['msg']);
  659. }
  660. }
  661. //增加赠送用户上级余额
  662. if ($touserinfo['intro_uid']) {
  663. //获取返利比率
  664. $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
  665. $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  666. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  667. //上级获得金额
  668. $intro_uid_money = bcdiv(bcmul($data['getmoney'],$intro_income_rebate_rate,2),100,2);
  669. if ($intro_uid_money > 0) {
  670. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人动态礼物获赠奖励','gift_user_dongtai',$log_id);
  671. if($intro_result['status']===false)
  672. {
  673. Db::rollback();
  674. $this->error($intro_result['msg']);
  675. }
  676. }
  677. }
  678. }
  679. if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
  680. //增加亲密度
  681. $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
  682. if (!$user_intimacy_rs['status']) {
  683. Db::rollback();
  684. $this->error('您的网络开小差啦~');
  685. }
  686. }
  687. Db::commit();
  688. //发送消息
  689. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  690. $tenim = new \app\api\controller\Tenim;
  691. $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
  692. }
  693. $return_data['money'] = $data['getmoney']; //获得金额
  694. $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
  695. $this->success('赠送成功', $return_data);
  696. }
  697. //动态通知
  698. public function message(){
  699. $list = Db::name('topic_dongtai_message')->alias('msg')->field('msg.*,user.avatar,user.nickname')
  700. ->join('user','msg.from_user_id = user.id','LEFT')
  701. ->where('msg.user_id',$this->auth->id)->autopage()->select();
  702. $list = list_domain_image($list,['avatar']);
  703. $this->success('success',$list);
  704. }
  705. //删除动态
  706. public function delete() {
  707. $id = input('id', 0, 'intval');
  708. if (!$id) {
  709. $this->error('您的网络开小差啦~');
  710. }
  711. $info = Db::name('topic_dongtai')->find($id);
  712. if (!$info) {
  713. $this->error('您的网络开小差啦~');
  714. }
  715. if ($info['user_id'] != $this->auth->id) {
  716. $this->error('您的网络开小差啦~');
  717. }
  718. $rs = Db::name('topic_dongtai')->where(['id' => $id])->setField('status', 1);
  719. if (!$rs) {
  720. $this->error('您的网络开小差啦~');
  721. }
  722. $this->success('删除成功');
  723. }
  724. //举报
  725. public function report(){
  726. $dt_id = input('dt_id',0);
  727. $check = Db::name('topic_dongtai')->where('id',$dt_id)->find();
  728. if(empty($check)){
  729. $this->error('不存在的动态');
  730. }
  731. $data['dt_id'] = $dt_id;
  732. $data['user_id'] = $this->auth->id;
  733. $data['to_user_id'] = $check['user_id'];
  734. $data['createtime'] = time();
  735. Db::name('topic_dongtai_report')->insertGetId($data);
  736. $this->success('举报成功');
  737. }
  738. //收藏,取消收藏
  739. public function collect(){
  740. $where = [
  741. 'user_id' => $this->auth->id,
  742. 'table' => 'topic_dongtai',
  743. 'table_id' => input('id',0),
  744. ];
  745. $check = Db::name('user_collect')->where($where)->find();
  746. if($check){
  747. Db::name('user_collect')->where($where)->delete();
  748. $this->success('已取消收藏');
  749. }else{
  750. Db::name('user_collect')->insertGetId($where);
  751. $this->success('收藏成功');
  752. }
  753. }
  754. //我的收藏
  755. public function my_collect(){
  756. $collect_id = Db::name('user_collect')->where(['table'=>'topic_dongtai','user_id'=>$this->auth->id])->column('table_id');
  757. $where = ['dt.id'=>['IN',$collect_id]];
  758. $where['dt.status'] = 0;
  759. $where['dt.auit_status'] = 1;
  760. $orderby = 'dt.id desc';
  761. $list = Db::name('topic_dongtai')->alias('dt')
  762. ->join('user','dt.user_id = user.id','LEFT')
  763. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  764. ->join('topic_hub th','dt.topic_id = th.id','LEFT')
  765. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status,user.is_hideaddress,th.name,uw.vip_endtime')
  766. ->where($where)
  767. ->order($orderby)->autopage()->select();
  768. $list = list_domain_image($list,['images','avatar']);
  769. //追加是否点赞
  770. if(!empty($list)){
  771. $ids = array_column($list,'id');
  772. $map = [
  773. 'dt_id' => ['IN',$ids],
  774. 'user_id' => $this->auth->id,
  775. ];
  776. $good_list = Db::name('topic_dongtai_good')->where($map)->select();
  777. $mt_user_greet = Db::name('user_greet'); //是否打过招呼
  778. $mt_gift_user_dongtai = Db::name('gift_user_dongtai');
  779. foreach ($list as &$val) {
  780. $val['name'] = $val['name'] ? : '';
  781. $val['birthday'] = birthtime_to_age($val['birthday']);
  782. $val['createtime'] = get_last_time($val['createtime']);
  783. $val['cityname'] = $val['is_hideaddress'] ? '' : $val['address'];
  784. //用户vip
  785. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  786. unset($val['vip_endtime']);
  787. //是否点过赞:0否 1是
  788. $val['isgood'] = 0;
  789. foreach($good_list as $k => $v){
  790. if($val['id'] == $v['dt_id']){
  791. $val['isgood'] = 1;
  792. }
  793. }
  794. //礼物数量
  795. $val['gift_count'] = $mt_gift_user_dongtai->where(['dt_id' => $val['id']])->count('id');
  796. //查询是否打过招呼
  797. $count = $mt_user_greet->where(['user_id' => $this->auth->id, 'user_to_id' => $val['user_id']])->count('id');
  798. if ($count) {
  799. $val['is_chat'] = 1; //是否打过招呼: 1是 0否
  800. } else {
  801. $val['is_chat'] = 0; //是否打过招呼: 1是 0否
  802. }
  803. //创建视频缩略图
  804. $val['images_thumb'] = '';
  805. if ($val['type'] == 2) {
  806. $images_url = explode('.', $val['images']);
  807. unset($images_url[count($images_url) - 1]);
  808. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  809. }
  810. }
  811. }
  812. $this->success('success',$list);
  813. }
  814. //动态是否收藏
  815. private function is_collect($dt_id,$uid){
  816. $where = [
  817. 'user_id' => $uid,
  818. 'table' => 'topic_dongtai',
  819. 'table_id' => $dt_id,
  820. ];
  821. $check = Db::name('user_collect')->where($where)->find();
  822. if($check){
  823. return 1;
  824. }else{
  825. return 0;
  826. }
  827. }
  828. /////////////////////////////////////////////////////////////////////////////////
  829. //动态收到礼物列表
  830. public function dongtaigiftlist() {
  831. $id = input('id', 0, 'intval');
  832. if (!$id) {
  833. $this->error('您的网络开小差啦~');
  834. }
  835. //点赞只能查看异性
  836. $dongtaiWhere['td.id'] = $id;
  837. $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
  838. ->join('user u','u.id = td.user_id','LEFT')
  839. ->where($dongtaiWhere)->find();
  840. $where['a.dt_id'] = $id;
  841. $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
  842. if ($gender == 1) {
  843. $where['user.gender'] = 0;
  844. } elseif ($gender == 0) {
  845. $where['user.gender'] = 1;
  846. }
  847. $list = Db::name('gift_user_dongtai')->alias('a')->field('a.user_id, count(a.id) count')
  848. ->join('user', 'a.user_id = user.id', 'left')
  849. ->where($where)->group('a.user_id')->order('a.id desc')->autopage()->select();
  850. if (!$list) {
  851. $this->success('success', $list);
  852. }
  853. $mt_user = Db::name('user');
  854. foreach ($list as &$v) {
  855. $user_info = $mt_user->field('nickname, avatar, gender, birthday')->where(['id' => $v['user_id']])->find();
  856. $v['nickname'] = $user_info['nickname'];
  857. $v['avatar'] = one_domain_image($user_info['avatar']);
  858. $v['birthday'] = birthtime_to_age($user_info['birthday']);
  859. }
  860. $this->success('success', $list);
  861. }
  862. //点赞列表
  863. public function dongtaigoodlist() {
  864. $id = input('id', 0, 'intval');
  865. if (!$id) {
  866. $this->error('您的网络开小差啦~');
  867. }
  868. //点赞只能查看异性
  869. $dongtaiWhere['td.id'] = $id;
  870. $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
  871. ->join('user u','u.id = td.user_id','LEFT')
  872. ->where($dongtaiWhere)->find();
  873. $where['a.dt_id'] = $id;
  874. $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
  875. if ($gender == 1) {
  876. $where['user.gender'] = 0;
  877. } elseif ($gender == 0) {
  878. $where['user.gender'] = 1;
  879. }
  880. $list = Db::name('topic_dongtai_good')->alias('a')
  881. ->join('user', 'a.user_id = user.id', 'left')
  882. ->field('a.*, user.nickname,user.avatar,user.gender,user.birthday')
  883. ->where($where)
  884. ->order('a.id desc')
  885. ->autopage()->select();
  886. if (!$list) {
  887. $this->success('success', $list);
  888. }
  889. $list = list_domain_image($list,['avatar']);
  890. foreach ($list as &$v) {
  891. $v['birthday'] = birthtime_to_age($v['birthday']);
  892. $v['createtime'] = get_last_time($v['createtime']);
  893. }
  894. $this->success('success', $list);
  895. }
  896. }