ProjectController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Services\ProjectService;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Http\Request;
  6. use Slowlyo\OwlAdmin\Controllers\AdminController;
  7. /**
  8. * @group 后台
  9. * 项目服务
  10. *
  11. * @property ProjectService $service
  12. */
  13. class ProjectController extends AdminController
  14. {
  15. protected string $serviceName = ProjectService::class;
  16. public function list()
  17. {
  18. $crud = $this->baseCRUD()
  19. ->filterTogglable(false)
  20. ->headerToolbar([
  21. $this->createButton('dialog'),
  22. ...$this->baseHeaderToolBar(),
  23. ])
  24. ->columns([
  25. amis()->TableColumn('id', 'ID')->sortable(),
  26. amis()->TableColumn('cate_id', '项目分类编号'),
  27. amis()->TableColumn('cover', '项目封面'),
  28. amis()->TableColumn('title', '项目标题'),
  29. amis()->TableColumn('subtitle', '项目副标题'),
  30. amis()->TableColumn('price', '项目金额'),
  31. amis()->TableColumn('original_price', '项目原价'),
  32. amis()->TableColumn('sales', '虚拟销量')->sortable(),
  33. amis()->TableColumn('duration', '服务时长')->sortable(),
  34. amis()->TableColumn('project_desc', '项目介绍'),
  35. amis()->TableColumn('service_desc', '服务说明'),
  36. amis()->TableColumn('type', '服务类型')->sortable(),
  37. amis()->TableColumn('state', '状态')->sortable(),
  38. amis()->TableColumn('created_at', admin_trans('admin.created_at'))->type('datetime')->sortable(),
  39. amis()->TableColumn('updated_at', admin_trans('admin.updated_at'))->type('datetime')->sortable(),
  40. $this->rowActions('dialog'),
  41. ]);
  42. return $this->baseList($crud);
  43. }
  44. public function form($isEdit = false)
  45. {
  46. return $this->baseForm()->body([
  47. amis()->TextControl('cate_id', '项目分类编号'),
  48. amis()->TextControl('cover', '项目封面'),
  49. amis()->TextControl('title', '项目标题'),
  50. amis()->TextControl('subtitle', '项目副标题'),
  51. amis()->TextControl('price', '项目金额'),
  52. amis()->TextControl('original_price', '项目原价'),
  53. amis()->TextControl('sales', '虚拟销量'),
  54. amis()->TextControl('duration', '服务时长'),
  55. amis()->TextControl('project_desc', '项目介绍'),
  56. amis()->TextControl('service_desc', '服务说明'),
  57. amis()->TextControl('type', '服务类型'),
  58. amis()->TextControl('state', '状态'),
  59. ]);
  60. }
  61. public function detail()
  62. {
  63. return $this->baseDetail()->body([
  64. amis()->TextControl('id', 'ID')->static(),
  65. amis()->TextControl('cate_id', '项目分类编号')->static(),
  66. amis()->TextControl('cover', '项目封面')->static(),
  67. amis()->TextControl('title', '项目标题')->static(),
  68. amis()->TextControl('subtitle', '项目副标题')->static(),
  69. amis()->TextControl('price', '项目金额')->static(),
  70. amis()->TextControl('original_price', '项目原价')->static(),
  71. amis()->TextControl('sales', '虚拟销量')->static(),
  72. amis()->TextControl('duration', '服务时长')->static(),
  73. amis()->TextControl('project_desc', '项目介绍')->static(),
  74. amis()->TextControl('service_desc', '服务说明')->static(),
  75. amis()->TextControl('type', '服务类型')->static(),
  76. amis()->TextControl('state', '状态')->static(),
  77. amis()->TextControl('created_at', admin_trans('admin.created_at'))->static(),
  78. amis()->TextControl('updated_at', admin_trans('admin.updated_at'))->static(),
  79. ]);
  80. }
  81. /**
  82. * [项目管理]获取项目列表
  83. *
  84. * @description 获取项目列表,支持分页和搜索
  85. *
  86. * @queryParam page int 页码 Example: 1
  87. * @queryParam per_page int 每页数量 Example: 10
  88. * @queryParam keyword string 搜索关键词 Example: null
  89. *
  90. * @response {
  91. * "code": 0,
  92. * "message": "success",
  93. * "data": {
  94. * "items": [],
  95. * "total": 0
  96. * }
  97. * }
  98. */
  99. public function getProjects(Request $request): JsonResponse
  100. {
  101. $result = $this->service->getProjects(
  102. $request->input('page', 1),
  103. $request->input('per_page', 10),
  104. $request->input('keyword')
  105. );
  106. return response()->json([
  107. 'code' => 0,
  108. 'message' => 'success',
  109. 'data' => $result,
  110. ]);
  111. }
  112. /**
  113. * [项目管理]创建项目
  114. *
  115. * @description 创建新的项目
  116. *
  117. * @header x-xsrf-token string required CSRF令牌 Example: 7d9ht4EM6kGpzE9eSfSVsqC2RJRyZaBZQAnXCPB3
  118. *
  119. * @bodyParam title string required 项目标题 Example: 精油SPA
  120. * @bodyParam subtitle string required 项目副标题 Example: 专业精油按摩
  121. * @bodyParam cover string required 项目封面 Example: http://example.com/cover.jpg
  122. * @bodyParam price numeric required 项目金额 Example: 299.00
  123. * @bodyParam original_price numeric required 项目原价 Example: 399.00
  124. * @bodyParam duration int required 服务时长(分钟) Example: 60
  125. * @bodyParam project_desc string required 项目介绍 Example: 专业精油按摩服务
  126. * @bodyParam service_desc string required 服务说明 Example: 服务流程说明
  127. * @bodyParam type int required 服务类型 Example: 1
  128. * @bodyParam state int required 状态(0:下架 1:上架) Example: 1
  129. *
  130. * @response {
  131. * "code": 0,
  132. * "message": "success",
  133. * "data": null
  134. * }
  135. */
  136. public function createProject(Request $request): JsonResponse
  137. {
  138. // 验证请求数据
  139. $validated = $request->validate([
  140. 'title' => 'required|string|max:100',
  141. 'subtitle' => 'required|string|max:200',
  142. 'cover' => 'required|string|max:255',
  143. 'price' => 'required|numeric|min:0',
  144. 'original_price' => 'required|numeric|min:0',
  145. 'duration' => 'required|integer|min:1',
  146. 'project_desc' => 'required|string',
  147. 'service_desc' => 'required|string',
  148. 'type' => 'required|integer|in:1,2,3', // 根据实际类型值修改
  149. 'state' => 'required|integer|in:0,1',
  150. ]);
  151. // 设置默认分类
  152. $validated['cate_id'] = 1;
  153. try {
  154. $result = $this->service->createProject($validated);
  155. return response()->json([
  156. 'code' => 0,
  157. 'message' => 'success',
  158. 'data' => $result,
  159. ]);
  160. } catch (\Exception $e) {
  161. return response()->json([
  162. 'code' => 500,
  163. 'message' => '创建项目失败:'.$e->getMessage(),
  164. ], 500);
  165. }
  166. }
  167. /**
  168. * [项目管理]更新项目
  169. *
  170. * @description 更新项目信息
  171. *
  172. * @header x-xsrf-token string required CSRF令牌 Example: 7d9ht4EM6kGpzE9eSfSVsqC2RJRyZaBZQAnXCPB3
  173. *
  174. * @bodyParam title string 项目标题 Example: 精油SPA
  175. * @bodyParam subtitle string 项目副标题 Example: 专业精油按摩
  176. * @bodyParam cover string 项目封面 Example: http://example.com/cover.jpg
  177. * @bodyParam price numeric 项目金额 Example: 299.00
  178. * @bodyParam original_price numeric 项目原价 Example: 399.00
  179. * @bodyParam duration int 服务时长(分钟) Example: 60
  180. * @bodyParam project_desc string 项目介绍 Example: 专业精油按摩服务
  181. * @bodyParam service_desc string 服务说明 Example: 服务流程说明
  182. * @bodyParam type int 服务类型 Example: 1
  183. * @bodyParam state int 状态(0:下架 1:上架) Example: 1
  184. *
  185. * @response {
  186. * "code": 0,
  187. * "message": "success",
  188. * "data": null
  189. * }
  190. */
  191. public function updateProject(Request $request, int $id): JsonResponse
  192. {
  193. // 验证请求数据
  194. $validated = $request->validate([
  195. 'title' => 'sometimes|string|max:100',
  196. 'subtitle' => 'sometimes|string|max:200',
  197. 'cover' => 'sometimes|string|max:255',
  198. 'price' => 'sometimes|numeric|min:0',
  199. 'original_price' => 'sometimes|numeric|min:0',
  200. 'duration' => 'sometimes|integer|min:1',
  201. 'project_desc' => 'sometimes|string',
  202. 'service_desc' => 'sometimes|string',
  203. 'type' => 'sometimes|integer|in:1,2,3',
  204. 'state' => 'sometimes|integer|in:0,1',
  205. ]);
  206. try {
  207. $result = $this->service->updateProject($id, $validated);
  208. return response()->json([
  209. 'code' => 0,
  210. 'message' => 'success',
  211. 'data' => $result,
  212. ]);
  213. } catch (\Exception $e) {
  214. return response()->json([
  215. 'code' => 500,
  216. 'message' => '更新项目失败:'.$e->getMessage(),
  217. ], 500);
  218. }
  219. }
  220. /**
  221. * [项目管理]删除项目
  222. *
  223. * @description 删除指定项目
  224. *
  225. * @header x-xsrf-token string required CSRF令牌 Example: 7d9ht4EM6kGpzE9eSfSVsqC2RJRyZaBZQAnXCPB3
  226. *
  227. * @response {
  228. * "code": 0,
  229. * "message": "success",
  230. * "data": null
  231. * }
  232. */
  233. public function deleteProject(int $id): JsonResponse
  234. {
  235. $result = $this->service->deleteProject($id);
  236. return response()->json([
  237. 'code' => 0,
  238. 'message' => 'success',
  239. 'data' => $result,
  240. ]);
  241. }
  242. /**
  243. * [项目管理]获取项目详情
  244. *
  245. * @description 获取指定项目的详细信息
  246. *
  247. * @response {
  248. * "code": 0,
  249. * "message": "success",
  250. * "data": {}
  251. * }
  252. */
  253. public function getProject(int $id): JsonResponse
  254. {
  255. $result = $this->service->getProject($id);
  256. return response()->json([
  257. 'code' => 0,
  258. 'message' => 'success',
  259. 'data' => $result,
  260. ]);
  261. }
  262. }