Topicdongtai.php 33 KB

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