ConfigInfo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\admin\controller\exam;
  3. use addons\exam\library\FrontService;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 参数配置
  10. * @icon fa fa-circle-o
  11. */
  12. class ConfigInfo extends Backend
  13. {
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * ConfigInfoModel模型对象
  17. * @var \app\admin\model\exam\ConfigInfoModel
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\exam\ConfigInfoModel;
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. public function index()
  36. {
  37. $isUpdate = false;
  38. $this->model = $this->model->get(1);
  39. if (!empty($this->model['id'])) {
  40. $isUpdate = true;
  41. $configs = $this->model->toArray();
  42. unset($configs['id']);
  43. $row = [];
  44. foreach ($configs as $key => $val) {
  45. $val = $val ? json_decode($val, true) : [];
  46. $row = array_merge($row, $val);
  47. }
  48. // dump($row);exit;
  49. $this->assign('row', $row);
  50. $this->assign('config_id', $this->model['id']);
  51. } else {
  52. $this->model = new \app\admin\model\exam\ConfigInfoModel;
  53. $this->model->save(['id' => 1]);
  54. }
  55. if ($this->request->isPost()) {
  56. $params = $this->request->post("row/a");
  57. if ($params) {
  58. $params = $this->preExcludeFields($params);
  59. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  60. $params[$this->dataLimitField] = $this->auth->id;
  61. }
  62. $result = false;
  63. Db::startTrans();
  64. try {
  65. $type = $this->request->param('type');
  66. $data = $params;
  67. //转json存到字段
  68. $params["{$type}_config"] = json_encode($data);
  69. $result = $this->model->isUpdate($isUpdate)->allowField(true)->save($params);
  70. Db::commit();
  71. } catch (ValidateException $e) {
  72. Db::rollback();
  73. $this->error($e->getMessage());
  74. } catch (PDOException $e) {
  75. Db::rollback();
  76. $this->error($e->getMessage());
  77. } catch (\Exception $e) {
  78. Db::rollback();
  79. $this->error($e->getMessage());
  80. }
  81. if ($result !== false) {
  82. $this->success();
  83. } else {
  84. $this->error(__('No rows were inserted'));
  85. }
  86. }
  87. $this->error(__('Parameter %s can not be empty', ''));
  88. }
  89. return $this->view->fetch();
  90. }
  91. /**
  92. * 设置前端跳转页面
  93. */
  94. public function frontend()
  95. {
  96. if ($this->request->isPost()) {
  97. $params = $this->request->post("info/a");
  98. $full_url = $params['url'] ?? '';
  99. if (isset($params['params'])) {
  100. $full_url = FrontService::buildUrl($full_url, $params['params']);
  101. }
  102. $params['full_url'] = $full_url;
  103. $this->success('', '', $params);
  104. } else {
  105. $pages = FrontService::PAGES;
  106. $this->assign('pages', $pages);
  107. return $this->view->fetch();
  108. }
  109. }
  110. }