mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-14 02:50:52 +08:00
refactor: 规范状态码、抛出异常的使用
This commit is contained in:
@@ -12,21 +12,20 @@ class CommController extends Controller
|
||||
{
|
||||
public function config()
|
||||
{
|
||||
return response([
|
||||
'data' => [
|
||||
'is_telegram' => (int)admin_setting('telegram_bot_enable', 0),
|
||||
'telegram_discuss_link' => admin_setting('telegram_discuss_link'),
|
||||
'stripe_pk' => admin_setting('stripe_pk_live'),
|
||||
'withdraw_methods' => admin_setting('commission_withdraw_method', Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT),
|
||||
'withdraw_close' => (int)admin_setting('withdraw_close_enable', 0),
|
||||
'currency' => admin_setting('currency', 'CNY'),
|
||||
'currency_symbol' => admin_setting('currency_symbol', '¥'),
|
||||
'commission_distribution_enable' => (int)admin_setting('commission_distribution_enable', 0),
|
||||
'commission_distribution_l1' => admin_setting('commission_distribution_l1'),
|
||||
'commission_distribution_l2' => admin_setting('commission_distribution_l2'),
|
||||
'commission_distribution_l3' => admin_setting('commission_distribution_l3')
|
||||
]
|
||||
]);
|
||||
$data = [
|
||||
'is_telegram' => (int)admin_setting('telegram_bot_enable', 0),
|
||||
'telegram_discuss_link' => admin_setting('telegram_discuss_link'),
|
||||
'stripe_pk' => admin_setting('stripe_pk_live'),
|
||||
'withdraw_methods' => admin_setting('commission_withdraw_method', Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT),
|
||||
'withdraw_close' => (int)admin_setting('withdraw_close_enable', 0),
|
||||
'currency' => admin_setting('currency', 'CNY'),
|
||||
'currency_symbol' => admin_setting('currency_symbol', '¥'),
|
||||
'commission_distribution_enable' => (int)admin_setting('commission_distribution_enable', 0),
|
||||
'commission_distribution_l1' => admin_setting('commission_distribution_l1'),
|
||||
'commission_distribution_l2' => admin_setting('commission_distribution_l2'),
|
||||
'commission_distribution_l3' => admin_setting('commission_distribution_l3')
|
||||
];
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function getStripePublicKey(Request $request)
|
||||
@@ -34,9 +33,7 @@ class CommController extends Controller
|
||||
$payment = Payment::where('id', $request->input('id'))
|
||||
->where('payment', 'StripeCredit')
|
||||
->first();
|
||||
if (!$payment) throw new ApiException(500, 'payment is not found');
|
||||
return response([
|
||||
'data' => $payment->config['stripe_pk_live']
|
||||
]);
|
||||
if (!$payment) throw new ApiException('payment is not found');
|
||||
return $this->success($payment->config['stripe_pk_live']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,12 @@ class CouponController extends Controller
|
||||
public function check(Request $request)
|
||||
{
|
||||
if (empty($request->input('code'))) {
|
||||
throw new ApiException(500, __('Coupon cannot be empty'));
|
||||
return $this->fail([422,__('Coupon cannot be empty')]);
|
||||
}
|
||||
$couponService = new CouponService($request->input('code'));
|
||||
$couponService->setPlanId($request->input('plan_id'));
|
||||
$couponService->setUserId($request->user['id']);
|
||||
$couponService->check();
|
||||
return response([
|
||||
'data' => $couponService->getCoupon()
|
||||
]);
|
||||
return $this->success($couponService->getCoupon());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,14 +16,12 @@ class InviteController extends Controller
|
||||
public function save(Request $request)
|
||||
{
|
||||
if (InviteCode::where('user_id', $request->user['id'])->where('status', 0)->count() >= admin_setting('invite_gen_limit', 5)) {
|
||||
throw new ApiException(500, __('The maximum number of creations has been reached'));
|
||||
return $this->fail([400,__('The maximum number of creations has been reached')]);
|
||||
}
|
||||
$inviteCode = new InviteCode();
|
||||
$inviteCode->user_id = $request->user['id'];
|
||||
$inviteCode->code = Helper::randomChar(8);
|
||||
return response([
|
||||
'data' => $inviteCode->save()
|
||||
]);
|
||||
return $this->success($inviteCode->save());
|
||||
}
|
||||
|
||||
public function details(Request $request)
|
||||
@@ -79,11 +77,12 @@ class InviteController extends Controller
|
||||
//可用佣金
|
||||
(int)$user->commission_balance
|
||||
];
|
||||
return response([
|
||||
$data = [
|
||||
'data' => [
|
||||
'codes' => $codes,
|
||||
'stat' => $stat
|
||||
]
|
||||
]);
|
||||
];
|
||||
return $this->success($data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class KnowledgeController extends Controller
|
||||
->where('show', 1)
|
||||
->first()
|
||||
->toArray();
|
||||
if (!$knowledge) throw new ApiException(500, __('Article does not exist'));
|
||||
if (!$knowledge) return $this->fail([500, __('Article does not exist')]);
|
||||
$user = User::find($request->user['id']);
|
||||
$userService = new UserService();
|
||||
if (!$userService->isAvailable($user)) {
|
||||
@@ -38,9 +38,7 @@ class KnowledgeController extends Controller
|
||||
),
|
||||
$knowledge['body']
|
||||
);
|
||||
return response([
|
||||
'data' => $knowledge
|
||||
]);
|
||||
return $this->success($knowledge);
|
||||
}
|
||||
$builder = Knowledge::select(['id', 'category', 'title', 'updated_at'])
|
||||
->where('language', $request->input('language'))
|
||||
@@ -56,9 +54,7 @@ class KnowledgeController extends Controller
|
||||
|
||||
$knowledges = $builder->get()
|
||||
->groupBy('category');
|
||||
return response([
|
||||
'data' => $knowledges
|
||||
]);
|
||||
return $this->success($knowledges);
|
||||
}
|
||||
|
||||
private function formatAccessData(&$body)
|
||||
|
||||
@@ -16,14 +16,7 @@ use App\Services\PlanService;
|
||||
use App\Services\UserService;
|
||||
use App\Utils\Helper;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Library\BitpayX;
|
||||
use Library\Epay;
|
||||
use Library\MGate;
|
||||
use Omnipay\Omnipay;
|
||||
use Stripe\Source;
|
||||
use Stripe\Stripe;
|
||||
|
||||
class OrderController extends Controller
|
||||
{
|
||||
@@ -43,9 +36,7 @@ class OrderController extends Controller
|
||||
}
|
||||
}
|
||||
}
|
||||
return response([
|
||||
'data' => $order->makeHidden(['id', 'user_id'])
|
||||
]);
|
||||
return $this->success($order->makeHidden(['id', 'user_id']));
|
||||
}
|
||||
|
||||
public function detail(Request $request)
|
||||
@@ -54,26 +45,24 @@ class OrderController extends Controller
|
||||
->where('trade_no', $request->input('trade_no'))
|
||||
->first();
|
||||
if (!$order) {
|
||||
throw new ApiException(500, __('Order does not exist or has been paid'));
|
||||
return $this->fail([400, __('Order does not exist or has been paid')]);
|
||||
}
|
||||
$order['plan'] = Plan::find($order->plan_id);
|
||||
$order['try_out_plan_id'] = (int)admin_setting('try_out_plan_id');
|
||||
if (!$order['plan']) {
|
||||
throw new ApiException(500, __('Subscription plan does not exist'));
|
||||
return $this->fail([400, __('Subscription plan does not exist')]);
|
||||
}
|
||||
if ($order->surplus_order_ids) {
|
||||
$order['surplus_orders'] = Order::whereIn('id', $order->surplus_order_ids)->get();
|
||||
}
|
||||
return response([
|
||||
'data' => $order
|
||||
]);
|
||||
return $this->success($order);
|
||||
}
|
||||
|
||||
public function save(OrderSave $request)
|
||||
{
|
||||
$userService = new UserService();
|
||||
if ($userService->isNotCompleteOrderByUserId($request->user['id'])) {
|
||||
throw new ApiException(500, __('You have an unpaid or pending order, please try again later or cancel it'));
|
||||
return $this->fail([400, __('You have an unpaid or pending order, please try again later or cancel it')]);
|
||||
}
|
||||
|
||||
$planService = new PlanService($request->input('plan_id'));
|
||||
@@ -82,36 +71,36 @@ class OrderController extends Controller
|
||||
$user = User::find($request->user['id']);
|
||||
|
||||
if (!$plan) {
|
||||
throw new ApiException(500, __('Subscription plan does not exist'));
|
||||
return $this->fail([400, __('Subscription plan does not exist')]);
|
||||
}
|
||||
|
||||
if ($user->plan_id !== $plan->id && !$planService->haveCapacity() && $request->input('period') !== 'reset_price') {
|
||||
throw new ApiException(500, __('Current product is sold out'));
|
||||
throw new ApiException(__('Current product is sold out'));
|
||||
}
|
||||
|
||||
if ($plan[$request->input('period')] === NULL) {
|
||||
throw new ApiException(500, __('This payment period cannot be purchased, please choose another period'));
|
||||
return $this->fail([400, __('This payment period cannot be purchased, please choose another period')]);
|
||||
}
|
||||
|
||||
if ($request->input('period') === 'reset_price') {
|
||||
if (!$userService->isAvailable($user) || $plan->id !== $user->plan_id) {
|
||||
throw new ApiException(500, __('Subscription has expired or no active subscription, unable to purchase Data Reset Package'));
|
||||
return $this->fail([400, __('Subscription has expired or no active subscription, unable to purchase Data Reset Package')]);
|
||||
}
|
||||
}
|
||||
|
||||
if ((!$plan->show && !$plan->renew) || (!$plan->show && $user->plan_id !== $plan->id)) {
|
||||
if ($request->input('period') !== 'reset_price') {
|
||||
throw new ApiException(500, __('This subscription has been sold out, please choose another subscription'));
|
||||
return $this->fail([400, __('This subscription has been sold out, please choose another subscription')]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$plan->renew && $user->plan_id == $plan->id && $request->input('period') !== 'reset_price') {
|
||||
throw new ApiException(500, __('This subscription cannot be renewed, please change to another subscription'));
|
||||
return $this->fail([400, __('This subscription cannot be renewed, please change to another subscription')]);
|
||||
}
|
||||
|
||||
|
||||
if (!$plan->show && $plan->renew && !$userService->isAvailable($user)) {
|
||||
throw new ApiException(500, __('This subscription has expired, please change to another subscription'));
|
||||
return $this->fail([400, __('This subscription has expired, please change to another subscription')]);
|
||||
}
|
||||
|
||||
try{
|
||||
@@ -127,7 +116,7 @@ class OrderController extends Controller
|
||||
if ($request->input('coupon_code')) {
|
||||
$couponService = new CouponService($request->input('coupon_code'));
|
||||
if (!$couponService->use($order)) {
|
||||
throw new ApiException(500, __('Coupon failed'));
|
||||
return $this->fail([400, __('Coupon failed')]);
|
||||
}
|
||||
$order->coupon_id = $couponService->getId();
|
||||
}
|
||||
@@ -141,13 +130,13 @@ class OrderController extends Controller
|
||||
$userService = new UserService();
|
||||
if ($remainingBalance > 0) {
|
||||
if (!$userService->addBalance($order->user_id, - $order->total_amount)) {
|
||||
throw new ApiException(500, __('Insufficient balance'));
|
||||
return $this->fail([400, __('Insufficient balance')]);
|
||||
}
|
||||
$order->balance_amount = $order->total_amount;
|
||||
$order->total_amount = 0;
|
||||
} else {
|
||||
if (!$userService->addBalance($order->user_id, - $user->balance)) {
|
||||
throw new ApiException(500, __('Insufficient balance'));
|
||||
return $this->fail([400, __('Insufficient balance')]);
|
||||
}
|
||||
$order->balance_amount = $user->balance;
|
||||
$order->total_amount = $order->total_amount - $user->balance;
|
||||
@@ -155,7 +144,7 @@ class OrderController extends Controller
|
||||
}
|
||||
|
||||
if (!$order->save()) {
|
||||
throw new ApiException(500, __('Failed to create order'));
|
||||
return $this->fail([400, __('Failed to create order')]);
|
||||
}
|
||||
DB::commit();
|
||||
}catch (\Exception $e){
|
||||
@@ -163,9 +152,7 @@ class OrderController extends Controller
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return response([
|
||||
'data' => $order->trade_no
|
||||
]);
|
||||
return $this->success($order->trade_no);
|
||||
}
|
||||
|
||||
public function checkout(Request $request)
|
||||
@@ -177,26 +164,26 @@ class OrderController extends Controller
|
||||
->where('status', 0)
|
||||
->first();
|
||||
if (!$order) {
|
||||
throw new ApiException(500, __('Order does not exist or has been paid'));
|
||||
return $this->fail([400, __('Order does not exist or has been paid')]);
|
||||
}
|
||||
// free process
|
||||
if ($order->total_amount <= 0) {
|
||||
$orderService = new OrderService($order);
|
||||
if (!$orderService->paid($order->trade_no)) throw new ApiException(500, '');
|
||||
if (!$orderService->paid($order->trade_no)) return $this->fail([400, '支付失败']);
|
||||
return response([
|
||||
'type' => -1,
|
||||
'data' => true
|
||||
]);
|
||||
}
|
||||
$payment = Payment::find($method);
|
||||
if (!$payment || $payment->enable !== 1) throw new ApiException(500, __('Payment method is not available'));
|
||||
if (!$payment || $payment->enable !== 1) return $this->fail([400, __('Payment method is not available')]);
|
||||
$paymentService = new PaymentService($payment->payment, $payment->id);
|
||||
$order->handling_amount = NULL;
|
||||
if ($payment->handling_fee_fixed || $payment->handling_fee_percent) {
|
||||
$order->handling_amount = round(($order->total_amount * ($payment->handling_fee_percent / 100)) + $payment->handling_fee_fixed);
|
||||
}
|
||||
$order->payment_id = $method;
|
||||
if (!$order->save()) throw new ApiException(500, __('Request failed, please try again later'));
|
||||
if (!$order->save()) return $this->fail([400, __('Request failed, please try again later')]);
|
||||
$result = $paymentService->pay([
|
||||
'trade_no' => $tradeNo,
|
||||
'total_amount' => isset($order->handling_amount) ? ($order->total_amount + $order->handling_amount) : $order->total_amount,
|
||||
@@ -216,11 +203,9 @@ class OrderController extends Controller
|
||||
->where('user_id', $request->user['id'])
|
||||
->first();
|
||||
if (!$order) {
|
||||
throw new ApiException(500, __('Order does not exist'));
|
||||
return $this->fail([400, __('Order does not exist')]);
|
||||
}
|
||||
return response([
|
||||
'data' => $order->status
|
||||
]);
|
||||
return $this->success($order->status);
|
||||
}
|
||||
|
||||
public function getPaymentMethod()
|
||||
@@ -237,31 +222,27 @@ class OrderController extends Controller
|
||||
->orderBy('sort', 'ASC')
|
||||
->get();
|
||||
|
||||
return response([
|
||||
'data' => $methods
|
||||
]);
|
||||
return $this->success($methods);
|
||||
}
|
||||
|
||||
public function cancel(Request $request)
|
||||
{
|
||||
if (empty($request->input('trade_no'))) {
|
||||
throw new ApiException(500, __('Invalid parameter'));
|
||||
return $this->fail([422, __('Invalid parameter')]);
|
||||
}
|
||||
$order = Order::where('trade_no', $request->input('trade_no'))
|
||||
->where('user_id', $request->user['id'])
|
||||
->first();
|
||||
if (!$order) {
|
||||
throw new ApiException(500, __('Order does not exist'));
|
||||
return $this->fail([400, __('Order does not exist')]);
|
||||
}
|
||||
if ($order->status !== 0) {
|
||||
throw new ApiException(500, __('You can only cancel pending orders'));
|
||||
return $this->fail([400, __('You can only cancel pending orders')]);
|
||||
}
|
||||
$orderService = new OrderService($order);
|
||||
if (!$orderService->cancel()) {
|
||||
throw new ApiException(500, __('Cancel failed'));
|
||||
return $this->fail([400, __('Cancel failed')]);
|
||||
}
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ use App\Models\Plan;
|
||||
use App\Models\User;
|
||||
use App\Services\PlanService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PlanController extends Controller
|
||||
{
|
||||
@@ -18,14 +17,12 @@ class PlanController extends Controller
|
||||
if ($request->input('id')) {
|
||||
$plan = Plan::where('id', $request->input('id'))->first();
|
||||
if (!$plan) {
|
||||
throw new ApiException(500, __('Subscription plan does not exist'));
|
||||
return $this->fail([400, __('Subscription plan does not exist')]);
|
||||
}
|
||||
if ((!$plan->show && !$plan->renew) || (!$plan->show && $user->plan_id !== $plan->id)) {
|
||||
throw new ApiException(500, __('Subscription plan does not exist'));
|
||||
return $this->fail([400, __('Subscription plan does not exist')]);
|
||||
}
|
||||
return response([
|
||||
'data' => $plan
|
||||
]);
|
||||
return $this->success($plan);
|
||||
}
|
||||
|
||||
$counts = PlanService::countActiveUsers();
|
||||
@@ -37,8 +34,6 @@ class PlanController extends Controller
|
||||
if (!isset($counts[$plans[$k]->id])) continue;
|
||||
$plans[$k]->capacity_limit = $plans[$k]->capacity_limit - $counts[$plans[$k]->id]->count;
|
||||
}
|
||||
return response([
|
||||
'data' => $plans
|
||||
]);
|
||||
return $this->success($plans);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,8 +37,6 @@ class StatController extends Controller
|
||||
}
|
||||
};
|
||||
|
||||
return response([
|
||||
'data' => $records
|
||||
]);
|
||||
return $this->success($records);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,11 +13,10 @@ class TelegramController extends Controller
|
||||
{
|
||||
$telegramService = new TelegramService();
|
||||
$response = $telegramService->getMe();
|
||||
return response([
|
||||
'data' => [
|
||||
'username' => $response->result->username
|
||||
]
|
||||
]);
|
||||
$data = [
|
||||
'username' => $response->result->username
|
||||
];
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function unbind(Request $request)
|
||||
|
||||
@@ -24,7 +24,7 @@ class TicketController extends Controller
|
||||
->where('user_id', $request->user['id'])
|
||||
->first();
|
||||
if (!$ticket) {
|
||||
throw new ApiException(500, __('Ticket does not exist'));
|
||||
return $this->fail([400, __('Ticket does not exist')]);
|
||||
}
|
||||
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
|
||||
for ($i = 0; $i < count($ticket['message']); $i++) {
|
||||
@@ -34,16 +34,12 @@ class TicketController extends Controller
|
||||
$ticket['message'][$i]['is_me'] = false;
|
||||
}
|
||||
}
|
||||
return response([
|
||||
'data' => $ticket
|
||||
]);
|
||||
return $this->success($ticket);
|
||||
}
|
||||
$ticket = Ticket::where('user_id', $request->user['id'])
|
||||
->orderBy('created_at', 'DESC')
|
||||
->get();
|
||||
return response([
|
||||
'data' => $ticket
|
||||
]);
|
||||
return $this->success($ticket);
|
||||
}
|
||||
|
||||
public function save(TicketSave $request)
|
||||
@@ -51,7 +47,7 @@ class TicketController extends Controller
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
if ((int)Ticket::where('status', 0)->where('user_id', $request->user['id'])->lockForUpdate()->count()) {
|
||||
throw new ApiException(500, __('There are other unresolved tickets'));
|
||||
return $this->fail([400, __('There are other unresolved tickets')]);
|
||||
}
|
||||
$ticket = Ticket::create(array_merge($request->only([
|
||||
'subject',
|
||||
@@ -60,7 +56,7 @@ class TicketController extends Controller
|
||||
'user_id' => $request->user['id']
|
||||
]));
|
||||
if (!$ticket) {
|
||||
throw new ApiException(500, __('Failed to open ticket'));
|
||||
return $this->fail([400, __('Failed to open ticket')]);
|
||||
}
|
||||
$ticketMessage = TicketMessage::create([
|
||||
'user_id' => $request->user['id'],
|
||||
@@ -68,7 +64,7 @@ class TicketController extends Controller
|
||||
'message' => $request->input('message')
|
||||
]);
|
||||
if (!$ticketMessage) {
|
||||
throw new ApiException(500, __('Failed to open ticket'));
|
||||
return $this->fail([400, __('Failed to open ticket')]);
|
||||
}
|
||||
DB::commit();
|
||||
}catch(\Exception $e){
|
||||
@@ -76,30 +72,28 @@ class TicketController extends Controller
|
||||
throw $e;
|
||||
}
|
||||
$this->sendNotify($ticket, $request->input('message'));
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
public function reply(Request $request)
|
||||
{
|
||||
if (empty($request->input('id'))) {
|
||||
throw new ApiException(500, __('Invalid parameter'));
|
||||
return $this->fail([400, __('Invalid parameter')]);
|
||||
}
|
||||
if (empty($request->input('message'))) {
|
||||
throw new ApiException(500, __('Message cannot be empty'));
|
||||
return $this->fail([400, __('Message cannot be empty')]);
|
||||
}
|
||||
$ticket = Ticket::where('id', $request->input('id'))
|
||||
->where('user_id', $request->user['id'])
|
||||
->first();
|
||||
if (!$ticket) {
|
||||
throw new ApiException(500, __('Ticket does not exist'));
|
||||
return $this->fail([400, __('Ticket does not exist')]);
|
||||
}
|
||||
if ($ticket->status) {
|
||||
throw new ApiException(500, __('The ticket is closed and cannot be replied'));
|
||||
return $this->fail([400, __('The ticket is closed and cannot be replied')]);
|
||||
}
|
||||
if ($request->user['id'] == $this->getLastMessage($ticket->id)->user_id) {
|
||||
throw new ApiException(500, __('Please wait for the technical enginneer to reply'));
|
||||
return $this->fail([400, __('Please wait for the technical enginneer to reply')]);
|
||||
}
|
||||
$ticketService = new TicketService();
|
||||
if (!$ticketService->reply(
|
||||
@@ -107,33 +101,29 @@ class TicketController extends Controller
|
||||
$request->input('message'),
|
||||
$request->user['id']
|
||||
)) {
|
||||
throw new ApiException(500, __('Ticket reply failed'));
|
||||
return $this->fail([400, __('Ticket reply failed')]);
|
||||
}
|
||||
$this->sendNotify($ticket, $request->input('message'));
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
|
||||
public function close(Request $request)
|
||||
{
|
||||
if (empty($request->input('id'))) {
|
||||
throw new ApiException(500, __('Invalid parameter'));
|
||||
return $this->fail([422, __('Invalid parameter')]);
|
||||
}
|
||||
$ticket = Ticket::where('id', $request->input('id'))
|
||||
->where('user_id', $request->user['id'])
|
||||
->first();
|
||||
if (!$ticket) {
|
||||
throw new ApiException(500, __('Ticket does not exist'));
|
||||
return $this->fail([400, __('Ticket does not exist')]);
|
||||
}
|
||||
$ticket->status = 1;
|
||||
if (!$ticket->save()) {
|
||||
throw new ApiException(500, __('Close failed'));
|
||||
return $this->fail([500, __('Close failed')]);
|
||||
}
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
private function getLastMessage($ticketId)
|
||||
@@ -146,18 +136,18 @@ class TicketController extends Controller
|
||||
public function withdraw(TicketWithdraw $request)
|
||||
{
|
||||
if ((int)admin_setting('withdraw_close_enable', 0)) {
|
||||
throw new ApiException(500, 'user.ticket.withdraw.not_support_withdraw');
|
||||
return $this->fail([400, 'Unsupported withdraw']);
|
||||
}
|
||||
if (!in_array(
|
||||
$request->input('withdraw_method'),
|
||||
admin_setting('commission_withdraw_method',Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT)
|
||||
)) {
|
||||
throw new ApiException(500, __('Unsupported withdrawal method'));
|
||||
return $this->fail([422, __('Unsupported withdrawal method')]);
|
||||
}
|
||||
$user = User::find($request->user['id']);
|
||||
$limit = admin_setting('commission_withdraw_limit', 100);
|
||||
if ($limit > ($user->commission_balance / 100)) {
|
||||
throw new ApiException(500, __('The current required minimum withdrawal commission is :limit', ['limit' => $limit]));
|
||||
return $this->fail([422, __('The current required minimum withdrawal commission is :limit', ['limit' => $limit])]);
|
||||
}
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
@@ -168,7 +158,7 @@ class TicketController extends Controller
|
||||
'user_id' => $request->user['id']
|
||||
]);
|
||||
if (!$ticket) {
|
||||
throw new ApiException(500, __('Failed to open ticket'));
|
||||
return $this->fail([400, __('Failed to open ticket')]);
|
||||
}
|
||||
$message = sprintf("%s\r\n%s",
|
||||
__('Withdrawal method') . ":" . $request->input('withdraw_method'),
|
||||
@@ -180,7 +170,7 @@ class TicketController extends Controller
|
||||
'message' => $message
|
||||
]);
|
||||
if (!$ticketMessage) {
|
||||
throw new ApiException(500, __('Failed to open ticket'));
|
||||
return $this->fail([400, __('Failed to open ticket')]);
|
||||
}
|
||||
DB::commit();
|
||||
}catch(\Exception $e){
|
||||
@@ -188,9 +178,7 @@ class TicketController extends Controller
|
||||
throw $e;
|
||||
}
|
||||
$this->sendNotify($ticket, $message);
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
private function sendNotify(Ticket $ticket, string $message)
|
||||
|
||||
@@ -24,24 +24,20 @@ class UserController extends Controller
|
||||
{
|
||||
$user = User::find($request->user['id']);
|
||||
if (!$user) {
|
||||
throw new ApiException(500, __('The user does not exist'));
|
||||
return $this->fail([400, __('The user does not exist')]);
|
||||
}
|
||||
$authService = new AuthService($user);
|
||||
return response([
|
||||
'data' => $authService->getSessions()
|
||||
]);
|
||||
return $this->success($authService->getSessions());
|
||||
}
|
||||
|
||||
public function removeActiveSession(Request $request)
|
||||
{
|
||||
$user = User::find($request->user['id']);
|
||||
if (!$user) {
|
||||
throw new ApiException(500, __('The user does not exist'));
|
||||
return $this->fail([400, __('The user does not exist')]);
|
||||
}
|
||||
$authService = new AuthService($user);
|
||||
return response([
|
||||
'data' => $authService->removeSession($request->input('session_id'))
|
||||
]);
|
||||
return $this->success($authService->removeSession($request->input('session_id')));
|
||||
}
|
||||
|
||||
public function checkLogin(Request $request)
|
||||
@@ -52,16 +48,14 @@ class UserController extends Controller
|
||||
if ($request->user['is_admin']) {
|
||||
$data['is_admin'] = true;
|
||||
}
|
||||
return response([
|
||||
'data' => $data
|
||||
]);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function changePassword(UserChangePassword $request)
|
||||
{
|
||||
$user = User::find($request->user['id']);
|
||||
if (!$user) {
|
||||
throw new ApiException(500, __('The user does not exist'));
|
||||
return $this->fail([400, __('The user does not exist')]);
|
||||
}
|
||||
if (!Helper::multiPasswordVerify(
|
||||
$user->password_algo,
|
||||
@@ -69,17 +63,15 @@ class UserController extends Controller
|
||||
$request->input('old_password'),
|
||||
$user->password)
|
||||
) {
|
||||
throw new ApiException(500, __('The old password is wrong'));
|
||||
return $this->fail([400, __('The old password is wrong')]);
|
||||
}
|
||||
$user->password = password_hash($request->input('new_password'), PASSWORD_DEFAULT);
|
||||
$user->password_algo = NULL;
|
||||
$user->password_salt = NULL;
|
||||
if (!$user->save()) {
|
||||
throw new ApiException(500, __('Save failed'));
|
||||
return $this->fail([400, __('Save failed')]);
|
||||
}
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
public function info(Request $request)
|
||||
@@ -104,12 +96,10 @@ class UserController extends Controller
|
||||
])
|
||||
->first();
|
||||
if (!$user) {
|
||||
throw new ApiException(500, __('The user does not exist'));
|
||||
return $this->fail([400, __('The user does not exist')]);
|
||||
}
|
||||
$user['avatar_url'] = 'https://cdn.v2ex.com/gravatar/' . md5($user->email) . '?s=64&d=identicon';
|
||||
return response([
|
||||
'data' => $user
|
||||
]);
|
||||
return $this->success($user);
|
||||
}
|
||||
|
||||
public function getStat(Request $request)
|
||||
@@ -124,9 +114,7 @@ class UserController extends Controller
|
||||
User::where('invite_user_id', $request->user['id'])
|
||||
->count()
|
||||
];
|
||||
return response([
|
||||
'data' => $stat
|
||||
]);
|
||||
return $this->success($stat);
|
||||
}
|
||||
|
||||
public function getSubscribe(Request $request)
|
||||
@@ -144,36 +132,32 @@ class UserController extends Controller
|
||||
])
|
||||
->first();
|
||||
if (!$user) {
|
||||
throw new ApiException(500, __('The user does not exist'));
|
||||
return $this->fail([400, __('The user does not exist')]);
|
||||
}
|
||||
if ($user->plan_id) {
|
||||
$user['plan'] = Plan::find($user->plan_id);
|
||||
if (!$user['plan']) {
|
||||
throw new ApiException(500, __('Subscription plan does not exist'));
|
||||
return $this->fail([400, __('Subscription plan does not exist')]);
|
||||
}
|
||||
}
|
||||
$user['subscribe_url'] = Helper::getSubscribeUrl("/api/v1/client/subscribe?token={$user['token']}");
|
||||
$userService = new UserService();
|
||||
$user['reset_day'] = $userService->getResetDay($user);
|
||||
return response([
|
||||
'data' => $user
|
||||
]);
|
||||
return $this->success($user);
|
||||
}
|
||||
|
||||
public function resetSecurity(Request $request)
|
||||
{
|
||||
$user = User::find($request->user['id']);
|
||||
if (!$user) {
|
||||
throw new ApiException(500, __('The user does not exist'));
|
||||
return $this->fail([400, __('The user does not exist')]);
|
||||
}
|
||||
$user->uuid = Helper::guid(true);
|
||||
$user->token = Helper::guid();
|
||||
if (!$user->save()) {
|
||||
throw new ApiException(500, __('Reset failed'));
|
||||
return $this->fail([400, __('Reset failed')]);
|
||||
}
|
||||
return response([
|
||||
'data' => Helper::getSubscribeUrl('/api/v1/client/subscribe?token=' . $user->token)
|
||||
]);
|
||||
return $this->success(Helper::getSubscribeUrl('/api/v1/client/subscribe?token=' . $user->token));
|
||||
}
|
||||
|
||||
public function update(UserUpdate $request)
|
||||
@@ -185,43 +169,39 @@ class UserController extends Controller
|
||||
|
||||
$user = User::find($request->user['id']);
|
||||
if (!$user) {
|
||||
throw new ApiException(500, __('The user does not exist'));
|
||||
return $this->fail([400, __('The user does not exist')]);
|
||||
}
|
||||
try {
|
||||
$user->update($updateData);
|
||||
} catch (\Exception $e) {
|
||||
throw new ApiException(500, __('Save failed'));
|
||||
return $this->fail([400, __('Save failed')]);
|
||||
}
|
||||
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
public function transfer(UserTransfer $request)
|
||||
{
|
||||
$user = User::find($request->user['id']);
|
||||
if (!$user) {
|
||||
throw new ApiException(500, __('The user does not exist'));
|
||||
return $this->fail([400, __('The user does not exist')]);
|
||||
}
|
||||
if ($request->input('transfer_amount') > $user->commission_balance) {
|
||||
throw new ApiException(500, __('Insufficient commission balance'));
|
||||
return $this->fail([400, __('Insufficient commission balance')]);
|
||||
}
|
||||
$user->commission_balance = $user->commission_balance - $request->input('transfer_amount');
|
||||
$user->balance = $user->balance + $request->input('transfer_amount');
|
||||
if (!$user->save()) {
|
||||
throw new ApiException(500, __('Transfer failed'));
|
||||
return $this->fail([400, __('Transfer failed')]);
|
||||
}
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
public function getQuickLoginUrl(Request $request)
|
||||
{
|
||||
$user = User::find($request->user['id']);
|
||||
if (!$user) {
|
||||
throw new ApiException(500, __('The user does not exist'));
|
||||
return $this->fail([400, __('The user does not exist')]);
|
||||
}
|
||||
|
||||
$code = Helper::guid();
|
||||
@@ -233,8 +213,6 @@ class UserController extends Controller
|
||||
} else {
|
||||
$url = url($redirect);
|
||||
}
|
||||
return response([
|
||||
'data' => $url
|
||||
]);
|
||||
return $this->success($url);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user