StoreBaseController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/20
  6. * @time: 11:24
  7. */
  8. namespace app\common;
  9. use app\Request;
  10. use qiniu\basic\BaseController;
  11. use qiniu\exceptions\AdminException;
  12. abstract class StoreBaseController extends BaseController
  13. {
  14. /**
  15. * 当前登陆管理员信息
  16. * @var
  17. */
  18. protected $adminInfo;
  19. /**
  20. * 当前登陆管理员ID
  21. * @var
  22. */
  23. protected $adminId;
  24. protected $storeId;
  25. protected $storeInfo;
  26. protected $auth = [];
  27. // 搜索条件
  28. protected $searchable = [];
  29. // 搜索处理
  30. protected $searchDeal = null;
  31. // 创建参数
  32. protected $createParams = [];
  33. // 新增处理
  34. protected $saveDeal = null;
  35. // 更新处理
  36. protected $updateDeal = null;
  37. protected $validate = null;
  38. protected $service = null;
  39. protected $with = [];
  40. protected $storeSearch = false;
  41. /**
  42. * 初始化
  43. */
  44. public function __construct(Request $request)
  45. {
  46. parent::__construct($request);
  47. $this->initialize();
  48. }
  49. protected function initialize()
  50. {
  51. $this->adminId = $this->request->adminId();
  52. $this->adminInfo = $this->request->adminInfo();
  53. $this->storeId = $this->request->adminStoreId();
  54. $this->storeInfo = $this->request->adminStoreInfo();
  55. $this->auth = $this->adminInfo['rule'] ?? [];
  56. }
  57. public function index()
  58. {
  59. if (!$this->service) {
  60. throw new AdminException('接口不存在');
  61. }
  62. $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
  63. if ($this->storeSearch) $where['store_id'] = $this->storeId;
  64. list($page, $limit) = $this->service->getPageValue();
  65. $list = $this->service->getList($where, '*', $page, $limit, $this->with);
  66. $count = $this->service->getCount($where);
  67. return $this->success(compact('list', 'count'));
  68. }
  69. public function read($id)
  70. {
  71. if (!$this->service) {
  72. throw new AdminException('接口不存在');
  73. }
  74. if ($this->storeSearch) {
  75. $info = $this->service->getOne(['id' => $id, 'store_id' => $this->storeId], '*', $this->with);
  76. } else {
  77. $info = $this->service->get($id, '*', $this->with);
  78. }
  79. if (!$info)
  80. return $this->error('数据不存在');
  81. return $this->success('ok', $info->toArray());
  82. }
  83. public function save()
  84. {
  85. if (!$this->service) {
  86. throw new AdminException('接口不存在');
  87. }
  88. $data = $this->request->postMore($this->createParams, false, $this->saveDeal);
  89. if ($this->validate) {
  90. $this->validate($data, $this->validate, 'save');
  91. }
  92. $res = $this->service->create($data);
  93. if ($res) return $this->success('添加成功');
  94. return $this->error('添加失败');
  95. }
  96. public function validate($data, $validate, $s = '')
  97. {
  98. $scene = method_exists($validate, 'allScene') ? ($validate->allScene() ?? []) : [];
  99. if (in_array($s, $scene))
  100. $res = $validate->scene($s)->check($data);
  101. else
  102. $res = $validate->check($data);
  103. if (!$res) throw new AdminException($validate->getError());
  104. }
  105. public function update($id)
  106. {
  107. if (!$this->service) {
  108. throw new AdminException('接口不存在');
  109. }
  110. if ($this->storeSearch) {
  111. $info = $this->service->getOne(['id' => $id, 'store_id' => $this->storeId], '*', $this->with);
  112. } else {
  113. $info = $this->service->get($id);
  114. }
  115. if (!$info)
  116. return $this->error('数据不存在');
  117. $data = $this->request->postMore($this->createParams, false, $this->updateDeal, $id);
  118. if ($this->validate) {
  119. $this->validate($data, $this->validate, 'update');
  120. }
  121. $res = $this->service->update($id, $data);
  122. if ($res) return $this->success('修改成功');
  123. return $this->error('修改失败');
  124. }
  125. public function delete($id)
  126. {
  127. if (!$this->service) {
  128. throw new AdminException('接口不存在');
  129. }
  130. if ($this->storeSearch) {
  131. $info = $this->service->getOne(['id' => $id, 'store_id' => $this->storeId], '*', $this->with);
  132. } else {
  133. $info = $this->service->get($id);
  134. }
  135. if (!$info)
  136. return $this->error('数据不存在');
  137. $res = $this->service->delete($id);
  138. if ($res) return $this->success('已删除');
  139. return $this->error('删除失败');
  140. }
  141. public function export()
  142. {
  143. if (!$this->service) {
  144. throw new AdminException('接口不存在');
  145. }
  146. $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
  147. if ($this->storeSearch) $where['store_id'] = $this->storeId;
  148. $export_type = sys_config('export_type', 1);
  149. return $this->success($this->service->export($where, $export_type));
  150. }
  151. }