GrantedAuth.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace app\model;
  3. use think\facade\Cache;
  4. use think\Model;
  5. class GrantedAuth extends Model
  6. {
  7. // 权限组
  8. protected $json = ['auth'];
  9. /**
  10. * 权限来源
  11. */
  12. public function from()
  13. {
  14. return $this->belongsTo(Org::class, 'from', 'id');
  15. }
  16. /**
  17. * 权限授予
  18. */
  19. public function to()
  20. {
  21. return $this->belongsTo(Org::class, 'to', 'id');
  22. }
  23. /**
  24. * 授权权限获取
  25. */
  26. public function getIdsByAuth($auth, $to)
  27. {
  28. // 从缓存中获取授权企业id
  29. $key = $auth.'-'.$to;
  30. $data = Cache::get($key);
  31. if($data !== null){
  32. $data = json_decode($data);
  33. }
  34. // 如果缓存没有则进行查询
  35. $condition = [
  36. // ['auth->'.$auth, '=', true],
  37. ['to', '=', $to]
  38. ];
  39. $data = self::where($condition)->column('from');
  40. // 将查询结果放入到缓存中,并添加标签
  41. Cache::tag('auth_to_'.$to)->set($auth.'-'.$to, json_encode($data), 3600);
  42. return $data;
  43. }
  44. }