CheckBook.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Consts\ErrorConst;
  4. use App\Facade\Site;
  5. use App\Libs\Utils;
  6. use Closure;
  7. use App\Exceptions\ApiException;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Route;
  10. use Vinkla\Hashids\Facades\Hashids;
  11. class CheckBook
  12. {
  13. /**
  14. * @param $request
  15. * @param Closure $next
  16. * @return mixed
  17. * @throws ApiException
  18. */
  19. public function handle($request, Closure $next)
  20. {
  21. $params = $request->all();
  22. $cid = getProp($params, 'cid');
  23. $bid = getProp($params, 'bid');
  24. if ($bid) {
  25. if (mb_strlen($bid) === 32) {
  26. $bid = Hashids::decode($bid)[0];
  27. if (!$bid) { // 无法解密的是数据异常
  28. Utils::throwError(ErrorConst::DATA_EXCEPTION);
  29. }
  30. }
  31. $isOnShelf = (int)DB::table('book_configs')->where('bid', $bid)->value('is_on_shelf');
  32. if (!in_array($isOnShelf, [1, 2])) {
  33. Utils::throwError(ErrorConst::BOOK_NOT_EXIST);
  34. }
  35. }
  36. if (!$cid) return $next($request);
  37. if (mb_strlen($cid) === 32) {
  38. $cid = Hashids::decode($cid)[0];
  39. if (!$cid) { // 无法解密的是数据异常
  40. Utils::throwError(ErrorConst::DATA_EXCEPTION);
  41. }
  42. }
  43. // 获取章节信息
  44. $chapter_info = DB::table('chapters')->leftJoin('book_configs', 'chapters.bid', 'book_configs.bid')
  45. ->whereIn('book_configs.is_on_shelf', [1, 2])->where('chapters.id', $cid)
  46. ->select('chapters.is_vip', 'chapters.sequence')->first();
  47. if (!$chapter_info) {
  48. Utils::throwError(ErrorConst::DATA_EXCEPTION);
  49. }
  50. $uid = Site::getUid();
  51. // 获取站点信息
  52. $distribution_channel_id = Site::getChannelId();
  53. $distribution_channel_info = DB::table('distribution_channels')->where('id', $distribution_channel_id)->first();
  54. $channel_type = getProp($distribution_channel_info, 'channel_type');
  55. switch ($channel_type) {
  56. case 'PERIOD': // 时间周期(会员制)
  57. // 用户未登录不可看第10章以后的章节
  58. if (!$uid && getProp($chapter_info, 'sequence') > 10) {
  59. Utils::throwError(ErrorConst::NOT_LOGIN);
  60. }
  61. // 验证收费章节是否可以阅读
  62. if ($uid && getProp($chapter_info, 'is_vip')) {
  63. $vip_limit_date = DB::table('users')->where('id', $uid)->value('vip_limit_date');
  64. $vip_limit_date = transDate($vip_limit_date, 'Y-m-d');
  65. if (!$vip_limit_date || $vip_limit_date < date('Y-m-d')) {
  66. Utils::throwError(ErrorConst::VIP_VALID);
  67. }
  68. }
  69. break;
  70. case 'FREE': // 全免费
  71. //TODO 全免费站点无需检验章节内容
  72. break;
  73. case 'RECHARGE': // 充值
  74. //TODO 预留逻辑(后续开发充值类型的章节判断)
  75. break;
  76. default:
  77. break;
  78. }
  79. return $next($request);
  80. }
  81. }