Common.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. //公共控制器
  6. class Common extends BaseController
  7. {
  8. protected $noNeedLogin = ['statisticInfo', 'getUserProfile', 'visit', 'viewFriendList'];
  9. //虚假初始会员数量
  10. private $falseUserCount = 1000;
  11. //获取首页统计信息
  12. public function statisticInfo()
  13. {
  14. //今日新上架游戏商品数
  15. $todayGameCount = Db::table('tb_game')
  16. ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00'))
  17. ->count();
  18. //昨日上架游戏商品数
  19. $yesterdayGameCount = Db::table('tb_game')
  20. ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00') - 60 * 60 * 24)
  21. ->where('created_at', '<', strtotime(date('Y-m-d') . ' 00:00:00'))
  22. ->count();
  23. //全部上架游戏商品数
  24. $allGameCount = Db::table('tb_game')->count();
  25. //会员数量
  26. $userCount = Db::table('tb_user')->count() + $this->falseUserCount;
  27. //欢迎新会员
  28. $user = Db::table('tb_user')
  29. ->order('created_at DESC')
  30. ->find();
  31. $this->success('success', [
  32. 'todayGameCount' => $todayGameCount, //今日新上架游戏数量
  33. 'yesterdayGameCount' => $yesterdayGameCount, //昨日上架游戏数量
  34. 'allGameCount' => $allGameCount, //所有游戏数量
  35. 'userCount' => $userCount, //总用户数量
  36. 'newUser' => [ //新用户信息
  37. 'userId' => !isset($user['id']) ? null : $user['id'], //用户ID,如果站点目前没人注册会员返回null
  38. 'username' => !isset($user['username']) ? null : $user['username'], //用户名,如果站点目前没人注册会员返回null
  39. ],
  40. ]);
  41. }
  42. //获取用户资料
  43. public function getUserProfile()
  44. {
  45. $userId = $this->request->post('userId'); //用户ID
  46. if (!is_numeric($userId)) {
  47. $this->fail(500, 'userId参数校验错误');
  48. }
  49. $user = Db::table('tb_user')
  50. ->where(['id' => $userId])
  51. ->find();
  52. if (!$user) {
  53. $this->fail(501, '找不到userId对应的用户资料');
  54. }
  55. //解析获取头像url
  56. if (str_starts_with($user['avatar'], 'system://')) {
  57. $data = explode('//', $user['avatar']);
  58. $avatarId = intval($data[1]);
  59. $avatar = Db::table('tb_system_avatar')
  60. ->where(['id' => $avatarId])
  61. ->find();
  62. $user['avatar'] = $avatar['image_url'];
  63. }
  64. //获取好友数量
  65. $friendCount = Db::table('tb_friend')
  66. ->where(['user_id' => $userId])
  67. ->where(['state' => 1]) //状态必须是已通过
  68. ->count();
  69. unset($user['password']);
  70. unset($user['balance']);
  71. //好友数量
  72. $user['friend_count'] = $friendCount;
  73. $userGroupList = Db::table('tb_user_group')
  74. ->order('exp DESC')
  75. ->select();
  76. $userGroupName = '新手上路';
  77. foreach ($userGroupList as $userGroup) {
  78. if ($user['exp'] >= $userGroup['exp']) {
  79. $userGroupName = $userGroup['name'];
  80. break;
  81. }
  82. }
  83. $user['user_group'] = $userGroupName;
  84. $user['theme_count'] = Db::table('tb_thread')->where('user_id', $userId)->count();
  85. $user['reply_count'] = Db::table('tb_thread_reply')->where('user_id', $userId)->count();
  86. $user['user_score'] = Db::table('tb_user')->where('id', $userId)->value('score');
  87. $this->success('success', $user);
  88. }
  89. //查看好友列表
  90. public function viewFriendList()
  91. {
  92. $userId = $this->request->post('userId'); //用户ID
  93. if (!is_numeric($userId)) {
  94. $this->fail(500, 'userId参数校验错误');
  95. }
  96. $map1 = ['user_id', '=', $userId];
  97. $map2 = ['friend_id', '=', $userId];
  98. $friends = Db::table('tb_friend')
  99. ->where(['state' => 1]) //状态必须是已通过
  100. ->where(function ($query) use($map1, $map2) {
  101. $query->whereOr([$map1, $map2]);
  102. })
  103. ->select();
  104. $friendsData = [];
  105. foreach ($friends as $friend) {
  106. $friendId = 0;
  107. //好友是双向的,判断是否当前userId
  108. if ($friend['user_id'] == $userId) {
  109. $friendId = $friend['friend_id'];
  110. } else {
  111. $friendId = $friend['user_id'];
  112. }
  113. $user = Db::table('tb_user')
  114. ->where(['id' => $friendId])
  115. ->find();
  116. unset($user['password']);
  117. unset($user['balance']);
  118. $friendsData[] = $user;
  119. }
  120. $this->success('success', [
  121. 'friendCount' => count($friendsData), //好友列表数量
  122. 'data' => $friendsData, //返回所有好友的user数据
  123. ]);
  124. }
  125. //创建网站访问记录
  126. public function visit()
  127. {
  128. //如果有登录需要POST方式传递token
  129. $token = $this->request->post('token');
  130. $userToken = Db::table('tb_user_token')
  131. ->where(['token' => $token])
  132. ->where('expired_at', '<', time())
  133. ->find();
  134. Db::table('tb_website_visit')
  135. ->insert([
  136. 'ip_address' => $this->request->ip(),
  137. 'is_user' => !is_null($userToken) && !empty($token) ? 1 : 0, //是否注册用户1:是 0:否
  138. 'user_id' => !is_null($userToken) && !empty($token) ? $userToken['user_id'] : 0, //如果是注册用户写用户ID,非注册用户写0
  139. 'user_agent' => $this->request->header('user-agent'),
  140. 'created_at' => time(),
  141. ]);
  142. $this->success('success', null);
  143. }
  144. }