Просмотр исходного кода

引导图权重哦配置,点击统计

Tree 8 часов назад
Родитель
Сommit
bd1fc5f75c

+ 84 - 0
app/Http/Controllers/Admin/WeightConfigController.php

@@ -0,0 +1,84 @@
+<?php
+
+namespace App\Http\Controllers\Admin;
+
+use App\Http\Controllers\Api\WeightConfigController as ApiWeightConfigController;
+use App\Http\Controllers\Controller;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Redis;
+
+class WeightConfigController extends Controller
+{
+    public function index()
+    {
+        $config = ApiWeightConfigController::getConfig();
+
+        $totalRaw = Redis::hgetall(ApiWeightConfigController::REDIS_KEY_CLICKS);
+        $totalClicks = [];
+        foreach (ApiWeightConfigController::VALID_IDS as $id) {
+            $totalClicks[$id] = isset($totalRaw[$id]) ? intval($totalRaw[$id]) : 0;
+        }
+
+        $days = 7;
+        $dailyClicks = [];
+        for ($i = $days - 1; $i >= 0; $i--) {
+            $date = date('Y-m-d', strtotime("-{$i} days"));
+            $raw = Redis::hgetall(ApiWeightConfigController::REDIS_KEY_CLICKS_DAILY_PREFIX . $date);
+            $row = ['date' => $date];
+            foreach (ApiWeightConfigController::VALID_IDS as $id) {
+                $row[$id] = isset($raw[$id]) ? intval($raw[$id]) : 0;
+            }
+            $dailyClicks[] = $row;
+        }
+
+        return view('admin.weight_config.index', compact('config', 'totalClicks', 'dailyClicks'));
+    }
+
+    public function update(Request $request)
+    {
+        $config = $request->input('config');
+        if (is_string($config)) {
+            $config = json_decode($config, true);
+        }
+        if (!is_array($config)) {
+            return response()->json(['status' => 'error', 'message' => '参数错误']);
+        }
+
+        $save = [];
+        foreach (ApiWeightConfigController::VALID_IDS as $id) {
+            $val = isset($config[$id]) ? intval($config[$id]) : 0;
+            if ($val < 0) $val = 0;
+            $save[$id] = $val;
+        }
+
+        if (array_sum($save) <= 0) {
+            return response()->json(['status' => 'error', 'message' => '权重之和必须大于 0']);
+        }
+
+        Redis::set(ApiWeightConfigController::REDIS_KEY_CONFIG, json_encode($save));
+        return response()->json(['status' => 'success', 'message' => '更新成功', 'config' => $save]);
+    }
+
+    public function resetStats(Request $request)
+    {
+        $type = $request->input('type', 'total');
+        if ($type === 'all') {
+            Redis::del(ApiWeightConfigController::REDIS_KEY_CLICKS);
+            $keys = Redis::keys(ApiWeightConfigController::REDIS_KEY_CLICKS_DAILY_PREFIX . '*');
+            if (!empty($keys)) {
+                $prefix = config('database.redis.options.prefix', '');
+                if ($prefix) {
+                    $keys = array_map(function ($k) use ($prefix) {
+                        return strpos($k, $prefix) === 0 ? substr($k, strlen($prefix)) : $k;
+                    }, $keys);
+                }
+                foreach ($keys as $k) {
+                    Redis::del($k);
+                }
+            }
+        } else {
+            Redis::del(ApiWeightConfigController::REDIS_KEY_CLICKS);
+        }
+        return response()->json(['status' => 'success', 'message' => '清除成功']);
+    }
+}

+ 59 - 0
app/Http/Controllers/Api/WeightConfigController.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Http\Controllers\Controller;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Redis;
+
+class WeightConfigController extends Controller
+{
+    const REDIS_KEY_CONFIG = 'WeightConfig1234:config';
+    const REDIS_KEY_CLICKS = 'WeightConfig1234:clicks';
+    const REDIS_KEY_CLICKS_DAILY_PREFIX = 'WeightConfig1234:clicks:daily:';
+    const VALID_IDS = [1, 2, 3, 4];
+
+    public static function defaultConfig()
+    {
+        return [1 => 20, 2 => 30, 3 => 40, 4 => 10];
+    }
+
+    public static function getConfig()
+    {
+        $raw = Redis::get(self::REDIS_KEY_CONFIG);
+        if (!$raw) {
+            return self::defaultConfig();
+        }
+        $data = json_decode($raw, true);
+        if (!is_array($data)) {
+            return self::defaultConfig();
+        }
+        $result = [];
+        foreach (self::VALID_IDS as $id) {
+            $result[$id] = isset($data[$id]) ? intval($data[$id]) : 0;
+        }
+        return $result;
+    }
+
+    public function getWeights(Request $request)
+    {
+        $config = self::getConfig();
+        return apiReturnSuc($config);
+    }
+
+    public function clickRecord(Request $request)
+    {
+        $id = intval($request->input('id'));
+        if (!in_array($id, self::VALID_IDS)) {
+            return apiReturnFail('invalid id');
+        }
+
+        Redis::hincrby(self::REDIS_KEY_CLICKS, $id, 1);
+
+        $dailyKey = self::REDIS_KEY_CLICKS_DAILY_PREFIX . date('Y-m-d');
+        Redis::hincrby($dailyKey, $id, 1);
+        Redis::expire($dailyKey, 60 * 60 * 24 * 90);
+
+        return apiReturnSuc(['id' => $id]);
+    }
+}

+ 222 - 0
resources/views/admin/weight_config/index.blade.php

@@ -0,0 +1,222 @@
+@extends('base.base')
+@section('base')
+<meta name="csrf-token" content="{{ csrf_token() }}">
+<div class="container-fluid">
+    <div class="row">
+        <div class="col-12">
+            <div class="card">
+                <div class="card-header d-flex align-items-center justify-content-between">
+                    <h3 class="card-title mb-0">1234 权重配置 & 点击统计</h3>
+                    <small class="text-muted">配置 1/2/3/4 的权重(整数),前端按权重抽取,后台记录用户点击</small>
+                </div>
+                <div class="card-body">
+                    <ul class="nav nav-tabs mb-3" role="tablist">
+                        <li class="nav-item">
+                            <a class="nav-link active" data-toggle="tab" href="#tab-config" role="tab">
+                                <i class="mdi mdi-cog"></i> 权重配置
+                            </a>
+                        </li>
+                        <li class="nav-item">
+                            <a class="nav-link" data-toggle="tab" href="#tab-stats" role="tab">
+                                <i class="mdi mdi-chart-bar"></i> 点击统计
+                            </a>
+                        </li>
+                    </ul>
+
+                    <div class="tab-content">
+                        <div class="tab-pane fade show active" id="tab-config" role="tabpanel">
+                            @php
+                                $totalWeight = array_sum($config);
+                            @endphp
+                            <div class="alert alert-info">
+                                当前总权重: <strong id="total-weight">{{ $totalWeight }}</strong>
+                                ,前端按权重比例随机返回 1/2/3/4
+                            </div>
+                            <div class="table-responsive">
+                                <table class="table table-striped table-hover align-middle mb-0" style="max-width: 600px;">
+                                    <thead class="thead-light">
+                                        <tr>
+                                            <th style="width:80px;">ID</th>
+                                            <th style="width:200px;">权重 (整数)</th>
+                                            <th>占比</th>
+                                        </tr>
+                                    </thead>
+                                    <tbody class="weight-form">
+                                        @foreach([1,2,3,4] as $id)
+                                            @php
+                                                $w = intval($config[$id] ?? 0);
+                                                $percent = $totalWeight > 0 ? round($w / $totalWeight * 100, 2) : 0;
+                                            @endphp
+                                            <tr>
+                                                <td><span class="badge badge-primary">{{ $id }}</span></td>
+                                                <td>
+                                                    <input type="number" min="0" class="form-control form-control-sm weight-input"
+                                                           data-id="{{ $id }}" name="weight[{{ $id }}]" value="{{ $w }}" />
+                                                </td>
+                                                <td>
+                                                    <span class="weight-percent" data-id="{{ $id }}">{{ $percent }}%</span>
+                                                </td>
+                                            </tr>
+                                        @endforeach
+                                    </tbody>
+                                </table>
+                            </div>
+
+                            <div class="d-flex align-items-center mt-4">
+                                <button class="btn btn-gradient-primary btn-sm" id="save-weights">
+                                    <i class="mdi mdi-content-save"></i> 保存权重
+                                </button>
+                                <span class="save-status ml-3"></span>
+                            </div>
+                        </div>
+
+                        <div class="tab-pane fade" id="tab-stats" role="tabpanel">
+                            <div class="d-flex align-items-center justify-content-between mb-3">
+                                <h5 class="mb-0">总点击统计</h5>
+                                <button class="btn btn-sm btn-outline-danger" id="reset-stats">
+                                    <i class="mdi mdi-delete"></i> 清除总统计
+                                </button>
+                            </div>
+                            @php
+                                $totalClicksAll = array_sum($totalClicks);
+                            @endphp
+                            <div class="table-responsive mb-4">
+                                <table class="table table-bordered align-middle" style="max-width: 600px;">
+                                    <thead class="thead-light">
+                                        <tr>
+                                            <th>ID</th>
+                                            <th>点击次数</th>
+                                            <th>占比</th>
+                                        </tr>
+                                    </thead>
+                                    <tbody>
+                                        @foreach([1,2,3,4] as $id)
+                                            @php
+                                                $c = intval($totalClicks[$id] ?? 0);
+                                                $p = $totalClicksAll > 0 ? round($c / $totalClicksAll * 100, 2) : 0;
+                                            @endphp
+                                            <tr>
+                                                <td><span class="badge badge-info">{{ $id }}</span></td>
+                                                <td>{{ $c }}</td>
+                                                <td>{{ $p }}%</td>
+                                            </tr>
+                                        @endforeach
+                                        <tr class="table-secondary">
+                                            <td><strong>合计</strong></td>
+                                            <td colspan="2"><strong>{{ $totalClicksAll }}</strong></td>
+                                        </tr>
+                                    </tbody>
+                                </table>
+                            </div>
+
+                            <h5 class="mb-3">最近 7 天每日点击</h5>
+                            <div class="table-responsive">
+                                <table class="table table-bordered align-middle" style="max-width: 800px;">
+                                    <thead class="thead-light">
+                                        <tr>
+                                            <th>日期</th>
+                                            <th>1</th>
+                                            <th>2</th>
+                                            <th>3</th>
+                                            <th>4</th>
+                                            <th>合计</th>
+                                        </tr>
+                                    </thead>
+                                    <tbody>
+                                        @foreach($dailyClicks as $row)
+                                            @php
+                                                $sum = intval($row[1]) + intval($row[2]) + intval($row[3]) + intval($row[4]);
+                                            @endphp
+                                            <tr>
+                                                <td>{{ $row['date'] }}</td>
+                                                <td>{{ $row[1] }}</td>
+                                                <td>{{ $row[2] }}</td>
+                                                <td>{{ $row[3] }}</td>
+                                                <td>{{ $row[4] }}</td>
+                                                <td><strong>{{ $sum }}</strong></td>
+                                            </tr>
+                                        @endforeach
+                                    </tbody>
+                                </table>
+                            </div>
+                        </div>
+                    </div>
+                </div>
+            </div>
+        </div>
+    </div>
+</div>
+
+<script>
+$(function() {
+    function refreshPercents() {
+        let total = 0;
+        $('.weight-input').each(function(){
+            total += parseInt($(this).val() || 0);
+        });
+        $('#total-weight').text(total);
+        $('.weight-input').each(function(){
+            const id = $(this).data('id');
+            const w = parseInt($(this).val() || 0);
+            const p = total > 0 ? (w / total * 100).toFixed(2) : 0;
+            $('.weight-percent[data-id="' + id + '"]').text(p + '%');
+        });
+    }
+
+    $('.weight-input').on('input change', refreshPercents);
+
+    $('#save-weights').click(function() {
+        const $btn = $(this);
+        const $status = $btn.siblings('.save-status');
+        const config = {};
+        $('.weight-input').each(function(){
+            const id = $(this).data('id');
+            let v = parseInt($(this).val() || 0);
+            if (isNaN(v) || v < 0) v = 0;
+            config[id] = v;
+        });
+
+        $btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 保存中...');
+        $status.removeClass('text-success text-danger').text('');
+
+        $.post("{{ url('/admin/weight-config/update') }}", {
+            config: JSON.stringify(config),
+            _token: "{{ csrf_token() }}"
+        }).done(function(res){
+            $btn.prop('disabled', false).html('<i class="mdi mdi-content-save"></i> 保存权重');
+            if (res.status === 'success') {
+                $status.text('更新成功').addClass('text-success');
+            } else {
+                $status.text(res.message || '更新失败').addClass('text-danger');
+            }
+            setTimeout(function(){ $status.fadeOut(function(){ $(this).text('').show().removeClass('text-success text-danger'); }); }, 3000);
+        }).fail(function(){
+            $btn.prop('disabled', false).html('<i class="mdi mdi-content-save"></i> 保存权重');
+            $status.text('系统错误').addClass('text-danger');
+        });
+    });
+
+    $('#reset-stats').click(function(){
+        if (!confirm('确认清除总点击统计?(每日明细仍保留)')) return;
+        $.post("{{ url('/admin/weight-config/reset-stats') }}", {
+            type: 'total',
+            _token: "{{ csrf_token() }}"
+        }).done(function(res){
+            if (res.status === 'success') {
+                location.reload();
+            } else {
+                alert(res.message || '操作失败');
+            }
+        }).fail(function(){
+            alert('系统错误');
+        });
+    });
+});
+</script>
+
+<style>
+.table thead th { white-space: nowrap; }
+.table tbody td { vertical-align: middle; }
+.badge { font-size: .85rem; padding: .5em .6em; }
+</style>
+@endsection

+ 3 - 0
routes/api.php

@@ -41,6 +41,9 @@ Route::any('/rate_us', 'Api\ApiController@rateUs');
 Route::any('/apk/cconfig', 'Api\AutoApkController@channelConfig');
 Route::any('/apk/jump', 'Api\AutoApkController@jumpApkByConfig');
 
+Route::any('/weight_config/get', 'Api\WeightConfigController@getWeights');
+Route::any('/weight_config/click', 'Api\WeightConfigController@clickRecord');
+
 Route::group([
     'middleware' => 'newApiSign'
 ], function ($route) {

+ 5 - 0
routes/web.php

@@ -793,6 +793,11 @@ Route::group([
         $route->any('/pg-game-config', 'Admin\PGGameConfigController@index')->name('admin.pg-game-config');
         $route->any('/common-config', 'Admin\CommonConfigController@index')->name('admin.common-config');
         $route->any('/common-config/update', 'Admin\CommonConfigController@update')->name('admin.common-config.update');
+
+        // 1234 权重配置 & 点击统计
+        $route->any('/weight-config', 'Admin\WeightConfigController@index')->name('admin.weight-config');
+        $route->post('/weight-config/update', 'Admin\WeightConfigController@update')->name('admin.weight-config.update');
+        $route->post('/weight-config/reset-stats', 'Admin\WeightConfigController@resetStats')->name('admin.weight-config.reset-stats');
         $route->any('/special-config', 'Admin\SpecialConfigController@index')->name('admin.special-config');
         $route->any('/special-config/update', 'Admin\SpecialConfigController@update')->name('admin.special-config.update');
         $route->any('/pg-game-config/update', 'Admin\PGGameConfigController@update')->name('admin.pg-game-config.update');