ith5 5 сар өмнө
parent
commit
a891db42c7

+ 58 - 0
app/v1/controller/WelcomeController.php

@@ -0,0 +1,58 @@
+<?php
+namespace app\v1\controller;
+
+use app\v1\logic\WelcomeLogic;
+use plugin\saiadmin\app\cache\UserInfoCache;
+use plugin\saiadmin\basic\BaseController;
+use support\Request;
+use support\Response;
+
+/**
+ * wellcome接口管理控制器
+ */
+class WelcomeController extends BaseController {
+
+
+   public function __construct()
+    {
+        $this->logic = new WelcomeLogic();
+        parent::__construct();
+    }
+
+
+  public function index(Request $request) {
+    // 根据用户id获取用户权限
+    $token = getCurrentInfo();
+    // 用户权限缓存
+    $userAuthCache = new UserInfoCache($token['id']);
+    $user_info = $userAuthCache->getUserInfo();
+    $currentUserRoleList = $user_info['roleList'];
+    $currentUserRoleList = array_column($currentUserRoleList, 'id');
+
+    if(in_array(1,$currentUserRoleList)){
+      $data = $this->logic->index();
+      return $this->success($data);
+
+    }else{
+      return $this->success('非管理员');
+    }
+  }
+
+  public function chartData(Request $request) {
+    // 根据用户id获取用户权限
+    $token = getCurrentInfo();
+    // 用户权限缓存
+    $userAuthCache = new UserInfoCache($token['id']);
+    $user_info = $userAuthCache->getUserInfo();
+    $currentUserRoleList = $user_info['roleList'];
+    $currentUserRoleList = array_column($currentUserRoleList, 'id');
+
+    if(in_array(1,$currentUserRoleList)){
+      $data = $this->logic->chartData();
+      return $this->success($data);
+
+    }else{
+      return $this->success('非管理员');
+    }
+  }
+}

+ 137 - 0
app/v1/logic/WelcomeLogic.php

@@ -0,0 +1,137 @@
+<?php
+
+namespace app\v1\logic;
+
+use plugin\saiadmin\basic\BaseLogic;
+use support\think\Db;
+
+/**
+ * 欢迎页逻辑层
+ */
+class WelcomeLogic extends BaseLogic
+{
+  public function index()
+  {
+    $data = [
+        'reg' => [
+            'name' => '注册用户',
+        ],
+        'login' => [
+            'name' => '登录用户',
+        ],
+        'pay' => [
+            'name' => '充值金额',
+        ],
+        'cost' => [
+            'name' => '消耗金额',
+        ],
+      ];
+      $tday  = date('Y-m-d');
+      $lday  = date('Y-m-d',time()-86400);
+      $tmon  = date('Y-m');
+      $lmon  = date('Y-m',strtotime($tmon)-86400);
+
+      $res = [];
+
+      $baseTotalDayField = [
+        'sum(reg_total)' => 'reg',
+        'sum(old_login_total)' => 'login',
+        'sum(pay_total)' => 'pay',
+      ];
+
+      $tYear = (int)date('Y', strtotime($tday));
+      $lYear = (int)date('Y', strtotime($lday));
+      $res['tday'] = Db::connect('db_data_report')->table('base_total_day_'.$tYear)->field($baseTotalDayField)->where('tdate',$tday)->find();
+      $res['lday'] = Db::connect('db_data_report')->table('base_total_day_'.$lYear)->field($baseTotalDayField)->where('tdate',$lday)->find();
+
+
+      $baseTotalMonthField = [
+        'sum(reg_total)' => 'reg',
+        'sum(login_total)' => 'login',
+        'sum(pay_total)' => 'pay',
+      ];
+      $res['tmon'] = Db::connect('db_data_report')->table('game_total_month')->field($baseTotalMonthField)->where('tmonth',$tmon)->find();
+      $res['lmon'] = Db::connect('db_data_report')->table('game_total_month')->field($baseTotalMonthField)->where('tmonth',$lmon)->find();
+
+
+      $res['tday']['cost'] = Db::connect('db_advert')->table('media_cost')->where('tdate',$tday)->sum('money');
+      $res['lday']['cost'] = Db::connect('db_advert')->table('media_cost')->where('tdate',$lday)->sum('money');
+
+      $res['tmon']['cost'] = Db::connect('db_advert')->table('media_cost')->where('tdate',"{$tmon}-01,{$tday}")->sum('money');
+      $llday = date('Y-m-d',strtotime($tmon)-86400);
+      $res['lmon']['cost'] = Db::connect('db_advert')->table('media_cost')->where('tdate',"{$lmon}-01,{$llday}")->sum('money');
+
+
+      foreach($res as $ak=>$val){
+        foreach($data as $bk=>$v){
+            $data[$bk][$ak] = $val[$bk];
+        }
+      }
+      return [
+        'data' => $data,
+      ];
+  }
+
+  // 一周数据,图表
+  public function chartData()
+  {
+    
+        $days = [];
+        for($i=-6;$i<=0;$i++){
+            $days[] = date('Y-m-d',time()+86400*$i);
+        }
+        $where = [
+            ['tdate','between',[$days[0],$days[6]]],
+        ];
+
+        $baseTotalDayField = [
+            'sum(reg_total)' => 'reg',
+            'sum(old_login_total)' => 'login',
+            'sum(pay_total)' => 'pay',
+            'tdate' => 'tdate',
+        ];
+
+        $baseTotal = Db::connect('db_data_report')
+          ->table('base_total_day_'.date('Y'))
+          ->field($baseTotalDayField)
+          ->where($where)
+          ->group('tdate')
+          ->select()->toArray();
+
+
+        $temp = $baseTotal;
+        foreach($temp as $tmp){
+            $res[$tmp['tdate']]['reg'] = $tmp['reg'];
+            $res[$tmp['tdate']]['pay'] = $tmp['pay'];
+            $res[$tmp['tdate']]['login'] = $tmp['login'];
+        }
+
+        $costField = [
+          'SUM(money)' => 'cost',
+          'tdate'
+        ];
+
+        $temp = Db::connect('db_advert')
+          ->table('media_cost')
+          ->field($costField)
+          ->where($where)
+          ->group('tdate')
+          ->select()->toArray();
+
+
+        foreach($temp as $tmp){
+            $res[$tmp['tdate']]['cost'] = $tmp['cost'];
+        }
+        foreach($days as $k=>$d){
+            $data['days'][$k] = $d;
+            $data['reg'][$k]  = $res[$d]['reg']??0+0;
+            $data['pay'][$k]  = $res[$d]['pay']??0+0;
+            $data['cost'][$k] = round($res[$d]['cost']??0+0);
+            $data['login'][$k]= $res[$d]['login']??0+0;
+        }
+        return $data;
+
+    }
+   
+ 
+}