FormSetController.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 programCode(Request $request)
  47. {
  48. $app = app('wechat.mini_program');
  49. $response = $app->app_code->getUnlimit('ad', ['page' => 'pages/form/index']);
  50. $response->saveAs('codes', 'app-code.png');
  51. return redirect('/public/codes/app-code.png');
  52. }
  53. }