Datadictitem.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\admin\controller\stock;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 字典数据
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Datadictitem extends Backend {
  11. /**
  12. * Datadictitem模型对象
  13. * @var \app\admin\model\stock\Datadictitem
  14. */
  15. protected $model = null;
  16. protected $relationSearch = true;
  17. protected $searchFields = 'name,value';
  18. public function _initialize() {
  19. parent::_initialize();
  20. $this->model = model('\app\admin\model\stock\Datadictitem');
  21. $dataDiclist = collection(model('\app\admin\model\stock\Datadict')->order('weigh', 'desc')->select())->toArray();
  22. Tree::instance()->init($dataDiclist);
  23. $this->dataDiclist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  24. foreach ($this->dataDiclist as $k => &$v) {
  25. $dataDicdata[$v['id']] = $v['name'];
  26. }
  27. $this->view->assign('dataDicdata', $dataDicdata);
  28. $this->view->assign("enabledList", $this->model->getEnabledList());
  29. }
  30. /**
  31. * 查看
  32. */
  33. public function index() {
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags']);
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $datadict_id = $this->request->request('datadict_id');
  43. if ($datadict_id) {
  44. $total = $this->model->where($where)->where('datadict_id', 'in', $datadict_id)->order($sort, $order)->count();
  45. $list = $this->model->where($where)->where('datadict_id', 'in', $datadict_id)->order($sort, $order)->limit($offset, $limit)->select();
  46. } else {
  47. // $total = $this->model->where($where)->count();
  48. // $list = $this->model->where($where)->order($sort, $order)->limit($offset, $limit)->select();
  49. $total = [];
  50. $list = [];
  51. }
  52. $list = collection($list)->toArray();
  53. $result = array("total" => $total, "rows" => $list);
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 添加
  60. */
  61. public function add() {
  62. if ($this->request->isPost()) {
  63. $params = $this->request->post("row/a");
  64. if ($params) {
  65. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  66. $params[$this->dataLimitField] = $this->auth->id;
  67. }
  68. try {
  69. //是否采用模型验证
  70. if ($this->modelValidate) {
  71. $name = basename(str_replace('\\', '/', get_class($this->model)));
  72. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  73. $this->model->validate($validate);
  74. }
  75. $result = $this->model->allowField(true)->save($params);
  76. if ($result !== false) {
  77. $this->success();
  78. } else {
  79. $this->error($this->model->getError());
  80. }
  81. } catch (\think\exception\PDOException $e) {
  82. $this->error($e->getMessage());
  83. }
  84. }
  85. $this->error(__('Parameter %s can not be empty', ''));
  86. }
  87. $datadict_id = $this->request->param("datadict_id");
  88. $this->view->assign("datadict_id", $datadict_id);
  89. return $this->view->fetch();
  90. }
  91. /**
  92. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  93. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  94. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  95. */
  96. }