CheckTokenTrait.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Consts\ErrorConst;
  4. use App\Libs\Utils;
  5. use App\Models\Channel\Channel;
  6. use App\Models\Channel\ChannelUser;
  7. trait CheckTokenTrait
  8. {
  9. public function checkTokenTrait($token, $channelId)
  10. {
  11. // 获取用户信息
  12. $user = ChannelUser::where('token', $token)->first();
  13. $uid = (int)getProp($user, 'id');
  14. if (!$uid) {
  15. Utils::throwError(ErrorConst::NOT_LOGIN);
  16. }
  17. // 将数据绑定到全局
  18. $site = app('siteData');
  19. $site->uid = $uid;
  20. $site->account = getProp($user, 'account');
  21. $site->phone = getProp($user, 'phone');
  22. $site->nickname = getProp($user, 'nickname');
  23. $site->token = $token;
  24. // 获取当前站点id
  25. $channel = Channel::getById($channelId);
  26. $channelUserId = (int)getProp($channel, 'channel_user_id');
  27. // 判断站点权限
  28. if ($uid != $channelUserId) {
  29. Utils::throwError(ErrorConst::USER_NO_ACCESS_CHANNEL);
  30. }
  31. $site->current_channel_id = $channelId;
  32. $site->current_channel_name = getProp($channel, 'name');
  33. }
  34. }