SsConfigController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Http\Controllers\Admin\Config;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\SsConfig;
  5. use Exception;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Http\Request;
  8. use Log;
  9. use Validator;
  10. class SsConfigController extends Controller
  11. {
  12. public function store(Request $request): JsonResponse
  13. { // 添加SS配置
  14. $validator = Validator::make($request->all(), [
  15. 'name' => 'required|string|unique:ss_config,name',
  16. 'type' => 'required|numeric|between:1,3',
  17. ]);
  18. if ($validator->fails()) {
  19. return response()->json(['status' => 'fail', 'message' => $validator->errors()->all()]);
  20. }
  21. if (SsConfig::create($validator->validated())) {
  22. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.add')])]);
  23. }
  24. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')])]);
  25. }
  26. public function update(SsConfig $ss): JsonResponse
  27. { // 设置SS默认配置
  28. if ($ss->setDefault()) {
  29. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.edit')])]);
  30. }
  31. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')])]);
  32. }
  33. public function destroy(SsConfig $ss): JsonResponse
  34. { // 删除SS配置
  35. try {
  36. if ($ss->delete()) {
  37. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
  38. }
  39. } catch (Exception $e) {
  40. Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('user.node.info')]).': '.$e->getMessage());
  41. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')]).', '.$e->getMessage()]);
  42. }
  43. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
  44. }
  45. }