FormSetController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. $form_set = FormData::first();
  52. if(empty($form_set)) {
  53. return response()->json(['status' => 'fail', 'info' => '参数错误']);
  54. }
  55. $data = $request->input('data');
  56. if(!isset($data['type']) || !in_array($data['type'], ['pay', 'form'])) {
  57. return response()->json(['status' => 'fail', 'info' => '参数错误']);
  58. }
  59. $items = ['text_1', 'text_2', 'text_3', 'text_4', 'multi_text', 'radio'];
  60. foreach($items as $item) {
  61. if(isset($data[$item])) {
  62. $data[$item] = $form_set[$item] . ':' . $data[$item];
  63. }
  64. }
  65. if(isset($data['checkbox']) && is_array($data['checkbox'])) {
  66. $data['checkbox'] = implode(',', $data['checkbox']);
  67. $data['checkbox'] = $form_set['checkbox'] . ':' . $data['checkbox'];
  68. }
  69. unset($data['type']);
  70. $res = FormData::create($data);
  71. if(empty($res)) {
  72. return response()->json(['status' => 'fail', 'info' => '保存失败']);
  73. }
  74. return response()->json(['status' => 'success', 'info' => '提交成功']);
  75. }
  76. }