Form.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\admin\controller\qingdong\general;
  3. use addons\qingdong\model\FormField;
  4. use app\admin\controller\qingdong\Base;
  5. use addons\qingdong\model\Field;
  6. use think\Db;
  7. use think\Exception;
  8. /**
  9. * 字段管理
  10. */
  11. class Form extends Base {
  12. protected $relationSearch = true;
  13. /**
  14. * @var \addons\qingdong\model\Form
  15. */
  16. protected $model = null;
  17. public function _initialize() {
  18. parent::_initialize();
  19. $this->model =new \addons\qingdong\model\Form;
  20. }
  21. /**
  22. * 查看
  23. */
  24. public function index() {
  25. //设置过滤方法
  26. $this->request->filter(['strip_tags', 'trim']);
  27. if ($this->request->isAjax()) {
  28. $forms = $this->model->where([])->select();
  29. $forms = collection($forms)->toArray();
  30. if (empty($forms)) {
  31. $insertALl = $this->model->field('name,type,data')->select();
  32. $insertALl = collection($insertALl)->toArray();
  33. foreach ($insertALl as $k => $item) {
  34. $item['createtime'] = time();
  35. $item['updatetime'] = time();
  36. $insertALl[$k] = $item;
  37. }
  38. $this->model->insertAll($insertALl);
  39. $forms = $this->model->select();
  40. $forms = collection($forms)->toArray();
  41. }
  42. $result = array("total" => count($forms), "rows" => $forms);
  43. return json($result);
  44. }
  45. return $this->view->fetch();
  46. }
  47. /**
  48. * 修改字段
  49. */
  50. public function edit($ids = null) {
  51. if ($this->request->isPost()) {
  52. $id = input('id');
  53. $data = input('data');
  54. if (empty($id) || empty($data)) {
  55. $this->error('参数错误');
  56. }
  57. $row = $this->model->where(['id' => $id])->find();
  58. if (empty($row)) {
  59. $this->error('表单不存在');
  60. }
  61. Db::startTrans();
  62. try {
  63. $result=$this->model->updateForm($id, $data);
  64. if($result === false){
  65. throw new Exception('修改表单失败');
  66. }
  67. Db::commit();
  68. } catch (Exception $e) {
  69. Db::rollback();
  70. $this->error($e->getMessage());
  71. }
  72. $this->success('设置成功');
  73. }
  74. $basefile = request()->baseFile();
  75. $this->assign("basefile", $basefile);
  76. $this->assign("token", $this->request->token());
  77. $this->view->engine->layout(false);
  78. return $this->view->fetch();
  79. }
  80. /**
  81. * 获取模板详情
  82. */
  83. public function getinfo() {
  84. $id = input('id');
  85. if (empty($id)) {
  86. $this->error('参数不存在');
  87. }
  88. $find = $this->model->where(['id' => $id])->find();
  89. $this->success('请求成功', '', $find);
  90. }
  91. }