ApiService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $list = $this->model->from('apis as a')
  53. ->where(['mark'=> 1])
  54. ->where(function ($query) use($params){
  55. $keyword = isset($params['username'])? $params['username'] : '';
  56. if($keyword){
  57. $query->where('a.username','like',"%{$keyword}%")->orWhere('m.account','like',"%{$keyword}%");
  58. }
  59. $status = isset($params['status'])? $params['status'] : 0;
  60. if($status>0){
  61. $query->where('a.status','=', $status);
  62. }
  63. })
  64. ->select(['a.*'])
  65. ->orderBy('a.create_time','desc')
  66. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  67. $list = $list? $list->toArray() :[];
  68. if($list){
  69. $date = isset($params['date']) ? $params['date'] : [];
  70. foreach($list['data'] as &$item){
  71. $item['buy_total'] = TradeOrderService::make()->getTotalByApiAndDate($item['id'], 1, $date);
  72. $item['sell_total'] = TradeOrderService::make()->getTotalByApiAndDate($item['id'],2, $date);
  73. $item['user_count'] = MemberService::make()->getCountByApi($item['id']);
  74. // 权限
  75. if (isset($item['user_limits'])) {
  76. $item['user_limits'] = $item['user_limits']? explode(',', $item['user_limits']) : [0];
  77. }
  78. // 有效期
  79. if ($item['expired_at']) {
  80. $item['expired_at'] = $item['expired_at'] > date('Y-m-d H:i:s')? $item['expired_at'] : '';
  81. }
  82. }
  83. }
  84. return [
  85. 'pageSize'=> $pageSize,
  86. 'total'=>isset($list['total'])? $list['total'] : 0,
  87. 'list'=> isset($list['data'])? $list['data'] : []
  88. ];
  89. }
  90. /**
  91. * 添加或编辑
  92. * @return array
  93. */
  94. public function saveData($adminId, $data)
  95. {
  96. $data['admin_id'] = $adminId;
  97. // 权限
  98. if ($data['user_limits']) {
  99. $data['user_limits'] = array_filter($data['user_limits']);
  100. $data['user_limits'] = $data['user_limits']? implode(',', $data['user_limits']) : '';
  101. }
  102. // 结束时间
  103. $id = isset($data['id'])? $data['id'] : 0;
  104. if (!$id) {
  105. $data['api_key'] = getCode('Ti', 12);
  106. }
  107. if($data['expired_at'] <= date('Y-m-d')){
  108. return returnJson(2028, false);
  109. }
  110. if($this->model->where(['username'=> $data['username']])->whereNotIn('id',[$id])->value('id')){
  111. return returnJson(2005, false);
  112. }
  113. return parent::edit($data); // TODO: Change the autogenerated stub
  114. }
  115. /**
  116. * 验证接口
  117. * @param $apiKey
  118. * @return false
  119. */
  120. public function checkApi($apiKey)
  121. {
  122. if(empty($apiKey)){
  123. $this->error = '6001';
  124. return false;
  125. }
  126. $cachekey = "apis:apiKeys:{$apiKey}";
  127. $info = RedisService::get($cachekey);
  128. if(empty($info)){
  129. $info = ApiModel::where(['api_key'=> $apiKey,'status'=> 1,'mark'=>1])
  130. ->select(['id','username','user_limits','expired_at'])
  131. ->first();
  132. if(empty($info)){
  133. $this->error = '6002';
  134. return false;
  135. }
  136. // 接口是否已到期
  137. $expiredAt = isset($info['expired_at']) && $info['expired_at']? strtotime($info['expired_at']) : 0;
  138. if($expiredAt && $expiredAt<= time()){
  139. $this->error = '6003';
  140. return false;
  141. }
  142. }
  143. return $info;
  144. }
  145. /**
  146. * 获取详情
  147. * @param $id
  148. * @return mixed
  149. */
  150. public function getInfo($id, $refresh = false)
  151. {
  152. $cachekey = "caches:apis:info:{$id}";
  153. $info = RedisService::get($cachekey);
  154. if($info && !$refresh){
  155. return $info;
  156. }
  157. $info = ApiModel::where(['id'=> $id,'status'=> 1,'mark'=>1])
  158. ->select(['id','username','notify_url','account','user_limits','expired_at'])
  159. ->first();
  160. if($info){
  161. $info['user_limits'] = $info['user_limits']? explode(',', $info['user_limits']) : [];
  162. RedisService::set($cachekey, $info, 30);
  163. }
  164. return $info;
  165. }
  166. }