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