MerchantServerMiddleware.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\middleware;
  12. use app\common\repositories\store\service\StoreServiceRepository;
  13. use think\exception\HttpResponseException;
  14. use app\Request;
  15. use think\Response;
  16. use Throwable;
  17. class MerchantServerMiddleware extends BaseMiddleware
  18. {
  19. public function before(Request $request)
  20. {
  21. $this->merId = $this->request->route('merId');
  22. $type = $this->getArg(0);
  23. $field = 'customer';
  24. switch ($type) {
  25. case 0:
  26. $field = 'customer';
  27. break;
  28. case 1:
  29. $field = 'is_goods';
  30. break;
  31. }
  32. $userInfo = $this->request->userInfo();
  33. $service = app()->make(StoreServiceRepository::class)->getService($userInfo->uid, $this->merId);
  34. if (!$service && $userInfo->main_uid) {
  35. $service = app()->make(StoreServiceRepository::class)->getService($userInfo->main_uid, $this->merId);
  36. }
  37. if (!$service || !$service->$field) {
  38. throw new HttpResponseException(app('json')->fail('您没有权限操作'));
  39. }
  40. $request->macro('serviceInfo', function () use (&$service) {
  41. return $service;
  42. });
  43. }
  44. public function after(Response $response)
  45. {
  46. // TODO: Implement after() method.
  47. }
  48. }