Explorar o código

染色规则配置

liuzejian hai 1 ano
pai
achega
286e7c98de

+ 71 - 0
modules/Tuiguang/Http/Controllers/RanseConfigController.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace Modules\Tuiguang\Http\Controllers;
+
+use Catch\Base\CatchController;
+use Illuminate\Foundation\Validation\ValidatesRequests;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use Modules\Common\Errors\Errors;
+use Modules\Common\Exceptions\CommonBusinessException;
+use Modules\User\Http\Controllers\UserTrait;
+
+class RanseConfigController extends CatchController
+{
+    use UserTrait;
+    use ValidatesRequests;
+    /**
+     * 设置染色时间
+     * @param Request $request
+     */
+    public function setRanseDuration(Request $request) {
+        $this->validate($request, [
+            'no_charge_user_duration' => 'required|integer|min:0',
+            'charge_user_duration' => 'required|integer|gte:no_charge_user_duration',
+        ]);
+        $uid = $this->getOptimizerUid();
+        if(!$uid) {
+            CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
+        }
+        $now = date('Y-m-d H:i:s');
+        DB::table('ranse_config')->where('user_id', $uid)
+            ->where('is_enabled', 1)
+            ->update(['is_enabled' => 0, 'updated_at' => $now]);
+        DB::table('ranse_config')
+            ->insert([
+                'user_id' => $uid, 'is_enabled' => 1,
+                'no_charge_user_duration' => $request->input('no_charge_user_duration'),
+                'charge_user_duration' => $request->input('charge_user_duration'),
+                'created_at' => $now, 'updated_at' => $now,
+            ]);
+
+        return 'ok';
+    }
+
+    /**
+     * 获取染色时间
+     * @param Request $request
+     * @return array
+     */
+    public function getRanseDuration(Request $request) {
+        $uid = $this->getOptimizerUid();
+        if(!$uid) {
+            CommonBusinessException::throwError(Errors::NO_OPERATE_PERMISSION);
+        }
+
+        $config = DB::table('ranse_config')->where('user_id', $uid)
+            ->where('is_enabled', 1)
+            ->first();
+        if(!$config) {
+            $config = DB::table('ranse_config')
+                ->where([
+                    'user_id' => 0, 'miniprogram_id' => 0, 'is_enabled' =>1
+                ])->first();
+        }
+
+        return [
+            'charge_user_duration' => $config->charge_user_duration,
+            'no_charge_user_duration' => $config->no_charge_user_duration,
+        ];
+    }
+}

+ 6 - 0
modules/Tuiguang/routes/route.php

@@ -2,6 +2,7 @@
 
 use Illuminate\Support\Facades\Route;
 use Modules\Tuiguang\Http\Controllers\PromotionController;
+use Modules\Tuiguang\Http\Controllers\RanseConfigController;
 
 Route::prefix('tuiguang')->group(function () {
     Route::prefix('promotion')->group(function(){
@@ -11,5 +12,10 @@ Route::prefix('tuiguang')->group(function () {
         Route::post('add', [PromotionController::class, 'add']);
         Route::post('updateCallbackConfig', [PromotionController::class, 'updateCallbackConfig']);
     });
+
+    Route::prefix('ranseConfig')->group(function(){
+        Route::get('getRanseDuration', [RanseConfigController::class, 'getRanseDuration']);
+        Route::post('setRanseDuration', [RanseConfigController::class, 'setRanseDuration']);
+    });
 });
 

+ 29 - 0
tests/Tuiguang/Http/Controllers/RanseConfigControllerTest.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace Tests\Tuiguang\Http\Controllers;
+
+use Modules\Tuiguang\Http\Controllers\RanseConfigController;
+use PHPUnit\Framework\TestCase;
+use Tests\UsedTestCase;
+
+class RanseConfigControllerTest extends UsedTestCase
+{
+
+    public function testGetRanseDuration()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/tuiguang/ranseConfig/getRanseDuration');
+        $res->dump();
+    }
+    public function testsetRanseDuration()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('post','http://localhost/api/tuiguang/ranseConfig/setRanseDuration', [
+            'charge_user_duration' => 150,
+            'no_charge_user_duration' => 50
+        ]);
+        $res->dump();
+    }
+}