Service.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Service extends Backend
  10. {
  11. /**
  12. * Service模型对象
  13. * @var \app\admin\model\store\service\Service
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\store\service\Service;
  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. ["service.type", "=", \E_SERVICE_TYPE::Store]
  30. ];
  31. if (\E_ADMIN_TYPE::Store === $admin["type"]) {
  32. array_push($p_where, ["service.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, ["service.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. if ($this->request->isAjax()) {
  56. //如果发送的来源是Selectpage,则转发到Selectpage
  57. if ($this->request->request('keyField')) {
  58. return $this->c_selectpage($this->fetchWhere());
  59. }
  60. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  61. $query = $this->model
  62. ->with(['store', 'category'])
  63. ->where($where);
  64. foreach ($this->fetchWhere() as $item) {
  65. $query->where($item[0], $item[1], $item[2]);
  66. }
  67. $list = $query
  68. ->order($sort, $order)
  69. ->paginate($limit);
  70. foreach ($list as $row) {
  71. $row->visible(['id', 'name', 'cover_image', 'real_price', 'duration_minute', 'introduce_images', 'hot', 'sift', 'count', 'sort', 'status', 'updatetime']);
  72. $row->visible(['store']);
  73. $row->getRelation('store')->visible(['name']);
  74. $row->visible(['category']);
  75. $row->getRelation('category')->visible(['name']);
  76. }
  77. $result = array("total" => $list->total(), "rows" => $list->items());
  78. return json($result);
  79. }
  80. return $this->view->fetch();
  81. }
  82. public function add()
  83. {
  84. return parent::c_add(["type" => \E_SERVICE_TYPE::Store]);
  85. }
  86. }