Category.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\admin\controller\store\service;
  3. use app\common\controller\Backend;
  4. /**
  5. * 服务类别
  6. *
  7. * @icon fa fa-circle-o
  8. */
  9. class Category extends Backend
  10. {
  11. /**
  12. * Category模型对象
  13. * @var \app\admin\model\store\service\Category
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\store\service\Category;
  20. $this->view->assign("typeList", $this->model->getTypeList());
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. }
  23. private function fetchWhere()
  24. {
  25. $admin = $this->auth->getUserInfo();
  26. if (!$admin)
  27. $this->error("error");
  28. $p_where = [
  29. ["type", "=", \E_SERVICE_TYPE::Store],
  30. ];
  31. if (\E_ADMIN_TYPE::Store === $admin["type"]) {
  32. array_push($p_where, ["store_id", "=", $admin["store_id"]]);
  33. } else if (\E_ADMIN_TYPE::Agency === $admin["type"]) {
  34. $stores = (new \app\admin\model\store\Store())->fetchByAgency($admin);
  35. array_push($p_where, ["store_id", "in", array_map(function ($data) {
  36. return $data["id"];
  37. }, $stores)]);
  38. }
  39. return $p_where;
  40. }
  41. /**
  42. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  43. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  44. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  45. */
  46. /**
  47. * 查看
  48. */
  49. public function index()
  50. {
  51. //当前是否为关联查询
  52. $this->relationSearch = true;
  53. //设置过滤方法
  54. $this->request->filter(['strip_tags', 'trim']);
  55. $c_where = $this->fetchWhere();
  56. if ($this->request->isAjax()) {
  57. //如果发送的来源是Selectpage,则转发到Selectpage
  58. if ($this->request->request('keyField')) {
  59. return $this->c_selectpage($c_where);
  60. }
  61. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  62. $query = $this->model
  63. ->with(['store'])
  64. ->where($where);
  65. foreach ($c_where as $item) {
  66. $query->where($item[0], $item[1], $item[2]);
  67. }
  68. $list = $query
  69. ->order($sort, $order)
  70. ->paginate($limit);
  71. foreach ($list as $row) {
  72. $row->visible(['id', 'name', 'icon_image', 'sort', 'status', 'updatetime']);
  73. $row->visible(['store']);
  74. $row->getRelation('store')->visible(['name']);
  75. }
  76. $result = array("total" => $list->total(), "rows" => $list->items());
  77. return json($result);
  78. }
  79. return $this->view->fetch();
  80. }
  81. /**
  82. * @return mixed
  83. */
  84. public function add()
  85. {
  86. return parent::c_add(["type" => \E_SERVICE_TYPE::Store]);
  87. }
  88. }