Fal.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\DbException;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use think\response\Json;
  10. /**
  11. * 功能
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Fal extends Backend
  16. {
  17. /**
  18. * Fal模型对象
  19. * @var \app\admin\model\functional\Fal
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\functional\Fal;
  26. $model = model('app\common\model\Category');
  27. $tree = Tree::instance();
  28. $tree->init(collection($model->where('type','web')->order('weigh desc,id desc')->select())->toArray(), 'pid');
  29. $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  30. $categorydata = array();
  31. foreach ($this->categorylist as $k => $v) {
  32. $categorydata[$v['id']] = $v;
  33. }
  34. $this->view->assign("parentList", $categorydata);
  35. $this->view->assign("statusList", $this->model->getStatusList());
  36. }
  37. /**
  38. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  39. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  40. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  41. */
  42. /**
  43. * 查看
  44. *
  45. * @return string|Json
  46. * @throws \think\Exception
  47. * @throws DbException
  48. */
  49. public function index()
  50. {
  51. //设置过滤方法
  52. $this->request->filter(['strip_tags', 'trim']);
  53. if (false === $this->request->isAjax()) {
  54. return $this->view->fetch();
  55. }
  56. //如果发送的来源是 Selectpage,则转发到 Selectpage
  57. if ($this->request->request('keyField')) {
  58. return $this->selectpage();
  59. }
  60. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  61. if($this->auth->id==1){
  62. $list = $this->model->where($where)->order($sort, $order)
  63. ->paginate($limit);
  64. }else{
  65. $website=$this->auth->username;
  66. $list = $this->model->where($where)->where("website='{$website}'")->order($sort, $order)
  67. ->paginate($limit);
  68. }
  69. $result = ['total' => $list->total(), 'rows' => $list->items()];
  70. return json($result);
  71. }
  72. /**
  73. * 添加
  74. *
  75. * @return string
  76. * @throws \think\Exception
  77. */
  78. public function add()
  79. {
  80. if (false === $this->request->isPost()) {
  81. return $this->view->fetch();
  82. }
  83. $params = $this->request->post('row/a');
  84. if (empty($params)) {
  85. $this->error(__('Parameter %s can not be empty', ''));
  86. }
  87. $params = $this->preExcludeFields($params);
  88. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  89. $params[$this->dataLimitField] = $this->auth->id;
  90. }
  91. $params['website']=$this->auth->username;
  92. $result = false;
  93. Db::startTrans();
  94. try {
  95. //是否采用模型验证
  96. if ($this->modelValidate) {
  97. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  98. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  99. $this->model->validateFailException()->validate($validate);
  100. }
  101. $result = $this->model->allowField(true)->save($params);
  102. Db::commit();
  103. } catch (ValidateException|PDOException|Exception $e) {
  104. Db::rollback();
  105. $this->error($e->getMessage());
  106. }
  107. if ($result === false) {
  108. $this->error(__('No rows were inserted'));
  109. }
  110. $this->success();
  111. }
  112. }