Topicdongtai.php 33 KB

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