MerchantServerMiddleware.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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 app\common\repositories\store\staff\StaffsRepository;
  14. use think\exception\HttpResponseException;
  15. use think\Response;
  16. use app\Request;
  17. class MerchantServerMiddleware extends BaseMiddleware
  18. {
  19. public function before(Request $request)
  20. {
  21. $userInfo = $this->request->userInfo();
  22. $this->merId = $this->request->route('merId');
  23. /**
  24. * [reqire => true,auth => []], [reqire => true]
  25. */
  26. $serverParams = $this->getArg(0);//0 客服
  27. $staffsParams = $this->getArg(1);//1 员工
  28. if (empty($serverParams) && empty($staffsParams))
  29. throw new HttpResponseException(app('json')->fail('缺少角色标识'));
  30. if (!empty($serverParams)) {
  31. $service = $this->getServers($userInfo, $serverParams);
  32. if ($serverParams['reqire']){
  33. if (!$service) {
  34. throw new HttpResponseException(app('json')->fail('[1]您没有权限操作'));
  35. }
  36. $field = $serverParams['auth'] ? 'is_goods' : 'customer';
  37. if (!$service->{$field}) {
  38. throw new HttpResponseException(app('json')->fail('[2]您没有权限操作'));
  39. }
  40. }
  41. $request->macro('isServer', function () use (&$service) {
  42. return $service ? true : false;
  43. });
  44. $request->macro('serviceInfo', function () use (&$service) {
  45. return $service;
  46. });
  47. }
  48. if (!empty($staffsParams)) {
  49. $staffs = $this->getStaffs($userInfo);
  50. if ($staffsParams['reqire'] && !$staffs) {
  51. throw new HttpResponseException(app('json')->fail('[3]您没有权限操作'));
  52. }
  53. $request->macro('isStaffs', function () use (&$staffs) {
  54. return $staffs ? true : false;
  55. });
  56. $request->macro('staffsList', function () use (&$staffs) {
  57. return $staffs;
  58. });
  59. $request->macro('staffsIds', function () use (&$staffs) {
  60. $staffs_id = array_column($staffs,'staffs_id');
  61. return $staffs_id;
  62. });
  63. $request->macro('staffsMerIds', function () use (&$staffs) {
  64. return array_keys($staffs);
  65. });
  66. }
  67. if (!($service ?? false) && !($staffs ?? false))
  68. throw new HttpResponseException(app('json')->fail('[4]您没有权限操作'));
  69. }
  70. protected function getStaffs($userInfo)
  71. {
  72. $staffsRepository = app()->make(StaffsRepository::class);
  73. $staffs= $staffsRepository->getSearch(['uid' => $userInfo->uid, 'status' => 1])
  74. ->column('staffs_id,mer_id,uid,photo,name,phone,status','mer_id');
  75. return $staffs;
  76. }
  77. protected function getServers($userInfo, $params)
  78. {
  79. $merId = $this->request->route('merId',0);
  80. $storeServiceRepository = app()->make(StoreServiceRepository::class);
  81. $service = $storeServiceRepository->getService($userInfo->uid, $merId);
  82. if (!$service && $userInfo->main_uid) {
  83. $service = $storeServiceRepository->getService($userInfo->main_uid, $merId);
  84. }
  85. return $service;
  86. }
  87. public function after(Response $response)
  88. {
  89. // TODO: Implement after() method.
  90. }
  91. }