BaseService.php 963 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Base\Services;
  3. use App\Base\Exceptions\ApiException;
  4. use Illuminate\Support\Facades\Redis;
  5. class BaseService extends AbstractBaseService
  6. {
  7. use AuthUser;
  8. /**
  9. * 获取并发锁
  10. * @param $lockKey
  11. * @param $expire
  12. * @return bool
  13. */
  14. function getLock($lockKey, $expire = 3)
  15. {
  16. $redis = Redis::connection()->client();
  17. if (!$redis->setnx($lockKey, 1)) {
  18. return false;
  19. }
  20. $redis->expire($lockKey, $expire);
  21. return true;
  22. }
  23. /**
  24. * 释放锁
  25. * @param $lockKey
  26. * @return mixed
  27. */
  28. function releaseLock($lockKey)
  29. {
  30. $redis = Redis::connection()->client();
  31. return $redis->del($lockKey);
  32. }
  33. /**
  34. * 没有操作权限
  35. * @throws ApiException
  36. */
  37. function throwNoPermission()
  38. {
  39. throw new ApiException('common.no_operator_permission', '您没有权限操作此功能');
  40. }
  41. }