SsConfigController.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. try {
  22. if (SsConfig::create($validator->validated())) {
  23. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.add')])]);
  24. }
  25. } catch (Exception $e) {
  26. Log::error(trans('common.error_action_item', ['action' => trans('common.add'), 'attribute' => trans('user.node.info')]).': '.$e->getMessage());
  27. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')]).', '.$e->getMessage()]);
  28. }
  29. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.add')])]);
  30. }
  31. public function update(SsConfig $ss): JsonResponse
  32. { // 设置SS默认配置
  33. try {
  34. if ($ss->setDefault()) {
  35. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.edit')])]);
  36. }
  37. } catch (Exception $e) {
  38. Log::error(trans('common.error_action_item', ['action' => trans('common.edit'), 'attribute' => trans('user.node.info')]).': '.$e->getMessage());
  39. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')]).', '.$e->getMessage()]);
  40. }
  41. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.edit')])]);
  42. }
  43. public function destroy(SsConfig $ss): JsonResponse
  44. { // 删除SS配置
  45. // 检查是否为默认配置
  46. if ($ss->is_default) {
  47. return response()->json(['status' => 'fail', 'message' => trans('admin.setting.common.config_default_cannot_delete')]);
  48. }
  49. try {
  50. if ($ss->delete()) {
  51. return response()->json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.delete')])]);
  52. }
  53. } catch (Exception $e) {
  54. Log::error(trans('common.error_action_item', ['action' => trans('common.delete'), 'attribute' => trans('user.node.info')]).': '.$e->getMessage());
  55. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')]).', '.$e->getMessage()]);
  56. }
  57. return response()->json(['status' => 'fail', 'message' => trans('common.failed_item', ['attribute' => trans('common.delete')])]);
  58. }
  59. }