Topicdongtai.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 addone(){
  15. $content = input('content','');
  16. $images = input('images','');
  17. $topic_id = input('topic_id','');
  18. if(!$content && !$images){
  19. $this->error(__('Invalid parameters'));
  20. }
  21. //关键字替换
  22. //$content = Keyworld::sensitive($content);
  23. $data = [
  24. 'topic_id' => $topic_id,
  25. 'user_id' => $this->auth->id,
  26. 'content' => $content,
  27. 'images' => $images,
  28. 'type' => input('type',1),
  29. 'createtime' => time(),
  30. 'updatetime' => time(),
  31. ];
  32. Db::startTrans();
  33. $id = Db::name('topic_dongtai')->insertGetId($data);
  34. //圈子新增一个贴
  35. $rs = Db::name('topic_hub')->where('id',$topic_id)->setInc('t_number');
  36. Db::commit();
  37. $this->success('发布成功',$id);
  38. }
  39. //自己看列表
  40. //某用户的帖子列表
  41. public function my_lists(){
  42. $uid = input('uid',$this->auth->id);
  43. $list = Db::name('topic_dongtai')->alias('dt')
  44. ->join('user','dt.user_id = user.id','LEFT')
  45. ->field('dt.*,user.nickname,user.avatar')
  46. ->where('dt.user_id',$uid)
  47. ->order('dt.id desc')->autopage()->select();
  48. $list = list_domain_image($list,['images','avatar']);
  49. if(!empty($list)){
  50. foreach($list as $key => &$val){
  51. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  52. //创建视频缩略图
  53. $val['images_thumb'] = '';
  54. if ($val['type'] == 2) {
  55. $images_url = explode('.', $val['images']);
  56. unset($images_url[count($images_url) - 1]);
  57. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  58. }
  59. }
  60. }
  61. $this->success('success',$list);
  62. }
  63. //是否点赞
  64. private function is_good($dt_id,$uid){
  65. $where = [
  66. 'dt_id' => $dt_id,
  67. 'user_id' => $uid,
  68. ];
  69. $check = Db::name('topic_dongtai_good')->where($where)->find();
  70. if($check){
  71. return 1;
  72. }else{
  73. return 0;
  74. }
  75. }
  76. //详情
  77. public function info(){
  78. $id = input('id');
  79. $info = Db::name('topic_dongtai')->alias('dt')
  80. ->join('user','dt.user_id = user.id','LEFT')
  81. ->field('dt.*,user.nickname,user.avatar')
  82. ->where('dt.id',$id)->find();
  83. $info = info_domain_image($info,['images','avatar']);
  84. //是否点赞过
  85. if($info){
  86. $info['isgood'] = $this->is_good($id,$this->auth->id);
  87. //创建视频缩略图
  88. $info['images_thumb'] = '';
  89. if ($info['type'] == 2) {
  90. $images_url = explode('.', $info['images']);
  91. unset($images_url[count($images_url) - 1]);
  92. $info['images_thumb'] = join('.', $images_url) . '_0.jpg';
  93. }
  94. }
  95. //评论
  96. if($info){
  97. $info['answer'] = $this->answer_list($id);
  98. }
  99. $this->success('success',$info);
  100. }
  101. //点赞
  102. public function good(){
  103. $id = input('id');
  104. $where = [
  105. 'dt_id' => $id,
  106. 'user_id' => $this->auth->id,
  107. ];
  108. $check = Db::name('topic_dongtai_good')->where($where)->find();
  109. if($check){
  110. $this->error('已经赞过了');
  111. }
  112. Db::startTrans();
  113. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  114. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  115. if($rs && $up !== false){
  116. Db::commit();
  117. $this->success('点赞成功');
  118. }
  119. Db::rollback();
  120. $this->error('点赞失败');
  121. }
  122. //评论
  123. public function answer(){
  124. $id = input('id',0);
  125. $content = input('content','');
  126. $to_user_id = input('to_user_id',0);
  127. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  128. $floor = input('floor',0);
  129. if(empty($content) || empty($id)){
  130. $this->error();
  131. }
  132. //关键字替换
  133. //$content = Keyworld::sensitive($content);
  134. //判断
  135. if($level == 2 && $floor == 0){
  136. $this->error('楼层错误');
  137. }
  138. //回复楼主,最新楼层
  139. if($level == 1 || $floor == 0){
  140. $to_user_id = 0;
  141. $floor = 1; //默认一楼
  142. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  143. if($last_floor){
  144. $floor = $last_floor + 1;
  145. }
  146. }
  147. //判断user_id
  148. if($to_user_id){
  149. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  150. if(empty($to_user)){
  151. $this->error('被回复的用户不存在');
  152. }
  153. }
  154. //data
  155. $data = [
  156. 'dt_id' => $id,
  157. 'floor' => $floor,
  158. 'user_id' => $this->auth->id,
  159. 'content' => $content,
  160. 'to_user_id' => $to_user_id,
  161. 'level' => $level,
  162. 'createtime' => time(),
  163. 'updatetime' => time(),
  164. ];
  165. Db::startTrans();
  166. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  167. Db::commit();
  168. $this->success('评价成功');
  169. }
  170. //评论列表
  171. private function answer_list($dt_id){
  172. //楼
  173. $floor_list = Db::name('topic_dongtai_answer')
  174. ->alias('a')
  175. ->field('a.*,user.nickname,user.avatar')
  176. ->join('user','a.user_id = user.id','LEFT')
  177. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('id asc')->autopage()->select();
  178. $floor_list = list_domain_image($floor_list,['avatar']);
  179. if(empty($floor_list)){
  180. return [];
  181. }
  182. //层
  183. $floors = array_column($floor_list,'floor');
  184. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  185. ->field('a.*,user.nickname,user.avatar,tuser.nickname as to_nickname,tuser.avatar as to_avatar')
  186. ->join('user','a.user_id = user.id','LEFT')
  187. ->join('user tuser','a.to_user_id = tuser.id','LEFT')
  188. ->where(['a.dt_id'=>$dt_id,'a.floor'=>['IN',$floors],'a.level'=>2])->order('id asc')->select();
  189. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  190. /*if(empty($child_lists)){
  191. return $floor_list;
  192. }*/
  193. //合并
  194. foreach($floor_list as $key => $val){
  195. $child = [];
  196. foreach($child_lists as $k => $v){
  197. if($val['floor'] == $v['floor']){
  198. $child[] = $v;
  199. }
  200. }
  201. //追加到外循环
  202. $floor_list[$key]['childcount'] = 0;
  203. if(count($child) > 4){
  204. $floor_list[$key]['childcount'] = count($child) - 4;
  205. }
  206. $floor_list[$key]['child'] = array_slice($child,0,4);
  207. }
  208. return $floor_list;
  209. }
  210. //单独某一层的详细
  211. public function floor_info(){
  212. $floor_id = input('floor_id');
  213. //楼
  214. $floor_info = Db::name('topic_dongtai_answer')
  215. ->alias('a')
  216. ->field('a.*,user.nickname,user.avatar')
  217. ->join('user','a.user_id = user.id','LEFT')
  218. ->where(['a.id'=>$floor_id])->find();
  219. $floor_info = info_domain_image($floor_info,['avatar']);
  220. //层
  221. $floors = $floor_info['floor'];
  222. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  223. ->field('a.*,user.nickname,user.avatar,tuser.nickname as to_nickname,tuser.avatar as to_avatar')
  224. ->join('user','a.user_id = user.id','LEFT')
  225. ->join('user tuser','a.to_user_id = tuser.id','LEFT')
  226. ->where(['a.floor'=>$floors,'a.level'=>2])->order('id asc')->autopage()->select();
  227. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  228. //合并
  229. $floor_info['child'] = $child_lists;
  230. $this->success('success',$floor_info);
  231. }
  232. //某个圈子里的动态列表,全部,最新,最热
  233. public function topic_list(){
  234. $topic_id = input('topic_id',0);
  235. $order = input('orderby','new');
  236. $orderby = 'dt.id desc';
  237. if($order == 'hot'){
  238. $orderby = 'dt.goodnum desc';
  239. }
  240. $list = Db::name('topic_dongtai')->alias('dt')
  241. ->join('user','dt.user_id = user.id','LEFT')
  242. ->field('dt.*,user.nickname,user.avatar')
  243. ->where('dt.topic_id',$topic_id)
  244. ->order($orderby)->autopage()->select();
  245. $list = list_domain_image($list,['images','avatar']);
  246. //追加是否点赞
  247. if(!empty($list)){
  248. $ids = array_column($list,'id');
  249. $map = [
  250. 'dt_id' => ['IN',$ids],
  251. 'user_id' => $this->auth->id,
  252. ];
  253. $good_list = Db::name('topic_dongtai_good')->where('dt_id','IN',$ids)->select();
  254. foreach($list as $key => $val){
  255. $list[$key]['isgood'] = 0;
  256. foreach($good_list as $k => $v){
  257. if($val['id'] == $v['dt_id']){
  258. $list[$key]['isgood'] = 1;
  259. }
  260. }
  261. }
  262. }
  263. $this->success('success',$list);
  264. }
  265. //动态赠送礼物
  266. public function givegiftdongtai() {
  267. // 接口防并发
  268. if (!$this->apiLimit(1, 1000)) {
  269. $this->error(__('Operation frequently'));
  270. }
  271. // $user_id = input('user_id');// 赠送对象
  272. $dt_id = input('dt_id', 0, 'intval'); //动态id
  273. $gift_id = input('gift_id');// 礼物ID
  274. $number = input('number',1,'intval');//数量
  275. if (!$dt_id || !$gift_id || $number < 1) {
  276. $this->error();
  277. }
  278. //查询动态
  279. $dongtai_info = Db::name('topic_dongtai')->find($dt_id);
  280. if (!$dongtai_info) {
  281. $this->error('您的网络开小差啦~');
  282. }
  283. if ($dongtai_info['status'] != 0) {
  284. $this->error('您的网络开小差啦~');
  285. }
  286. $user_id = $dongtai_info['user_id'];
  287. // 不可以赠送给自己
  288. if($this->auth->id == $user_id) {
  289. $this->error("不可以赠送给自己");
  290. }
  291. // 获取礼物信息
  292. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  293. if (!$giftinfo)
  294. {
  295. $this->error("请选择礼物");
  296. }
  297. $giftvalue = bcmul($giftinfo['value'],$number);
  298. //被赠送人信息
  299. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  300. if (!$touserinfo) {
  301. $this->error("不存在的用户");
  302. }
  303. // 判断当前用户余额
  304. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  305. if($user_gold < $giftvalue) {
  306. $this->error("您的金币余额不足");
  307. }
  308. Db::startTrans();
  309. // 添加礼物赠送记录表
  310. $data = [
  311. 'user_id' => $this->auth->id,
  312. 'user_to_id' => $user_id,
  313. 'dt_id' => $dt_id,
  314. 'gift_id' => $giftinfo['id'],
  315. 'gift_name' => $giftinfo['name'],
  316. 'number' => $number,
  317. 'price' => $giftvalue,
  318. 'createtime' => time(),
  319. ];
  320. $log_id = Db::name('gift_user_dongtai')->insertGetId($data);
  321. if(!$log_id){
  322. Db::rollback();
  323. $this->error('赠送失败');
  324. }
  325. if($giftvalue > 0){
  326. // 扣除当前用户余额
  327. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,59,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id);
  328. if($wallet_rs['status'] === false){
  329. Db::rollback();
  330. $this->error($wallet_rs['msg']);
  331. }
  332. // 添加赠送用户余额
  333. $money_to_gold = config('site.money_to_gold');
  334. $gift_plat_scale = config('site.gift_plat_scale');
  335. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  336. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  337. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,60,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id,2);
  338. if($wallet_rs['status'] === false){
  339. Db::rollback();
  340. $this->error($wallet_rs['msg']);
  341. }
  342. /*//增加赠送用户上级金币
  343. if ($touserinfo['intro_uid']) {
  344. //获取返利比率
  345. $intro_gift_rebate_rate = (int)config('site.intro_gift_rebate_rate'); //邀请人收礼物返利比率
  346. if ($intro_gift_rebate_rate > 0 && $intro_gift_rebate_rate <= 100) {
  347. //上级获得金币数量
  348. $intro_uid_gold = floor($giftvalue * $intro_gift_rebate_rate / 100);
  349. if ($intro_uid_gold > 0) {
  350. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'gold',$intro_uid_gold,66, '动态礼物获赠奖励','gift_user_dongtai',$log_id);
  351. if($intro_result['status']===false)
  352. {
  353. Db::rollback();
  354. $this->error($intro_result['msg']);
  355. }
  356. }
  357. }
  358. }*/
  359. //增加赠送用户上级余额
  360. if ($touserinfo['intro_uid']) {
  361. //获取返利比率
  362. $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
  363. $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  364. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  365. //上级获得金额
  366. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  367. if ($intro_uid_money > 0) {
  368. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人动态礼物获赠奖励','gift_user_dongtai',$log_id);
  369. if($intro_result['status']===false)
  370. {
  371. Db::rollback();
  372. $this->error($intro_result['msg']);
  373. }
  374. }
  375. }
  376. }
  377. if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
  378. //增加亲密度
  379. $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
  380. if (!$user_intimacy_rs['status']) {
  381. Db::rollback();
  382. $this->error('您的网络开小差啦~');
  383. }
  384. }
  385. }
  386. //tag任务赠送金币
  387. //搭讪奖励
  388. // $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  389. // if($task_rs === false){
  390. // Db::rollback();
  391. // $this->error('完成任务赠送奖励失败');
  392. // }
  393. Db::commit();
  394. //发送消息
  395. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  396. $tenim = new \app\api\controller\Tenim;
  397. $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
  398. }
  399. $return_data['money'] = $money; //获得金额
  400. $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
  401. $this->success('赠送成功', $return_data);
  402. }
  403. //动态收到礼物列表
  404. public function dongtaigiftlist() {
  405. $id = input('id', 0, 'intval');
  406. if (!$id) {
  407. $this->error('您的网络开小差啦~');
  408. }
  409. $list = Db::name('gift_user_dongtai')->field('user_id, count(id) count')->where(['dt_id' => $id])->group('user_id')->order('id desc')->autopage()->select();
  410. if (!$list) {
  411. $this->success('success', $list);
  412. }
  413. $mt_user = Db::name('user');
  414. foreach ($list as &$v) {
  415. $user_info = $mt_user->field('nickname, avatar, gender, birthday')->where(['id' => $v['user_id']])->find();
  416. $v['nickname'] = $user_info['nickname'];
  417. $v['avatar'] = one_domain_image($user_info['avatar']);
  418. $v['birthday'] = birthtime_to_age($user_info['birthday']);
  419. }
  420. $this->success('success', $list);
  421. }
  422. }