| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use think\facade\Db;
- //公共控制器
- class Common extends BaseController
- {
- protected $noNeedLogin = ['statisticInfo', 'getUserProfile', 'visit', 'viewFriendList'];
-
- //虚假初始会员数量
- private $falseUserCount = 1000;
-
- //获取首页统计信息
- public function statisticInfo()
- {
- //今日新上架游戏商品数
- $todayGameCount = Db::table('tb_game')
- ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00'))
- ->count();
- //昨日上架游戏商品数
- $yesterdayGameCount = Db::table('tb_game')
- ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00') - 60 * 60 * 24)
- ->where('created_at', '<', strtotime(date('Y-m-d') . ' 00:00:00'))
- ->count();
- //全部上架游戏商品数
- $allGameCount = Db::table('tb_game')->count();
- //会员数量
- $userCount = Db::table('tb_user')->count() + $this->falseUserCount;
- //欢迎新会员
- $user = Db::table('tb_user')
- ->order('created_at DESC')
- ->find();
- $this->success('success', [
- 'todayGameCount' => $todayGameCount, //今日新上架游戏数量
- 'yesterdayGameCount' => $yesterdayGameCount, //昨日上架游戏数量
- 'allGameCount' => $allGameCount, //所有游戏数量
- 'userCount' => $userCount, //总用户数量
- 'newUser' => [ //新用户信息
- 'userId' => !isset($user['id']) ? null : $user['id'], //用户ID,如果站点目前没人注册会员返回null
- 'username' => !isset($user['username']) ? null : $user['username'], //用户名,如果站点目前没人注册会员返回null
- ],
- ]);
- }
-
- //获取用户资料
- public function getUserProfile()
- {
- $userId = $this->request->post('userId'); //用户ID
- if (!is_numeric($userId)) {
- $this->fail(500, 'userId参数校验错误');
- }
- $user = Db::table('tb_user')
- ->where(['id' => $userId])
- ->find();
- if (!$user) {
- $this->fail(501, '找不到userId对应的用户资料');
- }
- //解析获取头像url
- if (str_starts_with($user['avatar'], 'system://')) {
- $data = explode('//', $user['avatar']);
- $avatarId = intval($data[1]);
- $avatar = Db::table('tb_system_avatar')
- ->where(['id' => $avatarId])
- ->find();
- $user['avatar'] = $avatar['image_url'];
- }
- //获取好友数量
- $friendCount = Db::table('tb_friend')
- ->where(['user_id' => $userId])
- ->where(['state' => 1]) //状态必须是已通过
- ->count();
- unset($user['password']);
- unset($user['balance']);
- //好友数量
- $user['friend_count'] = $friendCount;
- $userGroupList = Db::table('tb_user_group')
- ->order('exp DESC')
- ->select();
- $userGroupName = '新手上路';
- foreach ($userGroupList as $userGroup) {
- if ($user['exp'] >= $userGroup['exp']) {
- $userGroupName = $userGroup['name'];
- break;
- }
- }
- $user['user_group'] = $userGroupName;
- $user['theme_count'] = Db::table('tb_thread')->where('user_id', $userId)->count();
- $user['reply_count'] = Db::table('tb_thread_reply')->where('user_id', $userId)->count();
- $user['user_score'] = Db::table('tb_user')->where('id', $userId)->value('score');
- $this->success('success', $user);
- }
-
- //查看好友列表
- public function viewFriendList()
- {
- $userId = $this->request->post('userId'); //用户ID
- if (!is_numeric($userId)) {
- $this->fail(500, 'userId参数校验错误');
- }
- $map1 = ['user_id', '=', $userId];
- $map2 = ['friend_id', '=', $userId];
- $friends = Db::table('tb_friend')
- ->where(['state' => 1]) //状态必须是已通过
- ->where(function ($query) use($map1, $map2) {
- $query->whereOr([$map1, $map2]);
- })
- ->select();
- $friendsData = [];
- foreach ($friends as $friend) {
- $friendId = 0;
- //好友是双向的,判断是否当前userId
- if ($friend['user_id'] == $userId) {
- $friendId = $friend['friend_id'];
- } else {
- $friendId = $friend['user_id'];
- }
- $user = Db::table('tb_user')
- ->where(['id' => $friendId])
- ->find();
- unset($user['password']);
- unset($user['balance']);
- $friendsData[] = $user;
- }
- $this->success('success', [
- 'friendCount' => count($friendsData), //好友列表数量
- 'data' => $friendsData, //返回所有好友的user数据
- ]);
- }
-
- //创建网站访问记录
- public function visit()
- {
- //如果有登录需要POST方式传递token
- $token = $this->request->post('token');
- $userToken = Db::table('tb_user_token')
- ->where(['token' => $token])
- ->where('expired_at', '<', time())
- ->find();
- Db::table('tb_website_visit')
- ->insert([
- 'ip_address' => $this->request->ip(),
- 'is_user' => !is_null($userToken) && !empty($token) ? 1 : 0, //是否注册用户1:是 0:否
- 'user_id' => !is_null($userToken) && !empty($token) ? $userToken['user_id'] : 0, //如果是注册用户写用户ID,非注册用户写0
- 'user_agent' => $this->request->header('user-agent'),
- 'created_at' => time(),
- ]);
- $this->success('success', null);
- }
- }
|