liuzejian před 1 rokem
rodič
revize
d74a76af4c

+ 127 - 0
modules/Callback/Http/Controllers/JLEvent/JLEventController.php

@@ -0,0 +1,127 @@
+<?php
+
+namespace Modules\Callback\Http\Controllers\JLEvent;
+
+use Catch\Base\CatchController;
+use Illuminate\Foundation\Validation\ValidatesRequests;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use Modules\Callback\Services\CallbackConst;
+use Modules\Callback\Services\JLEventService;
+use Modules\Common\Errors\Errors;
+use Modules\Common\Exceptions\CommonBusinessException;
+
+/**
+ * 巨量2.0事件-微信小程序
+ */
+class JLEventController extends CatchController
+{
+    use ValidatesRequests;
+    /**
+     * 回传配置列表
+     * @param Request $request
+     */
+    public function list(Request $request) {
+        $id = $request->input('id');
+        $name = $request->input('name');
+        $promotionId = $request->input('promotion_id');
+        $unbind = $request->input('unbind', 0);
+
+        $result =  DB::table('jl_event_callback_config as config')
+            ->leftJoin('promotions', 'promotions.callback_config_id', '=', 'config.id')
+            ->where([
+                'user_id' => $this->getLoginUserId(),
+            ])->when($id, function ($query, $id){
+                return $query->where('config.id', $id);
+            })->when($name, function ($query, $name){
+                return $query->where('config.name', 'like', '%' .$name. '%');
+            })->when($promotionId, function ($query, $promotionId){
+                return $query->where('promotions.id', $promotionId)
+                    ->where('promotions.callback_type' , CallbackConst::TYPE_JL_EVENT_20);
+            })->when(1 == $unbind, function ($query){
+                return $query->whereNull('promotions.id');
+            })
+            ->select('config.id', 'config.name', 'config.charge_type', 'config.updated_at',
+                'promotions.name as promotion_name', 'promotions.id as promotion_id',
+            'config.charge_money_map')
+            ->orderBy('id', 'desc')
+            ->paginate($request->input('limit', 15));
+        foreach ($result as $item){
+            $item->charge_type_str = CallbackConst::CHARGE_TYPE_MAP[$item->charge_type] ?? '-';
+            $item->charge_money_map = \json_decode($item->charge_money_map) ?? [];
+        }
+        return $result;
+    }
+
+    /**
+     * 新增回传配置
+     * @param Request $request
+     */
+    public function add(Request $request) {
+        $this->validate($request, [
+            'name' => 'required|string|max:64',
+            // roi 全量上报:1-全量上报,2-不全量上报
+            'is_roi' => 'required|integer|in:1,2',
+            // 充值行为:1-首充,2-所有充值
+            'charge_type' => 'required_if:is_roi,2|integer|in:1,2',
+            // 金额项
+            'charge_money_map'  => 'required_if:is_roi,2|array',
+            'charge_money_map.*.min_money' => 'integer|min:0',
+        ]);
+        $now = date('Y-m-d H:i:s');
+        if(2 == $request->input('is_roi') &&
+            (!JLEventService::judgeChargeMoneyOk($request->input('charge_money_map')))) {
+            CommonBusinessException::throwError(Errors::CALLBACK_CHARGE_MONEY_MAP_ERROR);
+        }
+        DB::table('jl_event_callback_config')
+            ->insert(array_merge($request->only('name', 'is_roi', 'charge_type'), [
+                'created_at' => $now, 'updated_at' => $now,
+                'charge_money_map' => \json_encode($request->input('charge_money_map')),
+                'user_id' => $this->getLoginUserId()
+            ]));
+
+        return 'ok';
+    }
+
+    /**
+     * 更新回传配置
+     * @param Request $request
+     */
+    public function update(Request $request) {
+        $this->validate($request, [
+            'ids' => 'required|array',
+            'ids.*' => 'integer',
+            // roi 全量上报:1-全量上报,2-不全量上报
+            'is_roi' => 'required|integer|in:1,2',
+            // 充值行为:1-首充,2-所有充值
+            'charge_type' => 'required_if:is_roi,2|integer|in:1,2',
+            // 金额项
+            'charge_money_map'  => 'required_if:is_roi,2|array',
+            'charge_money_map.*.min_money' => 'integer|min:0',
+        ]);
+
+        if(1 == count($request->input('ids'))) {
+            $updateData = $request->only('is_roi', 'charge_type', 'name');
+        } else {
+            $updateData = $request->only('is_roi', 'charge_type');
+        }
+        $now = date('Y-m-d H:i:s');
+        DB::table('jl_event_callback_config')
+            ->whereIn('id', $request->input('ids'))
+            ->where('user_id', $this->getLoginUserId())
+            ->update(array_merge($updateData, [
+                'charge_money_map' => \json_encode($request->input('charge_money_map', [])),
+                'updated_at' => $now,
+            ]));
+        // todo: 更新对应配置的计算比例信息
+        return 'ok';
+    }
+
+    /**
+     * 金额项卡比例选项
+     * @param Request $request
+     */
+    public function listCustomRate(Request $request) {
+        return JLEventService::listCustomCallBackRate();
+    }
+}

+ 5 - 4
modules/Callback/Http/Controllers/JuliangAccountController.php

@@ -6,6 +6,7 @@ use Catch\Base\CatchController;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\DB;
+use Modules\Callback\Services\CallbackConst;
 use Modules\Common\Errors\Errors;
 use Modules\Common\Exceptions\CommonBusinessException;
 use Modules\User\Http\Controllers\UserTrait;
@@ -27,7 +28,7 @@ class JuliangAccountController extends CatchController
             $alreadyBindConfigIds = DB::table('promotions')
                 ->where([
                     'uid' => $this->getOptimizerUid(),
-                    'callback_type' => 1,
+                    'callback_type' => CallbackConst::TYPE_JL_ACCOUNT,
                     'status' => 1,
                     'is_enabled' => 1,
                 ])->where('callback_config_id' , '<>', 0)
@@ -41,7 +42,7 @@ class JuliangAccountController extends CatchController
                 ->where([
                     'uid' => $this->getOptimizerUid(),
                     'id' =>  $promotionId,
-                    'callback_type' => 1,
+                    'callback_type' => CallbackConst::TYPE_JL_ACCOUNT,
                     'status' => 1,
                     'is_enabled' => 1,
                 ])->where('callback_config_id' , '<>', 0)
@@ -73,7 +74,7 @@ class JuliangAccountController extends CatchController
         $promotions = DB::table('promotions')
             ->where([
                 'uid' => $this->getOptimizerUid(),
-                'callback_type' => 1,
+                'callback_type' => CallbackConst::TYPE_JL_ACCOUNT,
                 'status' => 1,
                 'is_enabled' => 1,
             ])->whereIn('callback_config_id', $ids)
@@ -354,7 +355,7 @@ class JuliangAccountController extends CatchController
         $now = date('Y-m-d H:i:s');
         $affected = DB::table('promotions')
             ->where([
-                'callback_type' => 1,
+                'callback_type' => CallbackConst::TYPE_JL_ACCOUNT,
                 'callback_config_id' => $request->input('id'),
                 'is_enabled' => 1,
                 'status' => 1,

+ 22 - 0
modules/Callback/Services/CallbackConst.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Modules\Callback\Services;
+
+class CallbackConst
+{
+    /**
+     * 回传类型:巨量账户级
+     */
+    const TYPE_JL_ACCOUNT = 1;
+    /**
+     * 回传类型:巨量2.0事件
+     */
+    const TYPE_JL_EVENT_20 = 2;
+    /**
+     * 充值行为类型
+     */
+    const CHARGE_TYPE_MAP=  [
+        '1' => '首充',
+        '2' => '所有充值'
+    ];
+}

+ 52 - 0
modules/Callback/Services/JLEventService.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace Modules\Callback\Services;
+
+/**
+ * 巨量2.0事件-微信小程序
+ */
+class JLEventService
+{
+    /**
+     * 判断回传配置的金额项
+     * @param $chargeMoneyMap
+     * <pre>
+     * [
+     *      [
+     *          'min_money' => 1, // 最小金额
+     *          'max_money' => 2, // 最大金额(不包含)
+     *          'callback_type' => 1, // 回传参数,类型:1-全部回传,2-全部不回传,3-自定义
+     *          'callback_param' => '1:1', // callback_type == 3 时, 自定义,传几卡几
+     *      ]
+     * ]
+     * </pre>
+     */
+    public static function judgeChargeMoneyOk($chargeMoneyMap) {
+        $len = count($chargeMoneyMap);
+        for($i = 0; $i < $len; $i++) {
+            for($j = $i+1; $j < $len ; $j ++) {
+                if($chargeMoneyMap[$i]['min_money'] >= $chargeMoneyMap[$j]['max_money'] ||
+                    $chargeMoneyMap[$i]['max_money'] <= $chargeMoneyMap[$j]['min_money']) {
+                    continue;
+                } else {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    /**
+     * 金额项的卡比例选项
+     * @return array
+     */
+    public static function listCustomCallBackRate() {
+        $arr = explode(',', config('callback.jlEvent.customRate'));
+        $result = [];
+        foreach ($arr as $item) {
+            $temp = explode(':', $item);
+            $result[$item] = '传'.$temp[0].'卡'.$temp[1];
+        }
+        return $result;
+    }
+}

+ 6 - 0
modules/Callback/config/jlEvent.php

@@ -0,0 +1,6 @@
+<?php
+
+return [
+    'customRate' => env('CALLBACK_JLEVENT_CUSTOM_RATE',
+        '1:1,3:1,4:1,5:1,6:1,7:1,8:1,9:1,1:2,2:1,1:3,1:4,2:3,1:5,1:6,1:9,1:7,1:8,3:4,3:5'),
+];

+ 8 - 0
modules/Callback/routes/route.php

@@ -2,6 +2,7 @@
 
 use Illuminate\Support\Facades\Route;
 use Modules\Callback\Http\Controllers\CallbackLogController;
+use Modules\Callback\Http\Controllers\JLEvent\JLEventController;
 use Modules\Callback\Http\Controllers\JuliangAccountController;
 
 Route::prefix('callback')->group(function () {
@@ -14,5 +15,12 @@ Route::prefix('callback')->group(function () {
         Route::get('log/list', [CallbackLogController::class, 'list']);
         Route::post('log/callbackAgain', [CallbackLogController::class, 'callbackAgain']);
     });
+
+    Route::prefix('jlEvent')->group(function(){
+        Route::post('add', [JLEventController::class, 'add']);
+        Route::post('update', [JLEventController::class, 'update']);
+        Route::get('list', [JLEventController::class, 'list']);
+        Route::get('listCustomRate', [JLEventController::class, 'listCustomRate']);
+    });
 });
 

+ 1 - 0
modules/Common/Errors/Errors.php

@@ -38,4 +38,5 @@ class Errors
     public const  OPENPLATFORM_COMPANY_INFO_NOT_EXISTS= [5000601, '公司没有对应的开放平台信息'];
     public const  OPENPLATFORM_OPTIMIZER_INFO_ERROR= [5000602, '优化师信息有误'];
     public const  WECHAT_CHECK_RECORD_NOT_EXISTS = [500305, '微信提审记录不存在'];
+    public const  CALLBACK_CHARGE_MONEY_MAP_ERROR = [500107, '金额项配置不合规'];
 }

+ 70 - 0
tests/Callback/Http/Controllers/JLEvent/JLEventControllerTest.php

@@ -0,0 +1,70 @@
+<?php
+
+namespace Tests\Callback\Http\Controllers\JLEvent;
+
+use Modules\Callback\Http\Controllers\JLEvent\JLEventController;
+use PHPUnit\Framework\TestCase;
+use Tests\UsedTestCase;
+
+class JLEventControllerTest extends UsedTestCase
+{
+
+    public function testAdd()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('post','http://localhost/api/callback/jlEvent/add', [
+            'name' => 'liuzj-test-'.rand(1, 100000),
+            'is_roi' => 2,
+            'charge_type' => 1,
+            'charge_money_map' => [
+                ['min_money' => 10, 'max_money' => 20, 'callback_type' => 1, 'callback_param' => '1:3'],
+                ['min_money' => 20, 'max_money' => 25, 'callback_type' => 2, 'callback_param' => ''],
+                ['min_money' => 25, 'max_money' => 30, 'callback_type' => 2, 'callback_param' => ''],
+            ]
+        ]);
+
+        $this->dumpJson($res);
+    }
+
+    public function testUpdate()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('post','http://localhost/api/callback/jlEvent/update', [
+            'ids' => [5],
+            'name' => 'liuzj-test-'.rand(1, 100000),
+            'is_roi' => 1,
+            'charge_type' => 1,
+            'charge_money_map' => [
+                ['min_money' => 10, 'max_money' => 20, 'callback_type' => 1, 'callback_param' => ''],
+//                ['min_money' => 20, 'max_money' => 25, 'callback_type' => 2, 'callback_param' => ''],
+                ['min_money' => 25, 'max_money' => 30, 'callback_type' => 2, 'callback_param' => ''],
+            ]
+        ]);
+
+        $this->dumpJson($res);
+    }
+
+    public function testList()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/callback/jlEvent/list', [
+            'id' => 5,
+//            'name' => 'liuzj',
+//        'promotion_id' => 21
+        ]);
+
+        $this->dumpJson($res);
+    }
+    public function testlistCustomRate()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/callback/jlEvent/listCustomRate', [
+        ]);
+
+        $this->dumpJson($res);
+    }
+}

+ 31 - 0
tests/Callback/Services/JLEventServiceTest.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace Tests\Callback\Services;
+
+use Modules\Callback\Services\JLEventService;
+use PHPUnit\Framework\TestCase;
+
+class JLEventServiceTest extends \Tests\TestCase
+{
+
+    public function testJudgeChargeMoney()
+    {
+        $chargeMoneyMap = [
+            ['min_money' => 11, 'max_money' => 15],
+//            ['min_money' => 10, 'max_money' => 11],
+            ['min_money' => 15, 'max_money' => 16],
+            ['min_money' => 15, 'max_money' => 17],
+        ];
+        $res  = JLEventService::judgeChargeMoneyOk($chargeMoneyMap);
+        if($res) {
+            dump('check ok');
+        } else {
+            dump('check not ok');
+        }
+    }
+
+    public function testlistCustomCallBackRate() {
+        $res = JLEventService::listCustomCallBackRate();
+        dump(\json_encode($res, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
+    }
+}

+ 3 - 3
tests/UsedTestCase.php

@@ -13,11 +13,11 @@ abstract class UsedTestCase extends BaseTestCase
     {
         parent::setUp(); // TODO: Change the autogenerated stub
         $tokenInfo = $this->post('http://localhost/api/login', [
-            'email' => 'catch@admin.com',
+//            'email' => 'catch@admin.com',
             'remember' => false,
-            'email' => 'xiaoli@qq.com',
+//            'email' => 'xiaoli@qq.com',
             'password' => 'catchadmin',
-//            'email' => 'aa4@test.com',
+            'email' => 'aa4@test.com',
         ])->json();
         $this->token = $tokenInfo['data']['token'];
     }