Topicdongtai.php 34 KB

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