fix: 规范数据库事物的使用,解决在swoole环境下可能会出现事物一直不被提交的问题

This commit is contained in:
xboard
2023-12-06 19:00:26 +08:00
parent 64cc2d79da
commit 1fcb6fa911
13 changed files with 293 additions and 251 deletions
@@ -87,21 +87,26 @@ class CouponController extends Controller
$coupon['code'] = Helper::randomChar(8);
array_push($coupons, $coupon);
}
DB::beginTransaction();
if (!Coupon::insert(array_map(function ($item) use ($coupon) {
// format data
if (isset($item['limit_plan_ids']) && is_array($item['limit_plan_ids'])) {
$item['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']);
try{
DB::beginTransaction();
if (!Coupon::insert(array_map(function ($item) use ($coupon) {
// format data
if (isset($item['limit_plan_ids']) && is_array($item['limit_plan_ids'])) {
$item['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']);
}
if (isset($item['limit_period']) && is_array($item['limit_period'])) {
$item['limit_period'] = json_encode($coupon['limit_period']);
}
return $item;
}, $coupons))) {
throw new ApiException(500, '生成失败');
}
if (isset($item['limit_period']) && is_array($item['limit_period'])) {
$item['limit_period'] = json_encode($coupon['limit_period']);
}
return $item;
}, $coupons))) {
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw new ApiException(500, '生成失败');
throw $e;
}
DB::commit();
$data = "名称,类型,金额或比例,开始时间,结束时间,可用次数,可用于订阅,券码,生成时间\r\n";
foreach($coupons as $coupon) {
$type = ['', '金额', '比例'][$coupon['type']];
@@ -77,18 +77,18 @@ class KnowledgeController extends Controller
public function sort(KnowledgeSort $request)
{
DB::beginTransaction();
try {
DB::beginTransaction();
foreach ($request->input('knowledge_ids') as $k => $v) {
$knowledge = Knowledge::find($v);
$knowledge->timestamps = false;
$knowledge->update(['sort' => $k + 1]);
}
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
throw new ApiException(500, '保存失败');
}
DB::commit();
return response([
'data' => true
]);
@@ -156,34 +156,37 @@ class OrderController extends Controller
throw new ApiException(500, '该用户还有待支付的订单,无法分配');
}
DB::beginTransaction();
$order = new Order();
$orderService = new OrderService($order);
$order->user_id = $user->id;
$order->plan_id = $plan->id;
$order->period = $request->input('period');
$order->trade_no = Helper::guid();
$order->total_amount = $request->input('total_amount');
try {
DB::beginTransaction();
$order = new Order();
$orderService = new OrderService($order);
$order->user_id = $user->id;
$order->plan_id = $plan->id;
$order->period = $request->input('period');
$order->trade_no = Helper::guid();
$order->total_amount = $request->input('total_amount');
if ($order->period === 'reset_price') {
$order->type = 4;
} else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) {
$order->type = 3;
} else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) {
$order->type = 2;
} else {
$order->type = 1;
if ($order->period === 'reset_price') {
$order->type = 4;
} else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) {
$order->type = 3;
} else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) {
$order->type = 2;
} else {
$order->type = 1;
}
$orderService->setInvite($user);
if (!$order->save()) {
throw new ApiException(500, '订单创建失败');
}
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
}
$orderService->setInvite($user);
if (!$order->save()) {
DB::rollback();
throw new ApiException(500, '订单创建失败');
}
DB::commit();
return response([
'data' => $order->trade_no
]);
@@ -118,14 +118,19 @@ class PaymentController extends Controller
'ids.required' => '参数有误',
'ids.array' => '参数有误'
]);
DB::beginTransaction();
foreach ($request->input('ids') as $k => $v) {
if (!Payment::find($v)->update(['sort' => $k + 1])) {
DB::rollBack();
throw new ApiException(500, '保存失败');
try{
DB::beginTransaction();
foreach ($request->input('ids') as $k => $v) {
if (!Payment::find($v)->update(['sort' => $k + 1])) {
throw new ApiException(500, '保存失败');
}
}
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
}
DB::commit();
return response([
'data' => true
]);
@@ -50,11 +50,11 @@ class PlanController extends Controller
]);
}
$plan->update($params);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
throw new ApiException(500, '保存失败');
}
DB::commit();
return response([
'data' => true
]);
@@ -104,23 +104,24 @@ class PlanController extends Controller
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
]);
return $this->success();
}
public function sort(PlanSort $request)
{
DB::beginTransaction();
foreach ($request->input('plan_ids') as $k => $v) {
if (!Plan::find($v)->update(['sort' => $k + 1])) {
DB::rollBack();
throw new ApiException(500, '保存失败');
try{
DB::beginTransaction();
foreach ($request->input('plan_ids') as $k => $v) {
if (!Plan::find($v)->update(['sort' => $k + 1])) {
throw new ApiException(500, '保存失败');
}
}
DB::commit();
}catch (\Exception $e){
DB::rollBack();
throw $e;
}
DB::commit();
return response([
'data' => true
]);
return $this->success(true);
}
}
@@ -28,19 +28,21 @@ class ManageController extends Controller
'hysteria',
'vless'
) ?? [];
DB::beginTransaction();
foreach ($params as $k => $v) {
$model = 'App\\Models\\Server' . ucfirst($k);
foreach($v as $id => $sort) {
if (!$model::find($id)->update(['sort' => $sort])) {
DB::rollBack();
throw new ApiException(500, '保存失败');
try{
DB::beginTransaction();
foreach ($params as $k => $v) {
$model = 'App\\Models\\Server' . ucfirst($k);
foreach($v as $id => $sort) {
if (!$model::find($id)->update(['sort' => $sort])) {
throw new ApiException(500, '保存失败');
}
}
}
DB::commit();
}catch (\Exception $e){
DB::rollBack();
throw $e;
}
DB::commit();
return response([
'data' => true
]);
return $this->success(true);
}
}
@@ -230,12 +230,16 @@ class UserController extends Controller
$user['password'] = password_hash($request->input('password') ?? $user['email'], PASSWORD_DEFAULT);
array_push($users, $user);
}
DB::beginTransaction();
if (!User::insert($users)) {
try{
DB::beginTransaction();
if (!User::insert($users)) {
throw new ApiException(500, '生成失败');
}
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw new ApiException(500, '生成失败');
throw $e;
}
DB::commit();
$data = "账号,密码,过期时间,UUID,创建时间,订阅地址\r\n";
foreach($users as $user) {
$expireDate = $user['expired_at'] === NULL ? '长期有效' : date('Y-m-d H:i:s', $user['expired_at']);