Ken il y a 1 semaine
Parent
commit
6c6cd81f61

+ 30 - 0
app/Http/Controllers/api/ActivityReward.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Controllers\api;
+
+use App\Models\ActivityReward as ActivityRewardModel;
+use App\Services\ActivityRewardService;
+use Illuminate\Validation\ValidationException;
+use Exception;
+
+class ActivityReward extends BaseController
+{
+    public function index()
+    {
+        try {
+            $time = time();
+            $params = [
+                'start_time' => ['>=', $time],
+                'end_time' => ['<=', $time],
+            ];
+            $where = ActivityRewardService::getWhere($params);
+            $query = ActivityRewardModel::where($where);
+            $list = $query->orderByDesc('id')->get()->toArray();
+        } catch (ValidationException $e) {
+            return $this->error($e->validator->errors()->first());
+        } catch (Exception $e) {
+            return $this->error($e->getMessage());
+        }
+        return $this->success($list);
+    }
+}

+ 28 - 2
app/Services/ActivityRewardService.php

@@ -28,13 +28,39 @@ class ActivityRewardService extends BaseService
         if (isset($search['title']) && !empty($search['title'])) {
             $where[] = ['title', 'like', "%{$search['title']}%"];
         }
+
+        if (isset($search['start_time']) && !empty($search['start_time'])) {
+            if (is_array($search['start_time'])) {
+                if (count($search['start_time']) == 2) {
+                    $where[] = ['start_time', $search['start_time'][0], $search['start_time'][1]];
+                } else {
+                    $where[] = ['start_time', '=', $search['start_time'][0]];
+                }
+            } else {
+                $where[] = ['start_time', '=', $search['start_time']];
+            }
+        }
+
+        if (isset($search['end_time']) && !empty($search['end_time'])) {
+            if (is_array($search['end_time'])) {
+                if (count($search['end_time']) == 2) {
+                    $where[] = ['end_time', $search['end_time'][0], $search['end_time'][1]];
+                } else {
+                    $where[] = ['end_time', '=', $search['end_time'][0]];
+                }
+            } else {
+                $where[] = ['end_time', '=', $search['end_time']];
+            }
+        }
+
+
         return $where;
     }
 
     public static function deleteAll(array $search = []): bool
     {
         $count = static::$MODEL::where(static::getWhere($search))->delete();
-        if ($count < 1) throw new Exception('删除失败',HttpStatus::CUSTOM_ERROR);
+        if ($count < 1) throw new Exception('删除失败', HttpStatus::CUSTOM_ERROR);
         return true;
     }
 
@@ -54,7 +80,7 @@ class ActivityRewardService extends BaseService
 
         if (!empty($params['id'])) {
             $info = static::findOne(['id' => $params['id']]);
-            if (!$info) throw new Exception("操作失败",HttpStatus::CUSTOM_ERROR);
+            if (!$info) throw new Exception("操作失败", HttpStatus::CUSTOM_ERROR);
             $info->update($params);
         } else {
             static::$MODEL::create($params);

+ 6 - 0
routes/api.php

@@ -1,5 +1,6 @@
 <?php
 
+use App\Http\Controllers\api\ActivityReward;
 use App\Http\Controllers\api\Fingerprint;
 use Illuminate\Support\Facades\Route;
 use App\Constants\HttpStatus;
@@ -17,6 +18,11 @@ Route::get("/setMyCommands", [Home::class, 'setMyCommands']);
 Route::get("/setA", [Home::class, 'setA']);
 
 
+Route::prefix('/ActivityReward')->group(function () {
+    Route::get('/', [ActivityReward::class, 'index']);
+});
+
+
 Route::prefix('/fingerprint')->group(function () {
     Route::post('/setVisitorId', [Fingerprint::class, 'setVisitorId']);
 });