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. //某用户的帖子列表
  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. $address = $this->ip_to_address();
  287. $data = [
  288. 'topic_id' => $topic_id,
  289. 'user_id' => $this->auth->id,
  290. 'content' => $content,
  291. 'images' => $images,
  292. // 'longitude' => input('longitude',''),
  293. // 'latitude' => input('latitude',''),
  294. 'createtime' => time(),
  295. 'updatetime' => time(),
  296. 'is_show_real' => $show_real,
  297. 'address' => $address,
  298. 'type' => $type
  299. ];
  300. Db::startTrans();
  301. $id = Db::name('topic_dongtai')->insertGetId($data);
  302. if (!$id) {
  303. Db::rollback();
  304. $this->error('您的网络开小差啦~');
  305. }
  306. //圈子新增一个贴
  307. if ($topic_id) {
  308. $rs = Db::name('topic_hub')->where('id', $topic_id)->setInc('t_number');
  309. if (!$rs) {
  310. Db::rollback();
  311. $this->error('您的网络开小差啦~');
  312. }
  313. }
  314. Db::commit();
  315. $this->success('发布成功',$id);
  316. }
  317. //动态列表
  318. public function dongtailist() {
  319. $type = input('type', 0, 'intval'); //类型:0热门 1最新
  320. $topic_id = input('topic_id', 0); //热门话题id
  321. if (!in_array($type, [0, 1])) {
  322. $this->error('您的网络开小差啦~');
  323. }
  324. if ($type == 0) {
  325. // $orderby = 'dt.goodnum desc';
  326. $orderby = 'dt.id desc';
  327. } else {
  328. $orderby = 'dt.id desc';
  329. }
  330. $where['dt.status'] = 0;
  331. $where['dt.auit_status'] = 1;
  332. $where['user.is_kefu'] = 0;
  333. if ($this->auth->gender == 1) {
  334. $where['user.gender'] = 0;
  335. } elseif ($this->auth->gender == 0) {
  336. $where['user.gender'] = 1;
  337. } else {
  338. $this->success('success',[]);
  339. }
  340. if ($topic_id) {
  341. $where['dt.topic_id'] = $topic_id;
  342. $orderby = 'dt.id desc';
  343. }
  344. $list = Db::name('topic_dongtai')->alias('dt')
  345. ->join('user','dt.user_id = user.id','LEFT')
  346. ->join('topic_hub th','dt.topic_id = th.id','LEFT')
  347. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.cityname,user.is_hideaddress,th.name,user.real_status')
  348. ->where($where)
  349. ->order($orderby)->autopage()->select();
  350. $list = list_domain_image($list,['images','avatar']);
  351. //追加是否点赞
  352. if(!empty($list)){
  353. $ids = array_column($list,'id');
  354. $map = [
  355. 'dt_id' => ['IN',$ids],
  356. 'user_id' => $this->auth->id,
  357. ];
  358. $good_list = Db::name('topic_dongtai_good')->where($map)->select();
  359. $mt_user_greet = Db::name('user_greet'); //是否打过招呼
  360. $mt_gift_user_dongtai = Db::name('gift_user_dongtai');
  361. $mt_user_wallet = Db::name('user_wallet'); //钱包
  362. $mt_wealth_level = Db::name('wealth_level'); //财富等级
  363. $mt_charm_level = Db::name('charm_level'); //魅力等级
  364. foreach ($list as &$val) {
  365. $val['name'] = $val['name'] ? : '';
  366. $val['birthday'] = birthtime_to_age($val['birthday']);
  367. $val['createtime'] = get_last_time($val['createtime']);
  368. $val['cityname'] = $val['is_hideaddress'] ? '' : $val['address'] ;
  369. //是否点过赞:0否 1是
  370. $val['isgood'] = 0;
  371. foreach($good_list as $k => $v){
  372. if($val['id'] == $v['dt_id']){
  373. $val['isgood'] = 1;
  374. }
  375. }
  376. //礼物数量
  377. $val['gift_count'] = $mt_gift_user_dongtai->where(['dt_id' => $val['id']])->count('id');
  378. //查询是否打过招呼
  379. $count = $mt_user_greet->where(['user_id' => $this->auth->id, 'user_to_id' => $val['user_id']])->count('id');
  380. if ($count) {
  381. $val['is_chat'] = 1; //是否打过招呼: 1是 0否
  382. } else {
  383. $val['is_chat'] = 0; //是否打过招呼: 1是 0否
  384. }
  385. //查询财富等级和魅力等级
  386. $wallet_info = $mt_user_wallet->where(['user_id' => $val['user_id']])->find();
  387. $wealth_level = $mt_wealth_level->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
  388. if ($wealth_level) {
  389. $val['wealth_level'] = $wealth_level['name'];
  390. } else {
  391. $val['wealth_level'] = '';
  392. }
  393. $charm_level = $mt_charm_level->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
  394. if ($charm_level) {
  395. $val['charm_level'] = $charm_level['name'];
  396. } else {
  397. $val['charm_level'] = '';
  398. }
  399. //创建视频缩略图
  400. $val['images_thumb'] = '';
  401. if ($val['type'] == 2) {
  402. $images_url = explode('.', $val['images']);
  403. unset($images_url[count($images_url) - 1]);
  404. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  405. }
  406. }
  407. }
  408. $this->success('success',$list);
  409. }
  410. //动态详情
  411. public function dongtaiinfo(){
  412. $id = input('id', 0, 'intval');
  413. if (!$id) {
  414. $this->error('您的网络开小差啦~');
  415. }
  416. $info = Db::name('topic_dongtai')->alias('dt')
  417. ->join('user','dt.user_id = user.id','LEFT')
  418. ->join('topic_hub th','dt.topic_id = th.id','LEFT')
  419. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.cityname,user.is_hideaddress,th.name,user.real_status')
  420. ->where('dt.id',$id)->find();
  421. if (!$info) {
  422. $this->error('您的网络开小差啦~');
  423. }
  424. if ($info['status'] != 0) {
  425. $this->error('您的网络开小差啦~');
  426. }
  427. $info = info_domain_image($info,['images','avatar']);
  428. $info['birthday'] = birthtime_to_age($info['birthday']);
  429. $info['createtime'] = get_last_time($info['createtime']);
  430. $info['cityname'] = $info['is_hideaddress'] ? '' : $info['address'];
  431. //是否点赞过
  432. $info['isgood'] = $this->is_good($id,$this->auth->id);
  433. //礼物数量
  434. $info['gift_count'] = Db::name('gift_user_dongtai')->where(['dt_id' => $info['id']])->count('id');
  435. //查询是否打过招呼
  436. $count = Db::name('user_greet')->where(['user_id' => $this->auth->id, 'user_to_id' => $info['user_id']])->count('id');
  437. if ($count) {
  438. $info['is_chat'] = 1; //是否打过招呼: 1是 0否
  439. } else {
  440. $info['is_chat'] = 0; //是否打过招呼: 1是 0否
  441. }
  442. //查询财富等级和魅力等级
  443. $wallet_info = Db::name('user_wallet')->where(['user_id' => $info['user_id']])->find();
  444. $wealth_level = Db::name('wealth_level')->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
  445. if ($wealth_level) {
  446. $info['wealth_level'] = $wealth_level['name'];
  447. } else {
  448. $info['wealth_level'] = '';
  449. }
  450. $charm_level = Db::name('charm_level')->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
  451. if ($charm_level) {
  452. $info['charm_level'] = $charm_level['name'];
  453. } else {
  454. $info['charm_level'] = '';
  455. }
  456. //创建视频缩略图
  457. $info['images_thumb'] = '';
  458. if ($info['type'] == 2) {
  459. $images_url = explode('.', $info['images']);
  460. unset($images_url[count($images_url) - 1]);
  461. $info['images_thumb'] = join('.', $images_url) . '_0.jpg';
  462. }
  463. $this->success('success',$info);
  464. }
  465. //点赞
  466. public function dongtaigood(){
  467. $id = input('id', 0, 'intval');
  468. if (!$id) {
  469. $this->error('您的网络开小差啦~');
  470. }
  471. $info = Db::name('topic_dongtai')->find($id);
  472. if (!$info) {
  473. $this->error('您的网络开小差啦~');
  474. }
  475. if ($info['status'] != 0) {
  476. $this->error('您的网络开小差啦~');
  477. }
  478. $where = [
  479. 'dt_id' => $id,
  480. 'user_id' => $this->auth->id,
  481. ];
  482. $check = Db::name('topic_dongtai_good')->where($where)->find();
  483. if($check){
  484. $this->error('已经赞过了');
  485. }
  486. $where['createtime'] = time();
  487. Db::startTrans();
  488. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  489. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  490. //tag任务赠送金币
  491. //点赞奖励
  492. // $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,14);
  493. // if($task_rs === false){
  494. // Db::rollback();
  495. // $this->error('完成任务赠送奖励失败');
  496. // }
  497. if($rs && $up !== false){
  498. Db::commit();
  499. $this->success('点赞成功');
  500. }
  501. Db::rollback();
  502. $this->error('点赞失败');
  503. }
  504. //点赞列表
  505. public function dongtaigoodlist() {
  506. $id = input('id', 0, 'intval');
  507. if (!$id) {
  508. $this->error('您的网络开小差啦~');
  509. }
  510. //点赞只能查看异性
  511. $dongtaiWhere['td.id'] = $id;
  512. $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
  513. ->join('user u','u.id = td.user_id','LEFT')
  514. ->where($dongtaiWhere)->find();
  515. $where['a.dt_id'] = $id;
  516. $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
  517. if ($gender == 1) {
  518. $where['user.gender'] = 0;
  519. } elseif ($gender == 0) {
  520. $where['user.gender'] = 1;
  521. }
  522. $list = Db::name('topic_dongtai_good')->alias('a')
  523. ->join('user', 'a.user_id = user.id', 'left')
  524. ->field('a.*, user.nickname,user.avatar,user.gender,user.birthday')
  525. ->where($where)
  526. ->order('a.id desc')
  527. ->autopage()->select();
  528. if (!$list) {
  529. $this->success('success', $list);
  530. }
  531. $list = list_domain_image($list,['avatar']);
  532. foreach ($list as &$v) {
  533. $v['birthday'] = birthtime_to_age($v['birthday']);
  534. $v['createtime'] = get_last_time($v['createtime']);
  535. }
  536. $this->success('success', $list);
  537. }
  538. //动态赠送礼物
  539. public function givegiftdongtai() {
  540. // 接口防并发
  541. if (!$this->apiLimit(1, 1000)) {
  542. $this->error(__('Operation frequently'));
  543. }
  544. // $user_id = input('user_id');// 赠送对象
  545. $dt_id = input('dt_id', 0, 'intval'); //动态id
  546. $gift_id = input('gift_id');// 礼物ID
  547. $number = input('number',1,'intval');//数量
  548. if (!$dt_id || !$gift_id || $number < 1) {
  549. $this->error();
  550. }
  551. //查询动态
  552. $dongtai_info = Db::name('topic_dongtai')->find($dt_id);
  553. if (!$dongtai_info) {
  554. $this->error('您的网络开小差啦~');
  555. }
  556. if ($dongtai_info['status'] != 0) {
  557. $this->error('您的网络开小差啦~');
  558. }
  559. $user_id = $dongtai_info['user_id'];
  560. // 不可以赠送给自己
  561. if($this->auth->id == $user_id) {
  562. $this->error("不可以赠送给自己");
  563. }
  564. // 获取礼物信息
  565. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  566. if (!$giftinfo)
  567. {
  568. $this->error("请选择礼物");
  569. }
  570. $giftvalue = bcmul($giftinfo['value'],$number);
  571. //被赠送人信息
  572. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  573. if (!$touserinfo) {
  574. $this->error("不存在的用户");
  575. }
  576. // 判断当前用户余额
  577. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  578. if($user_gold < $giftvalue) {
  579. $this->error("您的金币余额不足");
  580. }
  581. Db::startTrans();
  582. // 添加礼物赠送记录表
  583. $data = [
  584. 'user_id' => $this->auth->id,
  585. 'user_to_id' => $user_id,
  586. 'dt_id' => $dt_id,
  587. 'gift_id' => $giftinfo['id'],
  588. 'gift_name' => $giftinfo['name'],
  589. 'number' => $number,
  590. 'price' => $giftvalue,
  591. 'createtime' => time(),
  592. ];
  593. $log_id = Db::name('gift_user_dongtai')->insertGetId($data);
  594. if(!$log_id){
  595. Db::rollback();
  596. $this->error('赠送失败');
  597. }
  598. if($giftvalue > 0){
  599. // 扣除当前用户余额
  600. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$user_id,'gold',-$giftvalue,59,'赠送礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id);
  601. if($wallet_rs['status'] === false){
  602. Db::rollback();
  603. $this->error($wallet_rs['msg']);
  604. }
  605. // 添加赠送用户余额
  606. $money_to_gold = config('site.money_to_gold');
  607. $gift_plat_scale = config('site.gift_plat_scale');
  608. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  609. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  610. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,$this->auth->id,'money',$money,60,'获得礼物:'.$giftinfo["name"] . '*' . $number,'gift_user_dongtai',$log_id,2);
  611. if($wallet_rs['status'] === false){
  612. Db::rollback();
  613. $this->error($wallet_rs['msg']);
  614. }
  615. /*//增加赠送用户上级金币
  616. if ($touserinfo['intro_uid']) {
  617. //获取返利比率
  618. $intro_gift_rebate_rate = (int)config('site.intro_gift_rebate_rate'); //邀请人收礼物返利比率
  619. if ($intro_gift_rebate_rate > 0 && $intro_gift_rebate_rate <= 100) {
  620. //上级获得金币数量
  621. $intro_uid_gold = floor($giftvalue * $intro_gift_rebate_rate / 100);
  622. if ($intro_uid_gold > 0) {
  623. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'gold',$intro_uid_gold,66, '动态礼物获赠奖励','gift_user_dongtai',$log_id);
  624. if($intro_result['status']===false)
  625. {
  626. Db::rollback();
  627. $this->error($intro_result['msg']);
  628. }
  629. }
  630. }
  631. }*/
  632. //增加赠送用户上级余额
  633. if ($touserinfo['intro_uid']) {
  634. //获取返利比率
  635. $is_agent = Db::name('user')->where(['id' => $touserinfo['intro_uid']])->value('is_agent');
  636. $intro_income_rebate_rate = $is_agent ? (int)config('site.h_intro_income_rebate_rate') : (int)config('site.intro_income_rebate_rate'); //邀请人收礼物返利比率
  637. if ($intro_income_rebate_rate > 0 && $intro_income_rebate_rate <= 100) {
  638. //上级获得金额
  639. $intro_uid_money = number_format($money * $intro_income_rebate_rate / 100, 2, '.', '');
  640. if ($intro_uid_money > 0) {
  641. $intro_result = model('Wallet')->lockChangeAccountRemain($touserinfo['intro_uid'],$user_id,'money',$intro_uid_money,68, '邀请人动态礼物获赠奖励','gift_user_dongtai',$log_id);
  642. if($intro_result['status']===false)
  643. {
  644. Db::rollback();
  645. $this->error($intro_result['msg']);
  646. }
  647. }
  648. }
  649. }
  650. if ($this->auth->gender == 1 && $touserinfo['gender'] == 0) {
  651. //增加亲密度
  652. $user_intimacy_rs = addintimacy($this->auth->id, $user_id, $giftvalue);
  653. if (!$user_intimacy_rs['status']) {
  654. Db::rollback();
  655. $this->error('您的网络开小差啦~');
  656. }
  657. }
  658. }
  659. //tag任务赠送金币
  660. //搭讪奖励
  661. // $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,15);
  662. // if($task_rs === false){
  663. // Db::rollback();
  664. // $this->error('完成任务赠送奖励失败');
  665. // }
  666. Db::commit();
  667. //发送消息
  668. if (isset($user_intimacy_rs) && $user_intimacy_rs['level_remark']) {
  669. $tenim = new \app\api\controller\Tenim;
  670. $tenim->sendMessageToUser($this->auth->id, $user_id, $user_intimacy_rs['level_remark'], 1);
  671. }
  672. $return_data['money'] = $money; //获得金额
  673. $return_data['level_remark'] = isset($user_intimacy_rs) ? $user_intimacy_rs['level_remark'] : ''; //亲密度等级提示语
  674. $this->success('赠送成功', $return_data);
  675. }
  676. //动态收到礼物列表
  677. public function dongtaigiftlist() {
  678. $id = input('id', 0, 'intval');
  679. if (!$id) {
  680. $this->error('您的网络开小差啦~');
  681. }
  682. //点赞只能查看异性
  683. $dongtaiWhere['td.id'] = $id;
  684. $dongtai = Db::name('topic_dongtai')->alias('td')->field('td.id,td.user_id,u.gender')
  685. ->join('user u','u.id = td.user_id','LEFT')
  686. ->where($dongtaiWhere)->find();
  687. $where['a.dt_id'] = $id;
  688. $gender = isset($dongtai['gender']) ? $dongtai['gender'] : 0;
  689. if ($gender == 1) {
  690. $where['user.gender'] = 0;
  691. } elseif ($gender == 0) {
  692. $where['user.gender'] = 1;
  693. }
  694. $list = Db::name('gift_user_dongtai')->alias('a')->field('a.user_id, count(a.id) count')
  695. ->join('user', 'a.user_id = user.id', 'left')
  696. ->where($where)->group('a.user_id')->order('a.id desc')->autopage()->select();
  697. if (!$list) {
  698. $this->success('success', $list);
  699. }
  700. $mt_user = Db::name('user');
  701. foreach ($list as &$v) {
  702. $user_info = $mt_user->field('nickname, avatar, gender, birthday')->where(['id' => $v['user_id']])->find();
  703. $v['nickname'] = $user_info['nickname'];
  704. $v['avatar'] = one_domain_image($user_info['avatar']);
  705. $v['birthday'] = birthtime_to_age($user_info['birthday']);
  706. }
  707. $this->success('success', $list);
  708. }
  709. //用户动态列表
  710. public function mydongtailist() {
  711. $user_id = input('user_id', $this->auth->id);
  712. if (!$user_id) {
  713. $this->error('您的网络开小差啦~');
  714. }
  715. $where['dt.user_id'] = $user_id;
  716. $where['dt.status'] = 0;
  717. $where['dt.auit_status'] = 1;
  718. $orderby = 'dt.id desc';
  719. $list = Db::name('topic_dongtai')->alias('dt')
  720. ->join('user','dt.user_id = user.id','LEFT')
  721. ->join('topic_hub th','dt.topic_id = th.id','LEFT')
  722. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.cityname,user.is_hideaddress,th.name,user.real_status')
  723. ->where($where)
  724. ->order($orderby)->autopage()->select();
  725. $list = list_domain_image($list,['images','avatar']);
  726. //追加是否点赞
  727. if(!empty($list)){
  728. $ids = array_column($list,'id');
  729. $map = [
  730. 'dt_id' => ['IN',$ids],
  731. 'user_id' => $this->auth->id,
  732. ];
  733. $good_list = Db::name('topic_dongtai_good')->where($map)->select();
  734. $mt_user_greet = Db::name('user_greet'); //是否打过招呼
  735. $mt_gift_user_dongtai = Db::name('gift_user_dongtai');
  736. $mt_user_wallet = Db::name('user_wallet'); //钱包
  737. $mt_wealth_level = Db::name('wealth_level'); //财富等级
  738. $mt_charm_level = Db::name('charm_level'); //魅力等级
  739. foreach ($list as &$val) {
  740. $val['birthday'] = birthtime_to_age($val['birthday']);
  741. $val['createtime'] = get_last_time($val['createtime']);
  742. $val['cityname'] = $val['is_hideaddress'] ? '' : $val['address'];
  743. //是否点过赞:0否 1是
  744. $val['isgood'] = 0;
  745. foreach($good_list as $k => $v){
  746. if($val['id'] == $v['dt_id']){
  747. $val['isgood'] = 1;
  748. }
  749. }
  750. //礼物数量
  751. $val['gift_count'] = $mt_gift_user_dongtai->where(['dt_id' => $val['id']])->count('id');
  752. //查询是否打过招呼
  753. $count = $mt_user_greet->where(['user_id' => $this->auth->id, 'user_to_id' => $val['user_id']])->count('id');
  754. if ($count) {
  755. $val['is_chat'] = 1; //是否打过招呼: 1是 0否
  756. } else {
  757. $val['is_chat'] = 0; //是否打过招呼: 1是 0否
  758. }
  759. //查询财富等级和魅力等级
  760. $wallet_info = $mt_user_wallet->where(['user_id' => $val['user_id']])->find();
  761. $wealth_level = $mt_wealth_level->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
  762. if ($wealth_level) {
  763. $val['wealth_level'] = $wealth_level['name'];
  764. } else {
  765. $val['wealth_level'] = '';
  766. }
  767. $charm_level = $mt_charm_level->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
  768. if ($charm_level) {
  769. $val['charm_level'] = $charm_level['name'];
  770. } else {
  771. $val['charm_level'] = '';
  772. }
  773. //创建视频缩略图
  774. $val['images_thumb'] = '';
  775. if ($val['type'] == 2) {
  776. $images_url = explode('.', $val['images']);
  777. unset($images_url[count($images_url) - 1]);
  778. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  779. }
  780. }
  781. }
  782. $this->success('success',$list);
  783. }
  784. //删除动态
  785. public function deldongtai() {
  786. $id = input('id', 0, 'intval');
  787. if (!$id) {
  788. $this->error('您的网络开小差啦~');
  789. }
  790. $info = Db::name('topic_dongtai')->find($id);
  791. if (!$info) {
  792. $this->error('您的网络开小差啦~');
  793. }
  794. if ($info['user_id'] != $this->auth->id) {
  795. $this->error('您的网络开小差啦~');
  796. }
  797. $rs = Db::name('topic_dongtai')->where(['id' => $id])->setField('status', 1);
  798. if (!$rs) {
  799. $this->error('您的网络开小差啦~');
  800. }
  801. $this->success('删除成功');
  802. }
  803. }