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.
virt/web/app/Controllers/AdminController.php

452 lines
17 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\Models\Bought;
use App\Models\Shop;
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)
{
$sts = new Analytics();
//每日数据
$days = [];
for ($i = 7; $i >= 1; $i--) {
$day = date("Y-m-d", strtotime("-$i days"));
$days[] = $day;
}
return $this->view()->assign(['sts' => $sts, 'days' => $days])->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 packageSales($request, $response, $args)
{
$date = $request->getParam('date');
if (!is_string($date) || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
$date = date('Y-m-d');
}
$dateStart = strtotime($date . ' 00:00:00');
if ($dateStart === false) {
$date = date('Y-m-d');
$dateStart = strtotime($date . ' 00:00:00');
}
$month = $request->getParam('month');
if (!is_string($month) || !preg_match('/^\d{4}-\d{2}$/', $month)) {
$month = date('Y-m', $dateStart);
}
$monthStart = strtotime($month . '-01 00:00:00');
if ($monthStart === false) {
$month = date('Y-m', $dateStart);
$monthStart = strtotime($month . '-01 00:00:00');
}
$dayBoughts = Bought::where('datetime', '>=', $dateStart)
->where('datetime', '<', $dateStart + 86400)
->get();
$monthBoughts = Bought::where('datetime', '>=', $monthStart)
->where('datetime', '<', strtotime('first day of next month', $monthStart))
->get();
$dayItems = $this->buildPackageSalesItems($dayBoughts);
$monthItems = $this->buildPackageSalesItems($monthBoughts);
usort($dayItems, [$this, 'sortPackageSalesItems']);
usort($monthItems, [$this, 'sortPackageSalesItems']);
$res['success'] = true;
$res['error'] = '';
$res['message'] = '';
$res['data'] = [
'date' => $date,
'month' => $month,
'todayTotal' => $dayBoughts->count(),
'todayItems' => $dayItems,
'monthTotal' => $monthBoughts->count(),
'monthIncome' => round((float) $monthBoughts->sum('price'), 2),
'monthTop' => array_slice($monthItems, 0, 5)
];
return $response->getBody()->write(json_encode($res));
}
private function buildPackageSalesItems($boughts)
{
$groups = [];
foreach ($boughts as $bought) {
$shopId = (int) $bought->shopid;
if (!isset($groups[$shopId])) {
$groups[$shopId] = [
'shopId' => $shopId,
'name' => '套餐 #' . $shopId,
'count' => 0,
'income' => 0,
'users' => []
];
}
$groups[$shopId]['count']++;
$groups[$shopId]['income'] += (float) $bought->price;
$groups[$shopId]['users'][(int) $bought->userid] = true;
}
if (count($groups) > 0) {
$shops = Shop::whereIn('id', array_keys($groups))->get()->keyBy('id');
foreach ($groups as $shopId => &$item) {
$shop = $shops->get($shopId);
if ($shop != null) {
$item['name'] = $shop->name;
}
}
unset($item);
}
$items = [];
foreach ($groups as $item) {
$item['income'] = round($item['income'], 2);
$item['buyers'] = count($item['users']);
unset($item['users']);
$items[] = $item;
}
return $items;
}
private function sortPackageSalesItems($left, $right)
{
if ($left['count'] === $right['count']) {
if ($left['income'] === $right['income']) {
return strcmp($left['name'], $right['name']);
}
return $left['income'] < $right['income'] ? 1 : -1;
}
return $left['count'] < $right['count'] ? 1 : -1;
}
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));
}
}