You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
345 lines
14 KiB
PHP
345 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\Payback;
|
|
use App\Models\Coupon;
|
|
use App\Models\User;
|
|
use App\Services\Config;
|
|
use App\Services\Mail;
|
|
use App\Services\AdminPermission;
|
|
use App\Services\AdminDataMasker;
|
|
use App\Services\Auth;
|
|
use App\Utils\Tools;
|
|
use App\Services\Analytics;
|
|
use App\Services\Cloud\CloudAdminOverviewService;
|
|
use App\Utils\SafeDatatables as Datatables;
|
|
use App\Utils\DatatablesHelper;
|
|
|
|
/**
|
|
* Admin Controller
|
|
*/
|
|
class AdminController extends UserController
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function index($request, $response, $args)
|
|
{
|
|
$overviewError = '';
|
|
try {
|
|
$overview = CloudAdminOverviewService::snapshot();
|
|
} catch (\Throwable $exception) {
|
|
error_log('Admin overview snapshot failed: ' . $exception->getMessage());
|
|
$overview = CloudAdminOverviewService::unavailable();
|
|
$overviewError = '运营数据暂时无法读取,请确认已执行 cloudInit 并检查业务数据库。';
|
|
}
|
|
|
|
return $this->view()->assign([
|
|
'overview' => $overview,
|
|
'overview_error' => $overviewError,
|
|
])->display('admin/index.tpl');
|
|
|
|
}
|
|
|
|
public function invite($request, $response, $args)
|
|
{
|
|
$tableConfig = [
|
|
'total_column' => [
|
|
"id" => "ID",
|
|
"total" => "原始金额",
|
|
"event_user_id" => "发起用户ID",
|
|
"event_user_name" => "发起用户名",
|
|
"ref_user_id" => "获利用户ID",
|
|
"ref_user_name" => "获利用户名",
|
|
"ref_get" => "获利金额",
|
|
"datetime" => "时间"
|
|
],
|
|
'default_show_column' => [],
|
|
'ajax_url' => 'payback/ajax'
|
|
];
|
|
|
|
foreach ($tableConfig['total_column'] as $column => $value) {
|
|
$tableConfig['default_show_column'][] = $column;
|
|
}
|
|
|
|
return $this->view()
|
|
->assign('table_config', $tableConfig)
|
|
->display('admin/invite.tpl');
|
|
}
|
|
|
|
public function coupon($request, $response, $args)
|
|
{
|
|
$tableConfig = [
|
|
'total_column' => [
|
|
"id" => "ID",
|
|
"code" => "优惠码",
|
|
"expire" => "过期时间",
|
|
"shop" => "限定商品ID",
|
|
"credit" => "额度"
|
|
],
|
|
'default_show_column' => [],
|
|
'ajax_url' => 'coupon/ajax'
|
|
];
|
|
if (!AdminPermission::isSuperAdmin()) {
|
|
unset($tableConfig['total_column']['code']);
|
|
}
|
|
|
|
foreach ($tableConfig['total_column'] as $column => $value) {
|
|
$tableConfig['default_show_column'][] = $column;
|
|
}
|
|
|
|
return $this->view()->assign('table_config', $tableConfig)->display('admin/coupon.tpl');
|
|
}
|
|
|
|
public function addCoupon($request, $response, $args)
|
|
{
|
|
$code = new Coupon();
|
|
$code->onetime=$request->getParam('onetime');
|
|
|
|
$code->code=$request->getParam('prefix').Tools::genRandomChar(8);
|
|
$code->expire=time()+$request->getParam('expire')*3600;
|
|
$code->shop=$request->getParam('shop');
|
|
$code->credit=$request->getParam('credit');
|
|
|
|
$code->save();
|
|
|
|
$res['ret'] = 1;
|
|
$res['msg'] = "优惠码添加成功";
|
|
return $response->getBody()->write(json_encode($res));
|
|
}
|
|
|
|
public function ajax_payback($request, $response, $args)
|
|
{
|
|
$datatables = new Datatables(new DatatablesHelper());
|
|
$datatables->query('Select payback.id,payback.total,payback.userid as event_user_id,event_user.user_name as event_user_name,payback.ref_by as ref_user_id,ref_user.user_name as ref_user_name,payback.ref_get,payback.datetime from payback,user as event_user,user as ref_user where event_user.id = payback.userid and ref_user.id = payback.ref_by');
|
|
|
|
$datatables->edit('datetime', function ($data) {
|
|
return date('Y-m-d H:i:s', $data['datetime']);
|
|
});
|
|
|
|
$datatables->edit('event_user_name', function ($data) {
|
|
return AdminDataMasker::account($data['event_user_name']);
|
|
});
|
|
|
|
$datatables->edit('ref_user_name', function ($data) {
|
|
return AdminDataMasker::account($data['ref_user_name']);
|
|
});
|
|
|
|
$body = $response->getBody();
|
|
$body->write($datatables->generate());
|
|
}
|
|
|
|
public function ajax_coupon($request, $response, $args)
|
|
{
|
|
$datatables = new Datatables(new DatatablesHelper());
|
|
$selectCode = AdminPermission::isSuperAdmin() ? ',code' : '';
|
|
$datatables->query('Select id'.$selectCode.',expire,shop,credit from coupon');
|
|
|
|
$datatables->edit('expire', function ($data) {
|
|
return date('Y-m-d H:i:s', $data['expire']);
|
|
});
|
|
|
|
$body = $response->getBody();
|
|
$body->write($datatables->generate());
|
|
}
|
|
|
|
public function getIncome($request, $response, $args)
|
|
{
|
|
$date = $request->getParam('date');
|
|
$date_in_timestamp = strtotime($date);
|
|
//$date_in_timestamp = strtotime('2019-12-12');
|
|
|
|
$stats = new Analytics();
|
|
|
|
$today_income = $stats->getIncome($date_in_timestamp, $date_in_timestamp+86400);
|
|
$today_new_user_income = $stats->getNewUserIncome($date_in_timestamp, $date_in_timestamp+86400);
|
|
$yesterday_income = $stats->getIncome($date_in_timestamp-86400, $date_in_timestamp);
|
|
$yesterday_new_user_income = $stats->getNewUserIncome($date_in_timestamp-86400, $date_in_timestamp);
|
|
$first_day_of_this_week_timestamp = strtotime("last monday midnight",$date_in_timestamp);
|
|
$first_day_of_next_week_timestamp = strtotime("next monday midnight",$date_in_timestamp);
|
|
$this_week_income = $stats->getIncome($first_day_of_this_week_timestamp, $first_day_of_next_week_timestamp);
|
|
$this_week_new_user_income = $stats->getNewUserIncome($first_day_of_this_week_timestamp, $first_day_of_next_week_timestamp);
|
|
|
|
$first_day_of_last_week_timestamp = strtotime("last monday midnight",$date_in_timestamp-604800);
|
|
$last_week_income = $stats->getIncome($first_day_of_last_week_timestamp, $first_day_of_this_week_timestamp);
|
|
$last_week_new_user_income = $stats->getNewUserIncome($first_day_of_last_week_timestamp, $first_day_of_this_week_timestamp);
|
|
|
|
$first_day_of_this_month_timestamp = strtotime("first day of this month",$date_in_timestamp);
|
|
$first_day_of_next_month_timestamp = strtotime("first day of next month",$date_in_timestamp);
|
|
$this_month_income = $stats->getIncome($first_day_of_this_month_timestamp, $first_day_of_next_month_timestamp);
|
|
$this_month_new_user_income = $stats->getNewUserIncome($first_day_of_this_month_timestamp, $first_day_of_next_month_timestamp);
|
|
|
|
$first_day_of_last_month_timestamp = strtotime("first day of last month",$date_in_timestamp);
|
|
$last_moneth_income = $stats->getIncome($first_day_of_last_month_timestamp, $first_day_of_this_month_timestamp);
|
|
$last_moneth_new_user_income = $stats->getNewUserIncome($first_day_of_last_month_timestamp, $first_day_of_this_month_timestamp);
|
|
|
|
$res['success'] = true;
|
|
$res['error'] = '';
|
|
$res['message'] = '';
|
|
$res['data'] = array(
|
|
"date" => $date,
|
|
"todayIncome" => $today_income,
|
|
"yesterdayIncome" => $yesterday_income,
|
|
"thisWeekIncome" => $this_week_income,
|
|
"lastWeekIncome" => $last_week_income,
|
|
"thisMonthIncome" => $this_month_income,
|
|
"lastMonthIncome" => $last_moneth_income,
|
|
"todayNewUserIncome" => $today_new_user_income,
|
|
"yesterdayNewUserIncome" => $yesterday_new_user_income,
|
|
"thisWeekNewUserIncome" => $this_week_new_user_income,
|
|
"lastWeekNewUserIncome" => $last_week_new_user_income,
|
|
"thisMonthNewUserIncome" => $this_month_new_user_income,
|
|
"lastMonthNewUserIncome" => $last_moneth_new_user_income
|
|
);
|
|
return $response->getBody()->write(json_encode($res));
|
|
}
|
|
|
|
public function monthIncome($request, $response, $args)
|
|
{
|
|
$month = $request->getParam('month');
|
|
if (!is_string($month) || !preg_match('/^\d{4}-\d{2}$/', $month)) {
|
|
$month = date('Y-m');
|
|
}
|
|
|
|
$monthStart = strtotime($month . '-01 00:00:00');
|
|
if ($monthStart === false) {
|
|
$month = date('Y-m');
|
|
$monthStart = strtotime($month . '-01 00:00:00');
|
|
}
|
|
|
|
$daysInMonth = (int) date('t', $monthStart);
|
|
$stats = new Analytics();
|
|
$days = [];
|
|
$total = 0;
|
|
|
|
for ($day = 1; $day <= $daysInMonth; $day++) {
|
|
$dayStart = strtotime($month . '-' . str_pad($day, 2, '0', STR_PAD_LEFT) . ' 00:00:00');
|
|
$income = (float) $stats->getIncome($dayStart, $dayStart + 86400);
|
|
$total += $income;
|
|
$days[] = [
|
|
'day' => $day,
|
|
'date' => date('Y-m-d', $dayStart),
|
|
'income' => $income
|
|
];
|
|
}
|
|
|
|
$res['success'] = true;
|
|
$res['error'] = '';
|
|
$res['message'] = '';
|
|
$res['data'] = [
|
|
'month' => $month,
|
|
'days' => $days,
|
|
'total' => $total,
|
|
'maxAxis' => 10000,
|
|
'ticks' => [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]
|
|
];
|
|
|
|
return $response->getBody()->write(json_encode($res));
|
|
}
|
|
|
|
public function monthNewUserPaidRate($request, $response, $args)
|
|
{
|
|
$month = $request->getParam('month');
|
|
if (!is_string($month) || !preg_match('/^\d{4}-\d{2}$/', $month)) {
|
|
$month = date('Y-m');
|
|
}
|
|
|
|
$monthStart = strtotime($month . '-01 00:00:00');
|
|
if ($monthStart === false) {
|
|
$month = date('Y-m');
|
|
$monthStart = strtotime($month . '-01 00:00:00');
|
|
}
|
|
|
|
$daysInMonth = (int) date('t', $monthStart);
|
|
$stats = new Analytics();
|
|
$days = [];
|
|
$totalUsers = 0;
|
|
$totalPaidUsers = 0;
|
|
|
|
for ($day = 1; $day <= $daysInMonth; $day++) {
|
|
$dayStart = strtotime($month . '-' . str_pad($day, 2, '0', STR_PAD_LEFT) . ' 00:00:00');
|
|
$newUsers = (int) $stats->getNewUsers($dayStart, $dayStart + 86400);
|
|
$paidUsers = (int) $stats->getNewPaidUsers($dayStart, $dayStart + 86400);
|
|
$rate = $newUsers > 0 ? round($paidUsers / $newUsers * 100, 2) : 0;
|
|
|
|
$totalUsers += $newUsers;
|
|
$totalPaidUsers += $paidUsers;
|
|
$days[] = [
|
|
'day' => $day,
|
|
'date' => date('Y-m-d', $dayStart),
|
|
'newUsers' => $newUsers,
|
|
'paidUsers' => $paidUsers,
|
|
'rate' => $rate
|
|
];
|
|
}
|
|
|
|
$res['success'] = true;
|
|
$res['error'] = '';
|
|
$res['message'] = '';
|
|
$res['data'] = [
|
|
'month' => $month,
|
|
'days' => $days,
|
|
'totalUsers' => $totalUsers,
|
|
'totalPaidUsers' => $totalPaidUsers,
|
|
'rate' => $totalUsers > 0 ? round($totalPaidUsers / $totalUsers * 100, 2) : 0,
|
|
'maxAxis' => 100,
|
|
'ticks' => [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|
|
];
|
|
|
|
return $response->getBody()->write(json_encode($res));
|
|
}
|
|
|
|
public function newUsers($request, $response, $args)
|
|
{
|
|
$date = $request->getParam('date');
|
|
$date_in_timestamp = strtotime($date);
|
|
|
|
$stats = new Analytics();
|
|
|
|
$today_users = $stats->getNewUsers($date_in_timestamp, $date_in_timestamp+86400);
|
|
$today_new_paid_users = $stats->getNewPaidUsers($date_in_timestamp, $date_in_timestamp+86400);
|
|
$yesterday_users = $stats->getNewUsers($date_in_timestamp-86400, $date_in_timestamp);
|
|
$yesterday_new_paid_users = $stats->getNewPaidUsers($date_in_timestamp-86400, $date_in_timestamp);
|
|
|
|
$first_day_of_this_week_timestamp = strtotime("last monday midnight",$date_in_timestamp);
|
|
$first_day_of_next_week_timestamp = strtotime("next monday midnight",$date_in_timestamp);
|
|
$this_week_users = $stats->getNewUsers($first_day_of_this_week_timestamp, $first_day_of_next_week_timestamp);
|
|
$this_week_new_paid_users = $stats->getNewPaidUsers($first_day_of_this_week_timestamp, $first_day_of_next_week_timestamp);
|
|
$first_day_of_last_week_timestamp = strtotime("last monday midnight",$date_in_timestamp-604800);
|
|
$last_week_users = $stats->getNewUsers($first_day_of_last_week_timestamp, $first_day_of_this_week_timestamp);
|
|
$last_week_new_paid_users = $stats->getNewPaidUsers($first_day_of_last_week_timestamp, $first_day_of_this_week_timestamp);
|
|
|
|
$first_day_of_this_month_timestamp = strtotime("first day of this month",$date_in_timestamp);
|
|
$first_day_of_next_month_timestamp = strtotime("first day of next month",$date_in_timestamp);
|
|
$this_month_users = $stats->getNewUsers($first_day_of_this_month_timestamp, $first_day_of_next_month_timestamp);
|
|
$this_month_new_paid_users = $stats->getNewPaidUsers($first_day_of_this_month_timestamp, $first_day_of_next_month_timestamp);
|
|
$first_day_of_last_month_timestamp = strtotime("first day of last month",$date_in_timestamp);
|
|
$last_moneth_users = $stats->getNewUsers($first_day_of_last_month_timestamp, $first_day_of_this_month_timestamp);
|
|
$last_moneth_new_paid_users = $stats->getNewPaidUsers($first_day_of_last_month_timestamp, $first_day_of_this_month_timestamp);
|
|
|
|
$res['success'] = true;
|
|
$res['error'] = '';
|
|
$res['message'] = '';
|
|
$res['data'] = array(
|
|
"date" => $date,
|
|
"today" => $today_users,
|
|
"yesterday" => $yesterday_users,
|
|
"thisWeek" => $this_week_users,
|
|
"lastWeek" => $last_week_users,
|
|
"thisMonth" => $this_month_users,
|
|
"lastMonth" => $last_moneth_users,
|
|
"todayPaid" => $today_new_paid_users,
|
|
"yesterdayPaid" => $yesterday_new_paid_users,
|
|
"thisWeekPaid" => $this_week_new_paid_users,
|
|
"lastWeekPaid" => $last_week_new_paid_users,
|
|
"thisMonthPaid" => $this_month_new_paid_users,
|
|
"lastMonthPaid" => $last_moneth_new_paid_users
|
|
);
|
|
return $response->getBody()->write(json_encode($res));
|
|
}
|
|
}
|