Topicdongtai.php 37 KB

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