优化导入,格式化代码
分离出充值系统 修复了充值的问题
This commit is contained in:
parent
1be6dea757
commit
4918efa597
@ -48,7 +48,7 @@ public function handle()
|
||||
|
||||
$user = User::find($user_id);
|
||||
|
||||
$this->warn('扣除金额: ' . $user->balance . ' 元');
|
||||
$this->warn('扣除金额: ' . $amount . ' 元');
|
||||
|
||||
$this->warn('用户当前余额:' . $user->balance . ' 元');
|
||||
|
||||
|
@ -109,7 +109,6 @@ public function handle()
|
||||
$this->info('Redis 总命令数: ' . $redis_total_commands_processed);
|
||||
|
||||
|
||||
|
||||
$this->warn('===== 莱云 统计 =====');
|
||||
$this->warn('要获取 莱云 统计信息,请运行 count 命令。');
|
||||
|
||||
|
@ -18,6 +18,7 @@ class Kernel extends ConsoleKernel
|
||||
* Define the application's command schedule.
|
||||
*
|
||||
* @param \Illuminate\Console\Scheduling\Schedule $schedule
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Throwable;
|
||||
|
||||
|
@ -5,6 +5,25 @@
|
||||
trait ApiResponse
|
||||
{
|
||||
// RESTful API response
|
||||
public function remoteResponse($response, $status = 200)
|
||||
{
|
||||
return response()->json($response, $status)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
public function notFound($message = 'Not found')
|
||||
{
|
||||
return $this->error($message, 404);
|
||||
}
|
||||
|
||||
// success
|
||||
|
||||
public function error($message = '', $code = 400)
|
||||
{
|
||||
return $this->apiResponse($message, $code);
|
||||
}
|
||||
|
||||
// error
|
||||
|
||||
public function apiResponse($data = [], $status = 200)
|
||||
{
|
||||
// if data is paginated, return paginated data
|
||||
@ -34,55 +53,44 @@ public function apiResponse($data = [], $status = 200)
|
||||
return response()->json($data, $status)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
public function remoteResponse($response, $status = 200)
|
||||
{
|
||||
return response()->json($response, $status)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
// success
|
||||
public function success($data = [])
|
||||
{
|
||||
return $this->apiResponse($data, 200);
|
||||
}
|
||||
|
||||
// error
|
||||
public function error($message = '', $code = 400)
|
||||
{
|
||||
return $this->apiResponse($message, $code);
|
||||
}
|
||||
|
||||
|
||||
// not found
|
||||
public function notFound($message = 'Not found')
|
||||
{
|
||||
return $this->error($message, 404);
|
||||
}
|
||||
|
||||
// forbidden
|
||||
public function forbidden($message = 'Forbidden')
|
||||
{
|
||||
return $this->error($message, 403);
|
||||
}
|
||||
|
||||
// unauthorized
|
||||
// forbidden
|
||||
|
||||
public function unauthorized($message = 'Unauthorized')
|
||||
{
|
||||
return $this->error($message, 401);
|
||||
}
|
||||
|
||||
// bad request
|
||||
// unauthorized
|
||||
|
||||
public function badRequest($message = 'Bad request')
|
||||
{
|
||||
return $this->error($message, 400);
|
||||
}
|
||||
|
||||
// created
|
||||
// bad request
|
||||
|
||||
public function created($message = 'Created')
|
||||
{
|
||||
return $this->success($message, 201);
|
||||
}
|
||||
|
||||
// created
|
||||
|
||||
public function success($data = [])
|
||||
{
|
||||
return $this->apiResponse($data, 200);
|
||||
}
|
||||
|
||||
// accepted
|
||||
|
||||
public function accepted($message = 'Accepted')
|
||||
{
|
||||
return $this->success($message, 202);
|
||||
|
@ -6,8 +6,10 @@
|
||||
use Illuminate\Contracts\Cache\LockTimeoutException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
trait Lock {
|
||||
public function await($name, Closure $callback) {
|
||||
trait Lock
|
||||
{
|
||||
public function await($name, Closure $callback)
|
||||
{
|
||||
// if env is local
|
||||
if (env('APP_ENV') == 'local') {
|
||||
return $callback();
|
||||
|
@ -20,7 +20,8 @@ public function index()
|
||||
}
|
||||
}
|
||||
|
||||
public function login(Request $request) {
|
||||
public function login(Request $request)
|
||||
{
|
||||
if (auth('admin')->attempt($request->only('email', 'password'))) {
|
||||
return redirect()->route('admin.index');
|
||||
} else {
|
||||
|
@ -31,6 +31,7 @@ public function create()
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
@ -42,6 +43,7 @@ public function store(Request $request)
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
@ -53,6 +55,7 @@ public function show($id)
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
@ -65,6 +68,7 @@ public function edit($id)
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
@ -76,6 +80,7 @@ public function update(Request $request, $id)
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
|
@ -2,134 +2,30 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Exceptions\ChargeException;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Balance;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Yansongda\LaravelPay\Facades\Pay;
|
||||
use Yansongda\Pay\Exception\InvalidResponseException;
|
||||
use function auth;
|
||||
use function config;
|
||||
use function now;
|
||||
use function route;
|
||||
use function view;
|
||||
|
||||
class BalanceController extends Controller
|
||||
{
|
||||
//
|
||||
|
||||
public function index(Request $request)
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$balances = Balance::thisUser()->simplePaginate(30);
|
||||
return $this->success($balances);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
// 充值
|
||||
$this->validate($request, [
|
||||
'amount' => 'required|integer|min:1|max:10000',
|
||||
]);
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
$balance = new Balance();
|
||||
|
||||
|
||||
$data = [
|
||||
'user_id' => $user->id,
|
||||
'amount' => $request->amount,
|
||||
'payment' => 'alipay',
|
||||
];
|
||||
|
||||
// if local
|
||||
// if (env('APP_ENV') == 'local') {
|
||||
// $data['payment'] = null;
|
||||
// $data['paid_at'] = now();
|
||||
// }
|
||||
|
||||
|
||||
$balance = $balance->create($data);
|
||||
|
||||
// 生成 18 位订单号
|
||||
$order_id = date('YmdHis') . $balance->id . rand(1000, 9999);
|
||||
$balance->order_id = $order_id;
|
||||
|
||||
$balance->save();
|
||||
|
||||
$balance = $balance->toArray();
|
||||
$balance['pay_url'] = route('balances.pay.show', ['balance' => $balance['order_id']]);
|
||||
|
||||
return $this->success($balance);
|
||||
}
|
||||
|
||||
|
||||
public function show(Balance $balance)
|
||||
{
|
||||
if ($balance->paid_at !== null) {
|
||||
return $this->error('订单已支付');
|
||||
}
|
||||
|
||||
if (now()->diffInDays($balance->created_at) > 1) {
|
||||
return $this->error('订单已失效');
|
||||
}
|
||||
|
||||
$pay = Pay::alipay()->web([
|
||||
'out_trade_no' => $balance->order_id,
|
||||
'total_amount' => $balance->amount,
|
||||
'subject' => config('app.display_name') . ' 充值',
|
||||
]);
|
||||
|
||||
return $pay;
|
||||
}
|
||||
|
||||
public function notify(Request $request)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'out_trade_no' => 'required',
|
||||
]);
|
||||
|
||||
// 检测订单是否存在
|
||||
$balance = Balance::where('order_id', $request->out_trade_no)->with('user')->first();
|
||||
if (!$balance) {
|
||||
return $this->notFound('找不到对应的订单');
|
||||
}
|
||||
|
||||
// 检测订单是否已支付
|
||||
if ($balance->paid_at !== null) {
|
||||
// return $this->success('订单已支付');
|
||||
return view('pay_process');
|
||||
}
|
||||
|
||||
// try {
|
||||
// $data = Pay::alipay()->callback();
|
||||
// } catch (InvalidResponseException $e) {
|
||||
// return $this->error('支付失败');
|
||||
// }
|
||||
|
||||
// // 检测 out_trade_no 是否为商户系统中创建的订单号
|
||||
// if ($data->out_trade_no != $balance->order_id) {
|
||||
// return $this->error('订单号不一致');
|
||||
// }
|
||||
|
||||
// if ((int) $data->total_amount != (int) $balance->amount) {
|
||||
// throw new ChargeException('金额不一致');
|
||||
// }
|
||||
|
||||
// // 验证 商户
|
||||
// if ($data['app_id'] != config('pay.alipay.default.app_id')) {
|
||||
// throw new ChargeException('商户不匹配');
|
||||
// }
|
||||
|
||||
if ($this->checkAndCharge($balance, true)) {
|
||||
return view('pay_process');
|
||||
} else {
|
||||
return $this->error('支付失败');
|
||||
}
|
||||
}
|
||||
|
||||
public function checkAndCharge(Balance $balance, $check = false)
|
||||
public function checkAndCharge(Balance $balance, $check = false): JsonResponse|bool
|
||||
{
|
||||
|
||||
if ($check) {
|
||||
@ -150,8 +46,13 @@ public function checkAndCharge(Balance $balance, $check = false)
|
||||
$balance->update([
|
||||
'paid_at' => now()
|
||||
]);
|
||||
} catch (InvalidResponseException) {
|
||||
} catch (InvalidResponseException $e) {
|
||||
Log::error($e->getMessage());
|
||||
return $this->error('无法验证支付结果。');
|
||||
} catch (ChargeException $e) {
|
||||
Log::error($e->getMessage());
|
||||
return $this->error('暂时无法处理充值。');
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -161,6 +62,7 @@ public function checkAndCharge(Balance $balance, $check = false)
|
||||
* 获取交易记录
|
||||
*
|
||||
* @param mixed $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function transactions(Request $request): JsonResponse
|
||||
|
@ -23,13 +23,6 @@ public function __construct()
|
||||
$this->http = Http::baseUrl($this->baseUrl . '/api')->throw();
|
||||
}
|
||||
|
||||
public function get($url)
|
||||
{
|
||||
$resp = $this->http->get($url)->json()['data'];
|
||||
return $resp;
|
||||
}
|
||||
|
||||
|
||||
public function announcements()
|
||||
{
|
||||
$resp = $this->cache(function () {
|
||||
@ -39,16 +32,6 @@ public function announcements()
|
||||
return $this->resp($resp);
|
||||
}
|
||||
|
||||
public function pinned()
|
||||
{
|
||||
$resp = $this->cache(function () {
|
||||
return $this->get('discussions?filter[tag]=pinned&page[offset]=0&sort=-createdAt');
|
||||
});
|
||||
|
||||
return $this->resp($resp);
|
||||
}
|
||||
|
||||
|
||||
public function cache(Closure $callback)
|
||||
{
|
||||
// 获取调用方法名
|
||||
@ -59,6 +42,11 @@ public function cache(Closure $callback)
|
||||
});
|
||||
}
|
||||
|
||||
public function get($url)
|
||||
{
|
||||
$resp = $this->http->get($url)->json()['data'];
|
||||
return $resp;
|
||||
}
|
||||
|
||||
public function resp($data)
|
||||
{
|
||||
@ -66,4 +54,13 @@ public function resp($data)
|
||||
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function pinned()
|
||||
{
|
||||
$resp = $this->cache(function () {
|
||||
return $this->get('discussions?filter[tag]=pinned&page[offset]=0&sort=-createdAt');
|
||||
});
|
||||
|
||||
return $this->resp($resp);
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,13 @@ public function broadcast_to_user(Request $request, User $user)
|
||||
|
||||
}
|
||||
|
||||
private function rules()
|
||||
{
|
||||
return [
|
||||
'message' => 'required',
|
||||
];
|
||||
}
|
||||
|
||||
public function broadcast_to_host(Request $request, Host $host)
|
||||
{
|
||||
$this->validate($request, $this->rules());
|
||||
@ -37,10 +44,4 @@ public function broadcast_to_host(Request $request, Host $host)
|
||||
|
||||
return $this->created($request->message);
|
||||
}
|
||||
|
||||
private function rules() {
|
||||
return [
|
||||
'message' => 'required',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,22 @@ public function index()
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function calcModule(Module|Authenticatable $module): array
|
||||
{
|
||||
|
||||
$default = [
|
||||
'balance' => 0,
|
||||
'drops' => 0,
|
||||
];
|
||||
|
||||
return [
|
||||
'transactions' => [
|
||||
'this_month' => Cache::get('this_month_balance_and_drops_' . $module->id, $default),
|
||||
'last_month' => Cache::get('last_month_balance_and_drops_' . $module->id, $default),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function call(Request $request, Module $module)
|
||||
{
|
||||
$path = request()->path();
|
||||
@ -58,7 +74,6 @@ public function call(Request $request, Module $module)
|
||||
return $this->remoteResponse($response['json'], $response['status']);
|
||||
}
|
||||
|
||||
|
||||
public function exportCall(Request $request, Module $module): \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory
|
||||
{
|
||||
$path = request()->path();
|
||||
@ -76,21 +91,4 @@ public function exportCall(Request $request, Module $module): \Illuminate\Http\R
|
||||
|
||||
return $this->remoteResponse($response['json'], $response['status']);
|
||||
}
|
||||
|
||||
|
||||
public function calcModule(Module|Authenticatable $module): array
|
||||
{
|
||||
|
||||
$default = [
|
||||
'balance' => 0,
|
||||
'drops' => 0,
|
||||
];
|
||||
|
||||
return [
|
||||
'transactions' => [
|
||||
'this_month' => Cache::get('this_month_balance_and_drops_' . $module->id, $default),
|
||||
'last_month' => Cache::get('last_month_balance_and_drops_' . $module->id, $default),
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ public function index(Request $request)
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function store(Request $request)
|
||||
|
@ -10,7 +10,8 @@
|
||||
class WorkOrderController extends Controller
|
||||
{
|
||||
//
|
||||
public function index(Request $request, WorkOrder $workOrder) {
|
||||
public function index(Request $request, WorkOrder $workOrder)
|
||||
{
|
||||
// $work_orders = new WorkOrder();
|
||||
// // if route has user
|
||||
// if ($request->route('user')) {
|
||||
@ -28,7 +29,8 @@ public function index(Request $request, WorkOrder $workOrder) {
|
||||
|
||||
// }
|
||||
|
||||
public function show(WorkOrderRequest $request, WorkOrder $workOrder) {
|
||||
public function show(WorkOrderRequest $request, WorkOrder $workOrder)
|
||||
{
|
||||
return $this->success($workOrder);
|
||||
}
|
||||
|
||||
|
@ -1,15 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
namespace App\Http\Controllers\Web;
|
||||
|
||||
// use App\Helpers\ApiResponse;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use function back;
|
||||
use function config;
|
||||
use function now;
|
||||
use function redirect;
|
||||
use function session;
|
||||
use function view;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
137
app/Http/Controllers/Web/BalanceController.php
Normal file
137
app/Http/Controllers/Web/BalanceController.php
Normal file
@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Balance;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Yansongda\LaravelPay\Facades\Pay;
|
||||
|
||||
class BalanceController extends Controller
|
||||
{
|
||||
//
|
||||
|
||||
public function index(Request $request): View
|
||||
{
|
||||
|
||||
$transaction = new Transaction();
|
||||
|
||||
$drops = $transaction->getDrops();
|
||||
|
||||
$balance = $request->user()->balance;
|
||||
|
||||
return view('balances.index', compact('drops', 'balance'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
// 充值
|
||||
$this->validate($request, [
|
||||
'amount' => 'required|integer|min:1|max:10000',
|
||||
]);
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
$balance = new Balance();
|
||||
|
||||
|
||||
$data = [
|
||||
'user_id' => $user->id,
|
||||
'amount' => $request->amount,
|
||||
'payment' => 'alipay',
|
||||
];
|
||||
|
||||
// if local
|
||||
// if (env('APP_ENV') == 'local') {
|
||||
// $data['payment'] = null;
|
||||
// $data['paid_at'] = now();
|
||||
// }
|
||||
|
||||
|
||||
$balance = $balance->create($data);
|
||||
|
||||
// 生成 18 位订单号
|
||||
$order_id = date('YmdHis') . $balance->id . rand(1000, 9999);
|
||||
$balance->order_id = $order_id;
|
||||
|
||||
$balance->save();
|
||||
|
||||
// $balances['pay_url'] = route('balances.balances.show', ['balances' => $balances['order_id']]);
|
||||
|
||||
return redirect()->route('balances.balances.show', ['balances' => $balance->order_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Laravel\Octane\Exceptions\DdException
|
||||
*/
|
||||
public function show(Balance $balance)
|
||||
{
|
||||
if ($balance->paid_at !== null) {
|
||||
return $this->error('订单已支付');
|
||||
}
|
||||
|
||||
if (now()->diffInDays($balance->created_at) > 1) {
|
||||
return $this->error('订单已失效');
|
||||
}
|
||||
|
||||
$web = Pay::alipay()->web([
|
||||
'out_trade_no' => $balance->order_id,
|
||||
'total_amount' => $balance->amount,
|
||||
'subject' => config('app.display_name') . ' 充值',
|
||||
]);
|
||||
|
||||
return view('balances.pay', ['html' => (string)$web->getBody()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function notify(Request $request): View|JsonResponse
|
||||
{
|
||||
$this->validate($request, [
|
||||
'out_trade_no' => 'required',
|
||||
]);
|
||||
|
||||
// 检测订单是否存在
|
||||
$balance = Balance::where('order_id', $request->out_trade_no)->with('user')->first();
|
||||
if (!$balance) {
|
||||
abort(404, '找不到订单。');
|
||||
}
|
||||
|
||||
// 检测订单是否已支付
|
||||
if ($balance->paid_at !== null) {
|
||||
// return $this->success('订单已支付');
|
||||
return view('pay_process');
|
||||
}
|
||||
|
||||
// try {
|
||||
// $data = Pay::alipay()->callback();
|
||||
// } catch (InvalidResponseException $e) {
|
||||
// return $this->error('支付失败');
|
||||
// }
|
||||
|
||||
// // 检测 out_trade_no 是否为商户系统中创建的订单号
|
||||
// if ($data->out_trade_no != $balances->order_id) {
|
||||
// return $this->error('订单号不一致');
|
||||
// }
|
||||
|
||||
// if ((int) $data->total_amount != (int) $balances->amount) {
|
||||
// throw new ChargeException('金额不一致');
|
||||
// }
|
||||
|
||||
// // 验证 商户
|
||||
// if ($data['app_id'] != config('balances.alipay.default.app_id')) {
|
||||
// throw new ChargeException('商户不匹配');
|
||||
// }
|
||||
|
||||
if ((new \App\Http\Controllers\Api\BalanceController())->checkAndCharge($balance, true)) {
|
||||
return view('pay_process');
|
||||
} else {
|
||||
abort(500, '支付失败');
|
||||
}
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ class Authenticate extends Middleware
|
||||
* Get the path the user should be redirected to when they are not authenticated.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected function redirectTo($request)
|
||||
|
@ -15,6 +15,7 @@ class RedirectIfAuthenticated
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
||||
* @param string|null ...$guards
|
||||
*
|
||||
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function handle(Request $request, Closure $next, ...$guards)
|
||||
|
@ -38,7 +38,7 @@ public function handle()
|
||||
|
||||
// this month transactions
|
||||
$this_month = [
|
||||
'balance' => $this_month->sum('outcome'),
|
||||
'balances' => $this_month->sum('outcome'),
|
||||
'drops' => $this_month->sum('outcome_drops')
|
||||
];
|
||||
|
||||
@ -48,7 +48,7 @@ public function handle()
|
||||
$last_moth = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
|
||||
|
||||
$last_moth = [
|
||||
'balance' => $last_moth->sum('outcome'),
|
||||
'balances' => $last_moth->sum('outcome'),
|
||||
'drops' => $last_moth->sum('outcome_drops')
|
||||
];
|
||||
|
||||
|
@ -17,6 +17,7 @@ class Host implements ShouldQueue
|
||||
|
||||
public HostModel $host;
|
||||
public string $type;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
|
@ -17,6 +17,7 @@ class Reply implements ShouldQueue
|
||||
use InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $reply;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
|
@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
|
||||
class Admin extends Authenticatable
|
||||
{
|
||||
|
@ -38,6 +38,55 @@ class Host extends Model
|
||||
|
||||
|
||||
// get user hosts
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::created(function ($model) {
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.created', $model));
|
||||
});
|
||||
|
||||
static::updating(function ($model) {
|
||||
if ($model->status == 'suspended') {
|
||||
$model->suspended_at = now();
|
||||
} else if ($model->status == 'running') {
|
||||
$model->suspended_at = null;
|
||||
}
|
||||
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.updating', $model));
|
||||
});
|
||||
|
||||
// when Updated
|
||||
static::updated(function ($model) {
|
||||
dispatch(new \App\Jobs\Module\Host($model, 'patch'));
|
||||
|
||||
Cache::forget('user_hosts_' . $model->user_id);
|
||||
Cache::forget('user_tasks_' . $model->user_id);
|
||||
|
||||
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.updated', $model));
|
||||
});
|
||||
|
||||
//
|
||||
// static::deleting(function ($model) {
|
||||
// broadcast(new UserEvent($model->user_id, 'hosts.deleting', $model));
|
||||
// });
|
||||
|
||||
static::deleting(function ($model) {
|
||||
Cache::forget('user_tasks_' . $model->user_id);
|
||||
});
|
||||
|
||||
static::deleted(function ($model) {
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.deleted', $model));
|
||||
Cache::forget('user_tasks_' . $model->user_id);
|
||||
Cache::forget('user_hosts_' . $model->user_id);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// user
|
||||
|
||||
public function getUserHosts($user_id = null)
|
||||
{
|
||||
return $this->where('user_id', $user_id)->with('module', function ($query) {
|
||||
@ -45,31 +94,35 @@ public function getUserHosts($user_id = null)
|
||||
})->get();
|
||||
}
|
||||
|
||||
// module
|
||||
|
||||
// user
|
||||
public function user(): BelongsToAlias
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// module
|
||||
// workOrders
|
||||
|
||||
public function module(): BelongsToAlias
|
||||
{
|
||||
return $this->belongsTo(Module::class);
|
||||
}
|
||||
|
||||
// workOrders
|
||||
// scope
|
||||
|
||||
public function workOrders(): HasManyAlias
|
||||
{
|
||||
return $this->hasMany(WorkOrder::class);
|
||||
}
|
||||
|
||||
// scope
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->where('status', 'running')->where('price', '!=', 0);
|
||||
}
|
||||
|
||||
|
||||
// cost
|
||||
|
||||
public function scopeThisUser($query, $module = null)
|
||||
{
|
||||
if ($module) {
|
||||
@ -79,8 +132,6 @@ public function scopeThisUser($query, $module = null)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// cost
|
||||
public function cost($price = null, $auto = true): bool
|
||||
{
|
||||
$this->load('user');
|
||||
@ -154,8 +205,7 @@ public function cost($price = null, $auto = true): bool
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public function costBalance($amount = 1)
|
||||
public function costBalance($amount = 1): bool
|
||||
{
|
||||
$transaction = new Transaction();
|
||||
|
||||
@ -185,49 +235,4 @@ public function costBalance($amount = 1)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::created(function ($model) {
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.created', $model));
|
||||
});
|
||||
|
||||
static::updating(function ($model) {
|
||||
if ($model->status == 'suspended') {
|
||||
$model->suspended_at = now();
|
||||
} else if ($model->status == 'running') {
|
||||
$model->suspended_at = null;
|
||||
}
|
||||
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.updating', $model));
|
||||
});
|
||||
|
||||
// when Updated
|
||||
static::updated(function ($model) {
|
||||
dispatch(new \App\Jobs\Module\Host($model, 'patch'));
|
||||
|
||||
Cache::forget('user_hosts_' . $model->user_id);
|
||||
Cache::forget('user_tasks_' . $model->user_id);
|
||||
|
||||
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.updated', $model));
|
||||
});
|
||||
|
||||
//
|
||||
// static::deleting(function ($model) {
|
||||
// broadcast(new UserEvent($model->user_id, 'hosts.deleting', $model));
|
||||
// });
|
||||
|
||||
static::deleting(function ($model) {
|
||||
Cache::forget('user_tasks_' . $model->user_id);
|
||||
});
|
||||
|
||||
static::deleted(function ($model) {
|
||||
broadcast(new UserEvent($model->user_id, 'hosts.deleted', $model));
|
||||
Cache::forget('user_tasks_' . $model->user_id);
|
||||
Cache::forget('user_hosts_' . $model->user_id);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -13,13 +13,12 @@ class Module extends Authenticatable
|
||||
{
|
||||
use Cachable;
|
||||
|
||||
protected $table = 'modules';
|
||||
public $incrementing = false;
|
||||
|
||||
// primary key
|
||||
public $incrementing = false;
|
||||
protected $keyType = 'string';
|
||||
public $timestamps = false;
|
||||
|
||||
protected $table = 'modules';
|
||||
protected $keyType = 'string';
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'name',
|
||||
@ -32,6 +31,16 @@ class Module extends Authenticatable
|
||||
'wecom_key'
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
static::creating(function ($model) {
|
||||
// if local
|
||||
if (!app()->environment('local')) {
|
||||
$model->api_token = Str::random(60);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function remoteHost($host_id, $func, $requests)
|
||||
{
|
||||
@ -44,6 +53,9 @@ public function remoteHost($host_id, $func, $requests)
|
||||
return [$json, $status];
|
||||
}
|
||||
|
||||
|
||||
// post, get, patch, delete 等请求
|
||||
|
||||
public function remote($func, $requests)
|
||||
{
|
||||
$http = Http::module($this->api_token, $this->url);
|
||||
@ -55,8 +67,6 @@ public function remote($func, $requests)
|
||||
return [$json, $status];
|
||||
}
|
||||
|
||||
|
||||
// post, get, patch, delete 等请求
|
||||
public function remoteRequest($method, $path, $requests)
|
||||
{
|
||||
$user = auth()->user();
|
||||
@ -115,8 +125,6 @@ public function moduleRequest($method, $path, $requests)
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function remotePost($path = '', $data = [])
|
||||
{
|
||||
$http = Http::module($this->api_token, $this->url);
|
||||
@ -128,6 +136,15 @@ public function remotePost($path = '', $data = [])
|
||||
return [$json, $status];
|
||||
}
|
||||
|
||||
|
||||
// // get cached modules
|
||||
// public static function cached_modules()
|
||||
// {
|
||||
// return Cache::remember('modules', 600, function () {
|
||||
// return Module::all();
|
||||
// });
|
||||
// }
|
||||
|
||||
public function check($module_id = null)
|
||||
{
|
||||
if ($module_id) {
|
||||
@ -150,24 +167,4 @@ public function check($module_id = null)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// // get cached modules
|
||||
// public static function cached_modules()
|
||||
// {
|
||||
// return Cache::remember('modules', 600, function () {
|
||||
// return Module::all();
|
||||
// });
|
||||
// }
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
static::creating(function ($model) {
|
||||
// if local
|
||||
if (!app()->environment('local')) {
|
||||
$model->api_token = Str::random(60);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ class PersonalAccessToken extends SanctumPersonalAccessToken
|
||||
* since we aren't going to use that column for anything.
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function save(array $options = [])
|
||||
|
@ -16,43 +16,21 @@ class Task extends Model
|
||||
{
|
||||
use HasFactory, Cachable;
|
||||
|
||||
public $incrementing = false;
|
||||
protected $fillable = [
|
||||
'host_id',
|
||||
'title',
|
||||
'progress',
|
||||
'status',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'string',
|
||||
'progress' => 'integer',
|
||||
];
|
||||
|
||||
public $incrementing = false;
|
||||
|
||||
// key type string
|
||||
protected $keyType = 'string';
|
||||
|
||||
public function scopeUser($query)
|
||||
{
|
||||
return $query->where('user_id', auth()->id());
|
||||
}
|
||||
|
||||
|
||||
public function getCurrentUserTasks()
|
||||
{
|
||||
return Cache::remember('user_tasks_' . auth()->id(), 3600, function () {
|
||||
return $this->user()->with('host')->latest()->get();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public function host()
|
||||
{
|
||||
return $this->belongsTo(Host::class);
|
||||
}
|
||||
|
||||
// before create
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
@ -82,7 +60,6 @@ protected static function boot()
|
||||
// dd($host);
|
||||
|
||||
|
||||
|
||||
$model->user_id = $model->host->user_id;
|
||||
|
||||
Cache::forget('user_tasks_' . $model->user_id);
|
||||
@ -117,4 +94,23 @@ protected static function boot()
|
||||
broadcast(new UserEvent($model->user_id, 'tasks.deleted', $model));
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeUser($query)
|
||||
{
|
||||
return $query->where('user_id', auth()->id());
|
||||
}
|
||||
|
||||
public function getCurrentUserTasks()
|
||||
{
|
||||
return Cache::remember('user_tasks_' . auth()->id(), 3600, function () {
|
||||
return $this->user()->with('host')->latest()->get();
|
||||
});
|
||||
}
|
||||
|
||||
// before create
|
||||
|
||||
public function host()
|
||||
{
|
||||
return $this->belongsTo(Host::class);
|
||||
}
|
||||
}
|
||||
|
@ -13,12 +13,11 @@ class Transaction extends Model
|
||||
{
|
||||
// $t = (new App\Models\Transaction)->create(['name' => 1])
|
||||
|
||||
const UPDATED_AT = null;
|
||||
protected $connection = 'mongodb';
|
||||
protected $collection = 'transactions';
|
||||
|
||||
// 停用 updated_at
|
||||
const UPDATED_AT = null;
|
||||
|
||||
protected $collection = 'transactions';
|
||||
protected $dates = [
|
||||
'created_at',
|
||||
'updated_at',
|
||||
@ -70,32 +69,11 @@ public function scopeThisUser($query)
|
||||
return $query->where('user_id', auth()->id());
|
||||
}
|
||||
|
||||
|
||||
public function getDrops($user_id = null): float
|
||||
{
|
||||
//
|
||||
if (!$user_id) {
|
||||
$user_id = auth()->id();
|
||||
}
|
||||
|
||||
$cache_key = 'user_drops_' . $user_id;
|
||||
|
||||
$drops = Cache::get($cache_key, [
|
||||
'drops' => 0,
|
||||
]);
|
||||
|
||||
// 保留 8 位
|
||||
$drops['drops'] = round($drops['drops'], 8);
|
||||
|
||||
return $drops['drops'];
|
||||
}
|
||||
|
||||
public function increaseCurrentUserDrops($amount = 0)
|
||||
{
|
||||
return $this->increaseDrops(auth()->id(), $amount);
|
||||
}
|
||||
|
||||
|
||||
public function increaseDrops($user_id, $amount = 0)
|
||||
{
|
||||
$cache_key = 'user_drops_' . $user_id;
|
||||
@ -111,7 +89,6 @@ public function increaseDrops($user_id, $amount = 0)
|
||||
return $current_drops['drops'];
|
||||
}
|
||||
|
||||
|
||||
public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount = 0)
|
||||
{
|
||||
|
||||
@ -136,7 +113,6 @@ public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount =
|
||||
$this->addPayoutDrops($user_id, $amount, $description, $host_id, $module_id);
|
||||
}
|
||||
|
||||
|
||||
public function addPayoutDrops($user_id, $amount, $description, $host_id, $module_id)
|
||||
{
|
||||
$data = [
|
||||
@ -163,186 +139,6 @@ public function addPayoutDrops($user_id, $amount, $description, $host_id, $modul
|
||||
return $this->addLog($user_id, $data);
|
||||
}
|
||||
|
||||
|
||||
public function addIncomeDrops($user_id, $amount, $description)
|
||||
{
|
||||
$data = [
|
||||
'type' => 'income',
|
||||
'payment' => 'balances',
|
||||
'description' => $description,
|
||||
'income' => 0,
|
||||
'income_drops' => (float)$amount,
|
||||
'outcome' => 0,
|
||||
'outcome_drops' => 0,
|
||||
];
|
||||
|
||||
return $this->addLog($user_id, $data);
|
||||
}
|
||||
|
||||
public function addIncomeBalances($user_id, $payment, $amount, $description)
|
||||
{
|
||||
$data = [
|
||||
'type' => 'income',
|
||||
'payment' => $payment,
|
||||
'description' => $description,
|
||||
'income' => (float)$amount,
|
||||
'income_drops' => 0,
|
||||
'outcome' => 0,
|
||||
'outcome_drops' => 0,
|
||||
];
|
||||
|
||||
return $this->addLog($user_id, $data);
|
||||
}
|
||||
|
||||
public function addPayoutBalances($user_id, $amount, $description, $module_id = null)
|
||||
{
|
||||
$data = [
|
||||
'type' => 'payout',
|
||||
'payment' => 'balances',
|
||||
'description' => $description,
|
||||
'income' => 0,
|
||||
'income_drops' => 0,
|
||||
'outcome' => (float)$amount,
|
||||
'outcome_drops' => 0
|
||||
];
|
||||
|
||||
if ($module_id) {
|
||||
$data['module_id'] = $module_id;
|
||||
}
|
||||
|
||||
return $this->addLog($user_id, $data);
|
||||
}
|
||||
|
||||
public function addHostPayoutBalances($user_id, $host_id, $module_id, $amount, $description)
|
||||
{
|
||||
$data = [
|
||||
'type' => 'payout',
|
||||
'payment' => 'balances',
|
||||
'description' => $description,
|
||||
'income' => 0,
|
||||
'income_drops' => 0,
|
||||
'outcome' => (float)$amount,
|
||||
'outcome_drops' => 0,
|
||||
'host_id' => $host_id,
|
||||
'module_id' => $module_id,
|
||||
];
|
||||
|
||||
return $this->addLog($user_id, $data);
|
||||
}
|
||||
|
||||
|
||||
public function reduceAmount($user_id, $amount = 0, $description = '扣除费用请求。')
|
||||
{
|
||||
|
||||
$lock = Cache::lock("user_balances_lock_" . $user_id, 10);
|
||||
try {
|
||||
|
||||
$lock->block(5);
|
||||
|
||||
$user = User::findOrFail($user_id);
|
||||
|
||||
$user->balances -= $amount;
|
||||
$user->save();
|
||||
|
||||
$this->addPayoutBalances($user_id, $amount, $description);
|
||||
|
||||
return $user->balances;
|
||||
} finally {
|
||||
optional($lock)->release();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function reduceAmountModuleFail($user_id, $module_id, $amount = 0, $description = '扣除费用请求。')
|
||||
{
|
||||
|
||||
$lock = Cache::lock("user_balances_lock_" . $user_id, 10);
|
||||
try {
|
||||
|
||||
$lock->block(5);
|
||||
|
||||
$user = User::findOrFail($user_id);
|
||||
|
||||
$user->balances -= $amount;
|
||||
|
||||
// if balances < 0
|
||||
if ($user->balances < 0) {
|
||||
throw new BalanceNotEnoughException('余额不足。');
|
||||
}
|
||||
|
||||
$user->save();
|
||||
|
||||
$this->addPayoutBalances($user_id, $amount, $description, $module_id);
|
||||
|
||||
return $user->balances;
|
||||
} finally {
|
||||
optional($lock)->release();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function reduceHostAmount($user_id, $host_id, $module_id, $amount = 0, $description = '扣除费用请求。')
|
||||
{
|
||||
|
||||
$lock = Cache::lock("user_balances_lock_" . $user_id, 10);
|
||||
try {
|
||||
|
||||
$lock->block(5);
|
||||
|
||||
$user = User::findOrFail($user_id);
|
||||
|
||||
$user->balances -= $amount;
|
||||
$user->save();
|
||||
|
||||
$this->addHostPayoutBalances($user_id, $host_id, $module_id, $amount, $description);
|
||||
|
||||
return $user->balances;
|
||||
} finally {
|
||||
optional($lock)->release();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ChargeException
|
||||
*/
|
||||
public function addAmount($user_id, $payment = 'console', $amount = 0, $description = null)
|
||||
{
|
||||
$lock = Cache::lock("user_balances_lock_" . $user_id, 10);
|
||||
try {
|
||||
|
||||
$lock->block(5);
|
||||
|
||||
$user = User::findOrFail($user_id);
|
||||
|
||||
$left_balances = $user->balances + $amount;
|
||||
|
||||
$user->increment('balances', $amount);
|
||||
|
||||
if (!$description) {
|
||||
$description = '充值金额。';
|
||||
} else {
|
||||
$description = '充值 ' . $amount . ' 元';
|
||||
}
|
||||
|
||||
$this->addIncomeBalances($user_id, $payment, $amount, $description);
|
||||
|
||||
return $left_balances;
|
||||
} catch (LockTimeoutException $e) {
|
||||
Log::error($e);
|
||||
throw new ChargeException('充值失败,请稍后再试。');
|
||||
} finally {
|
||||
optional($lock)->release();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private function addLog($user_id, $data)
|
||||
{
|
||||
$user = User::find($user_id);
|
||||
@ -362,4 +158,199 @@ private function addLog($user_id, $data)
|
||||
|
||||
return $this->create($data);
|
||||
}
|
||||
|
||||
public function getDrops($user_id = null): float
|
||||
{
|
||||
//
|
||||
if (!$user_id) {
|
||||
$user_id = auth()->id();
|
||||
}
|
||||
|
||||
$cache_key = 'user_drops_' . $user_id;
|
||||
|
||||
$drops = Cache::get($cache_key, [
|
||||
'drops' => 0,
|
||||
]);
|
||||
|
||||
// 保留 8 位
|
||||
$drops['drops'] = round($drops['drops'], 8);
|
||||
|
||||
return $drops['drops'];
|
||||
}
|
||||
|
||||
public function addIncomeDrops($user_id, $amount, $description)
|
||||
{
|
||||
$data = [
|
||||
'type' => 'income',
|
||||
'payment' => 'balances',
|
||||
'description' => $description,
|
||||
'income' => 0,
|
||||
'income_drops' => (float)$amount,
|
||||
'outcome' => 0,
|
||||
'outcome_drops' => 0,
|
||||
];
|
||||
|
||||
return $this->addLog($user_id, $data);
|
||||
}
|
||||
|
||||
public function reduceAmount($user_id, $amount = 0, $description = '扣除费用请求。')
|
||||
{
|
||||
|
||||
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
|
||||
try {
|
||||
|
||||
$lock->block(5);
|
||||
|
||||
$user = User::findOrFail($user_id);
|
||||
|
||||
$user->balance -= $amount;
|
||||
$user->save();
|
||||
|
||||
$this->addPayoutBalance($user_id, $amount, $description);
|
||||
|
||||
return $user->balance;
|
||||
} finally {
|
||||
optional($lock)->release();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function addPayoutBalance($user_id, $amount, $description, $module_id = null)
|
||||
{
|
||||
$data = [
|
||||
'type' => 'payout',
|
||||
'payment' => 'balances',
|
||||
'description' => $description,
|
||||
'income' => 0,
|
||||
'income_drops' => 0,
|
||||
'outcome' => (float)$amount,
|
||||
'outcome_drops' => 0
|
||||
];
|
||||
|
||||
if ($module_id) {
|
||||
$data['module_id'] = $module_id;
|
||||
}
|
||||
|
||||
return $this->addLog($user_id, $data);
|
||||
}
|
||||
|
||||
public function reduceAmountModuleFail($user_id, $module_id, $amount = 0, $description = '扣除费用请求。')
|
||||
{
|
||||
|
||||
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
|
||||
try {
|
||||
|
||||
$lock->block(5);
|
||||
|
||||
$user = User::findOrFail($user_id);
|
||||
|
||||
$user->balance -= $amount;
|
||||
|
||||
// if balance < 0
|
||||
if ($user->balance < 0) {
|
||||
throw new BalanceNotEnoughException('余额不足。');
|
||||
}
|
||||
|
||||
$user->save();
|
||||
|
||||
$this->addPayoutBalance($user_id, $amount, $description, $module_id);
|
||||
|
||||
return $user->balance;
|
||||
} finally {
|
||||
optional($lock)->release();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function reduceHostAmount($user_id, $host_id, $module_id, $amount = 0, $description = '扣除费用请求。')
|
||||
{
|
||||
|
||||
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
|
||||
try {
|
||||
|
||||
$lock->block(5);
|
||||
|
||||
$user = User::findOrFail($user_id);
|
||||
|
||||
$user->balance -= $amount;
|
||||
$user->save();
|
||||
|
||||
$this->addHostPayoutBalance($user_id, $host_id, $module_id, $amount, $description);
|
||||
|
||||
return $user->balance;
|
||||
} finally {
|
||||
optional($lock)->release();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function addHostPayoutBalance($user_id, $host_id, $module_id, $amount, $description)
|
||||
{
|
||||
$data = [
|
||||
'type' => 'payout',
|
||||
'payment' => 'balances',
|
||||
'description' => $description,
|
||||
'income' => 0,
|
||||
'income_drops' => 0,
|
||||
'outcome' => (float)$amount,
|
||||
'outcome_drops' => 0,
|
||||
'host_id' => $host_id,
|
||||
'module_id' => $module_id,
|
||||
];
|
||||
|
||||
return $this->addLog($user_id, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ChargeException
|
||||
*/
|
||||
public function addAmount($user_id, $payment = 'console', $amount = 0, $description = null)
|
||||
{
|
||||
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
|
||||
try {
|
||||
|
||||
$lock->block(5);
|
||||
|
||||
$user = User::findOrFail($user_id);
|
||||
|
||||
$left_balance = $user->balance + $amount;
|
||||
|
||||
$user->increment('balance', $amount);
|
||||
|
||||
if (!$description) {
|
||||
$description = '充值金额。';
|
||||
} else {
|
||||
$description = '充值 ' . $amount . ' 元';
|
||||
}
|
||||
|
||||
$this->addIncomeBalance($user_id, $payment, $amount, $description);
|
||||
|
||||
return $left_balance;
|
||||
} catch (LockTimeoutException $e) {
|
||||
Log::error($e);
|
||||
throw new ChargeException('充值失败,请稍后再试。');
|
||||
} finally {
|
||||
optional($lock)->release();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function addIncomeBalance($user_id, $payment, $amount, $description)
|
||||
{
|
||||
$data = [
|
||||
'type' => 'income',
|
||||
'payment' => $payment,
|
||||
'description' => $description,
|
||||
'income' => (float)$amount,
|
||||
'income_drops' => 0,
|
||||
'outcome' => 0,
|
||||
'outcome_drops' => 0,
|
||||
];
|
||||
|
||||
return $this->addLog($user_id, $data);
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,21 @@ class User extends Authenticatable
|
||||
'banned_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::updating(function ($model) {
|
||||
|
||||
// balance 四舍五入
|
||||
$model->balance = round($model->balance, 2);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CommonException
|
||||
* @throws BalanceNotEnoughException
|
||||
*/
|
||||
public function toDrops($amount = 1)
|
||||
{
|
||||
|
||||
@ -98,15 +112,4 @@ public function toDrops($amount = 1)
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::updating(function ($model) {
|
||||
|
||||
// balance 四舍五入
|
||||
$model->balance = round($model->balance, 2);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -22,23 +22,6 @@ class Reply extends Model
|
||||
'is_pending',
|
||||
];
|
||||
|
||||
public function workOrder()
|
||||
{
|
||||
return $this->belongsTo(WorkOrder::class, 'work_order_id', 'id');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function scopeWorkOrderId($query, $work_order_id)
|
||||
{
|
||||
return $query->where('work_order_id', $work_order_id);
|
||||
}
|
||||
|
||||
|
||||
// before create
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
@ -79,4 +62,22 @@ protected static function boot()
|
||||
dispatch(new \App\Jobs\Module\WorkOrder\WorkOrder($model->workOrder, 'put'));
|
||||
});
|
||||
}
|
||||
|
||||
public function workOrder()
|
||||
{
|
||||
return $this->belongsTo(WorkOrder::class, 'work_order_id', 'id');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
|
||||
// before create
|
||||
|
||||
public function scopeWorkOrderId($query, $work_order_id)
|
||||
{
|
||||
return $query->where('work_order_id', $work_order_id);
|
||||
}
|
||||
}
|
||||
|
@ -26,41 +26,7 @@ class WorkOrder extends Model
|
||||
];
|
||||
|
||||
// user
|
||||
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// replies
|
||||
public function replies(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Reply::class);
|
||||
}
|
||||
|
||||
// host
|
||||
public function host(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Host::class);
|
||||
}
|
||||
|
||||
public function module(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Module::class);
|
||||
}
|
||||
|
||||
// scope
|
||||
public function scopeThisModule($query)
|
||||
{
|
||||
return $query->where('module_id', auth('module')->id());
|
||||
}
|
||||
|
||||
public function scopeThisUser($query)
|
||||
{
|
||||
return $query->where('user_id', auth()->id());
|
||||
}
|
||||
|
||||
|
||||
// on create
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
@ -104,4 +70,43 @@ protected static function boot()
|
||||
dispatch(new \App\Jobs\Module\WorkOrder\WorkOrder($model, 'put'));
|
||||
});
|
||||
}
|
||||
|
||||
// replies
|
||||
|
||||
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// host
|
||||
|
||||
public function replies(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Reply::class);
|
||||
}
|
||||
|
||||
public function host(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Host::class);
|
||||
}
|
||||
|
||||
// scope
|
||||
|
||||
public function module(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Module::class);
|
||||
}
|
||||
|
||||
public function scopeThisModule($query)
|
||||
{
|
||||
return $query->where('module_id', auth('module')->id());
|
||||
}
|
||||
|
||||
|
||||
// on create
|
||||
|
||||
public function scopeThisUser($query)
|
||||
{
|
||||
return $query->where('user_id', auth()->id());
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ public function __construct(Module $module)
|
||||
* Get the mail representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return \Illuminate\Notifications\Messages\MailMessage
|
||||
*/
|
||||
public function toGroup($notifiable)
|
||||
|
@ -28,6 +28,7 @@ public function __construct()
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function via($notifiable)
|
||||
@ -49,7 +50,7 @@ public function toGroup($notifiable)
|
||||
|
||||
|
||||
$data = [
|
||||
'balance' => $notifiable,
|
||||
'balances' => $notifiable,
|
||||
'user' => $user,
|
||||
];
|
||||
|
||||
|
@ -29,6 +29,7 @@ public function __construct()
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function via($notifiable)
|
||||
|
@ -11,6 +11,7 @@ class BalanceObserve
|
||||
* Handle the Balance "created" event.
|
||||
*
|
||||
* @param \App\Models\Balance $balance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function created(Balance $balance)
|
||||
@ -24,6 +25,7 @@ public function created(Balance $balance)
|
||||
* Handle the Balance "updated" event.
|
||||
*
|
||||
* @param \App\Models\Balance $balance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updated(Balance $balance)
|
||||
@ -37,6 +39,7 @@ public function updated(Balance $balance)
|
||||
* Handle the Balance "deleted" event.
|
||||
*
|
||||
* @param \App\Models\Balance $balance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Balance $balance)
|
||||
@ -48,6 +51,7 @@ public function deleted(Balance $balance)
|
||||
* Handle the Balance "restored" event.
|
||||
*
|
||||
* @param \App\Models\Balance $balance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function restored(Balance $balance)
|
||||
@ -59,6 +63,7 @@ public function restored(Balance $balance)
|
||||
* Handle the Balance "force deleted" event.
|
||||
*
|
||||
* @param \App\Models\Balance $balance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function forceDeleted(Balance $balance)
|
||||
|
@ -11,6 +11,7 @@ class ReplyObserver
|
||||
* Handle the Reply "created" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function created(Reply $reply)
|
||||
@ -24,6 +25,7 @@ public function created(Reply $reply)
|
||||
* Handle the Reply "updated" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updated(Reply $reply)
|
||||
@ -35,6 +37,7 @@ public function updated(Reply $reply)
|
||||
* Handle the Reply "deleted" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Reply $reply)
|
||||
@ -46,6 +49,7 @@ public function deleted(Reply $reply)
|
||||
* Handle the Reply "restored" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function restored(Reply $reply)
|
||||
@ -57,6 +61,7 @@ public function restored(Reply $reply)
|
||||
* Handle the Reply "force deleted" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\Reply $reply
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function forceDeleted(Reply $reply)
|
||||
|
@ -12,6 +12,7 @@ class WorkOrderObserver
|
||||
* Handle the WorkOrder "created" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function created(WorkOrder $workOrder)
|
||||
@ -25,6 +26,7 @@ public function created(WorkOrder $workOrder)
|
||||
* Handle the WorkOrder "updated" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updated(WorkOrder $workOrder)
|
||||
@ -40,6 +42,7 @@ public function updated(WorkOrder $workOrder)
|
||||
* Handle the WorkOrder "deleted" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(WorkOrder $workOrder)
|
||||
@ -51,6 +54,7 @@ public function deleted(WorkOrder $workOrder)
|
||||
* Handle the WorkOrder "restored" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function restored(WorkOrder $workOrder)
|
||||
@ -62,6 +66,7 @@ public function restored(WorkOrder $workOrder)
|
||||
* Handle the WorkOrder "force deleted" event.
|
||||
*
|
||||
* @param \App\Models\WorkOrder\WorkOrder $workOrder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function forceDeleted(WorkOrder $workOrder)
|
||||
|
@ -85,7 +85,7 @@
|
||||
'connect_timeout' => 5.0,
|
||||
// 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
|
||||
],
|
||||
// optional,默认 warning;日志路径为:sys_get_temp_dir().'/logs/yansongda.pay.log'
|
||||
// optional,默认 warning;日志路径为:sys_get_temp_dir().'/logs/yansongda.balances.log'
|
||||
'logger' => [
|
||||
'enable' => false,
|
||||
'file' => null,
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -5,8 +5,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -5,8 +5,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -5,8 +5,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
@ -15,7 +14,7 @@ public function up()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
//
|
||||
$table->decimal('balance', 10, 2)->default(0)->after('password');
|
||||
$table->decimal('balances', 10, 2)->default(0)->after('password');
|
||||
|
||||
// drop column if exists
|
||||
if (Schema::hasColumn('users', 'drops')) {
|
||||
@ -33,7 +32,7 @@ public function down()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
//
|
||||
$table->dropColumn('balance');
|
||||
$table->dropColumn('balances');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
@ -15,7 +14,7 @@ public function up()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
//
|
||||
$table->dateTime('banned_at')->nullable()->comment('封禁时间')->after('balance');
|
||||
$table->dateTime('banned_at')->nullable()->comment('封禁时间')->after('balances');
|
||||
// reason
|
||||
$table->string('banned_reason')->nullable()->after('banned_at');
|
||||
});
|
||||
|
@ -3,8 +3,7 @@
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -3,8 +3,7 @@
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -4,8 +4,7 @@
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -3,8 +3,7 @@
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
define('LARAVEL_START', microtime(true));
|
||||
|
||||
|
6
resources/js/bootstrap.js
vendored
6
resources/js/bootstrap.js
vendored
@ -1,6 +1,4 @@
|
||||
import _ from 'lodash';
|
||||
window._ = _;
|
||||
|
||||
import 'bootstrap';
|
||||
|
||||
/**
|
||||
@ -8,8 +6,10 @@ import 'bootstrap';
|
||||
* to our Laravel back-end. This library automatically handles sending the
|
||||
* CSRF token as a header based on the value of the "XSRF" token cookie.
|
||||
*/
|
||||
|
||||
import axios from 'axios';
|
||||
|
||||
window._ = _;
|
||||
|
||||
window.axios = axios;
|
||||
|
||||
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
|
||||
|
@ -32,7 +32,8 @@ class="form-control @error('email') is-invalid @enderror" name="email"
|
||||
|
||||
<div class="col-md-6">
|
||||
<input id="password" type="password"
|
||||
class="form-control @error('password') is-invalid @enderror" name="password" required
|
||||
class="form-control @error('password') is-invalid @enderror" name="password"
|
||||
required
|
||||
autocomplete="current-password">
|
||||
|
||||
@error('password')
|
||||
|
19
resources/views/balances/index.blade.php
Normal file
19
resources/views/balances/index.blade.php
Normal file
@ -0,0 +1,19 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('title', '余额')
|
||||
|
||||
@section('content')
|
||||
<h2>余额</h2>
|
||||
|
||||
|
||||
<p>您的余额: {{ $balance }} 元 </p>
|
||||
<p>Drops: {{ $drops }} </p>
|
||||
|
||||
<form name="charge" method="POST" target="_blank" action="{{ route('balances.store') }}">
|
||||
@csrf
|
||||
<input type="number" name="amount" value="1" min="1" max="1000"/>
|
||||
<button type="submit" class="btn btn-primary">充值</button>
|
||||
</form>
|
||||
|
||||
|
||||
@endsection
|
@ -22,7 +22,7 @@
|
||||
<div id="app">
|
||||
<nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
|
||||
<div class="container">
|
||||
<a class="navbar-brand" href="{{ route('admin.index') }}">
|
||||
<a class="navbar-brand" href="{{ route('index') }}">
|
||||
{{ config('app.display_name') }}
|
||||
</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
|
||||
@ -38,7 +38,7 @@
|
||||
<a class="nav-link" href="{{ route('index') }}">首页</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('index') }}">首页</a>
|
||||
<a class="nav-link" href="{{ route('balances.index') }}">余额</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
|
@ -2,26 +2,25 @@
|
||||
# {{ $module->name }}
|
||||
==================================
|
||||
## 本月
|
||||
#### 现金 {{ round($data['transactions']['this_month']['balance'], 2) }} 元
|
||||
#### 现金 {{ round($data['transactions']['this_month']['balances'], 2) }} 元
|
||||
#### Drops {{ round($data['transactions']['this_month']['drops'], 4) }}
|
||||
#### 合计 {{ round($data['transactions']['this_month']['balance'] + $data['transactions']['this_month']['drops'] / $data['rate'], 2) }} 元
|
||||
#### 合计 {{ round($data['transactions']['this_month']['balances'] + $data['transactions']['this_month']['drops'] / $data['rate'], 2) }} 元
|
||||
==================================
|
||||
## 上个月
|
||||
#### 现金 {{ round($data['transactions']['last_month']['balance'], 2) }} 元
|
||||
#### 现金 {{ round($data['transactions']['last_month']['balances'], 2) }} 元
|
||||
#### Drops {{ round($data['transactions']['last_month']['drops'], 4) }}
|
||||
#### 合计 {{ round($data['transactions']['last_month']['balance'] + $data['transactions']['last_month']['drops'] / $data['rate'], 2) }} 元
|
||||
|
||||
#### 合计 {{ round($data['transactions']['last_month']['balances'] + $data['transactions']['last_month']['drops'] / $data['rate'], 2) }} 元
|
||||
|
||||
|
||||
{{--
|
||||
$module = $this->http->get('modules')->json()['data'];
|
||||
|
||||
$total = $module['transactions']['this_month']['balance'];
|
||||
$total = $module['transactions']['this_month']['balances'];
|
||||
|
||||
$drops = $module['transactions']['this_month']['drops'] / $module['rate'];
|
||||
|
||||
if ($drops < 0) { $drops=0; } $total +=$drops; $total=round($total, 2); $module=[ 'balance'=>
|
||||
$module['transactions']['this_month']['balance'],
|
||||
if ($drops < 0) { $drops=0; } $total +=$drops; $total=round($total, 2); $module=[ 'balances'=>
|
||||
$module['transactions']['this_month']['balances'],
|
||||
'drops' => $module['transactions']['this_month']['drops'],
|
||||
'total' => $total,
|
||||
];
|
||||
@ -32,7 +31,7 @@
|
||||
本月收益
|
||||
</h3>
|
||||
<p>
|
||||
直接扣费金额: {{ $module['balance'] }} 元
|
||||
直接扣费金额: {{ $module['balances'] }} 元
|
||||
</p>
|
||||
<p>
|
||||
Drops: {{ $module['drops'] }}
|
||||
|
@ -1,17 +1,9 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Api\BalanceController;
|
||||
use App\Http\Controllers\AuthController;
|
||||
use App\Http\Controllers\Web\AuthController;
|
||||
use App\Http\Controllers\Web\BalanceController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::prefix('auth')->group(function () {
|
||||
Route::get('redirect', [AuthController::class, 'redirect'])->name('login');
|
||||
Route::get('callback', [AuthController::class, 'callback'])->name('callback');
|
||||
});
|
||||
|
||||
Route::get('/', [AuthController::class, 'index'])->name('index');
|
||||
Route::view('not_verified', 'not_verified')->name('not_verified');
|
||||
|
||||
Route::middleware(['auth'])->group(function () {
|
||||
Route::view('banned', 'banned')->name('banned');
|
||||
|
||||
@ -20,9 +12,22 @@
|
||||
|
||||
// logout
|
||||
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
|
||||
|
||||
|
||||
Route::resource('/balances', BalanceController::class);
|
||||
|
||||
|
||||
});
|
||||
|
||||
Route::prefix('auth')->group(function () {
|
||||
Route::get('redirect', [AuthController::class, 'redirect'])->name('login');
|
||||
Route::get('callback', [AuthController::class, 'callback'])->name('callback');
|
||||
});
|
||||
|
||||
Route::get('/balance/{balance}', [BalanceController::class, 'show'])->name('balances.pay.show');
|
||||
Route::get('/pay/alipay/notify', [BalanceController::class, 'notify'])->name('balances.alipay.notify');
|
||||
Route::get('/', [AuthController::class, 'index'])->name('index');
|
||||
Route::view('not_verified', 'not_verified')->name('not_verified');
|
||||
|
||||
|
||||
Route::get('/balances/{balances}', [BalanceController::class, 'show'])->name('balances.balances.show');
|
||||
Route::get('/balances/alipay/notify', [BalanceController::class, 'notify'])->name('balances.alipay.notify');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user