Topicdongtai.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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,uw.vip_level,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'] = $this->is_vip($val['vip_endtime'],$val['vip_level']);
  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,uw.vip_level,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'] = $this->is_vip($val['vip_endtime'],$val['vip_level']);
  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,uw.vip_level,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'] = $this->is_vip($info['vip_endtime'],$info['vip_level']);
  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,ur.nickname_remark')
  481. ->join('user','a.user_id = user.id','LEFT')
  482. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  483. ->where(['a.id'=>$answer_id])->find();
  484. if(empty($floor_info)){
  485. $this->success('success',[]);
  486. }
  487. $floor_info = info_domain_image($floor_info,['avatar']);
  488. $floor_info['createtime'] = get_last_time($floor_info['createtime']);
  489. //用户年龄
  490. $floor_info['birthday'] = birthtime_to_age($floor_info['birthday']);
  491. //回复是否已赞
  492. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  493. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  494. $floor_info['nickname'] = !empty($floor_info['nickname_remark']) ? $floor_info['nickname_remark'] : $floor_info['nickname'];
  495. //层
  496. $floors = $floor_info['floor'];
  497. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  498. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday,ur.nickname_remark')
  499. ->join('user','a.user_id = user.id','LEFT')
  500. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  501. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();
  502. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  503. if(!empty($child_lists)){
  504. foreach($child_lists as $key => &$answer){
  505. //用户年龄
  506. $answer['birthday'] = birthtime_to_age($answer['birthday']);
  507. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  508. $answer['createtime'] = get_last_time($answer['createtime']);
  509. $answer['nickname'] = !empty($answer['nickname_remark']) ? $answer['nickname_remark'] : $answer['nickname'];
  510. }
  511. }
  512. //合并
  513. $floor_info['child'] = $child_lists;
  514. $this->success('success',$floor_info);
  515. }
  516. //对评论点赞
  517. public function answer_good(){
  518. $dt_id = input('dt_id',0);
  519. $answer_id = input('answer_id',0);
  520. $where = [
  521. 'dt_id' => $dt_id,
  522. 'answer_id' => $answer_id,
  523. 'user_id' => $this->auth->id,
  524. ];
  525. $check = Db::name('topic_answer_good')->where($where)->find();
  526. if($check){
  527. Db::name('topic_answer_good')->where($where)->delete();
  528. Db::name('topic_dongtai_answer')->where('id',$answer_id)->setDec('goodnum');
  529. $this->success('已取消点赞');
  530. }else{
  531. Db::startTrans();
  532. $where['createtime'] = time();
  533. $rs = Db::name('topic_answer_good')->insertGetId($where);
  534. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  535. if($rs && $up !== false){
  536. Db::commit();
  537. $this->success('点赞成功');
  538. }
  539. Db::rollback();
  540. $this->error('点赞失败');
  541. }
  542. }
  543. //回复是否点赞
  544. private function answer_is_good($answer_id,$uid){
  545. $where = [
  546. 'answer_id' => $answer_id,
  547. 'user_id' => $uid,
  548. ];
  549. $check = Db::name('topic_answer_good')->where($where)->find();
  550. if($check){
  551. return 1;
  552. }else{
  553. return 0;
  554. }
  555. }
  556. //删除我的某个评论
  557. public function delete_answer(){
  558. $id = input('id',0);
  559. if(!$id){
  560. $this->error();
  561. }
  562. Db::startTrans();
  563. $info = Db::name('topic_dongtai_answer')->where('id',$id)->where('user_id',$this->auth->id)->find();
  564. if(!$info){
  565. $this->error('不存在的动态评论');
  566. }
  567. if($info['level'] == 1){
  568. //楼层内都删
  569. $louceng_id = Db::name('topic_dongtai_answer')->where('dt_id',$info['dt_id'])->where('level',2)->where('floor',$info['floor'])->column('id');
  570. if(!empty($louceng_id)){
  571. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum',count($louceng_id));//回复数减1
  572. Db::name('topic_dongtai_answer')->where('id','IN',$louceng_id)->delete();//评论删掉
  573. Db::name('topic_answer_good')->where('answer_id','IN',$louceng_id)->delete();//评论点赞删掉
  574. }
  575. }
  576. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum');//回复数减1
  577. Db::name('topic_dongtai_answer')->where('id',$id)->delete(); //评论删掉
  578. Db::name('topic_answer_good')->where('answer_id',$id)->delete();//评论点赞删掉
  579. Db::commit();
  580. $this->success();
  581. }
  582. //动态赠送礼物
  583. public function givegiftdongtai() {
  584. // 接口防并发
  585. if (!$this->apiLimit(1, 1)) {
  586. $this->error(__('Operation frequently'));
  587. }
  588. // $user_id = input('user_id');// 赠送对象
  589. $dt_id = input('dt_id', 0, 'intval'); //动态id
  590. $gift_id = input('gift_id');// 礼物ID
  591. $number = input('number',1,'intval');//数量
  592. if (!$dt_id || !$gift_id || $number < 1) {
  593. $this->error();
  594. }
  595. //查询动态
  596. $dongtai_info = Db::name('topic_dongtai')->find($dt_id);
  597. if (!$dongtai_info) {
  598. $this->error('没有找到该动态,或许它被删除了');
  599. }
  600. if ($dongtai_info['status'] != 0) {
  601. $this->error('没有找到该动态,或许它被删除了');
  602. }
  603. $user_id = $dongtai_info['user_id'];
  604. // 不可以赠送给自己
  605. if($this->auth->id == $user_id) {
  606. $this->error("不可以赠送给自己");
  607. }
  608. // 获取礼物信息
  609. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  610. if (!$giftinfo)
  611. {
  612. $this->error("请选择礼物");
  613. }
  614. $giftvalue = bcmul($giftinfo['price'],$number);
  615. //被赠送人信息
  616. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  617. if (!$touserinfo) {
  618. $this->error("不存在的用户");
  619. }
  620. // 判断当前用户余额
  621. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  622. if($user_gold < $giftvalue) {
  623. $this->error("您的金币余额不足");
  624. }
  625. Db::startTrans();
  626. // 添加礼物赠送记录表
  627. $data = [
  628. 'user_id' => $this->auth->id,
  629. 'user_to_id' => $user_id,
  630. 'dt_id' => $dt_id,
  631. 'gift_id' => $giftinfo['id'],
  632. 'gift_name' => $giftinfo['name'],
  633. 'number' => $number,
  634. 'price' => $giftinfo['price'],
  635. 'total_price' => $giftvalue,
  636. 'createtime' => time(),
  637. ];
  638. //每个礼物都要计算平台抽成和房主抽成
  639. $gift_plat_scale = config('site.gift_plat_scale');
  640. $data['platvalue'] = bcmul($gift_plat_scale/100 ,$data['total_price'],1);//平台抽成
  641. $data['getvalue'] = bcsub($data['total_price'] ,$data['platvalue'],1);//减去抽成剩余价值
  642. $money_to_gold = config('site.money_to_gold');
  643. $data['getmoney'] = bcdiv($data['getvalue'] ,$money_to_gold,2);//收益
  644. $log_id = Db::name('gift_user_dongtai')->insertGetId($data);
  645. if(!$log_id){
  646. Db::rollback();
  647. $this->error('赠送失败');
  648. }
  649. if($giftvalue > 0){
  650. // 扣除当前用户余额
  651. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,59,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id);
  652. if($wallet_rs['status'] === false){
  653. Db::rollback();
  654. $this->error($wallet_rs['msg']);
  655. }
  656. }
  657. if($data['getmoney'] > 0){
  658. // 添加获赠用户收益
  659. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$data['getmoney'],60,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id,2);
  660. if($wallet_rs['status'] === false){
  661. Db::rollback();
  662. $this->error($wallet_rs['msg']);
  663. }
  664. }
  665. //增加赠送用户上级余额
  666. if ($touserinfo['intro_uid']) {
  667. //获取返利比率
  668. $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
  669. $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  670. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  671. //上级获得金额
  672. $intro_uid_money = bcdiv(bcmul($data['getmoney'],$intro_income_rebate_rate,2),100,2);
  673. if ($intro_uid_money > 0) {
  674. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人动态礼物获赠奖励','gift_user_dongtai',$log_id);
  675. if($intro_result['status']===false)
  676. {
  677. Db::rollback();
  678. $this->error($intro_result['msg']);
  679. }
  680. }
  681. }
  682. }
  683. if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
  684. //增加亲密度
  685. $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
  686. if (!$user_intimacy_rs['status']) {
  687. Db::rollback();
  688. $this->error('您的网络开小差啦~');
  689. }
  690. }
  691. Db::commit();
  692. //发送消息
  693. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  694. $tenim = new \app\api\controller\Tenim;
  695. $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
  696. }
  697. $return_data['money'] = $data['getmoney']; //获得金额
  698. $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
  699. $this->success('赠送成功', $return_data);
  700. }
  701. //动态通知
  702. public function message(){
  703. $list = Db::name('topic_dongtai_message')->alias('msg')->field('msg.*,user.avatar,user.nickname,ur.nickname_remark')
  704. ->join('user','msg.from_user_id = user.id','LEFT')
  705. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  706. ->where('msg.user_id',$this->auth->id)->autopage()->select();
  707. $list = list_domain_image($list,['avatar']);
  708. foreach ($list as &$val) {
  709. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  710. }
  711. $this->success('success',$list);
  712. }
  713. //删除动态
  714. public function delete() {
  715. $id = input('id', 0, 'intval');
  716. if (!$id) {
  717. $this->error('您的网络开小差啦~');
  718. }
  719. $info = Db::name('topic_dongtai')->find($id);
  720. if (!$info) {
  721. $this->error('没有找到该动态,或许它被删除了');
  722. }
  723. if ($info['user_id'] != $this->auth->id) {
  724. $this->error('您的网络开小差啦~');
  725. }
  726. $rs = Db::name('topic_dongtai')->where(['id' => $id])->setField('status', 1);
  727. if (!$rs) {
  728. $this->error('您的网络开小差啦~');
  729. }
  730. $this->success('删除成功');
  731. }
  732. //举报
  733. public function report(){
  734. $dt_id = input('dt_id',0);
  735. $check = Db::name('topic_dongtai')->where('id',$dt_id)->find();
  736. if(empty($check)){
  737. $this->error('没有找到该动态,或许它被删除了');
  738. }
  739. $data['dt_id'] = $dt_id;
  740. $data['user_id'] = $this->auth->id;
  741. $data['to_user_id'] = $check['user_id'];
  742. $data['createtime'] = time();
  743. Db::name('topic_dongtai_report')->insertGetId($data);
  744. $this->success('举报成功');
  745. }
  746. //收藏,取消收藏
  747. public function collect(){
  748. $where = [
  749. 'user_id' => $this->auth->id,
  750. 'table' => 'topic_dongtai',
  751. 'table_id' => input('id',0),
  752. ];
  753. $check = Db::name('user_collect')->where($where)->find();
  754. if($check){
  755. Db::name('user_collect')->where($where)->delete();
  756. $this->success('已取消收藏');
  757. }else{
  758. Db::name('user_collect')->insertGetId($where);
  759. $this->success('收藏成功');
  760. }
  761. }
  762. //我的收藏
  763. public function my_collect(){
  764. $collect_id = Db::name('user_collect')->where(['table'=>'topic_dongtai','user_id'=>$this->auth->id])->column('table_id');
  765. $where = ['dt.id'=>['IN',$collect_id]];
  766. $where['dt.status'] = 0;
  767. $where['dt.auit_status'] = 1;
  768. $orderby = 'dt.id desc';
  769. $list = Db::name('topic_dongtai')->alias('dt')
  770. ->join('user','dt.user_id = user.id','LEFT')
  771. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  772. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  773. ->join('topic_hub th','dt.topic_id = th.id','LEFT')
  774. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status,user.is_hideaddress,th.name,uw.vip_endtime,uw.vip_level,ur.nickname_remark')
  775. ->where($where)
  776. ->order($orderby)->autopage()->select();
  777. $list = list_domain_image($list,['images','avatar']);
  778. //追加是否点赞
  779. if(!empty($list)){
  780. $ids = array_column($list,'id');
  781. $map = [
  782. 'dt_id' => ['IN',$ids],
  783. 'user_id' => $this->auth->id,
  784. ];
  785. $good_list = Db::name('topic_dongtai_good')->where($map)->select();
  786. $mt_user_greet = Db::name('user_greet'); //是否打过招呼
  787. $mt_gift_user_dongtai = Db::name('gift_user_dongtai');
  788. foreach ($list as &$val) {
  789. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  790. $val['name'] = $val['name'] ? : '';
  791. $val['birthday'] = birthtime_to_age($val['birthday']);
  792. $val['createtime'] = get_last_time($val['createtime']);
  793. $val['cityname'] = $val['is_hideaddress'] ? '' : $val['address'];
  794. //用户vip
  795. $val['is_vip'] = $this->is_vip($val['vip_endtime'],$val['vip_level']);
  796. unset($val['vip_endtime']);
  797. //是否点过赞:0否 1是
  798. $val['isgood'] = 0;
  799. foreach($good_list as $k => $v){
  800. if($val['id'] == $v['dt_id']){
  801. $val['isgood'] = 1;
  802. }
  803. }
  804. //礼物数量
  805. $val['gift_count'] = $mt_gift_user_dongtai->where(['dt_id' => $val['id']])->count('id');
  806. //查询是否打过招呼
  807. $count = $mt_user_greet->where(['user_id' => $this->auth->id, 'user_to_id' => $val['user_id']])->count('id');
  808. if ($count) {
  809. $val['is_chat'] = 1; //是否打过招呼: 1是 0否
  810. } else {
  811. $val['is_chat'] = 0; //是否打过招呼: 1是 0否
  812. }
  813. //创建视频缩略图
  814. $val['images_thumb'] = '';
  815. if ($val['type'] == 2) {
  816. $images_url = explode('.', $val['images']);
  817. unset($images_url[count($images_url) - 1]);
  818. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  819. }
  820. }
  821. }
  822. $this->success('success',$list);
  823. }
  824. //动态是否收藏
  825. private function is_collect($dt_id,$uid){
  826. $where = [
  827. 'user_id' => $uid,
  828. 'table' => 'topic_dongtai',
  829. 'table_id' => $dt_id,
  830. ];
  831. $check = Db::name('user_collect')->where($where)->find();
  832. if($check){
  833. return 1;
  834. }else{
  835. return 0;
  836. }
  837. }
  838. /////////////////////////////////////////////////////////////////////////////////
  839. //动态收到礼物列表
  840. public function dongtaigiftlist() {
  841. $id = input('id', 0, 'intval');
  842. if (!$id) {
  843. $this->error('您的网络开小差啦~');
  844. }
  845. //点赞只能查看异性
  846. $dongtaiWhere['td.id'] = $id;
  847. $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
  848. ->join('user u','u.id = td.user_id','LEFT')
  849. ->where($dongtaiWhere)->find();
  850. $where['a.dt_id'] = $id;
  851. $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
  852. if ($gender == 1) {
  853. $where['user.gender'] = 0;
  854. } elseif ($gender == 0) {
  855. $where['user.gender'] = 1;
  856. }
  857. $list = Db::name('gift_user_dongtai')->alias('a')->field('a.user_id, count(a.id) count')
  858. ->join('user', 'a.user_id = user.id', 'left')
  859. ->where($where)->group('a.user_id')->order('a.id desc')->autopage()->select();
  860. if (!$list) {
  861. $this->success('success', $list);
  862. }
  863. $mt_user = Db::name('user');
  864. foreach ($list as &$v) {
  865. $user_info = $mt_user->field('nickname, avatar, gender, birthday')->where(['id' => $v['user_id']])->find();
  866. $v['nickname'] = $user_info['nickname'];
  867. $v['avatar'] = one_domain_image($user_info['avatar']);
  868. $v['birthday'] = birthtime_to_age($user_info['birthday']);
  869. }
  870. $this->success('success', $list);
  871. }
  872. //点赞列表
  873. public function dongtaigoodlist() {
  874. $id = input('id', 0, 'intval');
  875. if (!$id) {
  876. $this->error('您的网络开小差啦~');
  877. }
  878. //点赞只能查看异性
  879. $dongtaiWhere['td.id'] = $id;
  880. $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
  881. ->join('user u','u.id = td.user_id','LEFT')
  882. ->where($dongtaiWhere)->find();
  883. $where['a.dt_id'] = $id;
  884. $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
  885. if ($gender == 1) {
  886. $where['user.gender'] = 0;
  887. } elseif ($gender == 0) {
  888. $where['user.gender'] = 1;
  889. }
  890. $list = Db::name('topic_dongtai_good')->alias('a')
  891. ->join('user', 'a.user_id = user.id', 'left')
  892. ->field('a.*, user.nickname,user.avatar,user.gender,user.birthday')
  893. ->where($where)
  894. ->order('a.id desc')
  895. ->autopage()->select();
  896. if (!$list) {
  897. $this->success('success', $list);
  898. }
  899. $list = list_domain_image($list,['avatar']);
  900. foreach ($list as &$v) {
  901. $v['birthday'] = birthtime_to_age($v['birthday']);
  902. $v['createtime'] = get_last_time($v['createtime']);
  903. }
  904. $this->success('success', $list);
  905. }
  906. }