TaskController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\StudioApi;
  4. use App\Http\Api\TaskApi;
  5. use App\Http\Resources\TaskResource;
  6. use App\Models\Project;
  7. use App\Models\Task;
  8. use App\Models\TaskAssignee;
  9. use App\Services\AuthService;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Str;
  12. class TaskController extends Controller
  13. {
  14. /**
  15. * 列出任务
  16. *
  17. * 按 view 指定的口径返回任务列表,支持按执行人、责任人(指派)、参与者、
  18. * 关键字、状态过滤,并支持排序与分页。必须登录,未登录返回 401。
  19. * 注意:view 取值不在枚举内时不会构造查询,会导致请求失败。
  20. *
  21. * @queryParam view string required 查询口径:all=全部有归属的任务,instance=实例任务(type=instance),studio=当前用户名下(owner_id=当前用户)的任务,project=某工程及其子工程下的任务。Enum: all,instance,studio,project
  22. * @queryParam project_id string view=project 时必填,工程 uid;同时匹配该工程本身及 path 中包含它的子工程
  23. * @queryParam executor_id_includes string 实际执行人 uid,逗号分隔,只返回执行人在其中的任务
  24. * @queryParam executor_id_not-includes string 实际执行人 uid,逗号分隔,排除执行人在其中的任务
  25. * @queryParam assignees_id_includes string 责任人(指派人)uid,逗号分隔,只返回指派给其中任一人的任务
  26. * @queryParam assignees_id_not-includes string 责任人(指派人)uid,逗号分隔,排除指派给其中任一人的任务
  27. * @queryParam assignees_id_null string 传该参数(任意值)表示只返回没有任何责任人的任务
  28. * @queryParam assignees_id_not-null string 传该参数(任意值)表示只返回至少有一个责任人的任务
  29. * @queryParam sign_up_equals string 值为 true 时只返回可领取的任务,即责任人和实际执行人都为空。Enum: true,false
  30. * @queryParam participants_id_includes string 参与者 uid,逗号分隔,实际执行人或责任人命中其一即返回
  31. * @queryParam participants_id_not-includes string 参与者 uid,逗号分隔,排除这些人参与的任务
  32. * @queryParam keyword string 标题模糊搜索关键字
  33. * @queryParam status string 任务状态,逗号分隔可多选;传 all 表示不过滤。数据库默认值为 pending
  34. * @queryParam order string 排序字段。Default: created_at
  35. * @queryParam dir string 排序方向。Enum: asc,desc Default: asc
  36. * @queryParam offset integer 分页偏移量。Default: 0
  37. * @queryParam limit integer 每页条数。Default: 1000
  38. */
  39. public function index(Request $request)
  40. {
  41. $user = AuthService::current($request);
  42. if (! $user) {
  43. return $this->error(__('auth.failed'), 401, 401);
  44. }
  45. switch ($request->input('view')) {
  46. case 'all':
  47. $table = Task::whereNotNull('owner_id');
  48. break;
  49. case 'instance':
  50. $table = Task::where('type', 'instance');
  51. break;
  52. case 'studio':
  53. $table = Task::where('owner_id', $user['user_uid']);
  54. break;
  55. case 'project':
  56. $projects = Project::where('uid', $request->input('project_id'))
  57. ->orWhereJsonContains('path', $request->input('project_id'))
  58. ->select('uid')
  59. ->get();
  60. $table = Task::whereIn('project_id', $projects);
  61. break;
  62. default:
  63. // FIXME: view 缺失或不在枚举内时 $table 未被赋值,后续 $table->count() 会致命错误,
  64. // 建议参照 ProjectController::index 在此直接返回参数错误响应。
  65. // code...
  66. break;
  67. }
  68. if ($request->has('executor_id_includes')) {
  69. $table = $table->whereIn(
  70. 'executor_id',
  71. explode(',', $request->input('executor_id_includes'))
  72. );
  73. }
  74. if ($request->has('executor_id_not-includes')) {
  75. $table = $table->whereNotIn(
  76. 'executor_id',
  77. explode(',', $request->input('executor_id_not-includes'))
  78. );
  79. }
  80. if ($request->has('assignees_id_includes')) {
  81. $assigneesId = explode(',', $request->input('assignees_id_includes'));
  82. $assigneesTasks = TaskAssignee::whereIn('assignee_id', $assigneesId)
  83. ->select('task_id')->get();
  84. $table = $table->whereIn('id', $assigneesTasks);
  85. }
  86. if ($request->has('assignees_id_not-includes')) {
  87. $assigneesId = explode(',', $request->input('assignees_id_not-includes'));
  88. $assigneesTasks = TaskAssignee::whereIn('assignee_id', $assigneesId)
  89. ->select('task_id')->get();
  90. $table = $table->whereNotIn('id', $assigneesTasks);
  91. }
  92. // 指派给
  93. if ($request->has('assignees_id_null')) {
  94. $table = $table->doesntHave('task_assignees');
  95. }
  96. if ($request->has('assignees_id_not-null')) {
  97. $table = $table->has('task_assignees');
  98. }
  99. // FIXME: 责任人现已存放在 task_assignees 表,tasks.assignees_id 列不再写入,
  100. // 这里的 whereNull('assignees_id') 形同虚设,建议改用 doesntHave('task_assignees')。
  101. if ($request->input('sign_up_equals') === 'true') {
  102. $table = $table->whereNull('assignees_id')
  103. ->whereNull('executor_id');
  104. }
  105. /**某人参与的 */
  106. if ($request->has('participants_id_includes')) {
  107. $id = explode(',', $request->input('participants_id_includes'));
  108. $tasks_id = TaskAssignee::whereIn('assignee_id', $id)->select('task_id')->get();
  109. $table = $table->where(function ($query) use ($id, $tasks_id) {
  110. $query->whereIn('executor_id', $id)
  111. ->orWhereIn('id', $tasks_id);
  112. });
  113. }
  114. if ($request->has('participants_id_not-includes')) {
  115. $id = explode(',', $request->input('participants_id_not-includes'));
  116. $tasks_id = TaskAssignee::whereIn('assignee_id', $id)->select('task_id')->get();
  117. $table = $table->where(function ($query) use ($id, $tasks_id) {
  118. $query->whereNotIn('executor_id', $id)
  119. ->orWhereNotIn('id', $tasks_id);
  120. });
  121. }
  122. if ($request->has('keyword')) {
  123. $table = $table->where('title', 'like', '%'.$request->input('keyword').'%');
  124. }
  125. if ($request->has('status') && $request->input('status') !== 'all') {
  126. $table = $table->whereIn('status', explode(',', $request->input('status')));
  127. }
  128. $count = $table->count();
  129. $table = $table->orderBy(
  130. $request->input('order', 'created_at'),
  131. $request->input('dir', 'asc')
  132. );
  133. $table = $table->skip($request->input('offset', 0))
  134. ->take($request->input('limit', 1000));
  135. $result = $table->get();
  136. return $this->ok(
  137. [
  138. 'rows' => TaskResource::collection(resource: $result),
  139. 'count' => $count,
  140. ]
  141. );
  142. }
  143. /**
  144. * 新建任务
  145. *
  146. * 在指定 studio 下创建任务。必须登录(否则 401),且当前用户必须是该 studio 本人
  147. * (studio_name 解析出的 id 等于当前用户 uid),否则返回 403。
  148. * 新任务的 order 自动取同一 parent(无 parent 时取同一 project)下最大 order + 1,
  149. * 没有兄弟节点时为 1;owner_id 为该 studio,creator_id 与 editor_id 为当前用户。
  150. *
  151. * @bodyParam studio_name string required studio 名称,用于解析归属 studio 并做权限校验
  152. * @bodyParam id string 任务 uuid;为合法 uuid 时用它,否则服务端生成新 uuid
  153. * @bodyParam title string required 任务标题
  154. * @bodyParam project_id string 所属工程 uid
  155. * @bodyParam parent_id string 父任务 uuid,填写后为子任务
  156. * @bodyParam type string 任务类型。Default: project
  157. */
  158. public function store(Request $request)
  159. {
  160. $user = AuthService::current($request);
  161. if (! $user) {
  162. return $this->error(__('auth.failed'), 401, 401);
  163. }
  164. $studioId = StudioApi::getIdByName($request->input('studio_name'));
  165. if (! self::canEdit($user['user_uid'], $studioId)) {
  166. return $this->error(__('auth.failed'), 403, 403);
  167. }
  168. // FIXME: 传入已存在的 id 时会命中旧任务并覆盖其 title/type/parent_id 并重算 order,
  169. // 而权限只校验了 studio_name 而非该任务自身归属,建议按 id 存在与否区分新建/更新并校验任务 owner。
  170. $new = Task::firstOrNew(
  171. [
  172. 'id' => $request->input('id'),
  173. ],
  174. [
  175. 'owner_id' => $studioId,
  176. 'creator_id' => $user['user_uid'],
  177. 'project_id' => $request->input('project_id'),
  178. ],
  179. );
  180. if (Str::isUuid($request->input('id'))) {
  181. $new->id = $request->input('id');
  182. } else {
  183. $new->id = Str::uuid();
  184. }
  185. $new->title = $request->input('title');
  186. $new->editor_id = $user['user_uid'];
  187. $new->parent_id = $request->input('parent_id');
  188. $new->type = $request->input('type');
  189. // 处理任务顺序
  190. if ($request->input('parent_id')) {
  191. $maxOrder = Task::where('parent_id', $request->input('parent_id'))
  192. ->max('order');
  193. } else {
  194. $maxOrder = Task::where('project_id', $request->input('project_id'))
  195. ->max('order');
  196. }
  197. if ($maxOrder === null) {
  198. $new->order = 1;
  199. } else {
  200. $new->order = $maxOrder + 1;
  201. }
  202. $new->save();
  203. return $this->ok(new TaskResource($new));
  204. }
  205. /**
  206. * 查看单个任务
  207. *
  208. * 通过路由模型绑定按任务 uuid 取出任务并返回详情,任务不存在时返回 404。
  209. * 该接口不做登录与权限校验,任何人都能读取。
  210. *
  211. * @urlParam task string required 任务 uuid
  212. */
  213. public function show(Task $task)
  214. {
  215. // FIXME: 该接口完全不鉴权,任何匿名用户按 uuid 即可读取任意任务详情,
  216. // 建议补上 AuthService::current 校验并按任务归属/可见性过滤。
  217. return $this->ok(new TaskResource($task));
  218. }
  219. /**
  220. * 修改任务
  221. *
  222. * 局部更新:只更新请求体里出现的字段,未出现的字段保持不变。必须登录(否则 401);
  223. * 只有任务拥有者、实际执行人或责任人之一可以修改,否则返回 403。
  224. * 副作用:传 assignees_id 会先删除该任务全部责任人再整体重建;
  225. * 传 pre_task_id / next_task_id 会整体重写任务的前置 / 后续关联关系。
  226. * 每次保存都会把 editor_id 更新为当前用户。
  227. *
  228. * @urlParam task string required 任务 uuid
  229. *
  230. * @bodyParam title string 任务标题
  231. * @bodyParam description string 任务描述(markdown)
  232. * @bodyParam category string 类别,如 翻译、审稿、百科
  233. * @bodyParam progress integer 进度,0-100
  234. * @bodyParam assignees_id array 责任人 uid 数组,整体覆盖原有责任人;传空数组等于清空
  235. * @bodyParam roles array 领取该任务所需的角色要求,存为 json
  236. * @bodyParam executor_id string 实际执行人 uid
  237. * @bodyParam executor_relation_task_id string 执行人关联任务 uuid,表示执行人与该任务保持一致
  238. * @bodyParam project_id string 所属工程 uid
  239. * @bodyParam pre_task_id string 前置任务 uuid,逗号分隔,整体覆盖原有前置关系
  240. * @bodyParam next_task_id string 后续任务 uuid,逗号分隔,整体覆盖原有后续关系
  241. * @bodyParam is_milestone boolean 是否为里程碑
  242. * @bodyParam order integer 拖拽排序顺序
  243. */
  244. public function update(Request $request, Task $task)
  245. {
  246. $user = AuthService::current($request);
  247. if (! $user) {
  248. return $this->error(__('auth.failed'), 401, 401);
  249. }
  250. if (! self::canUpdate($user['user_uid'], $task)) {
  251. return $this->error(__('auth.failed'), 403, 403);
  252. }
  253. if ($request->has('title')) {
  254. $task->title = $request->input('title');
  255. }
  256. if ($request->has('description')) {
  257. $task->description = $request->input('description');
  258. }
  259. if ($request->has('category')) {
  260. $task->category = $request->input('category');
  261. }
  262. if ($request->has('progress')) {
  263. $task->progress = $request->input('progress');
  264. }
  265. if ($request->has('assignees_id')) {
  266. $delete = TaskAssignee::where('task_id', $task->id)->delete();
  267. $assigneesData = [];
  268. foreach ($request->input('assignees_id') as $key => $id) {
  269. $assigneesData[] = [
  270. 'id' => Str::uuid(),
  271. 'task_id' => $task->id,
  272. 'assignee_id' => $id,
  273. 'editor_id' => $user['user_uid'],
  274. ];
  275. }
  276. TaskAssignee::insert($assigneesData);
  277. }
  278. if ($request->has('roles')) {
  279. $task->roles = json_encode($request->input('roles'), JSON_UNESCAPED_UNICODE);
  280. }
  281. if ($request->has('executor_id')) {
  282. $task->executor_id = $request->input('executor_id');
  283. }
  284. if ($request->has('executor_relation_task_id')) {
  285. $task->executor_relation_task_id = $request->input('executor_relation_task_id');
  286. }
  287. if ($request->has('project_id')) {
  288. $task->project_id = $request->input('project_id');
  289. }
  290. if ($request->has('pre_task_id')) {
  291. TaskApi::setRelationTasks(
  292. $task->id,
  293. explode(',', $request->input('pre_task_id')),
  294. $user['user_uid'],
  295. 'pre'
  296. );
  297. }
  298. if ($request->has('next_task_id')) {
  299. TaskApi::setRelationTasks(
  300. $task->id,
  301. explode(',', $request->input('next_task_id')),
  302. $user['user_uid'],
  303. 'next'
  304. );
  305. }
  306. if ($request->has('is_milestone')) {
  307. $task->is_milestone = $request->input('is_milestone');
  308. }
  309. if ($request->has('order')) {
  310. $task->order = $request->input('order');
  311. }
  312. $task->editor_id = $user['user_uid'];
  313. $task->save();
  314. return $this->ok(new TaskResource($task));
  315. }
  316. /**
  317. * 删除任务
  318. *
  319. * 必须登录(否则 401),且当前用户必须是任务拥有者,否则返回 403。
  320. * 删除成功返回 ok,失败返回 500。
  321. *
  322. * @urlParam task string required 任务 uuid
  323. */
  324. public function destroy(Request $request, Task $task)
  325. {
  326. $user = AuthService::current($request);
  327. if (! $user) {
  328. return $this->error(__('auth.failed'), 401, 401);
  329. }
  330. // FIXME: tasks 表只有 owner_id 列,Task 模型也没有 owner 关系,$task->owner 恒为 null,
  331. // 导致删除接口对所有人恒返回 403;应改为 $task->owner_id,但需与下方 trashed() 一并修复。
  332. if (! self::canEdit($user['user_uid'], $task->owner)) {
  333. return $this->error(__('auth.failed'), 403, 403);
  334. }
  335. $task->delete();
  336. // FIXME: Task 模型未 use SoftDeletes(tasks 表也无 deleted_at 列),trashed() 会抛
  337. // BadMethodCallException;上面 owner 那条修好后就会走到这里,两处必须一起改(加软删除或改判 delete() 返回值)。
  338. if ($task->trashed()) {
  339. return $this->ok('ok');
  340. } else {
  341. return $this->error('fail', 500, 500);
  342. }
  343. }
  344. public static function canEdit($user_uid, $owner_uid)
  345. {
  346. return $user_uid === $owner_uid;
  347. }
  348. /**
  349. * 判断用户是否有权修改该任务
  350. *
  351. * 任务拥有者、实际执行人,或该任务的责任人之一,均可修改。
  352. *
  353. * @param string $user_uid 用户 uuid
  354. * @param Task $task 任务
  355. */
  356. public static function canUpdate($user_uid, $task): bool
  357. {
  358. if ($user_uid === $task->owner_id) {
  359. return true;
  360. }
  361. if ($user_uid === $task->executor_id) {
  362. return true;
  363. }
  364. if (TaskAssignee::where('task_id', $task->id)
  365. ->where('assignee_id', $user_uid)
  366. ->exists()
  367. ) {
  368. return true;
  369. }
  370. return false;
  371. }
  372. }