Topicdongtai.php 32 KB

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