FormSetController.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\FormData;
  4. use App\Models\FormSet;
  5. use Illuminate\Http\Request;
  6. class FormSetController extends Controller
  7. {
  8. protected $redirect_index = '/admin/FormSet/index';
  9. protected $view_path = 'admin.form-sets.';
  10. protected $pre_uri = '/admin/FormSet/';
  11. protected $model_name = '表单设置';
  12. protected $model;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->model = new FormSet();
  17. }
  18. public function index()
  19. {
  20. $item = $this->model->first();
  21. if(empty($item)) {
  22. $item = $this->model->createDefault();
  23. }
  24. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  25. return view($this->view_path . 'index', compact('pre_uri', 'model', 'model_name', 'item'));
  26. }
  27. public function update(Request $request)
  28. {
  29. if(!$request->isMethod('POST')) {
  30. return $this->showWarning('访问错误');
  31. }
  32. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id'))) || !is_array($request->input('data'))) {
  33. return $this->showWarning('参数错误');
  34. }
  35. $data = $request->input('data');
  36. $values = collect(['show_below_index', 'text_1_status', 'text_1_need', 'text_2_status', 'text_2_need', 'text_3_status', 'text_3_need', 'text_4_status', 'text_4_need', 'multi_text_status', 'multi_text_need', 'radio_status', 'radio_need', 'checkbox_status', 'checkbox_need']);
  37. foreach($values as $value) {
  38. $data[$value] = isset($data[$value]) ? $data[$value] : 2;
  39. }
  40. $data['money'] = floor($data['money'] * 100);
  41. if(!$item->update($data)) {
  42. return $this->showWarning('数据库保存失败');
  43. }
  44. return $this->showMessage('操作成功');
  45. }
  46. public function submitForm(Request $request)
  47. {
  48. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  49. return response()->json(['status' => 'fail', 'info' => '参数错误']);
  50. }
  51. $data = $request->input('data');
  52. if(!isset($data['type']) || !in_array($data['type'], ['pay', 'form'])) {
  53. return response()->json(['status' => 'fail', 'info' => '参数错误']);
  54. }
  55. if(isset($data['checkbox']) && is_array($data['checkbox'])) {
  56. $data['checkbox'] = implode(',', $data['checkbox']);
  57. }
  58. unset($data['type']);
  59. $res = FormData::create($data);
  60. if(empty($res)) {
  61. return response()->json(['status' => 'fail', 'info' => '保存失败']);
  62. }
  63. return response()->json(['status' => 'success', 'info' => '提交成功']);
  64. }
  65. }