| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\AdModel;
- use App\Models\ApiModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 接口管理-服务类
- * Class ApiService
- * @package App\Services\Common
- */
- class ApiService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * ApiService constructor.
- */
- public function __construct()
- {
- $this->model = new ApiModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 获取数据
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $list = $this->model->from('apis as a')
- ->where(['mark'=> 1])
- ->where(function ($query) use($params){
- $keyword = isset($params['username'])? $params['username'] : '';
- if($keyword){
- $query->where('a.username','like',"%{$keyword}%")->orWhere('m.account','like',"%{$keyword}%");
- }
- $status = isset($params['status'])? $params['status'] : 0;
- if($status>0){
- $query->where('a.status','=', $status);
- }
- })
- ->select(['a.*'])
- ->orderBy('a.create_time','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- $date = isset($params['date']) ? $params['date'] : [];
- foreach($list['data'] as &$item){
- $item['buy_total'] = TradeOrderService::make()->getTotalByApiAndDate($item['id'], 1, $date);
- $item['sell_total'] = TradeOrderService::make()->getTotalByApiAndDate($item['id'],2, $date);
- $item['user_count'] = MemberService::make()->getCountByApi($item['id']);
- // 权限
- if (isset($item['user_limits'])) {
- $item['user_limits'] = $item['user_limits']? explode(',', $item['user_limits']) : [0];
- }
- // 有效期
- if ($item['expired_at']) {
- $item['expired_at'] = $item['expired_at'] > date('Y-m-d H:i:s')? $item['expired_at'] : '';
- }
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 添加或编辑
- * @return array
- */
- public function saveData($adminId, $data)
- {
- $data['admin_id'] = $adminId;
- // 权限
- if ($data['user_limits']) {
- $data['user_limits'] = array_filter($data['user_limits']);
- $data['user_limits'] = $data['user_limits']? implode(',', $data['user_limits']) : '';
- }
- // 结束时间
- $id = isset($data['id'])? $data['id'] : 0;
- if (!$id) {
- $data['api_key'] = getCode('Ti', 12);
- }
- if($data['expired_at'] <= date('Y-m-d')){
- return returnJson(2028, false);
- }
- if($this->model->where(['username'=> $data['username']])->whereNotIn('id',[$id])->value('id')){
- return returnJson(2005, false);
- }
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 验证接口
- * @param $apiKey
- * @return false
- */
- public function checkApi($apiKey)
- {
- if(empty($apiKey)){
- $this->error = '6001';
- return false;
- }
- $cachekey = "apis:apiKeys:{$apiKey}";
- $info = RedisService::get($cachekey);
- if(empty($info)){
- $info = ApiModel::where(['api_key'=> $apiKey,'status'=> 1,'mark'=>1])
- ->select(['id','username','user_limits','expired_at'])
- ->first();
- if(empty($info)){
- $this->error = '6002';
- return false;
- }
- // 接口是否已到期
- $expiredAt = isset($info['expired_at']) && $info['expired_at']? strtotime($info['expired_at']) : 0;
- if($expiredAt && $expiredAt<= time()){
- $this->error = '6003';
- return false;
- }
- }
- return $info;
- }
- /**
- * 获取详情
- * @param $id
- * @return mixed
- */
- public function getInfo($id, $refresh = false)
- {
- $cachekey = "caches:apis:info:{$id}";
- $info = RedisService::get($cachekey);
- if($info && !$refresh){
- return $info;
- }
- $info = ApiModel::where(['id'=> $id,'status'=> 1,'mark'=>1])
- ->select(['id','username','notify_url','account','user_limits','expired_at'])
- ->first();
- if($info){
- $info['user_limits'] = $info['user_limits']? explode(',', $info['user_limits']) : [];
- RedisService::set($cachekey, $info, 30);
- }
- return $info;
- }
- }
|