Userfollow.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 关注
  7. */
  8. class Userfollow extends Api
  9. {
  10. //
  11. // 无需登录的接口,*表示全部
  12. protected $noNeedLogin = [];
  13. // 无需鉴权的接口,*表示全部
  14. protected $noNeedRight = ['*'];
  15. public function follow(){
  16. $to_user_id = input('to_user_id',0);
  17. $where = ['user_id' => $this->auth->id, 'to_user_id' => $to_user_id];
  18. $follow_info = Db::name('user_follow')->where($where)->find();
  19. if($follow_info){
  20. Db::name('user_follow')->where($where)->delete();
  21. $this->success('取关完成');
  22. }else{
  23. Db::name('user_follow')->insertGetId($where);
  24. $this->success('关注完成');
  25. }
  26. }
  27. /**
  28. * 获取关注列表
  29. */
  30. public function getFollowList() {
  31. $page = $this->request->request('page',1); // 分页
  32. $pageNum = $this->request->request('pageNum',10); // 分页
  33. $where = [];
  34. $where["a.user_id"] = $this->auth->id;
  35. $list = model('user_follow')->alias("a")
  36. ->field("u.id,u.nickname,u.avatar,u.age,u.constellation,u.wechat,hobby_ids,profession,u.copy_mobile,u.mobile,u.gender")
  37. ->join("hx_user u","u.id = a.to_user_id","left")
  38. ->where($where)
  39. ->page($page,$pageNum)
  40. ->select();
  41. if($list) {
  42. $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
  43. wordwrap(config('public_key'), 64, PHP_EOL, true) .
  44. PHP_EOL."-----END PUBLIC KEY-----";
  45. foreach($list as $k => &$v) {
  46. if ($v['wechat']) {
  47. $wechat = "";
  48. openssl_public_encrypt($v['wechat'], $wechat, $public_key);
  49. $v['wechat'] = base64_encode($wechat);
  50. } else {
  51. $v['wechat'] = '';
  52. }
  53. $mobile = "";
  54. // openssl_private_encrypt($data['mobile'], $mobile, $private_key); // 使用私钥加密数据
  55. openssl_public_encrypt($v['mobile'], $mobile, $public_key);
  56. $v['mobile'] = base64_encode($mobile);
  57. if($v['hobby_ids']) {
  58. $list[$k]['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($v['hobby_ids']);
  59. } else {
  60. $list[$k]['hobby_ids'] = [];
  61. }
  62. //
  63. $v['is_follow'] = 1;
  64. }
  65. }
  66. $this->success('success',$list);
  67. }
  68. /**
  69. * 获取粉丝列表
  70. */
  71. public function getFansList() {
  72. $page = $this->request->request('page',1); // 分页
  73. $pageNum = $this->request->request('pageNum',10); // 分页
  74. $where = [];
  75. $where["a.to_user_id"] = $this->auth->id;
  76. $list = model('user_follow')->alias("a")
  77. ->field("u.id,u.nickname,u.avatar,u.age,u.constellation,u.wechat,hobby_ids,profession,u.copy_mobile,u.mobile,u.gender")
  78. ->join("hx_user u","u.id = a.user_id","left")
  79. ->where($where)
  80. ->page($page,$pageNum)
  81. ->select();
  82. if($list) {
  83. $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
  84. wordwrap(config('public_key'), 64, PHP_EOL, true) .
  85. PHP_EOL."-----END PUBLIC KEY-----";
  86. foreach($list as $k => &$v) {
  87. if ($v['wechat']) {
  88. $wechat = "";
  89. openssl_public_encrypt($v['wechat'], $wechat, $public_key);
  90. $v['wechat'] = base64_encode($wechat);
  91. } else {
  92. $v['wechat'] = '';
  93. }
  94. $mobile = "";
  95. // openssl_private_encrypt($data['mobile'], $mobile, $private_key); // 使用私钥加密数据
  96. openssl_public_encrypt($v['mobile'], $mobile, $public_key);
  97. $v['mobile'] = base64_encode($mobile);
  98. if($v['hobby_ids']) {
  99. $list[$k]['hobby_ids'] = \app\common\model\Hobby::getHobbyNames($v['hobby_ids']);
  100. } else {
  101. $list[$k]['hobby_ids'] = [];
  102. }
  103. //
  104. $where = ['user_id' => $this->auth->id, 'to_user_id' => $v['id']];
  105. $follow_info = Db::name('user_follow')->where($where)->find();
  106. $v['is_follow'] = $follow_info ? 1 : 0;
  107. }
  108. }
  109. $this->success('success',$list);
  110. }
  111. }