ApiKeyService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\ActionLogModel;
  13. use App\Models\ApiKeyModel;
  14. use App\Models\MemberModel;
  15. use App\Services\BaseService;
  16. use App\Services\RedisService;
  17. /**
  18. * 接口账号管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Common
  22. */
  23. class ApiKeyService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new ApiKeyModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 获取接口配置
  49. * @param int $userId
  50. * @return array|false|mixed
  51. */
  52. public function getConfig($userId=0)
  53. {
  54. $cacheKey = "caches:apiKeys:user_{$userId}";
  55. $config = RedisService::get($cacheKey);
  56. if(empty($config)){
  57. $keyId = 0;
  58. if($userId){
  59. $keyId = MemberModel::where(['id'=>$userId])->value('api_key_id');
  60. }
  61. $config = $this->model->where(['mark'=>1])
  62. ->where(function($query) use($keyId){
  63. if($keyId){
  64. $query->where(['status'=>1,'id'=> $keyId]);
  65. }else{
  66. $query->where(['status'=>1]);
  67. }
  68. })
  69. ->select(['id','api_key','api_secret','passphrase','status'])
  70. ->first();
  71. $config = $config? $config->toArray() : [];
  72. if($config){
  73. RedisService::set($cacheKey, $config, rand(300, 600));
  74. }
  75. }
  76. return $config;
  77. }
  78. /**
  79. * 添加会编辑
  80. * @return array
  81. * @since 2020/11/11
  82. * @author laravel开发员
  83. */
  84. public function edit()
  85. {
  86. // 请求参数
  87. $data = request()->all();
  88. ActionLogModel::setTitle("修改OK接口账号信息");
  89. ActionLogModel::record();
  90. return parent::edit($data); // TODO: Change the autogenerated stub
  91. }
  92. }