Topicdongtai.php 37 KB

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