ProjectController.php 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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} [taskid] 任务ID (赋值返回详细数据,不返回列表数据)
  950. * @apiParam {Number} [level] 任务等级(1~4)
  951. * @apiParam {String} [archived] 任务是否归档
  952. * - 未归档 (默认)
  953. * - 已归档
  954. * - 全部
  955. * @apiParam {String} [type] 任务类型
  956. * - 全部(默认)
  957. * - 未完成
  958. * - 已超期
  959. * - 已完成
  960. * @apiParam {Number} [attention] 是否仅获取关注数据(1:是)
  961. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  962. * @apiParam {String} [startdate] 任务开始时间,格式:YYYY-MM-DD
  963. * @apiParam {String} [enddate] 任务结束时间,格式:YYYY-MM-DD
  964. *
  965. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  966. * - key: title|labelid|enddate|username|level|indate|type|inorder(默认)|userorder
  967. * - order: asc|desc
  968. * - 【archived=已归档】或【startdate和enddate赋值】时无效
  969. *
  970. * @apiParam {Number} [page] 当前页,默认:1
  971. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  972. */
  973. public function task__lists()
  974. {
  975. $user = Users::authE();
  976. if (Base::isError($user)) {
  977. return $user;
  978. } else {
  979. $user = $user['data'];
  980. }
  981. //
  982. $projectid = intval(Request::input('projectid'));
  983. if ($projectid > 0) {
  984. $inRes = Project::inThe($projectid, $user['username']);
  985. if (Base::isError($inRes)) {
  986. return $inRes;
  987. }
  988. }
  989. //
  990. $orderBy = '`inorder` DESC,`id` DESC';
  991. $sorts = Base::json2array(Request::input('sorts'));
  992. if (in_array($sorts['order'], ['asc', 'desc'])) {
  993. switch ($sorts['key']) {
  994. case 'title':
  995. case 'labelid':
  996. case 'enddate':
  997. case 'username':
  998. case 'level':
  999. case 'indate':
  1000. case 'inorder':
  1001. case 'userorder':
  1002. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1003. break;
  1004. case 'type':
  1005. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['order'] . ', `complete` ' . $sorts['order'] . ',`id` DESC';
  1006. break;
  1007. }
  1008. }
  1009. //
  1010. $taskid = intval(Request::input('taskid'));
  1011. $whereRaw = null;
  1012. $whereFunc = null;
  1013. $whereArray = [];
  1014. $whereArray[] = ['project_task.delete', '=', 0];
  1015. if ($projectid > 0) {
  1016. $whereArray[] = ['project_lists.id', '=', $projectid];
  1017. $whereArray[] = ['project_lists.delete', '=', 0];
  1018. if (trim(Request::input('username'))) {
  1019. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1020. }
  1021. } else {
  1022. $whereArray[] = ['project_task.username', '=', $user['username']];
  1023. }
  1024. if ($taskid > 0) {
  1025. $whereArray[] = ['project_task.id', '=', intval(Request::input('taskid'))];
  1026. }
  1027. if (intval(Request::input('labelid')) > 0) {
  1028. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  1029. }
  1030. if (intval(Request::input('level')) > 0) {
  1031. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  1032. }
  1033. if (intval(Request::input('attention')) === 1) {
  1034. $whereRaw.= $whereRaw ? ' AND ' : '';
  1035. $whereRaw.= "`username` in (select username from `" . env('DB_PREFIX') . "project_users` where `type`='关注' AND `username`='" . $user['username'] . "')";
  1036. }
  1037. $archived = trim(Request::input('archived'));
  1038. if (empty($archived)) $archived = "未归档";
  1039. switch ($archived) {
  1040. case '已归档':
  1041. $whereArray[] = ['project_task.archived', '=', 1];
  1042. $orderBy = '`archiveddate` DESC';
  1043. break;
  1044. case '未归档':
  1045. $whereArray[] = ['project_task.archived', '=', 0];
  1046. break;
  1047. }
  1048. $type = trim(Request::input('type'));
  1049. switch ($type) {
  1050. case '未完成':
  1051. $whereArray[] = ['project_task.complete', '=', 0];
  1052. break;
  1053. case '已超期':
  1054. $whereArray[] = ['project_task.complete', '=', 0];
  1055. $whereArray[] = ['project_task.enddate', '>', 0];
  1056. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  1057. break;
  1058. case '已完成':
  1059. $whereArray[] = ['project_task.complete', '=', 1];
  1060. break;
  1061. }
  1062. $startdate = trim(Request::input('startdate'));
  1063. $enddate = trim(Request::input('enddate'));
  1064. if (Base::isDate($startdate) || Base::isDate($enddate)) {
  1065. $startdate = strtotime($startdate . ' 00:00:00');
  1066. $enddate = strtotime($enddate . ' 23:59:59');
  1067. $whereRaw.= $whereRaw ? ' AND ' : '';
  1068. $whereRaw.= "((`startdate` >= " . $startdate . " OR `startdate` = 0) AND (`enddate` <= " . $enddate . " OR `enddate` = 0))";
  1069. $orderBy = '`startdate` DESC';
  1070. }
  1071. //
  1072. $builder = DB::table('project_task');
  1073. if ($projectid > 0) {
  1074. $builder->join('project_lists', 'project_lists.id', '=', 'project_task.projectid');
  1075. }
  1076. if ($whereRaw) {
  1077. $builder->whereRaw($whereRaw);
  1078. }
  1079. $lists = $builder->select(['project_task.*'])
  1080. ->where($whereArray)
  1081. ->orderByRaw($orderBy)
  1082. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1083. $lists = Base::getPageList($lists, $taskid > 0 ? false : true);
  1084. if (intval(Request::input('statistics')) == 1) {
  1085. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 0)->count();
  1086. $lists['statistics_overdue'] = $type === '已超期' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 0)->whereBetween('enddate', [1, Base::time()])->count();
  1087. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 1)->count();
  1088. }
  1089. if ($lists['total'] == 0) {
  1090. return Base::retError('未找到任何相关的任务', $lists);
  1091. }
  1092. foreach ($lists['lists'] AS $key => $info) {
  1093. $info['overdue'] = Project::taskIsOverdue($info);
  1094. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  1095. }
  1096. if ($taskid > 0) {
  1097. if (count($lists['lists']) == 0) {
  1098. return Base::retError('未能找到任何相关的任务');
  1099. }
  1100. return Base::retSuccess('success', $lists['lists'][0]);
  1101. } else {
  1102. return Base::retSuccess('success', $lists);
  1103. }
  1104. }
  1105. /**
  1106. * 项目任务-添加任务
  1107. *
  1108. * @apiParam {String} title 任务标题
  1109. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  1110. * @apiParam {String} [username] 任务负责人用户名(如果项目ID为空时此参数无效复制人为自己)
  1111. * @apiParam {Number} [projectid] 项目ID
  1112. * @apiParam {Number} [labelid] 项目子分类ID
  1113. *
  1114. * @throws \Throwable
  1115. */
  1116. public function task__add()
  1117. {
  1118. $user = Users::authE();
  1119. if (Base::isError($user)) {
  1120. return $user;
  1121. } else {
  1122. $user = $user['data'];
  1123. }
  1124. //
  1125. $projectid = intval(Request::input('projectid'));
  1126. $labelid = intval(Request::input('labelid'));
  1127. if ($projectid > 0) {
  1128. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  1129. if (empty($projectDetail)) {
  1130. return Base::retError('项目不存在或已被删除!');
  1131. }
  1132. //
  1133. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1134. if (empty($labelDetail)) {
  1135. return Base::retError('项目子分类不存在或已被删除!');
  1136. }
  1137. //
  1138. $inRes = Project::inThe($projectid, $user['username']);
  1139. if (Base::isError($inRes)) {
  1140. return $inRes;
  1141. }
  1142. //
  1143. $username = trim(Request::input('username'));
  1144. if (empty($username)) {
  1145. $username = $user['username'];
  1146. }
  1147. if ($username != $user['username']) {
  1148. $inRes = Project::inThe($projectid, $username);
  1149. if (Base::isError($inRes)) {
  1150. return Base::retError('负责人不在项目成员内!');
  1151. }
  1152. }
  1153. } else {
  1154. $username = $user['username'];
  1155. }
  1156. //
  1157. $title = trim(Request::input('title'));
  1158. if (empty($title)) {
  1159. return Base::retError('任务标题不能为空!');
  1160. } elseif (mb_strlen($title) > 255) {
  1161. return Base::retError('任务标题最多只能设置255个字!');
  1162. }
  1163. //
  1164. $level = max(1, min(4, intval(Request::input('level'))));
  1165. $inArray = [
  1166. 'projectid' => $projectid,
  1167. 'labelid' => $labelid,
  1168. 'createuser' => $user['username'],
  1169. 'username' => $username,
  1170. 'title' => $title,
  1171. 'level' => $level,
  1172. 'inorder' => empty($projectid) ? 0 : intval(DB::table('project_task')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  1173. 'userorder' => intval(DB::table('project_task')->where('username', $user['username'])->where('level', $level)->orderByDesc('userorder')->value('userorder')) + 1,
  1174. 'indate' => Base::time(),
  1175. 'startdate' => Base::time(),
  1176. 'subtask' => Base::array2string([]),
  1177. 'follower' => Base::array2string([]),
  1178. ];
  1179. return DB::transaction(function () use ($inArray) {
  1180. $taskid = DB::table('project_task')->insertGetId($inArray);
  1181. if (empty($taskid)) {
  1182. return Base::retError('系统繁忙,请稍后再试!');
  1183. }
  1184. DB::table('project_log')->insert([
  1185. 'type' => '日志',
  1186. 'projectid' => $inArray['projectid'],
  1187. 'taskid' => $taskid,
  1188. 'username' => $inArray['createuser'],
  1189. 'detail' => '添加任务',
  1190. 'indate' => Base::time(),
  1191. 'other' => Base::array2string([
  1192. 'type' => 'task',
  1193. 'id' => $taskid,
  1194. 'title' => $inArray['title'],
  1195. ])
  1196. ]);
  1197. Project::updateNum($inArray['projectid']);
  1198. //
  1199. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1200. $task['overdue'] = Project::taskIsOverdue($task);
  1201. $task = array_merge($task, Users::username2basic($task['username']));
  1202. return Base::retSuccess('添加成功!', $task);
  1203. });
  1204. }
  1205. /**
  1206. * 项目任务-修改
  1207. *
  1208. * @apiParam {Number} taskid 任务ID
  1209. * @apiParam {String} act 修改字段|操作类型
  1210. * - title: 标题
  1211. * - desc: 描述
  1212. * - level: 优先级
  1213. * - username: 负责人
  1214. * - plannedtime: 设置计划时间
  1215. * - unplannedtime: 取消计划时间
  1216. * - complete: 标记完成
  1217. * - unfinished: 标记未完成
  1218. * - archived: 归档
  1219. * - unarchived: 取消归档
  1220. * - delete: 删除任务
  1221. * - comment: 评论
  1222. * @apiParam {String} [content] 内容数据
  1223. */
  1224. public function task__edit()
  1225. {
  1226. $user = Users::authE();
  1227. if (Base::isError($user)) {
  1228. return $user;
  1229. } else {
  1230. $user = $user['data'];
  1231. }
  1232. //
  1233. $taskid = intval(Request::input('taskid'));
  1234. $task = Base::DBC2A(DB::table('project_task')
  1235. ->where([
  1236. ['delete', '=', 0],
  1237. ['id', '=', $taskid],
  1238. ])
  1239. ->first());
  1240. if (empty($task)) {
  1241. return Base::retError('任务不存在!');
  1242. }
  1243. if ($task['projectid'] > 0) {
  1244. $inRes = Project::inThe($task['projectid'], $user['username']);
  1245. if (Base::isError($inRes)) {
  1246. return $inRes;
  1247. }
  1248. if (!$inRes['data']['isowner'] && $task['username'] != $user['username']) {
  1249. return Base::retError('此操作只允许项目管理员或者任务负责人!');
  1250. }
  1251. } else {
  1252. if ($task['username'] != $user['username']) {
  1253. return Base::retError('此操作只允许任务负责人!');
  1254. }
  1255. }
  1256. //
  1257. $act = trim(Request::input('act'));
  1258. $content = trim(Request::input('content'));
  1259. $message = "";
  1260. $upArray = [];
  1261. $logArray = [];
  1262. switch ($act) {
  1263. /**
  1264. * 修改标题
  1265. */
  1266. case 'title': {
  1267. if ($content == $task['title']) {
  1268. return Base::retError('标题未做改变!');
  1269. }
  1270. $upArray['title'] = $content;
  1271. $logArray[] = [
  1272. 'type' => '日志',
  1273. 'projectid' => $task['projectid'],
  1274. 'taskid' => $task['id'],
  1275. 'username' => $user['username'],
  1276. 'detail' => '修改任务标题',
  1277. 'indate' => Base::time(),
  1278. 'other' => Base::array2string([
  1279. 'type' => 'task',
  1280. 'id' => $task['id'],
  1281. 'title' => $content,
  1282. 'old_title' => $task['title'],
  1283. ])
  1284. ];
  1285. break;
  1286. }
  1287. /**
  1288. * 修改描述
  1289. */
  1290. case 'desc': {
  1291. if ($content == $task['desc']) {
  1292. return Base::retError('描述未做改变!');
  1293. }
  1294. $upArray['desc'] = $content;
  1295. $logArray[] = [
  1296. 'type' => '日志',
  1297. 'projectid' => $task['projectid'],
  1298. 'taskid' => $task['id'],
  1299. 'username' => $user['username'],
  1300. 'detail' => '修改任务描述',
  1301. 'indate' => Base::time(),
  1302. 'other' => Base::array2string([
  1303. 'type' => 'task',
  1304. 'id' => $task['id'],
  1305. 'title' => $task['title'],
  1306. 'old_desc' => $task['desc'],
  1307. ])
  1308. ];
  1309. break;
  1310. }
  1311. /**
  1312. * 调整任务等级
  1313. */
  1314. case 'level': {
  1315. $content = intval($content);
  1316. if ($content == $task['level']) {
  1317. return Base::retError('优先级未做改变!');
  1318. }
  1319. if ($content > 4 || $content < 1) {
  1320. return Base::retError('优先级参数错误!');
  1321. }
  1322. $upArray['level'] = $content;
  1323. $logArray[] = [
  1324. 'type' => '日志',
  1325. 'projectid' => $task['projectid'],
  1326. 'taskid' => $task['id'],
  1327. 'username' => $user['username'],
  1328. 'detail' => '调整任务等级为【P' . $content . '】',
  1329. 'indate' => Base::time(),
  1330. 'other' => Base::array2string([
  1331. 'type' => 'task',
  1332. 'id' => $task['id'],
  1333. 'title' => $task['title'],
  1334. 'old_level' => $task['level'],
  1335. ])
  1336. ];
  1337. break;
  1338. }
  1339. /**
  1340. * 修改任务负责人
  1341. */
  1342. case 'username': {
  1343. if ($content == $task['username']) {
  1344. return Base::retError('负责人未做改变!');
  1345. }
  1346. if ($task['projectid'] > 0) {
  1347. $inRes = Project::inThe($task['projectid'], $content);
  1348. if (Base::isError($inRes)) {
  1349. return Base::retError($content . '不在成员列表内!');
  1350. }
  1351. }
  1352. $upArray['username'] = $content;
  1353. $logArray[] = [
  1354. 'type' => '日志',
  1355. 'projectid' => $task['projectid'],
  1356. 'taskid' => $task['id'],
  1357. 'username' => $user['username'],
  1358. 'detail' => '修改负责人',
  1359. 'indate' => Base::time(),
  1360. 'other' => Base::array2string([
  1361. 'type' => 'task',
  1362. 'id' => $task['id'],
  1363. 'title' => $task['title'],
  1364. 'old_username' => $task['username'],
  1365. ])
  1366. ];
  1367. break;
  1368. }
  1369. /**
  1370. * 修改计划时间
  1371. */
  1372. case 'plannedtime': {
  1373. list($startdate, $enddate) = explode(",", $content);
  1374. if (!Base::isDate($startdate) || !Base::isDate($enddate)) {
  1375. return Base::retError('计划时间参数错误!');
  1376. }
  1377. $startdate = strtotime($startdate);
  1378. $enddate = strtotime($enddate);
  1379. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1380. return Base::retError('与原计划时间一致!');
  1381. }
  1382. $upArray['startdate'] = $startdate;
  1383. $upArray['enddate'] = $enddate;
  1384. $logArray[] = [
  1385. 'type' => '日志',
  1386. 'projectid' => $task['projectid'],
  1387. 'taskid' => $task['id'],
  1388. 'username' => $user['username'],
  1389. 'detail' => '设置计划时间',
  1390. 'indate' => Base::time(),
  1391. 'other' => Base::array2string([
  1392. 'type' => 'task',
  1393. 'id' => $task['id'],
  1394. 'title' => $task['title'],
  1395. 'old_startdate' => $task['startdate'],
  1396. 'old_enddate' => $task['enddate'],
  1397. ])
  1398. ];
  1399. break;
  1400. }
  1401. /**
  1402. * 取消计划时间
  1403. */
  1404. case 'unplannedtime': {
  1405. $startdate = 0;
  1406. $enddate = 0;
  1407. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1408. return Base::retError('与原计划时间一致!');
  1409. }
  1410. $upArray['startdate'] = $startdate;
  1411. $upArray['enddate'] = $enddate;
  1412. $logArray[] = [
  1413. 'type' => '日志',
  1414. 'projectid' => $task['projectid'],
  1415. 'taskid' => $task['id'],
  1416. 'username' => $user['username'],
  1417. 'detail' => '取消计划时间',
  1418. 'indate' => Base::time(),
  1419. 'other' => Base::array2string([
  1420. 'type' => 'task',
  1421. 'id' => $task['id'],
  1422. 'title' => $task['title'],
  1423. 'old_startdate' => $task['startdate'],
  1424. 'old_enddate' => $task['enddate'],
  1425. ])
  1426. ];
  1427. break;
  1428. }
  1429. /**
  1430. * 标记完成
  1431. */
  1432. case 'complete': {
  1433. if ($task['complete'] == 1) {
  1434. return Base::retError('任务已标记完成,请勿重复操作!');
  1435. }
  1436. $upArray['complete'] = 1;
  1437. $upArray['completedate'] = Base::time();
  1438. $logArray[] = [
  1439. 'type' => '日志',
  1440. 'projectid' => $task['projectid'],
  1441. 'taskid' => $task['id'],
  1442. 'username' => $user['username'],
  1443. 'detail' => '标记已完成',
  1444. 'indate' => Base::time(),
  1445. 'other' => Base::array2string([
  1446. 'type' => 'task',
  1447. 'id' => $task['id'],
  1448. 'title' => $task['title'],
  1449. ])
  1450. ];
  1451. break;
  1452. }
  1453. /**
  1454. * 标记未完成
  1455. */
  1456. case 'unfinished': {
  1457. if ($task['complete'] == 0) {
  1458. return Base::retError('任务未完成,无法标记未完成!');
  1459. }
  1460. $upArray['complete'] = 0;
  1461. $logArray[] = [
  1462. 'type' => '日志',
  1463. 'projectid' => $task['projectid'],
  1464. 'taskid' => $task['id'],
  1465. 'username' => $user['username'],
  1466. 'detail' => '标记未完成',
  1467. 'indate' => Base::time(),
  1468. 'other' => Base::array2string([
  1469. 'type' => 'task',
  1470. 'id' => $task['id'],
  1471. 'title' => $task['title'],
  1472. ])
  1473. ];
  1474. break;
  1475. }
  1476. /**
  1477. * 归档
  1478. */
  1479. case 'archived': {
  1480. if ($task['archived'] == 1) {
  1481. return Base::retError('任务已经归档,请勿重复操作!');
  1482. }
  1483. $upArray['archived'] = 1;
  1484. $upArray['archiveddate'] = Base::time();
  1485. $logArray[] = [
  1486. 'type' => '日志',
  1487. 'projectid' => $task['projectid'],
  1488. 'taskid' => $task['id'],
  1489. 'username' => $user['username'],
  1490. 'detail' => '任务归档',
  1491. 'indate' => Base::time(),
  1492. 'other' => Base::array2string([
  1493. 'type' => 'task',
  1494. 'id' => $task['id'],
  1495. 'title' => $task['title'],
  1496. ])
  1497. ];
  1498. break;
  1499. }
  1500. /**
  1501. * 取消归档
  1502. */
  1503. case 'unarchived': {
  1504. if ($task['archived'] == 0) {
  1505. return Base::retError('任务未归档,无法取消归档操作!');
  1506. }
  1507. $upArray['archived'] = 0;
  1508. $logArray[] = [
  1509. 'type' => '日志',
  1510. 'projectid' => $task['projectid'],
  1511. 'taskid' => $task['id'],
  1512. 'username' => $user['username'],
  1513. 'detail' => '取消归档',
  1514. 'indate' => Base::time(),
  1515. 'other' => Base::array2string([
  1516. 'type' => 'task',
  1517. 'id' => $task['id'],
  1518. 'title' => $task['title'],
  1519. ])
  1520. ];
  1521. break;
  1522. }
  1523. /**
  1524. * 删除任务
  1525. */
  1526. case 'delete': {
  1527. if ($task['delete'] == 1) {
  1528. return Base::retError('任务已删除,请勿重复操作!');
  1529. }
  1530. $upArray['delete'] = 1;
  1531. $upArray['deletedate'] = Base::time();
  1532. $logArray[] = [
  1533. 'type' => '日志',
  1534. 'projectid' => $task['projectid'],
  1535. 'taskid' => $task['id'],
  1536. 'username' => $user['username'],
  1537. 'detail' => '删除任务',
  1538. 'indate' => Base::time(),
  1539. 'other' => Base::array2string([
  1540. 'type' => 'task',
  1541. 'id' => $task['id'],
  1542. 'title' => $task['title'],
  1543. ])
  1544. ];
  1545. $message = "删除成功!";
  1546. break;
  1547. }
  1548. /**
  1549. * 评论任务
  1550. */
  1551. case 'comment': {
  1552. if (mb_strlen($content) < 2) {
  1553. return Base::retError('评论内容至少2个字!');
  1554. }
  1555. $logArray[] = [
  1556. 'type' => '评论',
  1557. 'projectid' => $task['projectid'],
  1558. 'taskid' => $task['id'],
  1559. 'username' => $user['username'],
  1560. 'detail' => $content,
  1561. 'indate' => Base::time(),
  1562. 'other' => Base::array2string([
  1563. 'type' => 'task',
  1564. 'id' => $task['id'],
  1565. 'title' => $task['title'],
  1566. ])
  1567. ];
  1568. $message = "评论成功!";
  1569. break;
  1570. }
  1571. default: {
  1572. return Base::retError('参数错误!');
  1573. break;
  1574. }
  1575. }
  1576. //
  1577. if ($upArray) {
  1578. DB::table('project_task')->where('id', $taskid)->update($upArray);
  1579. }
  1580. if ($logArray) {
  1581. DB::table('project_log')->insert($logArray);
  1582. }
  1583. //
  1584. if (in_array($act, ['complete', 'unfinished'])) {
  1585. Project::updateNum($task['projectid']);
  1586. }
  1587. //
  1588. $task = array_merge($task, $upArray);
  1589. $task['overdue'] = Project::taskIsOverdue($task);
  1590. $task = array_merge($task, Users::username2basic($task['username']));
  1591. return Base::retSuccess($message ?: '修改成功!', $task);
  1592. }
  1593. /**
  1594. * 项目文件-列表
  1595. *
  1596. * @apiParam {Number} [projectid] 项目ID
  1597. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  1598. * @apiParam {String} [name] 文件名称
  1599. * @apiParam {String} [username] 上传者用户名
  1600. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  1601. * - key: name|size|username|indate
  1602. * - order: asc|desc
  1603. * @apiParam {Number} [page] 当前页,默认:1
  1604. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1605. */
  1606. public function files__lists()
  1607. {
  1608. $user = Users::authE();
  1609. if (Base::isError($user)) {
  1610. return $user;
  1611. } else {
  1612. $user = $user['data'];
  1613. }
  1614. //
  1615. $projectid = intval(Request::input('projectid'));
  1616. if ($projectid > 0) {
  1617. $inRes = Project::inThe($projectid, $user['username']);
  1618. if (Base::isError($inRes)) {
  1619. return $inRes;
  1620. }
  1621. }
  1622. //
  1623. $orderBy = '`id` DESC';
  1624. $sorts = Base::json2array(Request::input('sorts'));
  1625. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1626. switch ($sorts['key']) {
  1627. case 'name':
  1628. case 'size':
  1629. case 'download':
  1630. case 'username':
  1631. case 'indate':
  1632. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1633. break;
  1634. }
  1635. }
  1636. //
  1637. $taskid = intval(Request::input('taskid'));
  1638. $whereArray = [];
  1639. if ($projectid > 0) {
  1640. $whereArray[] = ['projectid', '=', $projectid];
  1641. if ($taskid > 0) {
  1642. $whereArray[] = ['taskid', '=', $taskid];
  1643. }
  1644. } else {
  1645. if ($taskid < 0) {
  1646. return Base::retError('参数错误!');
  1647. }
  1648. $count = DB::table('project_task')->where([ 'id' => $taskid, 'username' => $user['username']])->count();
  1649. if ($count <= 0) {
  1650. return Base::retError('你不是任务负责人!');
  1651. }
  1652. $whereArray[] = ['taskid', '=', $taskid];
  1653. }
  1654. $whereArray[] = ['delete', '=', 0];
  1655. if (intval(Request::input('taskid')) > 0) {
  1656. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1657. }
  1658. if (trim(Request::input('name'))) {
  1659. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  1660. }
  1661. if (trim(Request::input('username'))) {
  1662. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1663. }
  1664. //
  1665. $lists = DB::table('project_files')
  1666. ->where($whereArray)
  1667. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1668. $lists = Base::getPageList($lists);
  1669. if ($lists['total'] == 0) {
  1670. return Base::retError('未找到任何相关的文件', $lists);
  1671. }
  1672. foreach ($lists['lists'] AS $key => $item) {
  1673. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  1674. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  1675. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  1676. }
  1677. return Base::retSuccess('success', $lists);
  1678. }
  1679. /**
  1680. * 项目文件-上传
  1681. *
  1682. * @apiParam {Number} [projectid] 项目ID
  1683. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  1684. */
  1685. public function files__upload()
  1686. {
  1687. $user = Users::authE();
  1688. if (Base::isError($user)) {
  1689. return $user;
  1690. } else {
  1691. $user = $user['data'];
  1692. }
  1693. //
  1694. $projectid = intval(Request::input('projectid'));
  1695. $taskid = intval(Request::input('taskid'));
  1696. if ($projectid > 0) {
  1697. $inRes = Project::inThe($projectid, $user['username']);
  1698. if (Base::isError($inRes)) {
  1699. return $inRes;
  1700. }
  1701. } else {
  1702. if ($taskid <= 0) {
  1703. return Base::retError('参数错误!');
  1704. }
  1705. $row = Base::DBC2A(DB::table('project_task')->select(['projectid'])->where([ 'id' => $taskid, 'username' => $user['username']])->first());
  1706. if (empty($row)) {
  1707. return Base::retError('你不是任务负责人!');
  1708. }
  1709. $projectid = $row['projectid'];
  1710. }
  1711. //
  1712. $data = Base::upload([
  1713. "file" => Request::file('files'),
  1714. "type" => 'file',
  1715. "path" => "uploads/projects/" . $projectid . "/",
  1716. ]);
  1717. if (Base::isError($data)) {
  1718. return Base::retError($data['msg']);
  1719. } else {
  1720. $fileData = $data['data'];
  1721. $thumb = 'images/files/file.png';
  1722. switch ($fileData['ext']) {
  1723. case "docx":
  1724. $thumb = 'images/files/doc.png';
  1725. break;
  1726. case "xlsx":
  1727. $thumb = 'images/files/xls.png';
  1728. break;
  1729. case "pptx":
  1730. $thumb = 'images/files/ppt.png';
  1731. break;
  1732. case "doc":
  1733. case "xls":
  1734. case "ppt":
  1735. case "txt":
  1736. case "esp":
  1737. case "gif":
  1738. $thumb = 'images/files/' . $fileData['ext'] . '.png';
  1739. break;
  1740. case "jpg":
  1741. case "jpeg":
  1742. case "png":
  1743. if (Base::imgThumb($fileData['file'], $fileData['file'] . "_thumb.jpg", 64, 0)) {
  1744. $thumb = $fileData['path'] . "_thumb.jpg";
  1745. }
  1746. break;
  1747. }
  1748. $array = [
  1749. 'projectid' => $projectid,
  1750. 'taskid' => $taskid,
  1751. 'name' => $fileData['name'],
  1752. 'size' => $fileData['size'] * 1024,
  1753. 'ext' => $fileData['ext'],
  1754. 'path' => $fileData['path'],
  1755. 'thumb' => $thumb,
  1756. 'username' => $user['username'],
  1757. 'indate' => Base::time(),
  1758. ];
  1759. $id = DB::table('project_files')->insertGetId($array);
  1760. $array['id'] = $id;
  1761. $array['path'] = Base::fillUrl($array['path']);
  1762. $array['thumb'] = Base::fillUrl($array['thumb']);
  1763. $array['download'] = 0;
  1764. $array['yetdown'] = 0;
  1765. DB::table('project_log')->insert([
  1766. 'type' => '日志',
  1767. 'projectid' => $projectid,
  1768. 'taskid' => $taskid,
  1769. 'username' => $user['username'],
  1770. 'detail' => '上传文件',
  1771. 'indate' => Base::time(),
  1772. 'other' => Base::array2string([
  1773. 'type' => 'file',
  1774. 'id' => $id,
  1775. 'name' => $fileData['name'],
  1776. ])
  1777. ]);
  1778. if ($taskid > 0) {
  1779. DB::table('project_task')->where('id', $taskid)->increment('filenum');
  1780. }
  1781. return Base::retSuccess('success', $array);
  1782. }
  1783. }
  1784. /**
  1785. * 项目文件-下载
  1786. *
  1787. * @apiParam {Number} fileid 文件ID
  1788. */
  1789. public function files__download()
  1790. {
  1791. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1792. if (empty($fileDetail)) {
  1793. return abort(404, '文件不存在或已被删除!');
  1794. }
  1795. $filePath = public_path($fileDetail['path']);
  1796. if (!file_exists($filePath)) {
  1797. return abort(404, '文件不存在或已被删除。');
  1798. }
  1799. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  1800. Session::put('filesDownload:' . $fileDetail['id'], 1);
  1801. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  1802. }
  1803. return response()->download($filePath, $fileDetail['name']);
  1804. }
  1805. /**
  1806. * 项目文件-重命名
  1807. *
  1808. * @apiParam {Number} fileid 文件ID
  1809. * @apiParam {String} name 新文件名称
  1810. */
  1811. public function files__rename()
  1812. {
  1813. $user = Users::authE();
  1814. if (Base::isError($user)) {
  1815. return $user;
  1816. } else {
  1817. $user = $user['data'];
  1818. }
  1819. //
  1820. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1821. if (empty($fileDetail)) {
  1822. return Base::retError('文件不存在或已被删除!');
  1823. }
  1824. if ($fileDetail['username'] != $user['username']) {
  1825. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1826. if (Base::isError($inRes)) {
  1827. return Base::retError('此操作仅支持管理员或上传者!');
  1828. }
  1829. }
  1830. //
  1831. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  1832. if (empty($name)) {
  1833. return Base::retError('文件名称不能为空!');
  1834. } elseif (mb_strlen($name) > 32) {
  1835. return Base::retError('文件名称最多只能设置32个字!');
  1836. }
  1837. //
  1838. $name .= '.' . $fileDetail['ext'];
  1839. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  1840. DB::table('project_log')->insert([
  1841. 'type' => '日志',
  1842. 'projectid' => $fileDetail['projectid'],
  1843. 'taskid' => $fileDetail['taskid'],
  1844. 'username' => $user['username'],
  1845. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  1846. 'indate' => Base::time(),
  1847. 'other' => Base::array2string([
  1848. 'type' => 'file',
  1849. 'id' => $fileDetail['id'],
  1850. 'name' => $name,
  1851. ])
  1852. ]);
  1853. }
  1854. //
  1855. return Base::retSuccess('修改成功!', [
  1856. 'name' => $name,
  1857. ]);
  1858. }
  1859. /**
  1860. * 项目文件-删除
  1861. *
  1862. * @apiParam {Number} fileid 文件ID
  1863. */
  1864. public function files__delete()
  1865. {
  1866. $user = Users::authE();
  1867. if (Base::isError($user)) {
  1868. return $user;
  1869. } else {
  1870. $user = $user['data'];
  1871. }
  1872. //
  1873. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1874. if (empty($fileDetail)) {
  1875. return Base::retError('文件不存在或已被删除!');
  1876. }
  1877. if ($fileDetail['username'] != $user['username']) {
  1878. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1879. if (Base::isError($inRes)) {
  1880. return Base::retError('此操作仅支持管理员或上传者!');
  1881. }
  1882. }
  1883. //
  1884. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  1885. 'delete' => 1,
  1886. 'deletedate' => Base::time()
  1887. ]);
  1888. DB::table('project_log')->insert([
  1889. 'type' => '日志',
  1890. 'projectid' => $fileDetail['projectid'],
  1891. 'taskid' => $fileDetail['taskid'],
  1892. 'username' => $user['username'],
  1893. 'detail' => '删除文件',
  1894. 'indate' => Base::time(),
  1895. 'other' => Base::array2string([
  1896. 'type' => 'file',
  1897. 'id' => $fileDetail['id'],
  1898. 'name' => $fileDetail['name'],
  1899. ])
  1900. ]);
  1901. if ($fileDetail['taskid'] > 0) {
  1902. DB::table('project_task')->where('id', $fileDetail['taskid'])->decrement('filenum');
  1903. }
  1904. //
  1905. return Base::retSuccess('删除成功!');
  1906. }
  1907. /**
  1908. * 项目动态-列表
  1909. *
  1910. * @apiParam {Number} [projectid] 项目ID
  1911. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  1912. * @apiParam {String} [type] 类型
  1913. * - 全部: 日志+评论(默认)
  1914. * - 日志
  1915. * - 评论
  1916. * @apiParam {String} [username] 用户名
  1917. * @apiParam {Number} [page] 当前页,默认:1
  1918. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1919. */
  1920. public function log__lists()
  1921. {
  1922. $user = Users::authE();
  1923. if (Base::isError($user)) {
  1924. return $user;
  1925. } else {
  1926. $user = $user['data'];
  1927. }
  1928. //
  1929. $projectid = intval(Request::input('projectid'));
  1930. if ($projectid > 0) {
  1931. $inRes = Project::inThe($projectid, $user['username']);
  1932. if (Base::isError($inRes)) {
  1933. return $inRes;
  1934. }
  1935. }
  1936. //
  1937. $taskid = intval(Request::input('taskid'));
  1938. $whereArray = [];
  1939. $whereFunc = null;
  1940. if ($projectid > 0) {
  1941. $whereArray[] = ['projectid', '=', $projectid];
  1942. if ($taskid > 0) {
  1943. $whereArray[] = ['taskid', '=', $taskid];
  1944. }
  1945. } else {
  1946. if ($taskid < 0) {
  1947. return Base::retError('参数错误!');
  1948. }
  1949. $count = DB::table('project_task')->where([ 'id' => $taskid, 'username' => $user['username']])->count();
  1950. if ($count <= 0) {
  1951. return Base::retError('你不是任务负责人!');
  1952. }
  1953. $whereArray[] = ['taskid', '=', $taskid];
  1954. }
  1955. if (trim(Request::input('username'))) {
  1956. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1957. }
  1958. switch (trim(Request::input('type'))) {
  1959. case '日志': {
  1960. $whereArray[] = ['type', '=', '日志'];
  1961. break;
  1962. }
  1963. case '评论': {
  1964. $whereArray[] = ['type', '=', '评论'];
  1965. break;
  1966. }
  1967. default: {
  1968. $whereFunc = function ($query) {
  1969. $query->whereIn('type', ['日志', '评论']);
  1970. };
  1971. break;
  1972. }
  1973. }
  1974. //
  1975. $lists = DB::table('project_log')
  1976. ->where($whereArray)
  1977. ->where($whereFunc)
  1978. ->orderByDesc('indate')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1979. $lists = Base::getPageList($lists);
  1980. if ($lists['total'] == 0) {
  1981. return Base::retError('未找到任何相关的记录', $lists);
  1982. }
  1983. foreach ($lists['lists'] AS $key => $item) {
  1984. $item = array_merge($item, Users::username2basic($item['username']));
  1985. $item['timeData'] = [
  1986. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  1987. 'hi' => date("h:i", $item['indate']) ,
  1988. 'week' => "周" . Base::getTimeWeek($item['indate']),
  1989. 'segment' => Base::getTimeDayeSegment($item['indate']),
  1990. ];
  1991. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  1992. $lists['lists'][$key] = $item;
  1993. }
  1994. return Base::retSuccess('success', $lists);
  1995. }
  1996. }