ApiService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\AdModel;
  13. use App\Models\ApiModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 接口管理-服务类
  18. * Class ApiService
  19. * @package App\Services\Common
  20. */
  21. class ApiService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * ApiService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new ApiModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return static|null
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = (new static());
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * 添加或编辑
  46. * @return array
  47. */
  48. public function saveData($adminId, $data)
  49. {
  50. $data['admin_id'] = $adminId;
  51. // 权限
  52. if ($data['user_limits']) {
  53. $data['user_limits'] = array_filter($data['user_limits']);
  54. $data['user_limits'] = $data['user_limits']? implode(',', $data['user_limits']) : '';
  55. }
  56. // 结束时间
  57. $id = isset($data['id'])? $data['id'] : 0;
  58. if (!$id) {
  59. $data['api_key'] = getCode('Ti', 12);
  60. }
  61. if($data['expired_at'] <= date('Y-m-d')){
  62. return returnJson(2028, false);
  63. }
  64. if($this->model->where(['username'=> $data['username']])->whereNotIn('id',[$id])->value('id')){
  65. return returnJson(2005, false);
  66. }
  67. return parent::edit($data); // TODO: Change the autogenerated stub
  68. }
  69. /**
  70. * 验证接口
  71. * @param $apiKey
  72. * @return false
  73. */
  74. public function checkApi($apiKey)
  75. {
  76. if(empty($apiKey)){
  77. $this->error = '6001';
  78. return false;
  79. }
  80. $cachekey = "apis:apiKeys:{$apiKey}";
  81. $info = RedisService::get($cachekey);
  82. if(empty($info)){
  83. $info = ApiModel::where(['api_key'=> $apiKey,'status'=> 1,'mark'=>1])
  84. ->select(['id','username','user_limits','expired_at'])
  85. ->first();
  86. if(empty($info)){
  87. $this->error = '6002';
  88. return false;
  89. }
  90. // 接口是否已到期
  91. $expiredAt = isset($info['expired_at']) && $info['expired_at']? strtotime($info['expired_at']) : 0;
  92. if($expiredAt && $expiredAt<= time()){
  93. $this->error = '6003';
  94. return false;
  95. }
  96. }
  97. return $info;
  98. }
  99. /**
  100. * 获取详情
  101. * @param $id
  102. * @return mixed
  103. */
  104. public function getInfo($id, $refresh = false)
  105. {
  106. $cachekey = "caches:apis:info:{$id}";
  107. $info = RedisService::get($cachekey);
  108. if($info && !$refresh){
  109. return $info;
  110. }
  111. $info = ApiModel::where(['id'=> $id,'status'=> 1,'mark'=>1])
  112. ->select(['id','username','notify_url','user_limits','expired_at'])
  113. ->first();
  114. if($info){
  115. $info['user_limits'] = $info['user_limits']? explode(',', $info['user_limits']) : [];
  116. RedisService::set($cachekey, $info, 30);
  117. }
  118. return $info;
  119. }
  120. }