Page.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use think\Hook;
  5. /**
  6. * 单页表
  7. *
  8. * @icon fa fa-file
  9. */
  10. class Page extends Backend
  11. {
  12. /**
  13. * Page模型对象
  14. */
  15. protected $model = null;
  16. protected $noNeedRight = ['select', 'selectpage_type', 'check_element_available'];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\shop\Page;
  21. $this->view->assign("flagList", $this->model->getFlagList());
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. }
  24. /**
  25. * 查看
  26. */
  27. public function index()
  28. {
  29. $typeArr = \app\admin\model\shop\Page::distinct('type')->column('type');
  30. $this->view->assign('typeList', $typeArr);
  31. $this->assignconfig('typeList', $typeArr);
  32. return parent::index();
  33. }
  34. public function add()
  35. {
  36. if ($this->request->isPost()) {
  37. $row = $this->request->post("row/a", []);
  38. if (isset($row['parsetpl']) && $row['parsetpl']) {
  39. $this->token();
  40. }
  41. }
  42. $values = [];
  43. $fields = [];
  44. $this->view->assign('fields', $fields);
  45. $this->view->assign('values', $values);
  46. return parent::add();
  47. }
  48. public function edit($ids = null)
  49. {
  50. if ($this->request->isPost()) {
  51. $row = $this->request->post("row/a", []);
  52. if (isset($row['parsetpl']) && $row['parsetpl']) {
  53. $this->token();
  54. }
  55. }
  56. $values = \app\admin\model\shop\Page::get($ids);
  57. if (!$values) {
  58. $this->error(__('No Results were found'));
  59. }
  60. $values = $values->toArray();
  61. $fields = [];
  62. $this->view->assign('fields', $fields);
  63. $this->view->assign('values', $values);
  64. return parent::edit($ids);
  65. }
  66. /**
  67. * 选择单页
  68. */
  69. public function select()
  70. {
  71. if (!$this->auth->check('shop/page/index')) {
  72. Hook::listen('admin_nopermission', $this);
  73. $this->error(__('You have no permission'), '');
  74. }
  75. $typeArr = \app\admin\model\shop\Page::distinct('type')->column('type');
  76. $this->view->assign('typeList', $typeArr);
  77. $this->assignconfig('typeList', $typeArr);
  78. //设置过滤方法
  79. $this->request->filter(['strip_tags']);
  80. if ($this->request->isAjax()) {
  81. //如果发送的来源是Selectpage,则转发到Selectpage
  82. if ($this->request->request('keyField')) {
  83. return $this->selectpage();
  84. }
  85. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  86. $total = $this->model
  87. ->where($where)
  88. ->order($sort, $order)
  89. ->count();
  90. $list = $this->model
  91. ->where($where)
  92. ->order($sort, $order)
  93. ->limit($offset, $limit)
  94. ->select();
  95. $list = collection($list)->toArray();
  96. $result = array("total" => $total, "rows" => $list);
  97. return json($result);
  98. }
  99. return $this->view->fetch();
  100. }
  101. /**
  102. * 动态下拉选择类型
  103. * @internal
  104. */
  105. public function selectpage_type()
  106. {
  107. $list = [];
  108. $word = (array)$this->request->request("q_word/a");
  109. $field = $this->request->request('showField');
  110. $keyValue = $this->request->request('keyValue');
  111. if (!$keyValue) {
  112. if (array_filter($word)) {
  113. foreach ($word as $k => $v) {
  114. $list[] = ['id' => $v, $field => $v];
  115. }
  116. }
  117. $typeArr = \app\admin\model\shop\Page::column('type');
  118. $typeArr = array_unique($typeArr);
  119. foreach ($typeArr as $index => $item) {
  120. $list[] = ['id' => $item, $field => $item];
  121. }
  122. } else {
  123. $list[] = ['id' => $keyValue, $field => $keyValue];
  124. }
  125. return json(['total' => count($list), 'list' => $list]);
  126. }
  127. /**
  128. * 检测元素是否可用
  129. * @internal
  130. */
  131. public function check_element_available()
  132. {
  133. $id = $this->request->request('id');
  134. $name = $this->request->request('name');
  135. $value = $this->request->request('value');
  136. $name = substr($name, 4, -1);
  137. if (!$name) {
  138. $this->error(__('Parameter %s can not be empty', 'name'));
  139. }
  140. if ($id) {
  141. $this->model->where('id', '<>', $id);
  142. }
  143. $exist = $this->model->where($name, $value)->find();
  144. if ($exist) {
  145. $this->error(__('The data already exist'));
  146. } else {
  147. $this->success();
  148. }
  149. }
  150. }