AdminDistController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use Dcat\Admin\Layout\Content;
  4. use Illuminate\Routing\Controller;
  5. class AdminDistController extends Controller
  6. {
  7. /**
  8. * Title for current resource.
  9. *
  10. * @var string
  11. */
  12. protected $title;
  13. /**
  14. * Set description for following 4 action pages.
  15. *
  16. * @var array
  17. */
  18. protected $description = [
  19. // 'index' => 'Index',
  20. // 'show' => 'Show',
  21. // 'edit' => 'Edit',
  22. // 'create' => 'Create',
  23. ];
  24. /**
  25. * Set translation path.
  26. *
  27. * @var string
  28. */
  29. protected $translation;
  30. /**
  31. * Get content title.
  32. *
  33. * @return string
  34. */
  35. protected function title()
  36. {
  37. return $this->title ?: admin_trans_label();
  38. }
  39. /**
  40. * Get description for following 4 action pages.
  41. *
  42. * @return array
  43. */
  44. protected function description()
  45. {
  46. return $this->description;
  47. }
  48. /**
  49. * Get translation path.
  50. *
  51. * @return string
  52. */
  53. protected function translation()
  54. {
  55. return $this->translation;
  56. }
  57. /**
  58. * Index interface.
  59. *
  60. * @param Content $content
  61. * @return Content
  62. */
  63. public function index(Content $content)
  64. {
  65. return $content
  66. ->translation($this->translation())
  67. ->title($this->title())
  68. ->description($this->description()['index'] ?? trans('admin.list'))
  69. ->body($this->grid());
  70. }
  71. /**
  72. * Show interface.
  73. *
  74. * @param mixed $id
  75. * @param Content $content
  76. * @return Content
  77. */
  78. public function show($id, Content $content)
  79. {
  80. if ($this->distFindCountByID($id) === 0) {
  81. abort(404);
  82. }
  83. return $content
  84. ->translation($this->translation())
  85. ->title($this->title())
  86. ->description($this->description()['show'] ?? trans('admin.show'))
  87. ->body($this->detail($id));
  88. }
  89. /**
  90. * Edit interface.
  91. *
  92. * @param mixed $id
  93. * @param Content $content
  94. * @return Content
  95. */
  96. public function edit($id, Content $content)
  97. {
  98. if ($this->distFindCountByID($id) === 0) {
  99. abort(404);
  100. }
  101. return $content
  102. ->translation($this->translation())
  103. ->title($this->title())
  104. ->description($this->description()['edit'] ?? trans('admin.edit'))
  105. ->body($this->form()->edit($id));
  106. }
  107. /**
  108. * Create interface.
  109. *
  110. * @param Content $content
  111. * @return Content
  112. */
  113. public function create(Content $content)
  114. {
  115. return $content
  116. ->translation($this->translation())
  117. ->title($this->title())
  118. ->description($this->description()['create'] ?? trans('admin.create'))
  119. ->body($this->form());
  120. }
  121. /**
  122. * Update the specified resource in storage.
  123. *
  124. * @param int $id
  125. * @return \Illuminate\Http\Response
  126. */
  127. public function update($id)
  128. {
  129. if ($this->distFindCountByID($id) === 0) {
  130. abort(404);
  131. }
  132. return $this->form()->update($id);
  133. }
  134. /**
  135. * Store a newly created resource in storage.
  136. *
  137. * @return mixed
  138. */
  139. public function store()
  140. {
  141. return $this->form()->store();
  142. }
  143. /**
  144. * Remove the specified resource from storage.
  145. *
  146. * @param int $id
  147. * @return \Illuminate\Http\Response
  148. */
  149. public function destroy($id)
  150. {
  151. if ($this->distFindCountByID($id) === 0) {
  152. abort(404);
  153. }
  154. return $this->form()->destroy($id);
  155. }
  156. /*
  157. * 查找当前数据是否属于当前分销商
  158. * -1 form方法不存在
  159. * 0 数据不存在
  160. * 1以上 数据存在
  161. */
  162. protected function distFindCountByID($id)
  163. {
  164. //如果有form方法,查找数据
  165. if (method_exists($this, 'Form')) {
  166. $idArray = explode(',', $id);
  167. // 使用 array_map 将数组中的每个值转换为数字类型
  168. $idArray = array_map('intval', $idArray);
  169. $form = $this->Form();
  170. $repository = $form->repository();
  171. $count = $repository->model()->where('dist_id', getDistributorId())->wherein('id', $idArray)->count();
  172. if ($count === 0) {
  173. //0数据不存在
  174. return 0;
  175. } else if ($count !== count($idArray)) {
  176. //0数据异常
  177. return 0;
  178. } else {
  179. //数据存在
  180. return 1;
  181. }
  182. }
  183. //form方法不存在
  184. return -1;
  185. }
  186. }