Topicdongtai.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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('user_id', $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. $this->success('success',$info);
  261. }
  262. //点赞
  263. public function good(){
  264. $id = input('id', 0, 'intval');
  265. if (!$id) {
  266. $this->error('您的网络开小差啦~');
  267. }
  268. $info = Db::name('topic_dongtai')->find($id);
  269. if (!$info) {
  270. $this->error('您的网络开小差啦~');
  271. }
  272. if ($info['status'] != 0) {
  273. $this->error('您的网络开小差啦~');
  274. }
  275. $where = [
  276. 'dt_id' => $id,
  277. 'user_id' => $this->auth->id,
  278. ];
  279. $check = Db::name('topic_dongtai_good')->where($where)->find();
  280. $dt_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  281. if($check){
  282. Db::name('topic_dongtai_good')->where($where)->delete();
  283. $down = Db::name('topic_dongtai')->where('id',$id)->setDec('goodnum');
  284. $this->success('已取消点赞');
  285. }else{
  286. Db::startTrans();
  287. $where['createtime'] = time();
  288. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  289. if(!$rs){
  290. Db::rollback();
  291. $this->error('点赞失败');
  292. }
  293. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  294. if($up === false){
  295. Db::rollback();
  296. $this->error('点赞失败');
  297. }
  298. //系统消息
  299. /*if($dt_user_id != $this->auth->id){
  300. $msg_id = \app\common\model\Message::addMessage($dt_user_id,'动态点赞',$this->auth->nickname.'赞了你的动态','dongtai_good',$id);
  301. }*/
  302. Db::commit();
  303. $this->success('点赞成功');
  304. }
  305. }
  306. //是否点赞
  307. private function is_good($dt_id,$uid){
  308. $where = [
  309. 'dt_id' => $dt_id,
  310. 'user_id' => $uid,
  311. ];
  312. $check = Db::name('topic_dongtai_good')->where($where)->find();
  313. if($check){
  314. return 1;
  315. }else{
  316. return 0;
  317. }
  318. }
  319. //评论
  320. public function answer(){
  321. $id = input('id',0);
  322. $content = input('content','');
  323. $to_user_id = input('to_user_id',0);
  324. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  325. $floor = input('floor',0);
  326. if(empty($content) || empty($id)){
  327. $this->error();
  328. }
  329. //关键字替换
  330. $content = Keyworld::sensitive($content);
  331. //判断
  332. if($level == 2 && $floor == 0){
  333. $this->error('楼层错误');
  334. }
  335. //黑名单判断
  336. //是否被动态发布者拉黑
  337. $dongtai_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  338. $black_check = $this->is_black($dongtai_user_id,$this->auth->id);
  339. if($black_check){
  340. $this->error('您已被对方拉黑,禁止评论此动态');
  341. }
  342. //是否被层主拉黑
  343. if($level == 2){
  344. $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();
  345. $black_check = $this->is_black($answer_info['user_id'],$this->auth->id);
  346. if($black_check){
  347. $this->error('您已被对方拉黑,禁止点评此评论');
  348. }
  349. }
  350. //回复楼主,最新楼层
  351. if($level == 1 || $floor == 0){
  352. $to_user_id = 0;
  353. $floor = 1; //默认一楼
  354. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  355. if($last_floor){
  356. $floor = $last_floor + 1;
  357. }
  358. }
  359. //判断user_id
  360. if($to_user_id){
  361. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  362. if(empty($to_user)){
  363. $this->error('被回复的用户不存在');
  364. }
  365. }
  366. //data
  367. $data = [
  368. 'dt_id' => $id,
  369. 'floor' => $floor,
  370. 'user_id' => $this->auth->id,
  371. 'content' => $content,
  372. 'to_user_id' => $to_user_id,
  373. 'level' => $level,
  374. 'createtime' => time(),
  375. 'updatetime' => time(),
  376. ];
  377. Db::startTrans();
  378. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  379. if($level == 1){
  380. Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
  381. }
  382. //系统消息
  383. if($level == 1){
  384. //发给动态用户
  385. $msg_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  386. $msg_title = '动态评论';
  387. $msg_content = $this->auth->nickname.'评论了你的动态';
  388. $infotype_id = $rs;
  389. }else{
  390. //发给层主
  391. $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();
  392. $msg_user_id = $answer_info['user_id'];
  393. $msg_title = '动态评论点评';
  394. $msg_content = $this->auth->nickname.'点评了你的动态评论';
  395. $infotype_id = $answer_info['id'];
  396. }
  397. $msg_id = \app\common\model\Message::addMessage($msg_user_id,$msg_title,$msg_content,'dongtai_answer',$infotype_id);
  398. Db::commit();
  399. $this->success('评价成功');
  400. }
  401. //用户是否拉黑
  402. private function is_black($uid,$black_uid){
  403. $where = [
  404. 'uid' => $uid,
  405. 'black_uid' => $black_uid,
  406. ];
  407. $check = Db::name('user_black')->where($where)->find();
  408. if($check){
  409. return 1;
  410. }else{
  411. return 0;
  412. }
  413. }
  414. //某动态的评论列表
  415. public function answer_list(){
  416. $dt_id = input('id',0);
  417. //楼
  418. $floor_list = Db::name('topic_dongtai_answer')
  419. ->alias('a')
  420. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday')
  421. ->join('user','a.user_id = user.id','LEFT')
  422. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('a.id desc')->autopage()->select();
  423. $floor_list = list_domain_image($floor_list,['avatar']);
  424. //追加子评论
  425. if(!empty($floor_list)){
  426. foreach($floor_list as $key => &$val){
  427. //下面几条子回复,字符串
  428. $val['childremark'] = '';
  429. $map = [
  430. 'a.dt_id' => $dt_id,
  431. 'a.floor' => $val['floor'],
  432. 'a.level' => 2,
  433. ];
  434. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  435. if($number > 0){
  436. $answer_info = Db::name('topic_dongtai_answer')
  437. ->alias('a')
  438. ->field('user.nickname')
  439. ->join('user','a.user_id = user.id','LEFT')
  440. ->where($map)->order('a.id desc')->find();
  441. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  442. }
  443. //时间处理
  444. $val['createtime'] = get_last_time($val['createtime']);
  445. //回复是否已赞
  446. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  447. //用户年龄
  448. $val['birthday'] = birthtime_to_age($val['birthday']);
  449. }
  450. }
  451. $this->success(1,$floor_list);
  452. }
  453. //单独某一层的详细
  454. public function answer_info(){
  455. $answer_id = input('answer_id');
  456. //楼
  457. $floor_info = Db::name('topic_dongtai_answer')
  458. ->alias('a')
  459. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday')
  460. ->join('user','a.user_id = user.id','LEFT')
  461. ->where(['a.id'=>$answer_id])->find();
  462. if(empty($floor_info)){
  463. $this->success(1,[]);
  464. }
  465. $floor_info = info_domain_image($floor_info,['avatar']);
  466. $floor_info['createtime'] = get_last_time($floor_info['createtime']);
  467. //用户年龄
  468. $floor_info['birthday'] = birthtime_to_age($floor_info['birthday']);
  469. //回复是否已赞
  470. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  471. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  472. //层
  473. $floors = $floor_info['floor'];
  474. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  475. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday')
  476. ->join('user','a.user_id = user.id','LEFT')
  477. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();
  478. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  479. if(!empty($child_lists)){
  480. foreach($child_lists as $key => &$answer){
  481. //用户年龄
  482. $answer['birthday'] = birthtime_to_age($answer['birthday']);
  483. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  484. $answer['createtime'] = get_last_time($answer['createtime']);
  485. }
  486. }
  487. //合并
  488. $floor_info['child'] = $child_lists;
  489. $this->success('success',$floor_info);
  490. }
  491. //对评论点赞
  492. public function answer_good(){
  493. $dt_id = input('dt_id',0);
  494. $answer_id = input('answer_id',0);
  495. $where = [
  496. 'dt_id' => $dt_id,
  497. 'answer_id' => $answer_id,
  498. 'user_id' => $this->auth->id,
  499. ];
  500. $check = Db::name('topic_answer_good')->where($where)->find();
  501. if($check){
  502. Db::name('topic_answer_good')->where($where)->delete();
  503. Db::name('topic_dongtai_answer')->where('id',$answer_id)->setDec('goodnum');
  504. $this->success('已取消点赞');
  505. }else{
  506. Db::startTrans();
  507. $where['createtime'] = time();
  508. $rs = Db::name('topic_answer_good')->insertGetId($where);
  509. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  510. if($rs && $up !== false){
  511. Db::commit();
  512. $this->success('点赞成功');
  513. }
  514. Db::rollback();
  515. $this->error('点赞失败');
  516. }
  517. }
  518. //回复是否点赞
  519. private function answer_is_good($answer_id,$uid){
  520. $where = [
  521. 'answer_id' => $answer_id,
  522. 'user_id' => $uid,
  523. ];
  524. $check = Db::name('topic_answer_good')->where($where)->find();
  525. if($check){
  526. return 1;
  527. }else{
  528. return 0;
  529. }
  530. }
  531. //删除我的某个评论
  532. public function delete_answer(){
  533. $id = input('id',0);
  534. if(!$id){
  535. $this->error();
  536. }
  537. Db::startTrans();
  538. $info = Db::name('topic_dongtai_answer')->where('id',$id)->where('user_id',$this->auth->id)->find();
  539. if(!$info){
  540. $this->error('不存在的动态评论');
  541. }
  542. if($info['level'] == 1){
  543. //楼层内都删
  544. $louceng_id = Db::name('topic_dongtai_answer')->where('dt_id',$info['dt_id'])->where('level',2)->where('floor',$info['floor'])->column('id');
  545. if(!empty($louceng_id)){
  546. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum',count($louceng_id));//回复数减1
  547. Db::name('topic_dongtai_answer')->where('id','IN',$louceng_id)->delete();//评论删掉
  548. Db::name('topic_answer_good')->where('answer_id','IN',$louceng_id)->delete();//评论点赞删掉
  549. }
  550. }
  551. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum');//回复数减1
  552. Db::name('topic_dongtai_answer')->where('id',$id)->delete(); //评论删掉
  553. Db::name('topic_answer_good')->where('answer_id',$id)->delete();//评论点赞删掉
  554. Db::commit();
  555. $this->success();
  556. }
  557. //动态赠送礼物
  558. public function givegiftdongtai() {
  559. // 接口防并发
  560. if (!$this->apiLimit(1, 1)) {
  561. $this->error(__('Operation frequently'));
  562. }
  563. // $user_id = input('user_id');// 赠送对象
  564. $dt_id = input('dt_id', 0, 'intval'); //动态id
  565. $gift_id = input('gift_id');// 礼物ID
  566. $number = input('number',1,'intval');//数量
  567. if (!$dt_id || !$gift_id || $number < 1) {
  568. $this->error();
  569. }
  570. //查询动态
  571. $dongtai_info = Db::name('topic_dongtai')->find($dt_id);
  572. if (!$dongtai_info) {
  573. $this->error('您的网络开小差啦~');
  574. }
  575. if ($dongtai_info['status'] != 0) {
  576. $this->error('您的网络开小差啦~');
  577. }
  578. $user_id = $dongtai_info['user_id'];
  579. // 不可以赠送给自己
  580. if($this->auth->id == $user_id) {
  581. $this->error("不可以赠送给自己");
  582. }
  583. // 获取礼物信息
  584. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  585. if (!$giftinfo)
  586. {
  587. $this->error("请选择礼物");
  588. }
  589. $giftvalue = bcmul($giftinfo['value'],$number);
  590. //被赠送人信息
  591. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  592. if (!$touserinfo) {
  593. $this->error("不存在的用户");
  594. }
  595. // 判断当前用户余额
  596. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  597. if($user_gold < $giftvalue) {
  598. $this->error("您的金币余额不足");
  599. }
  600. Db::startTrans();
  601. // 添加礼物赠送记录表
  602. $data = [
  603. 'user_id' => $this->auth->id,
  604. 'user_to_id' => $user_id,
  605. 'dt_id' => $dt_id,
  606. 'gift_id' => $giftinfo['id'],
  607. 'gift_name' => $giftinfo['name'],
  608. 'number' => $number,
  609. 'price' => $giftvalue,
  610. 'createtime' => time(),
  611. ];
  612. $log_id = Db::name('gift_user_dongtai')->insertGetId($data);
  613. if(!$log_id){
  614. Db::rollback();
  615. $this->error('赠送失败');
  616. }
  617. if($giftvalue > 0){
  618. // 扣除当前用户余额
  619. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,59,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id);
  620. if($wallet_rs['status'] === false){
  621. Db::rollback();
  622. $this->error($wallet_rs['msg']);
  623. }
  624. // 添加赠送用户余额
  625. $money_to_gold = config('site.money_to_gold');
  626. $gift_plat_scale = config('site.gift_plat_scale');
  627. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  628. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  629. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,60,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id,2);
  630. if($wallet_rs['status'] === false){
  631. Db::rollback();
  632. $this->error($wallet_rs['msg']);
  633. }
  634. //增加赠送用户上级余额
  635. if ($touserinfo['intro_uid']) {
  636. //获取返利比率
  637. $agent_info = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->field('is_agent,h_intro_income_rebate_rate')->find();
  638. $intro_income_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_income_rebate_rate'] : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  639. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  640. //上级获得金额
  641. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  642. if ($intro_uid_money > 0) {
  643. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人动态礼物获赠奖励','gift_user_dongtai',$log_id);
  644. if($intro_result['status']===false)
  645. {
  646. Db::rollback();
  647. $this->error($intro_result['msg']);
  648. }
  649. }
  650. }
  651. }
  652. if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
  653. //增加亲密度
  654. $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
  655. if (!$user_intimacy_rs['status']) {
  656. Db::rollback();
  657. $this->error('您的网络开小差啦~');
  658. }
  659. }
  660. }
  661. Db::commit();
  662. //发送消息
  663. /*if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  664. $tenim = new \app\api\controller\Tenim;
  665. $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
  666. }*/
  667. $return_data['money'] = $money; //获得金额
  668. $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
  669. $this->success('赠送成功', $return_data);
  670. }
  671. //动态收到礼物列表
  672. public function dongtaigiftlist() {
  673. $id = input('id', 0, 'intval');
  674. if (!$id) {
  675. $this->error('您的网络开小差啦~');
  676. }
  677. //点赞只能查看异性
  678. $dongtaiWhere['td.id'] = $id;
  679. $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
  680. ->join('user u','u.id = td.user_id','LEFT')
  681. ->where($dongtaiWhere)->find();
  682. $where['a.dt_id'] = $id;
  683. $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
  684. if ($gender == 1) {
  685. $where['user.gender'] = 0;
  686. } elseif ($gender == 0) {
  687. $where['user.gender'] = 1;
  688. }
  689. $list = Db::name('gift_user_dongtai')->alias('a')->field('a.user_id, count(a.id) count')
  690. ->join('user', 'a.user_id = user.id', 'left')
  691. ->where($where)->group('a.user_id')->order('a.id desc')->autopage()->select();
  692. if (!$list) {
  693. $this->success('success', $list);
  694. }
  695. $mt_user = Db::name('user');
  696. foreach ($list as &$v) {
  697. $user_info = $mt_user->field('nickname, avatar, gender, birthday')->where(['id' => $v['user_id']])->find();
  698. $v['nickname'] = $user_info['nickname'];
  699. $v['avatar'] = one_domain_image($user_info['avatar']);
  700. $v['birthday'] = birthtime_to_age($user_info['birthday']);
  701. }
  702. $this->success('success', $list);
  703. }
  704. /////////////////////////////////////////////////////////////////////////////////
  705. //点赞列表
  706. public function dongtaigoodlist() {
  707. $id = input('id', 0, 'intval');
  708. if (!$id) {
  709. $this->error('您的网络开小差啦~');
  710. }
  711. //点赞只能查看异性
  712. $dongtaiWhere['td.id'] = $id;
  713. $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
  714. ->join('user u','u.id = td.user_id','LEFT')
  715. ->where($dongtaiWhere)->find();
  716. $where['a.dt_id'] = $id;
  717. $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
  718. if ($gender == 1) {
  719. $where['user.gender'] = 0;
  720. } elseif ($gender == 0) {
  721. $where['user.gender'] = 1;
  722. }
  723. $list = Db::name('topic_dongtai_good')->alias('a')
  724. ->join('user', 'a.user_id = user.id', 'left')
  725. ->field('a.*, user.nickname,user.avatar,user.gender,user.birthday')
  726. ->where($where)
  727. ->order('a.id desc')
  728. ->autopage()->select();
  729. if (!$list) {
  730. $this->success('success', $list);
  731. }
  732. $list = list_domain_image($list,['avatar']);
  733. foreach ($list as &$v) {
  734. $v['birthday'] = birthtime_to_age($v['birthday']);
  735. $v['createtime'] = get_last_time($v['createtime']);
  736. }
  737. $this->success('success', $list);
  738. }
  739. //删除动态
  740. public function deldongtai() {
  741. $id = input('id', 0, 'intval');
  742. if (!$id) {
  743. $this->error('您的网络开小差啦~');
  744. }
  745. $info = Db::name('topic_dongtai')->find($id);
  746. if (!$info) {
  747. $this->error('您的网络开小差啦~');
  748. }
  749. if ($info['user_id'] != $this->auth->id) {
  750. $this->error('您的网络开小差啦~');
  751. }
  752. $rs = Db::name('topic_dongtai')->where(['id' => $id])->setField('status', 1);
  753. if (!$rs) {
  754. $this->error('您的网络开小差啦~');
  755. }
  756. $this->success('删除成功');
  757. }
  758. }