Classes.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 班级管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Classes extends Backend
  11. {
  12. /**
  13. * Classes模型对象
  14. * @var \app\admin\model\unishop\Classes
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\unishop\Classes;
  21. $this->areamodel = new \app\admin\model\unishop\Area;
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //当前是否为关联查询
  34. $this->relationSearch = true;
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if ($this->request->isAjax())
  38. {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField'))
  41. {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $total = $this->model
  46. ->with(['grade'])
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->count();
  50. $list = $this->model
  51. ->with(['grade'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->limit($offset, $limit)
  55. ->select();
  56. foreach ($list as $row) {
  57. }
  58. $list = collection($list)->toArray();
  59. $result = array("total" => $total, "rows" => $list);
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 编辑
  66. */
  67. public function edit($ids = null)
  68. {
  69. $row = $this->model->get($ids);
  70. if (!$row) {
  71. $this->error(__('No Results were found'));
  72. }
  73. $adminIds = $this->getDataLimitAdminIds();
  74. if (is_array($adminIds)) {
  75. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  76. $this->error(__('You have no permission'));
  77. }
  78. }
  79. $areainfo = $this->areamodel->get($row["area_id"]);
  80. $row["area_name"] = "山东省/临沂市/".$areainfo->name;
  81. if ($this->request->isPost()) {
  82. $params = $this->request->post("row/a");
  83. if ($params) {
  84. $params = $this->preExcludeFields($params);
  85. $result = false;
  86. if(intval($params["area_id"]) == 0) {
  87. unset($params["area_id"]);
  88. }
  89. Db::startTrans();
  90. try {
  91. //是否采用模型验证
  92. if ($this->modelValidate) {
  93. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  94. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  95. $row->validateFailException(true)->validate($validate);
  96. }
  97. $result = $row->allowField(true)->save($params);
  98. Db::commit();
  99. } catch (ValidateException $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. } catch (PDOException $e) {
  103. Db::rollback();
  104. $this->error($e->getMessage());
  105. } catch (Exception $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. }
  109. if ($result !== false) {
  110. $this->success();
  111. } else {
  112. $this->error(__('No rows were updated'));
  113. }
  114. }
  115. $this->error(__('Parameter %s can not be empty', ''));
  116. }
  117. $this->view->assign("row", $row);
  118. return $this->view->fetch();
  119. }
  120. }