Fal.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\admin\controller\functional;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 功能
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Fal extends Backend
  14. {
  15. /**
  16. * Fal模型对象
  17. * @var \app\admin\model\functional\Fal
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\functional\Fal;
  24. $model = model('app\common\model\Category');
  25. $tree = Tree::instance();
  26. $tree->init(collection($model->where('type','web')->order('weigh desc,id desc')->select())->toArray(), 'pid');
  27. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  28. $categorydata = array();
  29. foreach ($this->categorylist as $k => $v) {
  30. $categorydata[$v['id']] = $v;
  31. }
  32. $this->view->assign("parentList", $categorydata);
  33. $this->view->assign("statusList", $this->model->getStatusList());
  34. }
  35. /**
  36. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  37. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  38. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  39. */
  40. /**
  41. * 添加
  42. *
  43. * @return string
  44. * @throws \think\Exception
  45. */
  46. public function add()
  47. {
  48. if (false === $this->request->isPost()) {
  49. return $this->view->fetch();
  50. }
  51. $params = $this->request->post('row/a');
  52. if (empty($params)) {
  53. $this->error(__('Parameter %s can not be empty', ''));
  54. }
  55. $params = $this->preExcludeFields($params);
  56. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  57. $params[$this->dataLimitField] = $this->auth->id;
  58. }
  59. $params['website']=$this->auth->username;
  60. $result = false;
  61. Db::startTrans();
  62. try {
  63. //是否采用模型验证
  64. if ($this->modelValidate) {
  65. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  66. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  67. $this->model->validateFailException()->validate($validate);
  68. }
  69. $result = $this->model->allowField(true)->save($params);
  70. Db::commit();
  71. } catch (ValidateException|PDOException|Exception $e) {
  72. Db::rollback();
  73. $this->error($e->getMessage());
  74. }
  75. if ($result === false) {
  76. $this->error(__('No rows were inserted'));
  77. }
  78. $this->success();
  79. }
  80. }