ProjectController.php 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Module\Base;
  5. use App\Module\Project;
  6. use App\Module\Users;
  7. use DB;
  8. use Request;
  9. use Session;
  10. /**
  11. * @apiDefine project
  12. *
  13. * 项目
  14. */
  15. class ProjectController extends Controller
  16. {
  17. public function __invoke($method, $action = '')
  18. {
  19. $app = $method ? $method : 'main';
  20. if ($action) {
  21. $app .= "__" . $action;
  22. }
  23. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  24. }
  25. /**
  26. * 项目列表
  27. *
  28. * @apiParam {String} act 类型
  29. * - join: 加入的项目(默认)
  30. * - favor: 收藏的项目
  31. * - manage: 管理的项目
  32. * @apiParam {Number} [page] 当前页,默认:1
  33. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  34. */
  35. public function lists()
  36. {
  37. $user = Users::authE();
  38. if (Base::isError($user)) {
  39. return $user;
  40. } else {
  41. $user = $user['data'];
  42. }
  43. //
  44. $whereArray = [];
  45. $whereArray[] = ['project_lists.delete', '=', 0];
  46. $whereArray[] = ['project_users.username', '=', $user['username']];
  47. switch (Request::input('act')) {
  48. case "favor": {
  49. $whereArray[] = ['project_users.type', '=', '收藏'];
  50. break;
  51. }
  52. case "manage": {
  53. $whereArray[] = ['project_users.type', '=', '成员'];
  54. $whereArray[] = ['project_users.isowner', '=', 1];
  55. break;
  56. }
  57. default: {
  58. $whereArray[] = ['project_users.type', '=', '成员'];
  59. break;
  60. }
  61. }
  62. $lists = DB::table('project_lists')
  63. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  64. ->select(['project_lists.*', 'project_users.isowner', 'project_users.indate as uindate'])
  65. ->where($whereArray)
  66. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  67. $lists = Base::getPageList($lists);
  68. if ($lists['total'] == 0) {
  69. return Base::retError('未找到任何相关的项目');
  70. }
  71. return Base::retSuccess('success', $lists);
  72. }
  73. /**
  74. * 项目详情
  75. *
  76. * @apiParam {Number} projectid 项目ID
  77. */
  78. public function detail()
  79. {
  80. $user = Users::authE();
  81. if (Base::isError($user)) {
  82. return $user;
  83. } else {
  84. $user = $user['data'];
  85. }
  86. //
  87. $projectid = trim(Request::input('projectid'));
  88. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  89. if (empty($projectDetail)) {
  90. return Base::retError('项目不存在或已被删除!');
  91. }
  92. $inRes = Project::inThe($projectid, $user['username']);
  93. if (Base::isError($inRes)) {
  94. return $inRes;
  95. }
  96. //子分类
  97. $label = Base::DBC2A(DB::table('project_label')->where('projectid', $projectid)->orderBy('inorder')->orderBy('id')->get());
  98. $simpleLabel = [];
  99. //任务
  100. $task = Base::DBC2A(DB::table('project_task')->where([ 'projectid' => $projectid, 'delete' => 0, 'complete' => 0 ])->orderByDesc('inorder')->orderByDesc('id')->get());
  101. //任务归类
  102. foreach ($label AS $index => $temp) {
  103. $taskLists = [];
  104. foreach ($task AS $info) {
  105. if ($temp['id'] == $info['labelid']) {
  106. $info['overdue'] = Project::taskIsOverdue($info);
  107. $taskLists[] = array_merge($info, Users::username2basic($info['username']));
  108. }
  109. }
  110. $label[$index]['taskLists'] = $taskLists;
  111. $simpleLabel[] = ['id' => $temp['id'], 'title' => $temp['title']];
  112. }
  113. //
  114. return Base::retSuccess('success', [
  115. 'project' => $projectDetail,
  116. 'label' => $label,
  117. 'simpleLabel' => $simpleLabel,
  118. ]);
  119. }
  120. /**
  121. * 添加项目
  122. *
  123. * @apiParam {String} title 项目名称
  124. * @apiParam {Array} labels 流程,格式[流程1, 流程2]
  125. */
  126. public function add()
  127. {
  128. $user = Users::authE();
  129. if (Base::isError($user)) {
  130. return $user;
  131. } else {
  132. $user = $user['data'];
  133. }
  134. //项目名称
  135. $title = trim(Request::input('title'));
  136. if (mb_strlen($title) < 2) {
  137. return Base::retError('项目名称不可以少于2个字!');
  138. } elseif (mb_strlen($title) > 32) {
  139. return Base::retError('项目名称最多只能设置32个字!');
  140. }
  141. //流程
  142. $labels = Request::input('labels');
  143. if (!is_array($labels)) $labels = [];
  144. $insertLabels = [];
  145. $inorder = 0;
  146. foreach ($labels AS $label) {
  147. $label = trim($label);
  148. if ($label) {
  149. $insertLabels[] = [
  150. 'title' => $label,
  151. 'inorder' => $inorder++,
  152. ];
  153. }
  154. }
  155. if (empty($insertLabels)) {
  156. $insertLabels[] = [
  157. 'title' => '默认',
  158. 'inorder' => 0,
  159. ];
  160. }
  161. //开始创建
  162. $projectid = DB::table('project_lists')->insertGetId([
  163. 'title' => $title,
  164. 'username' => $user['username'],
  165. 'createuser' => $user['username'],
  166. 'indate' => Base::time()
  167. ]);
  168. if ($projectid) {
  169. foreach ($insertLabels AS $key => $label) {
  170. $insertLabels[$key]['projectid'] = $projectid;
  171. }
  172. DB::table('project_label')->insert($insertLabels);
  173. DB::table('project_log')->insert([
  174. 'type' => '日志',
  175. 'projectid' => $projectid,
  176. 'username' => $user['username'],
  177. 'detail' => '创建项目',
  178. 'indate' => Base::time()
  179. ]);
  180. DB::table('project_users')->insert([
  181. 'type' => '成员',
  182. 'projectid' => $projectid,
  183. 'isowner' => 1,
  184. 'username' => $user['username'],
  185. 'indate' => Base::time()
  186. ]);
  187. return Base::retSuccess('添加成功!');
  188. } else {
  189. return Base::retError('添加失败!');
  190. }
  191. }
  192. /**
  193. * 收藏项目
  194. *
  195. * @apiParam {String} act 类型
  196. * - cancel: 取消收藏
  197. * - else: 添加收藏
  198. * @apiParam {Number} projectid 项目ID
  199. *
  200. * @throws \Throwable
  201. */
  202. public function favor()
  203. {
  204. $user = Users::authE();
  205. if (Base::isError($user)) {
  206. return $user;
  207. } else {
  208. $user = $user['data'];
  209. }
  210. //
  211. $projectid = trim(Request::input('projectid'));
  212. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  213. if (empty($projectDetail)) {
  214. return Base::retError('项目不存在或已被删除!');
  215. }
  216. return DB::transaction(function () use ($projectDetail, $user) {
  217. switch (Request::input('act')) {
  218. case 'cancel': {
  219. if (DB::table('project_users')->where([
  220. 'type' => '收藏',
  221. 'projectid' => $projectDetail['id'],
  222. 'username' => $user['username'],
  223. ])->delete()) {
  224. DB::table('project_log')->insert([
  225. 'type' => '日志',
  226. 'projectid' => $projectDetail['id'],
  227. 'username' => $user['username'],
  228. 'detail' => '取消收藏',
  229. 'indate' => Base::time()
  230. ]);
  231. return Base::retSuccess('取消成功!');
  232. }
  233. return Base::retSuccess('已取消!');
  234. }
  235. default: {
  236. $row = Base::DBC2A(DB::table('project_users')->where([
  237. 'type' => '收藏',
  238. 'projectid' => $projectDetail['id'],
  239. 'username' => $user['username'],
  240. ])->lockForUpdate()->first());
  241. if (empty($row)) {
  242. DB::table('project_users')->insert([
  243. 'type' => '收藏',
  244. 'projectid' => $projectDetail['id'],
  245. 'isowner' => $projectDetail['username'] == $user['username'] ? 1 : 0,
  246. 'username' => $user['username'],
  247. 'indate' => Base::time()
  248. ]);
  249. DB::table('project_log')->insert([
  250. 'type' => '日志',
  251. 'projectid' => $projectDetail['id'],
  252. 'username' => $user['username'],
  253. 'detail' => '收藏项目',
  254. 'indate' => Base::time()
  255. ]);
  256. return Base::retSuccess('收藏成功!');
  257. }
  258. return Base::retSuccess('已收藏!');
  259. }
  260. }
  261. });
  262. }
  263. /**
  264. * 重命名项目
  265. *
  266. * @apiParam {Number} projectid 项目ID
  267. * @apiParam {String} title 项目新名称
  268. */
  269. public function rename()
  270. {
  271. $user = Users::authE();
  272. if (Base::isError($user)) {
  273. return $user;
  274. } else {
  275. $user = $user['data'];
  276. }
  277. //
  278. $projectid = trim(Request::input('projectid'));
  279. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  280. if (empty($projectDetail)) {
  281. return Base::retError('项目不存在或已被删除!');
  282. }
  283. if ($projectDetail['username'] != $user['username']) {
  284. return Base::retError('你不是项目负责人!');
  285. }
  286. //
  287. $title = trim(Request::input('title'));
  288. if (mb_strlen($title) < 2) {
  289. return Base::retError('项目名称不可以少于2个字!');
  290. } elseif (mb_strlen($title) > 32) {
  291. return Base::retError('项目名称最多只能设置32个字!');
  292. }
  293. //
  294. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  295. 'title' => $title
  296. ]);
  297. DB::table('project_log')->insert([
  298. 'type' => '日志',
  299. 'projectid' => $projectDetail['id'],
  300. 'username' => $user['username'],
  301. 'detail' => '【' . $projectDetail['title'] . '】重命名【' . $title . '】',
  302. 'indate' => Base::time()
  303. ]);
  304. //
  305. return Base::retSuccess('修改成功!');
  306. }
  307. /**
  308. * 移交项目
  309. *
  310. * @apiParam {Number} projectid 项目ID
  311. * @apiParam {String} username 项目新负责人用户名
  312. *
  313. * @throws \Throwable
  314. */
  315. public function transfer()
  316. {
  317. $user = Users::authE();
  318. if (Base::isError($user)) {
  319. return $user;
  320. } else {
  321. $user = $user['data'];
  322. }
  323. //
  324. $projectid = trim(Request::input('projectid'));
  325. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  326. if (empty($projectDetail)) {
  327. return Base::retError('项目不存在或已被删除!');
  328. }
  329. if ($projectDetail['username'] != $user['username']) {
  330. return Base::retError('你不是项目负责人!');
  331. }
  332. //
  333. $username = trim(Request::input('username'));
  334. if ($username == $projectDetail['username']) {
  335. return Base::retError('你已是项目负责人!');
  336. }
  337. $count = DB::table('users')->where('username', $username)->count();
  338. if ($count <= 0) {
  339. return Base::retError('成员用户名(' . $username . ')不存在!');
  340. }
  341. //判断是否已在项目成员内
  342. $inRes = Project::inThe($projectDetail['id'], $username);
  343. if (Base::isError($inRes)) {
  344. DB::table('project_users')->insert([
  345. 'type' => '成员',
  346. 'projectid' => $projectDetail['id'],
  347. 'isowner' => 0,
  348. 'username' => $username,
  349. 'indate' => Base::time()
  350. ]);
  351. DB::table('project_log')->insert([
  352. 'type' => '日志',
  353. 'projectid' => $projectDetail['id'],
  354. 'username' => $username,
  355. 'detail' => '自动加入项目',
  356. 'indate' => Base::time()
  357. ]);
  358. }
  359. //开始移交
  360. return DB::transaction(function () use ($user, $username, $projectDetail) {
  361. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  362. 'username' => $username
  363. ]);
  364. DB::table('project_log')->insert([
  365. 'type' => '日志',
  366. 'projectid' => $projectDetail['id'],
  367. 'username' => $user['username'],
  368. 'detail' => '【' . $projectDetail['username'] . '】移交给【' . $username . '】',
  369. 'indate' => Base::time()
  370. ]);
  371. DB::table('project_users')->where([
  372. 'projectid' => $projectDetail['id'],
  373. 'username' => $projectDetail['username'],
  374. ])->update([
  375. 'isowner' => 0
  376. ]);
  377. DB::table('project_users')->where([
  378. 'projectid' => $projectDetail['id'],
  379. 'username' => $username,
  380. ])->update([
  381. 'isowner' => 1
  382. ]);
  383. return Base::retSuccess('移交成功!');
  384. });
  385. }
  386. /**
  387. * 删除项目
  388. *
  389. * @apiParam {Number} projectid 项目ID
  390. */
  391. public function delete()
  392. {
  393. $user = Users::authE();
  394. if (Base::isError($user)) {
  395. return $user;
  396. } else {
  397. $user = $user['data'];
  398. }
  399. //
  400. $projectid = trim(Request::input('projectid'));
  401. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  402. if (empty($projectDetail)) {
  403. return Base::retError('项目不存在或已被删除!');
  404. }
  405. if ($projectDetail['username'] != $user['username']) {
  406. return Base::retError('你不是项目负责人!');
  407. }
  408. //
  409. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  410. 'delete' => 1,
  411. 'deletedate' => Base::time()
  412. ]);
  413. DB::table('project_log')->insert([
  414. 'type' => '日志',
  415. 'projectid' => $projectDetail['id'],
  416. 'username' => $user['username'],
  417. 'detail' => '删除项目',
  418. 'indate' => Base::time()
  419. ]);
  420. //
  421. return Base::retSuccess('删除成功!');
  422. }
  423. /**
  424. * 排序任务
  425. *
  426. * @apiParam {Number} projectid 项目ID
  427. * @apiParam {String} oldsort 旧排序数据
  428. * @apiParam {String} newsort 新排序数据
  429. * @apiParam {Number} label 赋值表示排序分类,否则排序任务(调整任务所属分类)
  430. */
  431. public function sort()
  432. {
  433. $user = Users::authE();
  434. if (Base::isError($user)) {
  435. return $user;
  436. } else {
  437. $user = $user['data'];
  438. }
  439. //
  440. $projectid = trim(Request::input('projectid'));
  441. $inRes = Project::inThe($projectid, $user['username']);
  442. if (Base::isError($inRes)) {
  443. return $inRes;
  444. }
  445. //
  446. $oldSort = explode(";", Request::input('oldsort'));
  447. $newSort = explode(";", Request::input('newsort'));
  448. if (count($oldSort) != count($newSort)) {
  449. return Base::retError('参数错误!');
  450. }
  451. if (intval(Request::input('label'))) {
  452. //排序分类
  453. foreach ($newSort AS $sort => $item) {
  454. list($newLabelid, $newTask) = explode(':', $item);
  455. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  456. if ($newLabelid != $oldLabelid) {
  457. DB::table('project_label')->where([
  458. 'id' => $newLabelid,
  459. 'projectid' => $projectid
  460. ])->update([
  461. 'inorder' => intval($sort)
  462. ]);
  463. }
  464. }
  465. $detail = '调整任务列表排序';
  466. $sortType = 'label';
  467. } else {
  468. //排序任务(调整任务归类)
  469. foreach ($newSort AS $sort => $item) {
  470. list($newLabelid, $newTask) = explode(':', $item);
  471. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  472. if ($newTask != $oldTask) {
  473. $newTask = explode('-', $newTask);
  474. $inorder = count($newTask);
  475. foreach ($newTask AS $taskid) {
  476. DB::table('project_task')->where([
  477. 'id' => $taskid,
  478. 'projectid' => $projectid
  479. ])->update([
  480. 'labelid' => $newLabelid,
  481. 'inorder' => $inorder
  482. ]);
  483. $inorder--;
  484. }
  485. }
  486. }
  487. $detail = '调整任务排序';
  488. $sortType = 'task';
  489. }
  490. //
  491. $row = Base::DBC2A(DB::table('project_log')->where([ 'type' => '日志', 'projectid' => $projectid ])->orderByDesc('id')->first());
  492. $continue = 1;
  493. if ($row && $row['username'] == $user['username'] && $row['indate'] + 300 > Base::time()) {
  494. $other = Base::string2array($row['other']);
  495. if ($other['sortType'] == $sortType) {
  496. $continue = intval($other['continue']) + 1;
  497. if ($continue <= 100) {
  498. DB::table('project_log')->where('id', $row['id'])->update([
  499. 'detail' => $detail . '(' . $continue . '次)',
  500. 'other' => Base::array2string([
  501. 'sortType' => $sortType,
  502. 'continue' => $continue,
  503. 'times' => $other['times'] . '|' . Base::time(),
  504. ])
  505. ]);
  506. }
  507. }
  508. }
  509. if ($continue == 1) {
  510. DB::table('project_log')->insert([
  511. 'type' => '日志',
  512. 'projectid' => $projectid,
  513. 'username' => $user['username'],
  514. 'detail' => $detail,
  515. 'indate' => Base::time(),
  516. 'other' => Base::array2string([
  517. 'sortType' => $sortType,
  518. 'continue' => $continue,
  519. 'times' => Base::time(),
  520. ])
  521. ]);
  522. }
  523. return Base::retSuccess('保存成功!');
  524. }
  525. /**
  526. * 排序任务(todo待办)
  527. *
  528. * @apiParam {String} oldsort 旧排序数据
  529. * @apiParam {String} newsort 新排序数据
  530. */
  531. public function sort__todo()
  532. {
  533. $user = Users::authE();
  534. if (Base::isError($user)) {
  535. return $user;
  536. } else {
  537. $user = $user['data'];
  538. }
  539. //
  540. $oldSort = explode(";", Request::input('oldsort'));
  541. $newSort = explode(";", Request::input('newsort'));
  542. if (count($oldSort) != count($newSort)) {
  543. return Base::retError('参数错误!');
  544. }
  545. //
  546. $levels = [];
  547. $logArray = [];
  548. foreach ($newSort AS $sort => $item) {
  549. list($newLevel, $newTask) = explode(':', $item);
  550. list($oldLevel, $oldTask) = explode(':', $oldSort[$sort]);
  551. if ($newTask != $oldTask) {
  552. $newTask = explode('-', $newTask);
  553. $oldTask = explode('-', $oldTask);
  554. $userorder = intval(DB::table('project_task')->select('userorder')->where([
  555. 'delete' => 0,
  556. 'archived' => 0,
  557. 'level' => $newLevel,
  558. 'username' => $user['username'],
  559. ])->orderByDesc('userorder')->value('userorder'));
  560. if (count($newTask) < count($oldTask)) {
  561. $userorder--;
  562. } else {
  563. $userorder++;
  564. }
  565. foreach ($newTask AS $taskid) {
  566. $task = Base::DBC2A(DB::table('project_task')->select(['id', 'title', 'projectid', 'level', 'userorder'])->where([
  567. 'id' => $taskid,
  568. 'username' => $user['username']
  569. ])->first());
  570. $upArray = [];
  571. if ($task) {
  572. if ($task['level'] != $newLevel) {
  573. $upArray['level'] = $newLevel;
  574. $logArray[] = [
  575. 'type' => '日志',
  576. 'projectid' => $task['projectid'],
  577. 'taskid' => $task['id'],
  578. 'username' => $user['username'],
  579. 'detail' => '调整任务等级为【P' . $newLevel . '】',
  580. 'indate' => Base::time(),
  581. 'other' => Base::array2string([
  582. 'type' => 'task',
  583. 'id' => $task['id'],
  584. 'title' => $task['title'],
  585. ])
  586. ];
  587. }
  588. if ($task['userorder'] != $userorder) {
  589. $upArray['userorder'] = $userorder;
  590. }
  591. }
  592. if ($upArray) {
  593. DB::table('project_task')->where('id', $taskid)->update($upArray);
  594. }
  595. $userorder--;
  596. }
  597. $levels[] = $newLevel;
  598. }
  599. }
  600. if ($logArray) {
  601. DB::table('project_log')->insert($logArray);
  602. }
  603. //
  604. return Base::retSuccess('保存成功!', $levels);
  605. }
  606. /**
  607. * 退出项目
  608. *
  609. * @apiParam {Number} projectid 项目ID
  610. */
  611. public function out()
  612. {
  613. $user = Users::authE();
  614. if (Base::isError($user)) {
  615. return $user;
  616. } else {
  617. $user = $user['data'];
  618. }
  619. //
  620. $projectid = trim(Request::input('projectid'));
  621. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  622. if (empty($projectDetail)) {
  623. return Base::retError('项目不存在或已被删除!');
  624. }
  625. if ($projectDetail['username'] == $user['username']) {
  626. return Base::retError('你是项目负责人,不可退出项目!');
  627. }
  628. $inRes = Project::inThe($projectid, $user['username']);
  629. if (Base::isError($inRes)) {
  630. return $inRes;
  631. }
  632. //
  633. DB::table('project_users')->where([
  634. 'type' => '成员',
  635. 'projectid' => $projectDetail['id'],
  636. 'username' => $user['username'],
  637. ])->delete();
  638. DB::table('project_log')->insert([
  639. 'type' => '日志',
  640. 'projectid' => $projectDetail['id'],
  641. 'username' => $user['username'],
  642. 'detail' => '退出项目',
  643. 'indate' => Base::time()
  644. ]);
  645. //
  646. return Base::retSuccess('退出项目成功!');
  647. }
  648. /**
  649. * 项目成员-列表
  650. *
  651. * @apiParam {Number} projectid 项目ID
  652. * @apiParam {Number} [page] 当前页,默认:1
  653. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  654. */
  655. public function users__lists()
  656. {
  657. $user = Users::authE();
  658. if (Base::isError($user)) {
  659. return $user;
  660. } else {
  661. $user = $user['data'];
  662. }
  663. //
  664. $projectid = intval(Request::input('projectid'));
  665. $inRes = Project::inThe($projectid, $user['username']);
  666. if (Base::isError($inRes)) {
  667. return $inRes;
  668. }
  669. //
  670. $lists = DB::table('project_lists')
  671. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  672. ->select(['project_lists.title', 'project_users.*'])
  673. ->where([
  674. ['project_lists.id', $projectid],
  675. ['project_lists.delete', 0],
  676. ['project_users.type', '成员'],
  677. ])
  678. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  679. $lists = Base::getPageList($lists);
  680. if ($lists['total'] == 0) {
  681. return Base::retError('未找到任何相关的成员');
  682. }
  683. foreach ($lists['lists'] AS $key => $projectDetail) {
  684. $userInfo = Users::username2basic($projectDetail['username']);
  685. $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
  686. $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
  687. $lists['lists'][$key]['profession'] = $userInfo['profession'];
  688. }
  689. return Base::retSuccess('success', $lists);
  690. }
  691. /**
  692. * 项目成员-添加、删除
  693. *
  694. * @apiParam {String} act
  695. * - delete: 删除成员
  696. * - else: 添加成员
  697. * @apiParam {Number} projectid 项目ID
  698. * @apiParam {Array|String} username 用户名(或用户名组)
  699. */
  700. public function users__join()
  701. {
  702. $user = Users::authE();
  703. if (Base::isError($user)) {
  704. return $user;
  705. } else {
  706. $user = $user['data'];
  707. }
  708. //
  709. $projectid = trim(Request::input('projectid'));
  710. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  711. if (empty($projectDetail)) {
  712. return Base::retError('项目不存在或已被删除!');
  713. }
  714. if ($projectDetail['username'] != $user['username']) {
  715. return Base::retError('你是不是项目负责人!');
  716. }
  717. $usernames = Request::input('username');
  718. if (empty($usernames)) {
  719. return Base::retError('参数错误!');
  720. }
  721. if (!is_array($usernames)) {
  722. if (Base::strExists($usernames, ',')) {
  723. $usernames = explode(',', $usernames);
  724. } else {
  725. $usernames = [$usernames];
  726. }
  727. }
  728. //
  729. $logArray = [];
  730. foreach ($usernames AS $username) {
  731. $inRes = Project::inThe($projectid, $username);
  732. switch (Request::input('act')) {
  733. case 'delete': {
  734. if (!Base::isError($inRes) && $projectDetail['username'] != $username) {
  735. DB::table('project_users')->where([
  736. 'type' => '成员',
  737. 'projectid' => $projectid,
  738. 'username' => $username,
  739. ])->delete();
  740. $logArray[] = [
  741. 'type' => '日志',
  742. 'projectid' => $projectDetail['id'],
  743. 'username' => $user['username'],
  744. 'detail' => '将成员【' . $username . '】移出项目',
  745. 'indate' => Base::time()
  746. ];
  747. }
  748. break;
  749. }
  750. default: {
  751. if (Base::isError($inRes)) {
  752. DB::table('project_users')->insert([
  753. 'type' => '成员',
  754. 'projectid' => $projectid,
  755. 'isowner' => 0,
  756. 'username' => $username,
  757. 'indate' => Base::time()
  758. ]);
  759. $logArray[] = [
  760. 'type' => '日志',
  761. 'projectid' => $projectDetail['id'],
  762. 'username' => $username,
  763. 'detail' => '将成员【' . $username . '】加入项目',
  764. 'indate' => Base::time()
  765. ];
  766. }
  767. break;
  768. }
  769. }
  770. }
  771. return Base::retSuccess('操作完成!');
  772. }
  773. /**
  774. * 项目子分类-添加分类
  775. *
  776. * @apiParam {Number} projectid 项目ID
  777. * @apiParam {String} title 分类名称
  778. */
  779. public function label__add()
  780. {
  781. $user = Users::authE();
  782. if (Base::isError($user)) {
  783. return $user;
  784. } else {
  785. $user = $user['data'];
  786. }
  787. //
  788. $projectid = trim(Request::input('projectid'));
  789. $inRes = Project::inThe($projectid, $user['username']);
  790. if (Base::isError($inRes)) {
  791. return $inRes;
  792. }
  793. //
  794. $title = trim(Request::input('title'));
  795. if (empty($title)) {
  796. return Base::retError('列表名称不能为空!');
  797. } elseif (mb_strlen($title) > 32) {
  798. return Base::retError('列表名称最多只能设置32个字!');
  799. }
  800. //
  801. $count = DB::table('project_label')->where('projectid', $projectid)->where('title', $title)->count();
  802. if ($count > 0) {
  803. return Base::retError('列表名称已存在!');
  804. }
  805. //
  806. $id = DB::table('project_label')->insertGetId([
  807. 'projectid' => $projectid,
  808. 'title' => $title,
  809. 'inorder' => intval(DB::table('project_label')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  810. ]);
  811. if (empty($id)) {
  812. return Base::retError('系统繁忙,请稍后再试!');
  813. }
  814. DB::table('project_log')->insert([
  815. 'type' => '日志',
  816. 'projectid' => $projectid,
  817. 'username' => $user['username'],
  818. 'detail' => '添加任务列表【' . $title . '】',
  819. 'indate' => Base::time()
  820. ]);
  821. //
  822. $row = Base::DBC2A(DB::table('project_label')->where('id', $id)->first());
  823. $row['taskLists'] = [];
  824. return Base::retSuccess('添加成功!', $row);
  825. }
  826. /**
  827. * 项目子分类-重命名分类
  828. *
  829. * @apiParam {Number} projectid 项目ID
  830. * @apiParam {Number} labelid 分类ID
  831. * @apiParam {String} title 新分类名称
  832. */
  833. public function label__rename()
  834. {
  835. $user = Users::authE();
  836. if (Base::isError($user)) {
  837. return $user;
  838. } else {
  839. $user = $user['data'];
  840. }
  841. //
  842. $projectid = trim(Request::input('projectid'));
  843. $inRes = Project::inThe($projectid, $user['username']);
  844. if (Base::isError($inRes)) {
  845. return $inRes;
  846. }
  847. //
  848. $title = trim(Request::input('title'));
  849. if (empty($title)) {
  850. return Base::retError('列表名称不能为空!');
  851. } elseif (mb_strlen($title) > 32) {
  852. return Base::retError('列表名称最多只能设置32个字!');
  853. }
  854. //
  855. $labelid = intval(Request::input('labelid'));
  856. $count = DB::table('project_label')->where('id', '!=', $labelid)->where('projectid', $projectid)->where('title', $title)->count();
  857. if ($count > 0) {
  858. return Base::retError('列表名称已存在!');
  859. }
  860. //
  861. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  862. if (empty($labelDetail)) {
  863. return Base::retError('列表不存在或已被删除!');
  864. }
  865. //
  866. if (DB::table('project_label')->where('id', $labelDetail['id'])->update([ 'title' => $title ])) {
  867. DB::table('project_log')->insert([
  868. 'type' => '日志',
  869. 'projectid' => $projectid,
  870. 'username' => $user['username'],
  871. 'detail' => '任务列表【' . $labelDetail['title'] . '】重命名【' . $title . '】',
  872. 'indate' => Base::time()
  873. ]);
  874. }
  875. //
  876. return Base::retSuccess('修改成功!');
  877. }
  878. /**
  879. * 项目子分类-删除分类
  880. *
  881. * @apiParam {Number} projectid 项目ID
  882. * @apiParam {Number} labelid 分类ID
  883. *
  884. * @throws \Throwable
  885. */
  886. public function label__delete()
  887. {
  888. $user = Users::authE();
  889. if (Base::isError($user)) {
  890. return $user;
  891. } else {
  892. $user = $user['data'];
  893. }
  894. //
  895. $projectid = trim(Request::input('projectid'));
  896. $inRes = Project::inThe($projectid, $user['username']);
  897. if (Base::isError($inRes)) {
  898. return $inRes;
  899. }
  900. //
  901. $labelid = intval(Request::input('labelid'));
  902. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  903. if (empty($labelDetail)) {
  904. return Base::retError('列表不存在或已被删除!');
  905. }
  906. //
  907. return DB::transaction(function () use ($user, $projectid, $labelDetail) {
  908. $taskLists = Base::DBC2A(DB::table('project_task')->where('labelid', $labelDetail['id'])->get());
  909. $logArray = [];
  910. foreach ($taskLists AS $task) {
  911. $logArray[] = [
  912. 'type' => '日志',
  913. 'projectid' => $projectid,
  914. 'taskid' => $task['id'],
  915. 'username' => $user['username'],
  916. 'detail' => '删除列表任务',
  917. 'indate' => Base::time(),
  918. 'other' => Base::array2string([
  919. 'type' => 'task',
  920. 'id' => $task['id'],
  921. 'title' => $task['title'],
  922. ])
  923. ];
  924. }
  925. $logArray[] = [
  926. 'type' => '日志',
  927. 'projectid' => $projectid,
  928. 'taskid' => 0,
  929. 'username' => $user['username'],
  930. 'detail' => '删除任务列表【' . $labelDetail['title'] . '】',
  931. 'indate' => Base::time()
  932. ];
  933. DB::table('project_task')->where('labelid', $labelDetail['id'])->update([
  934. 'delete' => 1,
  935. 'deletedate' => Base::time()
  936. ]);
  937. DB::table('project_label')->where('id', $labelDetail['id'])->delete();
  938. DB::table('project_log')->insert($logArray);
  939. //
  940. return Base::retSuccess('删除成功!');
  941. });
  942. }
  943. /**
  944. * 项目任务-列表
  945. *
  946. * @apiParam {Number} [projectid] 项目ID
  947. * @apiParam {String} [username] 负责人用户名(如果项目ID为空时此参数无效只获取自己的任务)
  948. * @apiParam {Number} [labelid] 项目子分类ID
  949. * @apiParam {Number} [level] 任务等级(1~4)
  950. * @apiParam {String} [archived] 任务是否归档
  951. * - 未归档 (默认)
  952. * - 已归档
  953. * - 全部
  954. * @apiParam {String} [type] 任务类型
  955. * - 全部(默认)
  956. * - 未完成
  957. * - 已超期
  958. * - 已完成
  959. * @apiParam {Number} [attention] 是否仅获取关注数据(1:是)
  960. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  961. * @apiParam {String} [startdate] 任务开始时间,格式:YYYY-MM-DD
  962. * @apiParam {String} [enddate] 任务结束时间,格式:YYYY-MM-DD
  963. *
  964. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  965. * - key: title|labelid|enddate|username|level|indate|type|inorder(默认)|userorder
  966. * - order: asc|desc
  967. * - 【archived=已归档】或【startdate和enddate赋值】时无效
  968. *
  969. * @apiParam {Number} [page] 当前页,默认:1
  970. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  971. */
  972. public function task__lists()
  973. {
  974. $user = Users::authE();
  975. if (Base::isError($user)) {
  976. return $user;
  977. } else {
  978. $user = $user['data'];
  979. }
  980. //
  981. $projectid = intval(Request::input('projectid'));
  982. if ($projectid > 0) {
  983. $inRes = Project::inThe($projectid, $user['username']);
  984. if (Base::isError($inRes)) {
  985. return $inRes;
  986. }
  987. }
  988. //
  989. $orderBy = '`inorder` DESC,`id` DESC';
  990. $sorts = Base::json2array(Request::input('sorts'));
  991. if (in_array($sorts['order'], ['asc', 'desc'])) {
  992. switch ($sorts['key']) {
  993. case 'title':
  994. case 'labelid':
  995. case 'enddate':
  996. case 'username':
  997. case 'level':
  998. case 'indate':
  999. case 'inorder':
  1000. case 'userorder':
  1001. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1002. break;
  1003. case 'type':
  1004. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['order'] . ', `complete` ' . $sorts['order'] . ',`id` DESC';
  1005. break;
  1006. }
  1007. }
  1008. //
  1009. $whereRaw = null;
  1010. $whereFunc = null;
  1011. $whereArray = [];
  1012. $whereArray[] = ['project_task.delete', '=', 0];
  1013. if ($projectid > 0) {
  1014. $whereArray[] = ['project_lists.id', '=', $projectid];
  1015. $whereArray[] = ['project_lists.delete', '=', 0];
  1016. if (trim(Request::input('username'))) {
  1017. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1018. }
  1019. } else {
  1020. $whereArray[] = ['project_task.username', '=', $user['username']];
  1021. }
  1022. if (intval(Request::input('labelid')) > 0) {
  1023. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  1024. }
  1025. if (intval(Request::input('level')) > 0) {
  1026. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  1027. }
  1028. if (intval(Request::input('attention')) === 1) {
  1029. $whereRaw.= $whereRaw ? ' AND ' : '';
  1030. $whereRaw.= "`username` in (select username from `" . env('DB_PREFIX') . "project_users` where `type`='关注' AND `username`='" . $user['username'] . "')";
  1031. }
  1032. $archived = trim(Request::input('archived'));
  1033. if (empty($archived)) $archived = "未归档";
  1034. switch ($archived) {
  1035. case '已归档':
  1036. $whereArray[] = ['project_task.archived', '=', 1];
  1037. $orderBy = '`archiveddate` DESC';
  1038. break;
  1039. case '未归档':
  1040. $whereArray[] = ['project_task.archived', '=', 0];
  1041. break;
  1042. }
  1043. $type = trim(Request::input('type'));
  1044. switch ($type) {
  1045. case '未完成':
  1046. $whereArray[] = ['project_task.complete', '=', 0];
  1047. break;
  1048. case '已超期':
  1049. $whereArray[] = ['project_task.complete', '=', 0];
  1050. $whereArray[] = ['project_task.enddate', '>', 0];
  1051. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  1052. break;
  1053. case '已完成':
  1054. $whereArray[] = ['project_task.complete', '=', 1];
  1055. break;
  1056. }
  1057. $startdate = trim(Request::input('startdate'));
  1058. $enddate = trim(Request::input('enddate'));
  1059. if (Base::isDate($startdate) || Base::isDate($enddate)) {
  1060. $startdate = strtotime($startdate . ' 00:00:00');
  1061. $enddate = strtotime($enddate . ' 23:59:59');
  1062. $whereRaw.= $whereRaw ? ' AND ' : '';
  1063. $whereRaw.= "((`startdate` >= " . $startdate . " OR `startdate` = 0) AND (`enddate` <= " . $enddate . " OR `enddate` = 0))";
  1064. $orderBy = '`startdate` DESC';
  1065. }
  1066. //
  1067. $builder = DB::table('project_task');
  1068. if ($projectid > 0) {
  1069. $builder->join('project_lists', 'project_lists.id', '=', 'project_task.projectid');
  1070. }
  1071. if ($whereRaw) {
  1072. $builder->whereRaw($whereRaw);
  1073. }
  1074. $lists = $builder->select(['project_task.*'])
  1075. ->where($whereArray)
  1076. ->orderByRaw($orderBy)
  1077. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1078. $lists = Base::getPageList($lists);
  1079. if (intval(Request::input('statistics')) == 1) {
  1080. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->count();
  1081. $lists['statistics_overdue'] = $type === '已超期' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->whereBetween('enddate', [1, Base::time()])->count();
  1082. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 1)->count();
  1083. }
  1084. if ($lists['total'] == 0) {
  1085. return Base::retError('未找到任何相关的任务', $lists);
  1086. }
  1087. foreach ($lists['lists'] AS $key => $info) {
  1088. $info['overdue'] = Project::taskIsOverdue($info);
  1089. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  1090. }
  1091. return Base::retSuccess('success', $lists);
  1092. }
  1093. /**
  1094. * 项目任务-添加任务
  1095. *
  1096. * @apiParam {String} title 任务标题
  1097. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  1098. * @apiParam {String} [username] 任务负责人用户名(如果项目ID为空时此参数无效复制人为自己)
  1099. * @apiParam {Number} [projectid] 项目ID
  1100. * @apiParam {Number} [labelid] 项目子分类ID
  1101. *
  1102. * @throws \Throwable
  1103. */
  1104. public function task__add()
  1105. {
  1106. $user = Users::authE();
  1107. if (Base::isError($user)) {
  1108. return $user;
  1109. } else {
  1110. $user = $user['data'];
  1111. }
  1112. //
  1113. $projectid = intval(Request::input('projectid'));
  1114. $labelid = intval(Request::input('labelid'));
  1115. if ($projectid > 0) {
  1116. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  1117. if (empty($projectDetail)) {
  1118. return Base::retError('项目不存在或已被删除!');
  1119. }
  1120. //
  1121. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1122. if (empty($labelDetail)) {
  1123. return Base::retError('项目子分类不存在或已被删除!');
  1124. }
  1125. //
  1126. $inRes = Project::inThe($projectid, $user['username']);
  1127. if (Base::isError($inRes)) {
  1128. return $inRes;
  1129. }
  1130. //
  1131. $username = trim(Request::input('username'));
  1132. if (empty($username)) {
  1133. $username = $user['username'];
  1134. }
  1135. if ($username != $user['username']) {
  1136. $inRes = Project::inThe($projectid, $username);
  1137. if (Base::isError($inRes)) {
  1138. return Base::retError('负责人不在项目成员内!');
  1139. }
  1140. }
  1141. } else {
  1142. $username = $user['username'];
  1143. }
  1144. //
  1145. $title = trim(Request::input('title'));
  1146. if (empty($title)) {
  1147. return Base::retError('任务标题不能为空!');
  1148. } elseif (mb_strlen($title) > 255) {
  1149. return Base::retError('任务标题最多只能设置255个字!');
  1150. }
  1151. //
  1152. $level = max(1, min(4, intval(Request::input('level'))));
  1153. $inArray = [
  1154. 'projectid' => $projectid,
  1155. 'labelid' => $labelid,
  1156. 'createuser' => $user['username'],
  1157. 'username' => $username,
  1158. 'title' => $title,
  1159. 'level' => $level,
  1160. 'inorder' => empty($projectid) ? 0 : intval(DB::table('project_task')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  1161. 'userorder' => intval(DB::table('project_task')->where('username', $user['username'])->where('level', $level)->orderByDesc('userorder')->value('userorder')) + 1,
  1162. 'indate' => Base::time(),
  1163. 'startdate' => Base::time(),
  1164. 'subtask' => Base::array2string([]),
  1165. 'files' => Base::array2string([]),
  1166. 'follower' => Base::array2string([]),
  1167. ];
  1168. return DB::transaction(function () use ($inArray) {
  1169. $taskid = DB::table('project_task')->insertGetId($inArray);
  1170. if (empty($taskid)) {
  1171. return Base::retError('系统繁忙,请稍后再试!');
  1172. }
  1173. DB::table('project_log')->insert([
  1174. 'type' => '日志',
  1175. 'projectid' => $inArray['projectid'],
  1176. 'taskid' => $taskid,
  1177. 'username' => $inArray['createuser'],
  1178. 'detail' => '添加任务',
  1179. 'indate' => Base::time(),
  1180. 'other' => Base::array2string([
  1181. 'type' => 'task',
  1182. 'id' => $taskid,
  1183. 'title' => $inArray['title'],
  1184. ])
  1185. ]);
  1186. Project::updateNum($inArray['projectid']);
  1187. //
  1188. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1189. $task['overdue'] = Project::taskIsOverdue($task);
  1190. $task = array_merge($task, Users::username2basic($task['username']));
  1191. return Base::retSuccess('添加成功!', $task);
  1192. });
  1193. }
  1194. /**
  1195. * 项目任务-归档、取消归档
  1196. *
  1197. * @apiParam {String} act
  1198. * - cancel: 取消归档
  1199. * - else: 加入归档
  1200. * @apiParam {Number} taskid 任务ID
  1201. */
  1202. public function task__archived()
  1203. {
  1204. $user = Users::authE();
  1205. if (Base::isError($user)) {
  1206. return $user;
  1207. } else {
  1208. $user = $user['data'];
  1209. }
  1210. //
  1211. $taskid = intval(Request::input('taskid'));
  1212. $task = Base::DBC2A(DB::table('project_lists')
  1213. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  1214. ->select(['project_task.projectid', 'project_task.title', 'project_task.archived'])
  1215. ->where([
  1216. ['project_lists.delete', '=', 0],
  1217. ['project_task.delete', '=', 0],
  1218. ['project_task.id', '=', $taskid],
  1219. ])
  1220. ->first());
  1221. if (empty($task)) {
  1222. return Base::retError('任务不存在!');
  1223. }
  1224. $inRes = Project::inThe($task['projectid'], $user['username']);
  1225. if (Base::isError($inRes)) {
  1226. return $inRes;
  1227. }
  1228. //
  1229. switch (Request::input('act')) {
  1230. case 'cancel': {
  1231. if ($task['archived'] == 0) {
  1232. return Base::retError('任务未归档!');
  1233. }
  1234. DB::table('project_task')->where('id', $taskid)->update([
  1235. 'archived' => 1,
  1236. 'archiveddate' => Base::time()
  1237. ]);
  1238. DB::table('project_log')->insert([
  1239. 'type' => '日志',
  1240. 'projectid' => $task['projectid'],
  1241. 'taskid' => $taskid,
  1242. 'username' => $user['username'],
  1243. 'detail' => '取消归档',
  1244. 'indate' => Base::time(),
  1245. 'other' => Base::array2string([
  1246. 'type' => 'task',
  1247. 'id' => $taskid,
  1248. 'title' => $task['title'],
  1249. ])
  1250. ]);
  1251. return Base::retSuccess('取消归档成功!');
  1252. }
  1253. default: {
  1254. if ($task['archived'] == 1) {
  1255. return Base::retError('任务已归档!');
  1256. }
  1257. DB::table('project_task')->where('id', $taskid)->update([
  1258. 'archived' => 0,
  1259. ]);
  1260. DB::table('project_log')->insert([
  1261. 'type' => '日志',
  1262. 'projectid' => $task['projectid'],
  1263. 'taskid' => $taskid,
  1264. 'username' => $user['username'],
  1265. 'detail' => '加入归档',
  1266. 'indate' => Base::time(),
  1267. 'other' => Base::array2string([
  1268. 'type' => 'task',
  1269. 'id' => $taskid,
  1270. 'title' => $task['title'],
  1271. ])
  1272. ]);
  1273. return Base::retSuccess('加入归档成功!');
  1274. }
  1275. }
  1276. }
  1277. /**
  1278. * 项目文件-列表
  1279. *
  1280. * @apiParam {Number} projectid 项目ID
  1281. * @apiParam {Number} [taskid] 任务ID
  1282. * @apiParam {String} [name] 文件名称
  1283. * @apiParam {String} [username] 上传者用户名
  1284. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  1285. * - key: name|size|username|indate
  1286. * - order: asc|desc
  1287. * @apiParam {Number} [page] 当前页,默认:1
  1288. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1289. */
  1290. public function files__lists()
  1291. {
  1292. $user = Users::authE();
  1293. if (Base::isError($user)) {
  1294. return $user;
  1295. } else {
  1296. $user = $user['data'];
  1297. }
  1298. //
  1299. $projectid = intval(Request::input('projectid'));
  1300. $inRes = Project::inThe($projectid, $user['username']);
  1301. if (Base::isError($inRes)) {
  1302. return $inRes;
  1303. }
  1304. //
  1305. $orderBy = '`id` DESC';
  1306. $sorts = Base::json2array(Request::input('sorts'));
  1307. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1308. switch ($sorts['key']) {
  1309. case 'name':
  1310. case 'size':
  1311. case 'download':
  1312. case 'username':
  1313. case 'indate':
  1314. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1315. break;
  1316. }
  1317. }
  1318. //
  1319. $whereArray = [];
  1320. $whereArray[] = ['projectid', '=', $projectid];
  1321. $whereArray[] = ['delete', '=', 0];
  1322. if (intval(Request::input('taskid')) > 0) {
  1323. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1324. }
  1325. if (trim(Request::input('name'))) {
  1326. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  1327. }
  1328. if (trim(Request::input('username'))) {
  1329. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1330. }
  1331. //
  1332. $lists = DB::table('project_files')
  1333. ->where($whereArray)
  1334. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1335. $lists = Base::getPageList($lists);
  1336. if ($lists['total'] == 0) {
  1337. return Base::retError('未找到任何相关的文件', $lists);
  1338. }
  1339. foreach ($lists['lists'] AS $key => $item) {
  1340. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  1341. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  1342. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  1343. }
  1344. return Base::retSuccess('success', $lists);
  1345. }
  1346. /**
  1347. * 项目文件-上传
  1348. *
  1349. * @apiParam {Number} projectid 项目ID
  1350. * @apiParam {Number} [taskid] 任务ID
  1351. */
  1352. public function files__upload()
  1353. {
  1354. $user = Users::authE();
  1355. if (Base::isError($user)) {
  1356. return $user;
  1357. } else {
  1358. $user = $user['data'];
  1359. }
  1360. //
  1361. $projectid = intval(Request::input('projectid'));
  1362. $taskid = intval(Request::input('taskid'));
  1363. $inRes = Project::inThe($projectid, $user['username']);
  1364. if (Base::isError($inRes)) {
  1365. return $inRes;
  1366. }
  1367. //
  1368. $data = Base::upload([
  1369. "file" => Request::file('files'),
  1370. "type" => 'file',
  1371. "path" => "uploads/projects/" . $projectid . "/",
  1372. ]);
  1373. if (Base::isError($data)) {
  1374. return Base::retError($data['msg']);
  1375. } else {
  1376. $fileData = $data['data'];
  1377. $thumb = 'images/files/file.png';
  1378. switch ($fileData['ext']) {
  1379. case "docx":
  1380. $thumb = 'images/files/doc.png';
  1381. break;
  1382. case "xlsx":
  1383. $thumb = 'images/files/xls.png';
  1384. break;
  1385. case "pptx":
  1386. $thumb = 'images/files/ppt.png';
  1387. break;
  1388. case "doc":
  1389. case "xls":
  1390. case "ppt":
  1391. case "txt":
  1392. case "esp":
  1393. case "gif":
  1394. $thumb = 'images/files/' . $fileData['ext'] . '.png';
  1395. break;
  1396. case "jpg":
  1397. case "jpeg":
  1398. case "png":
  1399. if (Base::imgThumb($fileData['file'], $fileData['file'] . "_thumb.jpg", 64, 0)) {
  1400. $thumb = $fileData['path'] . "_thumb.jpg";
  1401. }
  1402. break;
  1403. }
  1404. $array = [
  1405. 'projectid' => $projectid,
  1406. 'taskid' => $taskid,
  1407. 'name' => $fileData['name'],
  1408. 'size' => $fileData['size'] * 1024,
  1409. 'ext' => $fileData['ext'],
  1410. 'path' => $fileData['path'],
  1411. 'thumb' => $thumb,
  1412. 'username' => $user['username'],
  1413. 'indate' => Base::time(),
  1414. ];
  1415. $id = DB::table('project_files')->insertGetId($array);
  1416. $array['id'] = $id;
  1417. $array['path'] = Base::fillUrl($array['path']);
  1418. $array['thumb'] = Base::fillUrl($array['thumb']);
  1419. $array['download'] = 0;
  1420. $array['yetdown'] = 0;
  1421. DB::table('project_log')->insert([
  1422. 'type' => '日志',
  1423. 'projectid' => $projectid,
  1424. 'taskid' => $taskid,
  1425. 'username' => $user['username'],
  1426. 'detail' => '上传文件',
  1427. 'indate' => Base::time(),
  1428. 'other' => Base::array2string([
  1429. 'type' => 'file',
  1430. 'id' => $id,
  1431. 'name' => $fileData['name'],
  1432. ])
  1433. ]);
  1434. return Base::retSuccess('success', $array);
  1435. }
  1436. }
  1437. /**
  1438. * 项目文件-上传
  1439. *
  1440. * @apiParam {Number} fileid 文件ID
  1441. */
  1442. public function files__download()
  1443. {
  1444. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1445. if (empty($fileDetail)) {
  1446. return abort(404, '文件不存在或已被删除!');
  1447. }
  1448. $filePath = public_path($fileDetail['path']);
  1449. if (!file_exists($filePath)) {
  1450. return abort(404, '文件不存在或已被删除。');
  1451. }
  1452. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  1453. Session::put('filesDownload:' . $fileDetail['id'], 1);
  1454. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  1455. }
  1456. return response()->download($filePath, $fileDetail['name']);
  1457. }
  1458. /**
  1459. * 项目文件-重命名
  1460. *
  1461. * @apiParam {Number} fileid 文件ID
  1462. * @apiParam {String} name 新文件名称
  1463. */
  1464. public function files__rename()
  1465. {
  1466. $user = Users::authE();
  1467. if (Base::isError($user)) {
  1468. return $user;
  1469. } else {
  1470. $user = $user['data'];
  1471. }
  1472. //
  1473. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1474. if (empty($fileDetail)) {
  1475. return Base::retError('文件不存在或已被删除!');
  1476. }
  1477. if ($fileDetail['username'] != $user['username']) {
  1478. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1479. if (Base::isError($inRes)) {
  1480. return Base::retError('此操作仅支持管理员或上传者!');
  1481. }
  1482. }
  1483. //
  1484. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  1485. if (empty($name)) {
  1486. return Base::retError('文件名称不能为空!');
  1487. } elseif (mb_strlen($name) > 32) {
  1488. return Base::retError('文件名称最多只能设置32个字!');
  1489. }
  1490. //
  1491. $name .= '.' . $fileDetail['ext'];
  1492. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  1493. DB::table('project_log')->insert([
  1494. 'type' => '日志',
  1495. 'projectid' => $fileDetail['projectid'],
  1496. 'taskid' => $fileDetail['taskid'],
  1497. 'username' => $user['username'],
  1498. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  1499. 'indate' => Base::time(),
  1500. 'other' => Base::array2string([
  1501. 'type' => 'file',
  1502. 'id' => $fileDetail['id'],
  1503. 'name' => $name,
  1504. ])
  1505. ]);
  1506. }
  1507. //
  1508. return Base::retSuccess('修改成功!', [
  1509. 'name' => $name,
  1510. ]);
  1511. }
  1512. /**
  1513. * 项目文件-删除
  1514. *
  1515. * @apiParam {Number} fileid 文件ID
  1516. */
  1517. public function files__delete()
  1518. {
  1519. $user = Users::authE();
  1520. if (Base::isError($user)) {
  1521. return $user;
  1522. } else {
  1523. $user = $user['data'];
  1524. }
  1525. //
  1526. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1527. if (empty($fileDetail)) {
  1528. return Base::retError('文件不存在或已被删除!');
  1529. }
  1530. if ($fileDetail['username'] != $user['username']) {
  1531. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1532. if (Base::isError($inRes)) {
  1533. return Base::retError('此操作仅支持管理员或上传者!');
  1534. }
  1535. }
  1536. //
  1537. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  1538. 'delete' => 1,
  1539. 'deletedate' => Base::time()
  1540. ]);
  1541. DB::table('project_log')->insert([
  1542. 'type' => '日志',
  1543. 'projectid' => $fileDetail['projectid'],
  1544. 'taskid' => $fileDetail['taskid'],
  1545. 'username' => $user['username'],
  1546. 'detail' => '删除文件',
  1547. 'indate' => Base::time(),
  1548. 'other' => Base::array2string([
  1549. 'type' => 'file',
  1550. 'id' => $fileDetail['id'],
  1551. 'name' => $fileDetail['name'],
  1552. ])
  1553. ]);
  1554. //
  1555. return Base::retSuccess('删除成功!');
  1556. }
  1557. /**
  1558. * 项目动态-列表
  1559. *
  1560. * @apiParam {Number} [projectid] 项目ID
  1561. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  1562. * @apiParam {String} [username] 用户名
  1563. * @apiParam {Number} [page] 当前页,默认:1
  1564. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1565. */
  1566. public function log__lists()
  1567. {
  1568. $user = Users::authE();
  1569. if (Base::isError($user)) {
  1570. return $user;
  1571. } else {
  1572. $user = $user['data'];
  1573. }
  1574. //
  1575. $projectid = intval(Request::input('projectid'));
  1576. if ($projectid > 0) {
  1577. $inRes = Project::inThe($projectid, $user['username']);
  1578. if (Base::isError($inRes)) {
  1579. return $inRes;
  1580. }
  1581. }
  1582. //
  1583. $taskid = intval(Request::input('taskid'));
  1584. $whereArray = [];
  1585. if ($projectid > 0) {
  1586. $whereArray[] = ['projectid', '=', $projectid];
  1587. if ($taskid > 0) {
  1588. $whereArray[] = ['taskid', '=', $taskid];
  1589. }
  1590. } else {
  1591. if ($taskid < 0) {
  1592. return Base::retError('参数错误!');
  1593. }
  1594. $count = DB::table('project_task')->where([ 'id' => $taskid, 'username' => $user['username']])->count();
  1595. if ($count <= 0) {
  1596. return Base::retError('你不是任务负责人!');
  1597. }
  1598. $whereArray[] = ['taskid', '=', $taskid];
  1599. }
  1600. if (trim(Request::input('username'))) {
  1601. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1602. }
  1603. //
  1604. $lists = DB::table('project_log')
  1605. ->where($whereArray)
  1606. ->orderByDesc('indate')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1607. $lists = Base::getPageList($lists);
  1608. if ($lists['total'] == 0) {
  1609. return Base::retError('未找到任何相关的记录', $lists);
  1610. }
  1611. foreach ($lists['lists'] AS $key => $item) {
  1612. $item = array_merge($item, Users::username2basic($item['username']));
  1613. $item['timeData'] = [
  1614. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  1615. 'hi' => date("h:i", $item['indate']) ,
  1616. 'week' => "周" . Base::getTimeWeek($item['indate']),
  1617. 'segment' => Base::getTimeDayeSegment($item['indate']),
  1618. ];
  1619. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  1620. $lists['lists'][$key] = $item;
  1621. }
  1622. return Base::retSuccess('success', $lists);
  1623. }
  1624. }