Userblack.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 黑名单
  7. */
  8. class Userblack extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //拉黑我的人列表
  13. public function black_me_list(){
  14. $list = Db::name('user_black')
  15. ->alias('black')
  16. ->join('user','black.uid = user.id','LEFT')
  17. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  18. ->field('user.id,user.nickname,user.avatar,user.bio,user.birthday,user.gender,ur.nickname_remark')
  19. ->where('black.black_uid',$this->auth->id)->order('black.id desc')->autopage()->select();
  20. $list = list_domain_image($list,['avatar']);
  21. $list = list_birthday_age($list);
  22. foreach ($list as &$val) {
  23. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  24. }
  25. $this->success('success',$list);
  26. }
  27. //我拉黑的人列表
  28. public function my_black_list(){
  29. $list = Db::name('user_black')
  30. ->alias('black')
  31. ->join('user','black.black_uid = user.id','LEFT')
  32. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  33. // ->field('user.id,user.nickname,user.avatar,user.bio,user.birthday,user.gender')
  34. ->field('black.createtime,user.id,user.nickname,user.avatar,ur.nickname_remark')
  35. ->where('black.uid',$this->auth->id)->order('black.id desc')->autopage()->select();
  36. if (!$list) {
  37. $this->success('success', $list);
  38. }
  39. $list = list_domain_image($list,['avatar']);
  40. // $list = list_birthday_age($list);
  41. foreach ($list as &$val) {
  42. $val['createtime'] = date('Y-m-d H:i', $val['createtime']);
  43. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  44. }
  45. $this->success('success',$list);
  46. }
  47. //拉黑某人
  48. public function black_one(){
  49. $black_uid = input('black_uid',0);
  50. if(!$black_uid){
  51. $this->error(__('Invalid parameters'));
  52. }
  53. $checkuser = Db::name('user')->find($black_uid);
  54. if(empty($checkuser)){
  55. $this->error('此用户不存在');
  56. }
  57. if($black_uid == $this->auth->id){
  58. $this->error('不能拉黑自己');
  59. }
  60. $map = [
  61. 'uid' => $this->auth->id,
  62. 'black_uid' => $black_uid,
  63. ];
  64. $check = Db::name('user_black')->where($map)->find();
  65. if($check){
  66. $this->error('已经拉黑了此人');
  67. }
  68. //开启事务
  69. Db::startTrans();
  70. $map['createtime'] = time();
  71. $id = Db::name('user_black')->insertGetId($map);
  72. if (!$id) {
  73. Db::rollback();
  74. $this->error('您的网络开小差了~');
  75. }
  76. $tenim = new \app\api\controller\Tenim;
  77. $result = $tenim->black_one($this->auth->id, $black_uid);
  78. if ($result['ErrorCode'] != 0 || $result['ResultItem'][0]['ResultCode'] != 0) {
  79. Db::rollback();
  80. $this->error('您的网络开小差了~');
  81. }
  82. //系统消息
  83. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'拉黑','您已将用户' . $checkuser['nickname'] . '拉入黑名单');
  84. Db::commit();
  85. $this->success('操作成功',$id);
  86. }
  87. //取消拉黑某人
  88. public function un_black_one(){
  89. $black_uid = input('black_uid',0);
  90. if(!$black_uid){
  91. $this->error(__('Invalid parameters'));
  92. }
  93. $map = [
  94. 'uid' => $this->auth->id,
  95. 'black_uid' => $black_uid,
  96. ];
  97. //不检查,全删
  98. Db::startTrans();
  99. $rs = Db::name('user_black')->where($map)->delete();
  100. if ($rs === false) {
  101. Db::rollback();
  102. $this->error('您的网络开小差了~');
  103. }
  104. if ($rs) {
  105. $tenim = new \app\api\controller\Tenim;
  106. $result = $tenim->un_black_one($this->auth->id, $black_uid);
  107. if ($result['ErrorCode'] != 0 || $result['ResultItem'][0]['ResultCode'] != 0) {
  108. Db::rollback();
  109. $this->error('您的网络开小差了~');
  110. }
  111. }
  112. Db::commit();
  113. $this->success('操作成功');
  114. }
  115. }