优化首页和收货地址管理
This commit is contained in:
@@ -5,6 +5,10 @@ declare(strict_types=1);
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\model\MallPlayxClaimLog;
|
||||
use app\common\model\MallPlayxOrder;
|
||||
use app\common\model\MallPlayxUserAsset;
|
||||
use support\think\Db;
|
||||
use Webman\Http\Request;
|
||||
use support\Response;
|
||||
|
||||
@@ -15,8 +19,78 @@ class Dashboard extends Backend
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) return $response;
|
||||
|
||||
$now = time();
|
||||
$todayStart = strtotime(date('Y-m-d', $now) . ' 00:00:00');
|
||||
$yesterdayStart = $todayStart - 86400;
|
||||
|
||||
$newPlayersToday = MallPlayxUserAsset::where('create_time', '>=', $todayStart)
|
||||
->where('create_time', '<=', $now)
|
||||
->count();
|
||||
|
||||
$yesterdayPointsClaimed = MallPlayxClaimLog::where('create_time', '>=', $yesterdayStart)
|
||||
->where('create_time', '<', $todayStart)
|
||||
->sum('claimed_amount');
|
||||
|
||||
$yesterdayRedeemQuery = MallPlayxOrder::where('create_time', '>=', $yesterdayStart)
|
||||
->where('create_time', '<', $todayStart);
|
||||
|
||||
$yesterdayRedeemCount = (clone $yesterdayRedeemQuery)->count();
|
||||
$yesterdayRedeemPointsCostSum = (clone $yesterdayRedeemQuery)->sum('points_cost');
|
||||
$yesterdayRedeemAmountSum = (clone $yesterdayRedeemQuery)->sum('amount');
|
||||
$yesterdayRedeemCompletedCount = (clone $yesterdayRedeemQuery)
|
||||
->where('status', MallPlayxOrder::STATUS_COMPLETED)
|
||||
->count();
|
||||
$yesterdayRedeemRejectedCount = (clone $yesterdayRedeemQuery)
|
||||
->where('status', MallPlayxOrder::STATUS_REJECTED)
|
||||
->count();
|
||||
|
||||
$yesterdayRedeemByItem = Db::name('mall_playx_order')
|
||||
->alias('o')
|
||||
->leftJoin('mall_item i', 'i.id = o.mall_item_id')
|
||||
->where('o.create_time', '>=', $yesterdayStart)
|
||||
->where('o.create_time', '<', $todayStart)
|
||||
->group('o.mall_item_id, i.title')
|
||||
->field([
|
||||
'o.mall_item_id',
|
||||
'i.title',
|
||||
Db::raw('COUNT(*) as order_count'),
|
||||
Db::raw('SUM(o.points_cost) as points_cost_sum'),
|
||||
Db::raw('SUM(o.amount) as amount_sum'),
|
||||
Db::raw('SUM(CASE WHEN o.status = "COMPLETED" THEN 1 ELSE 0 END) as completed_count'),
|
||||
Db::raw('SUM(CASE WHEN o.status = "REJECTED" THEN 1 ELSE 0 END) as rejected_count'),
|
||||
])
|
||||
->orderRaw('order_count DESC')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$pendingPhysicalToShip = MallPlayxOrder::where('type', MallPlayxOrder::TYPE_PHYSICAL)
|
||||
->where('status', MallPlayxOrder::STATUS_PENDING)
|
||||
->count();
|
||||
$grantFailedRetryableCount = MallPlayxOrder::whereIn('type', [MallPlayxOrder::TYPE_BONUS, MallPlayxOrder::TYPE_WITHDRAW])
|
||||
->where('grant_status', MallPlayxOrder::GRANT_FAILED_RETRYABLE)
|
||||
->count();
|
||||
|
||||
return $this->success('', [
|
||||
'remark' => get_route_remark()
|
||||
'remark' => get_route_remark(),
|
||||
'playx' => [
|
||||
'time_range' => [
|
||||
'today_start' => $todayStart,
|
||||
'yesterday_start' => $yesterdayStart,
|
||||
'now' => $now,
|
||||
],
|
||||
'new_players_today' => $newPlayersToday,
|
||||
'yesterday_points_claimed' => $yesterdayPointsClaimed,
|
||||
'yesterday_redeem' => [
|
||||
'order_count' => $yesterdayRedeemCount,
|
||||
'points_cost_sum' => $yesterdayRedeemPointsCostSum,
|
||||
'amount_sum' => $yesterdayRedeemAmountSum,
|
||||
'completed_count' => $yesterdayRedeemCompletedCount,
|
||||
'rejected_count' => $yesterdayRedeemRejectedCount,
|
||||
'by_item' => $yesterdayRedeemByItem,
|
||||
],
|
||||
'pending_physical_to_ship' => $pendingPhysicalToShip,
|
||||
'grant_failed_retryable' => $grantFailedRetryableCount,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use app\common\model\MallPlayxDailyPush;
|
||||
use app\common\model\MallPlayxSession;
|
||||
use app\common\model\MallPlayxOrder;
|
||||
use app\common\model\MallPlayxUserAsset;
|
||||
use app\common\model\MallAddress;
|
||||
use support\think\Db;
|
||||
use Webman\Http\Request;
|
||||
use support\Response;
|
||||
@@ -675,6 +676,197 @@ class Playx extends Api
|
||||
return $this->success('', ['list' => $list->toArray()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收货地址列表
|
||||
* GET /api/v1/playx/address/list?session_id=xxx
|
||||
*/
|
||||
public function addressList(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeApi($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$assetId = $this->resolvePlayxAssetIdFromRequest($request);
|
||||
if ($assetId === null) {
|
||||
return $this->error(__('Invalid token'), null, 0, ['statusCode' => 401]);
|
||||
}
|
||||
|
||||
$list = MallAddress::where('playx_user_asset_id', $assetId)
|
||||
->order('default_setting', 'desc')
|
||||
->order('id', 'desc')
|
||||
->select();
|
||||
|
||||
return $this->success('', ['list' => $list->toArray()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加收货地址(可设置默认)
|
||||
* POST /api/v1/playx/address/add
|
||||
*/
|
||||
public function addressAdd(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeApi($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$assetId = $this->resolvePlayxAssetIdFromRequest($request);
|
||||
if ($assetId === null) {
|
||||
return $this->error(__('Invalid token'), null, 0, ['statusCode' => 401]);
|
||||
}
|
||||
|
||||
$phone = trim(strval($request->post('phone', '')));
|
||||
$region = $request->post('region', '');
|
||||
$detailAddress = trim(strval($request->post('detail_address', '')));
|
||||
$address = trim(strval($request->post('address', '')));
|
||||
$defaultSetting = strval($request->post('default_setting', '0')) === '1' ? 1 : 0;
|
||||
|
||||
if ($phone === '' || $detailAddress === '' || $address === '' || $region === '' || $region === null) {
|
||||
return $this->error(__('Missing required fields'));
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
if ($defaultSetting === 1) {
|
||||
MallAddress::where('playx_user_asset_id', $assetId)->update(['default_setting' => 0]);
|
||||
}
|
||||
|
||||
$created = MallAddress::create([
|
||||
'playx_user_asset_id' => $assetId,
|
||||
'phone' => $phone,
|
||||
'region' => $region,
|
||||
'detail_address' => $detailAddress,
|
||||
'address' => $address,
|
||||
'default_setting' => $defaultSetting,
|
||||
'create_time' => time(),
|
||||
'update_time' => time(),
|
||||
]);
|
||||
|
||||
Db::commit();
|
||||
} catch (\Throwable $e) {
|
||||
Db::rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
|
||||
return $this->success('', [
|
||||
'id' => $created ? $created->id : null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改收货地址(包含设置默认地址)
|
||||
* POST /api/v1/playx/address/edit
|
||||
*/
|
||||
public function addressEdit(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeApi($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$assetId = $this->resolvePlayxAssetIdFromRequest($request);
|
||||
if ($assetId === null) {
|
||||
return $this->error(__('Invalid token'), null, 0, ['statusCode' => 401]);
|
||||
}
|
||||
|
||||
$id = intval($request->post('id', 0));
|
||||
if ($id <= 0) {
|
||||
return $this->error(__('Missing required fields'));
|
||||
}
|
||||
|
||||
$row = MallAddress::where('id', $id)->where('playx_user_asset_id', $assetId)->find();
|
||||
if (!$row) {
|
||||
return $this->error(__('Record not found'));
|
||||
}
|
||||
|
||||
$updates = [];
|
||||
if ($request->post('phone', null) !== null) {
|
||||
$updates['phone'] = trim(strval($request->post('phone', '')));
|
||||
}
|
||||
if ($request->post('region', null) !== null) {
|
||||
$updates['region'] = $request->post('region', '');
|
||||
}
|
||||
if ($request->post('detail_address', null) !== null) {
|
||||
$updates['detail_address'] = trim(strval($request->post('detail_address', '')));
|
||||
}
|
||||
if ($request->post('address', null) !== null) {
|
||||
$updates['address'] = trim(strval($request->post('address', '')));
|
||||
}
|
||||
if ($request->post('default_setting', null) !== null) {
|
||||
$updates['default_setting'] = strval($request->post('default_setting', '0')) === '1' ? 1 : 0;
|
||||
}
|
||||
|
||||
if (empty($updates)) {
|
||||
return $this->success('', ['updated' => false]);
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
if (isset($updates['default_setting']) && $updates['default_setting'] === 1) {
|
||||
MallAddress::where('playx_user_asset_id', $assetId)->update(['default_setting' => 0]);
|
||||
}
|
||||
$updates['update_time'] = time();
|
||||
MallAddress::where('id', $id)->where('playx_user_asset_id', $assetId)->update($updates);
|
||||
Db::commit();
|
||||
} catch (\Throwable $e) {
|
||||
Db::rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
|
||||
return $this->success('', ['updated' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除收货地址
|
||||
* POST /api/v1/playx/address/delete
|
||||
*/
|
||||
public function addressDelete(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeApi($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$assetId = $this->resolvePlayxAssetIdFromRequest($request);
|
||||
if ($assetId === null) {
|
||||
return $this->error(__('Invalid token'), null, 0, ['statusCode' => 401]);
|
||||
}
|
||||
|
||||
$id = intval($request->post('id', 0));
|
||||
if ($id <= 0) {
|
||||
return $this->error(__('Missing required fields'));
|
||||
}
|
||||
|
||||
$row = MallAddress::where('id', $id)->where('playx_user_asset_id', $assetId)->find();
|
||||
if (!$row) {
|
||||
return $this->error(__('Record not found'));
|
||||
}
|
||||
|
||||
$wasDefault = intval($row->default_setting ?? 0) === 1;
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
MallAddress::where('id', $id)->where('playx_user_asset_id', $assetId)->delete();
|
||||
|
||||
if ($wasDefault) {
|
||||
$fallback = MallAddress::where('playx_user_asset_id', $assetId)->order('id', 'desc')->find();
|
||||
if ($fallback) {
|
||||
$fallback->default_setting = 1;
|
||||
$fallback->update_time = time();
|
||||
$fallback->save();
|
||||
}
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
} catch (\Throwable $e) {
|
||||
Db::rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
|
||||
return $this->success('', ['deleted' => true]);
|
||||
}
|
||||
|
||||
private function formatAsset(?MallPlayxUserAsset $asset): array
|
||||
{
|
||||
if (!$asset) {
|
||||
|
||||
Reference in New Issue
Block a user