ProjectController.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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. public function sort()
  424. {
  425. $user = Users::authE();
  426. if (Base::isError($user)) {
  427. return $user;
  428. } else {
  429. $user = $user['data'];
  430. }
  431. //
  432. $projectid = trim(Request::input('projectid'));
  433. $inRes = Project::inThe($projectid, $user['username']);
  434. if (Base::isError($inRes)) {
  435. return $inRes;
  436. }
  437. //
  438. $oldSort = explode(";", Request::input('oldsort'));
  439. $newSort = explode(";", Request::input('newsort'));
  440. if (count($oldSort) != count($newSort)) {
  441. return Base::retError('参数错误!');
  442. }
  443. if (intval(Request::input('label'))) {
  444. //排序分类
  445. foreach ($newSort AS $sort => $item) {
  446. list($newLabelid, $newTask) = explode(':', $item);
  447. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  448. if ($newLabelid != $oldLabelid) {
  449. DB::table('project_label')->where([
  450. 'id' => $newLabelid,
  451. 'projectid' => $projectid
  452. ])->update([
  453. 'inorder' => intval($sort)
  454. ]);
  455. }
  456. }
  457. $detail = '调整任务列表排序';
  458. $sortType = 'label';
  459. } else {
  460. //排序任务(调整任务归类)
  461. foreach ($newSort AS $sort => $item) {
  462. list($newLabelid, $newTask) = explode(':', $item);
  463. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  464. if ($newTask != $oldTask) {
  465. $newTask = explode('-', $newTask);
  466. $inorder = count($newTask);
  467. foreach ($newTask AS $taskid) {
  468. DB::table('project_task')->where([
  469. 'id' => $taskid,
  470. 'projectid' => $projectid
  471. ])->update([
  472. 'labelid' => $newLabelid,
  473. 'inorder' => $inorder
  474. ]);
  475. $inorder--;
  476. }
  477. }
  478. }
  479. $detail = '调整任务排序';
  480. $sortType = 'task';
  481. }
  482. //
  483. $row = Base::DBC2A(DB::table('project_log')->where([ 'type' => '日志', 'projectid' => $projectid ])->orderByDesc('id')->first());
  484. $continue = 1;
  485. if ($row && $row['username'] == $user['username'] && $row['indate'] + 300 > Base::time()) {
  486. $other = Base::string2array($row['other']);
  487. if ($other['sortType'] == $sortType) {
  488. $continue = intval($other['continue']) + 1;
  489. if ($continue <= 100) {
  490. DB::table('project_log')->where('id', $row['id'])->update([
  491. 'detail' => $detail . '(' . $continue . '次)',
  492. 'other' => Base::array2string([
  493. 'sortType' => $sortType,
  494. 'continue' => $continue,
  495. 'times' => $other['times'] . '|' . Base::time(),
  496. ])
  497. ]);
  498. }
  499. }
  500. }
  501. if ($continue == 1) {
  502. DB::table('project_log')->insert([
  503. 'type' => '日志',
  504. 'projectid' => $projectid,
  505. 'username' => $user['username'],
  506. 'detail' => $detail,
  507. 'indate' => Base::time(),
  508. 'other' => Base::array2string([
  509. 'sortType' => $sortType,
  510. 'continue' => $continue,
  511. 'times' => Base::time(),
  512. ])
  513. ]);
  514. }
  515. return Base::retSuccess('保存成功!');
  516. }
  517. /**
  518. * 退出项目
  519. *
  520. * @apiParam {Number} projectid 项目ID
  521. */
  522. public function out()
  523. {
  524. $user = Users::authE();
  525. if (Base::isError($user)) {
  526. return $user;
  527. } else {
  528. $user = $user['data'];
  529. }
  530. //
  531. $projectid = trim(Request::input('projectid'));
  532. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  533. if (empty($projectDetail)) {
  534. return Base::retError('项目不存在或已被删除!');
  535. }
  536. if ($projectDetail['username'] == $user['username']) {
  537. return Base::retError('你是项目负责人,不可退出项目!');
  538. }
  539. $inRes = Project::inThe($projectid, $user['username']);
  540. if (Base::isError($inRes)) {
  541. return $inRes;
  542. }
  543. //
  544. DB::table('project_users')->where([
  545. 'type' => '成员',
  546. 'projectid' => $projectDetail['id'],
  547. 'username' => $user['username'],
  548. ])->delete();
  549. DB::table('project_log')->insert([
  550. 'type' => '日志',
  551. 'projectid' => $projectDetail['id'],
  552. 'username' => $user['username'],
  553. 'detail' => '退出项目',
  554. 'indate' => Base::time()
  555. ]);
  556. //
  557. return Base::retSuccess('退出项目成功!');
  558. }
  559. /**
  560. * 项目成员-列表
  561. *
  562. * @apiParam {Number} projectid 项目ID
  563. * @apiParam {Number} [page] 当前页,默认:1
  564. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  565. */
  566. public function users__lists()
  567. {
  568. $user = Users::authE();
  569. if (Base::isError($user)) {
  570. return $user;
  571. } else {
  572. $user = $user['data'];
  573. }
  574. //
  575. $projectid = intval(Request::input('projectid'));
  576. $inRes = Project::inThe($projectid, $user['username']);
  577. if (Base::isError($inRes)) {
  578. return $inRes;
  579. }
  580. //
  581. $lists = DB::table('project_lists')
  582. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  583. ->select(['project_lists.title', 'project_users.*'])
  584. ->where([
  585. ['project_lists.id', $projectid],
  586. ['project_lists.delete', 0],
  587. ['project_users.type', '成员'],
  588. ])
  589. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  590. $lists = Base::getPageList($lists);
  591. if ($lists['total'] == 0) {
  592. return Base::retError('未找到任何相关的成员');
  593. }
  594. foreach ($lists['lists'] AS $key => $projectDetail) {
  595. $userInfo = Users::username2basic($projectDetail['username']);
  596. $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
  597. $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
  598. $lists['lists'][$key]['profession'] = $userInfo['profession'];
  599. }
  600. return Base::retSuccess('success', $lists);
  601. }
  602. /**
  603. * 项目成员-添加、删除
  604. *
  605. * @apiParam {String} act
  606. * - delete: 删除成员
  607. * - else: 添加成员
  608. * @apiParam {Number} projectid 项目ID
  609. * @apiParam {Array|String} username 用户名(或用户名组)
  610. */
  611. public function users__join()
  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. $usernames = Request::input('username');
  629. if (empty($usernames)) {
  630. return Base::retError('参数错误!');
  631. }
  632. if (!is_array($usernames)) {
  633. if (Base::strExists($usernames, ',')) {
  634. $usernames = explode(',', $usernames);
  635. } else {
  636. $usernames = [$usernames];
  637. }
  638. }
  639. //
  640. $logArray = [];
  641. foreach ($usernames AS $username) {
  642. $inRes = Project::inThe($projectid, $username);
  643. switch (Request::input('act')) {
  644. case 'delete': {
  645. if (!Base::isError($inRes) && $projectDetail['username'] != $username) {
  646. DB::table('project_users')->where([
  647. 'type' => '成员',
  648. 'projectid' => $projectid,
  649. 'username' => $username,
  650. ])->delete();
  651. $logArray[] = [
  652. 'type' => '日志',
  653. 'projectid' => $projectDetail['id'],
  654. 'username' => $user['username'],
  655. 'detail' => '将成员【' . $username . '】移出项目',
  656. 'indate' => Base::time()
  657. ];
  658. }
  659. break;
  660. }
  661. default: {
  662. if (Base::isError($inRes)) {
  663. DB::table('project_users')->insert([
  664. 'type' => '成员',
  665. 'projectid' => $projectid,
  666. 'isowner' => 0,
  667. 'username' => $username,
  668. 'indate' => Base::time()
  669. ]);
  670. $logArray[] = [
  671. 'type' => '日志',
  672. 'projectid' => $projectDetail['id'],
  673. 'username' => $username,
  674. 'detail' => '将成员【' . $username . '】加入项目',
  675. 'indate' => Base::time()
  676. ];
  677. }
  678. break;
  679. }
  680. }
  681. }
  682. return Base::retSuccess('操作完成!');
  683. }
  684. /**
  685. * 项目子分类-添加分类
  686. *
  687. * @apiParam {Number} projectid 项目ID
  688. * @apiParam {String} title 分类名称
  689. */
  690. public function label__add()
  691. {
  692. $user = Users::authE();
  693. if (Base::isError($user)) {
  694. return $user;
  695. } else {
  696. $user = $user['data'];
  697. }
  698. //
  699. $projectid = trim(Request::input('projectid'));
  700. $inRes = Project::inThe($projectid, $user['username']);
  701. if (Base::isError($inRes)) {
  702. return $inRes;
  703. }
  704. //
  705. $title = trim(Request::input('title'));
  706. if (empty($title)) {
  707. return Base::retError('列表名称不能为空!');
  708. } elseif (mb_strlen($title) > 32) {
  709. return Base::retError('列表名称最多只能设置32个字!');
  710. }
  711. //
  712. $count = DB::table('project_label')->where('projectid', $projectid)->where('title', $title)->count();
  713. if ($count > 0) {
  714. return Base::retError('列表名称已存在!');
  715. }
  716. //
  717. $id = DB::table('project_label')->insertGetId([
  718. 'projectid' => $projectid,
  719. 'title' => $title,
  720. 'inorder' => intval(DB::table('project_label')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  721. ]);
  722. if (empty($id)) {
  723. return Base::retError('系统繁忙,请稍后再试!');
  724. }
  725. DB::table('project_log')->insert([
  726. 'type' => '日志',
  727. 'projectid' => $projectid,
  728. 'username' => $user['username'],
  729. 'detail' => '添加任务列表【' . $title . '】',
  730. 'indate' => Base::time()
  731. ]);
  732. //
  733. $row = Base::DBC2A(DB::table('project_label')->where('id', $id)->first());
  734. $row['taskLists'] = [];
  735. return Base::retSuccess('添加成功!', $row);
  736. }
  737. /**
  738. * 项目子分类-重命名分类
  739. *
  740. * @apiParam {Number} projectid 项目ID
  741. * @apiParam {Number} labelid 分类ID
  742. * @apiParam {String} title 新分类名称
  743. */
  744. public function label__rename()
  745. {
  746. $user = Users::authE();
  747. if (Base::isError($user)) {
  748. return $user;
  749. } else {
  750. $user = $user['data'];
  751. }
  752. //
  753. $projectid = trim(Request::input('projectid'));
  754. $inRes = Project::inThe($projectid, $user['username']);
  755. if (Base::isError($inRes)) {
  756. return $inRes;
  757. }
  758. //
  759. $title = trim(Request::input('title'));
  760. if (empty($title)) {
  761. return Base::retError('列表名称不能为空!');
  762. } elseif (mb_strlen($title) > 32) {
  763. return Base::retError('列表名称最多只能设置32个字!');
  764. }
  765. //
  766. $labelid = intval(Request::input('labelid'));
  767. $count = DB::table('project_label')->where('id', '!=', $labelid)->where('projectid', $projectid)->where('title', $title)->count();
  768. if ($count > 0) {
  769. return Base::retError('列表名称已存在!');
  770. }
  771. //
  772. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  773. if (empty($labelDetail)) {
  774. return Base::retError('列表不存在或已被删除!');
  775. }
  776. //
  777. if (DB::table('project_label')->where('id', $labelDetail['id'])->update([ 'title' => $title ])) {
  778. DB::table('project_log')->insert([
  779. 'type' => '日志',
  780. 'projectid' => $projectid,
  781. 'username' => $user['username'],
  782. 'detail' => '任务列表【' . $labelDetail['title'] . '】重命名【' . $title . '】',
  783. 'indate' => Base::time()
  784. ]);
  785. }
  786. //
  787. return Base::retSuccess('修改成功!');
  788. }
  789. /**
  790. * 项目子分类-删除分类
  791. *
  792. * @apiParam {Number} projectid 项目ID
  793. * @apiParam {Number} labelid 分类ID
  794. *
  795. * @throws \Throwable
  796. */
  797. public function label__delete()
  798. {
  799. $user = Users::authE();
  800. if (Base::isError($user)) {
  801. return $user;
  802. } else {
  803. $user = $user['data'];
  804. }
  805. //
  806. $projectid = trim(Request::input('projectid'));
  807. $inRes = Project::inThe($projectid, $user['username']);
  808. if (Base::isError($inRes)) {
  809. return $inRes;
  810. }
  811. //
  812. $labelid = intval(Request::input('labelid'));
  813. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  814. if (empty($labelDetail)) {
  815. return Base::retError('列表不存在或已被删除!');
  816. }
  817. //
  818. return DB::transaction(function () use ($user, $projectid, $labelDetail) {
  819. $taskLists = Base::DBC2A(DB::table('project_task')->where('labelid', $labelDetail['id'])->get());
  820. $logArray = [];
  821. foreach ($taskLists AS $task) {
  822. $logArray[] = [
  823. 'type' => '日志',
  824. 'projectid' => $projectid,
  825. 'taskid' => $task['id'],
  826. 'username' => $user['username'],
  827. 'detail' => '删除列表任务',
  828. 'indate' => Base::time(),
  829. 'other' => Base::array2string([
  830. 'type' => 'task',
  831. 'id' => $task['id'],
  832. 'title' => $task['title'],
  833. ])
  834. ];
  835. }
  836. $logArray[] = [
  837. 'type' => '日志',
  838. 'projectid' => $projectid,
  839. 'taskid' => 0,
  840. 'username' => $user['username'],
  841. 'detail' => '删除任务列表【' . $labelDetail['title'] . '】',
  842. 'indate' => Base::time()
  843. ];
  844. DB::table('project_task')->where('labelid', $labelDetail['id'])->update([
  845. 'delete' => 1,
  846. 'deletedate' => Base::time()
  847. ]);
  848. DB::table('project_label')->where('id', $labelDetail['id'])->delete();
  849. DB::table('project_log')->insert($logArray);
  850. //
  851. return Base::retSuccess('删除成功!');
  852. });
  853. }
  854. /**
  855. * 项目任务-列表
  856. *
  857. * @apiParam {Number} [projectid] 项目ID
  858. * @apiParam {String} [username] 负责人用户名(如果项目ID为空时此参数无效只获取自己的任务)
  859. * @apiParam {Number} [labelid] 项目子分类ID
  860. * @apiParam {Number} [level] 任务等级(1~4)
  861. * @apiParam {String} [archived] 任务是否归档
  862. * - 未归档 (默认)
  863. * - 已归档
  864. * - 全部
  865. * @apiParam {String} [type] 任务类型
  866. * - 全部(默认)
  867. * - 未完成
  868. * - 已超期
  869. * - 已完成
  870. * @apiParam {Number} [attention] 是否仅获取关注数据(1:是)
  871. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  872. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  873. * - key: title|labelid|enddate|username|level|indate|type
  874. * - order: asc|desc
  875. * - 【archived=已归档】或【startdate和enddate赋值】时无效
  876. * @apiParam {String} [startdate] 任务开始时间,格式:YYYY-MM-DD
  877. * @apiParam {String} [enddate] 任务结束时间,格式:YYYY-MM-DD
  878. * @apiParam {Number} [idlater] 获取数据ID之后的数据
  879. * @apiParam {Number} [page] 当前页,默认:1
  880. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  881. */
  882. public function task__lists()
  883. {
  884. $user = Users::authE();
  885. if (Base::isError($user)) {
  886. return $user;
  887. } else {
  888. $user = $user['data'];
  889. }
  890. //
  891. $projectid = intval(Request::input('projectid'));
  892. if ($projectid > 0) {
  893. $inRes = Project::inThe($projectid, $user['username']);
  894. if (Base::isError($inRes)) {
  895. return $inRes;
  896. }
  897. }
  898. //
  899. $orderBy = '`inorder` DESC,`id` DESC';
  900. if (intval(Request::input('labelid')) == 0) {
  901. $orderBy = '`indate` DESC,`id` DESC';
  902. }
  903. $sorts = Base::json2array(Request::input('sorts'));
  904. if (in_array($sorts['order'], ['asc', 'desc'])) {
  905. switch ($sorts['key']) {
  906. case 'title':
  907. case 'labelid':
  908. case 'enddate':
  909. case 'username':
  910. case 'level':
  911. case 'indate':
  912. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  913. break;
  914. case 'type':
  915. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['order'] . ', `complete` ' . $sorts['order'] . ',`id` DESC';
  916. break;
  917. }
  918. }
  919. //
  920. $whereRaw = null;
  921. $whereFunc = null;
  922. $whereArray = [];
  923. $whereArray[] = ['project_task.delete', '=', 0];
  924. if ($projectid > 0) {
  925. $whereArray[] = ['project_lists.id', '=', $projectid];
  926. $whereArray[] = ['project_lists.delete', '=', 0];
  927. if (trim(Request::input('username'))) {
  928. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  929. }
  930. } else {
  931. $whereArray[] = ['project_task.username', '=', $user['username']];
  932. }
  933. if (intval(Request::input('labelid')) > 0) {
  934. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  935. }
  936. if (intval(Request::input('level')) > 0) {
  937. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  938. }
  939. if (intval(Request::input('idlater')) > 0) {
  940. $whereArray[] = ['project_task.id', '<', intval(Request::input('idlater'))];
  941. }
  942. if (intval(Request::input('attention')) === 1) {
  943. $whereRaw.= $whereRaw ? ' AND ' : '';
  944. $whereRaw.= "`username` in (select username from `" . env('DB_PREFIX') . "project_users` where `type`='关注' AND `username`='" . $user['username'] . "')";
  945. }
  946. $archived = trim(Request::input('archived'));
  947. if (empty($archived)) $archived = "未归档";
  948. switch ($archived) {
  949. case '已归档':
  950. $whereArray[] = ['project_task.archived', '=', 1];
  951. $orderBy = '`archiveddate` DESC';
  952. break;
  953. case '未归档':
  954. $whereArray[] = ['project_task.archived', '=', 0];
  955. break;
  956. }
  957. $type = trim(Request::input('type'));
  958. switch ($type) {
  959. case '未完成':
  960. $whereArray[] = ['project_task.complete', '=', 0];
  961. break;
  962. case '已超期':
  963. $whereArray[] = ['project_task.complete', '=', 0];
  964. $whereArray[] = ['project_task.enddate', '>', 0];
  965. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  966. break;
  967. case '已完成':
  968. $whereArray[] = ['project_task.complete', '=', 1];
  969. break;
  970. }
  971. $startdate = trim(Request::input('startdate'));
  972. $enddate = trim(Request::input('enddate'));
  973. if (Base::isDate($startdate) || Base::isDate($enddate)) {
  974. $startdate = strtotime($startdate . ' 00:00:00');
  975. $enddate = strtotime($enddate . ' 23:59:59');
  976. $whereRaw.= $whereRaw ? ' AND ' : '';
  977. $whereRaw.= "((`startdate` >= " . $startdate . " OR `startdate` = 0) AND (`enddate` <= " . $enddate . " OR `enddate` = 0))";
  978. $orderBy = '`startdate` DESC';
  979. }
  980. //
  981. $builder = DB::table('project_task');
  982. if ($projectid > 0) {
  983. $builder->join('project_lists', 'project_lists.id', '=', 'project_task.projectid');
  984. }
  985. if ($whereRaw) {
  986. $builder->whereRaw($whereRaw);
  987. }
  988. $lists = $builder->select(['project_task.*'])
  989. ->where($whereArray)
  990. ->orderByRaw($orderBy)
  991. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  992. $lists = Base::getPageList($lists);
  993. if (intval(Request::input('statistics')) == 1) {
  994. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->count();
  995. $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();
  996. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 1)->count();
  997. }
  998. if ($lists['total'] == 0) {
  999. return Base::retError('未找到任何相关的任务', $lists);
  1000. }
  1001. foreach ($lists['lists'] AS $key => $info) {
  1002. $info['overdue'] = Project::taskIsOverdue($info);
  1003. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  1004. }
  1005. return Base::retSuccess('success', $lists);
  1006. }
  1007. /**
  1008. * 项目任务-添加任务
  1009. *
  1010. * @apiParam {String} title 任务标题
  1011. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  1012. * @apiParam {String} [username] 任务负责人用户名(如果项目ID为空时此参数无效复制人为自己)
  1013. * @apiParam {Number} [projectid] 项目ID
  1014. * @apiParam {Number} [labelid] 项目子分类ID
  1015. *
  1016. * @throws \Throwable
  1017. */
  1018. public function task__add()
  1019. {
  1020. $user = Users::authE();
  1021. if (Base::isError($user)) {
  1022. return $user;
  1023. } else {
  1024. $user = $user['data'];
  1025. }
  1026. //
  1027. $projectid = intval(Request::input('projectid'));
  1028. $labelid = intval(Request::input('labelid'));
  1029. if ($projectid > 0) {
  1030. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  1031. if (empty($projectDetail)) {
  1032. return Base::retError('项目不存在或已被删除!');
  1033. }
  1034. //
  1035. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1036. if (empty($labelDetail)) {
  1037. return Base::retError('项目子分类不存在或已被删除!');
  1038. }
  1039. //
  1040. $inRes = Project::inThe($projectid, $user['username']);
  1041. if (Base::isError($inRes)) {
  1042. return $inRes;
  1043. }
  1044. //
  1045. $username = trim(Request::input('username'));
  1046. if (empty($username)) {
  1047. $username = $user['username'];
  1048. }
  1049. if ($username != $user['username']) {
  1050. $inRes = Project::inThe($projectid, $username);
  1051. if (Base::isError($inRes)) {
  1052. return Base::retError('负责人不在项目成员内!');
  1053. }
  1054. }
  1055. } else {
  1056. $username = $user['username'];
  1057. }
  1058. //
  1059. $title = trim(Request::input('title'));
  1060. if (empty($title)) {
  1061. return Base::retError('任务标题不能为空!');
  1062. } elseif (mb_strlen($title) > 255) {
  1063. return Base::retError('任务标题最多只能设置255个字!');
  1064. }
  1065. //
  1066. $inArray = [
  1067. 'projectid' => $projectid,
  1068. 'labelid' => $labelid,
  1069. 'createuser' => $user['username'],
  1070. 'username' => $username,
  1071. 'title' => $title,
  1072. 'level' => max(1, min(4, intval(Request::input('level')))),
  1073. 'inorder' => intval(DB::table('project_task')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  1074. 'indate' => Base::time(),
  1075. 'startdate' => Base::time(),
  1076. 'subtask' => Base::array2string([]),
  1077. 'files' => Base::array2string([]),
  1078. 'follower' => Base::array2string([]),
  1079. ];
  1080. return DB::transaction(function () use ($inArray) {
  1081. $taskid = DB::table('project_task')->insertGetId($inArray);
  1082. if (empty($taskid)) {
  1083. return Base::retError('系统繁忙,请稍后再试!');
  1084. }
  1085. DB::table('project_log')->insert([
  1086. 'type' => '日志',
  1087. 'projectid' => $inArray['projectid'],
  1088. 'taskid' => $taskid,
  1089. 'username' => $inArray['createuser'],
  1090. 'detail' => '添加任务',
  1091. 'indate' => Base::time(),
  1092. 'other' => Base::array2string([
  1093. 'type' => 'task',
  1094. 'id' => $taskid,
  1095. 'title' => $inArray['title'],
  1096. ])
  1097. ]);
  1098. Project::updateNum($inArray['projectid']);
  1099. //
  1100. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1101. $task['overdue'] = Project::taskIsOverdue($task);
  1102. $task = array_merge($task, Users::username2basic($task['username']));
  1103. return Base::retSuccess('添加成功!', $task);
  1104. });
  1105. }
  1106. /**
  1107. * 项目任务-归档、取消归档
  1108. *
  1109. * @apiParam {String} act
  1110. * - cancel: 取消归档
  1111. * - else: 加入归档
  1112. * @apiParam {Number} taskid 任务ID
  1113. */
  1114. public function task__archived()
  1115. {
  1116. $user = Users::authE();
  1117. if (Base::isError($user)) {
  1118. return $user;
  1119. } else {
  1120. $user = $user['data'];
  1121. }
  1122. //
  1123. $taskid = intval(Request::input('taskid'));
  1124. $task = Base::DBC2A(DB::table('project_lists')
  1125. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  1126. ->select(['project_task.projectid', 'project_task.title', 'project_task.archived'])
  1127. ->where([
  1128. ['project_lists.delete', '=', 0],
  1129. ['project_task.delete', '=', 0],
  1130. ['project_task.id', '=', $taskid],
  1131. ])
  1132. ->first());
  1133. if (empty($task)) {
  1134. return Base::retError('任务不存在!');
  1135. }
  1136. $inRes = Project::inThe($task['projectid'], $user['username']);
  1137. if (Base::isError($inRes)) {
  1138. return $inRes;
  1139. }
  1140. //
  1141. switch (Request::input('act')) {
  1142. case 'cancel': {
  1143. if ($task['archived'] == 0) {
  1144. return Base::retError('任务未归档!');
  1145. }
  1146. DB::table('project_task')->where('id', $taskid)->update([
  1147. 'archived' => 1,
  1148. 'archiveddate' => Base::time()
  1149. ]);
  1150. DB::table('project_log')->insert([
  1151. 'type' => '日志',
  1152. 'projectid' => $task['projectid'],
  1153. 'taskid' => $taskid,
  1154. 'username' => $user['username'],
  1155. 'detail' => '取消归档',
  1156. 'indate' => Base::time(),
  1157. 'other' => Base::array2string([
  1158. 'type' => 'task',
  1159. 'id' => $taskid,
  1160. 'title' => $task['title'],
  1161. ])
  1162. ]);
  1163. return Base::retSuccess('取消归档成功!');
  1164. }
  1165. default: {
  1166. if ($task['archived'] == 1) {
  1167. return Base::retError('任务已归档!');
  1168. }
  1169. DB::table('project_task')->where('id', $taskid)->update([
  1170. 'archived' => 0,
  1171. ]);
  1172. DB::table('project_log')->insert([
  1173. 'type' => '日志',
  1174. 'projectid' => $task['projectid'],
  1175. 'taskid' => $taskid,
  1176. 'username' => $user['username'],
  1177. 'detail' => '加入归档',
  1178. 'indate' => Base::time(),
  1179. 'other' => Base::array2string([
  1180. 'type' => 'task',
  1181. 'id' => $taskid,
  1182. 'title' => $task['title'],
  1183. ])
  1184. ]);
  1185. return Base::retSuccess('加入归档成功!');
  1186. }
  1187. }
  1188. }
  1189. /**
  1190. * 项目文件-列表
  1191. *
  1192. * @apiParam {Number} projectid 项目ID
  1193. * @apiParam {Number} [taskid] 任务ID
  1194. * @apiParam {String} [name] 文件名称
  1195. * @apiParam {String} [username] 上传者用户名
  1196. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  1197. * - key: name|size|username|indate
  1198. * - order: asc|desc
  1199. * @apiParam {Number} [page] 当前页,默认:1
  1200. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1201. */
  1202. public function files__lists()
  1203. {
  1204. $user = Users::authE();
  1205. if (Base::isError($user)) {
  1206. return $user;
  1207. } else {
  1208. $user = $user['data'];
  1209. }
  1210. //
  1211. $projectid = intval(Request::input('projectid'));
  1212. $inRes = Project::inThe($projectid, $user['username']);
  1213. if (Base::isError($inRes)) {
  1214. return $inRes;
  1215. }
  1216. //
  1217. $orderBy = '`id` DESC';
  1218. $sorts = Base::json2array(Request::input('sorts'));
  1219. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1220. switch ($sorts['key']) {
  1221. case 'name':
  1222. case 'size':
  1223. case 'download':
  1224. case 'username':
  1225. case 'indate':
  1226. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1227. break;
  1228. }
  1229. }
  1230. //
  1231. $whereArray = [];
  1232. $whereArray[] = ['projectid', '=', $projectid];
  1233. $whereArray[] = ['delete', '=', 0];
  1234. if (intval(Request::input('taskid')) > 0) {
  1235. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1236. }
  1237. if (trim(Request::input('name'))) {
  1238. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  1239. }
  1240. if (trim(Request::input('username'))) {
  1241. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1242. }
  1243. //
  1244. $lists = DB::table('project_files')
  1245. ->where($whereArray)
  1246. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1247. $lists = Base::getPageList($lists);
  1248. if ($lists['total'] == 0) {
  1249. return Base::retError('未找到任何相关的文件', $lists);
  1250. }
  1251. foreach ($lists['lists'] AS $key => $item) {
  1252. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  1253. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  1254. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  1255. }
  1256. return Base::retSuccess('success', $lists);
  1257. }
  1258. /**
  1259. * 项目文件-上传
  1260. *
  1261. * @apiParam {Number} projectid 项目ID
  1262. * @apiParam {Number} [taskid] 任务ID
  1263. */
  1264. public function files__upload()
  1265. {
  1266. $user = Users::authE();
  1267. if (Base::isError($user)) {
  1268. return $user;
  1269. } else {
  1270. $user = $user['data'];
  1271. }
  1272. //
  1273. $projectid = intval(Request::input('projectid'));
  1274. $taskid = intval(Request::input('taskid'));
  1275. $inRes = Project::inThe($projectid, $user['username']);
  1276. if (Base::isError($inRes)) {
  1277. return $inRes;
  1278. }
  1279. //
  1280. $data = Base::upload([
  1281. "file" => Request::file('files'),
  1282. "type" => 'file',
  1283. "path" => "uploads/projects/" . $projectid . "/",
  1284. ]);
  1285. if (Base::isError($data)) {
  1286. return Base::retError($data['msg']);
  1287. } else {
  1288. $fileData = $data['data'];
  1289. $thumb = 'images/files/file.png';
  1290. switch ($fileData['ext']) {
  1291. case "docx":
  1292. $thumb = 'images/files/doc.png';
  1293. break;
  1294. case "xlsx":
  1295. $thumb = 'images/files/xls.png';
  1296. break;
  1297. case "pptx":
  1298. $thumb = 'images/files/ppt.png';
  1299. break;
  1300. case "doc":
  1301. case "xls":
  1302. case "ppt":
  1303. case "txt":
  1304. case "esp":
  1305. case "gif":
  1306. $thumb = 'images/files/' . $fileData['ext'] . '.png';
  1307. break;
  1308. case "jpg":
  1309. case "jpeg":
  1310. case "png":
  1311. if (Base::imgThumb($fileData['file'], $fileData['file'] . "_thumb.jpg", 64, 0)) {
  1312. $thumb = $fileData['path'] . "_thumb.jpg";
  1313. }
  1314. break;
  1315. }
  1316. $array = [
  1317. 'projectid' => $projectid,
  1318. 'taskid' => $taskid,
  1319. 'name' => $fileData['name'],
  1320. 'size' => $fileData['size'] * 1024,
  1321. 'ext' => $fileData['ext'],
  1322. 'path' => $fileData['path'],
  1323. 'thumb' => $thumb,
  1324. 'username' => $user['username'],
  1325. 'indate' => Base::time(),
  1326. ];
  1327. $id = DB::table('project_files')->insertGetId($array);
  1328. $array['id'] = $id;
  1329. $array['path'] = Base::fillUrl($array['path']);
  1330. $array['thumb'] = Base::fillUrl($array['thumb']);
  1331. $array['download'] = 0;
  1332. $array['yetdown'] = 0;
  1333. DB::table('project_log')->insert([
  1334. 'type' => '日志',
  1335. 'projectid' => $projectid,
  1336. 'taskid' => $taskid,
  1337. 'username' => $user['username'],
  1338. 'detail' => '上传文件',
  1339. 'indate' => Base::time(),
  1340. 'other' => Base::array2string([
  1341. 'type' => 'file',
  1342. 'id' => $id,
  1343. 'name' => $fileData['name'],
  1344. ])
  1345. ]);
  1346. return Base::retSuccess('success', $array);
  1347. }
  1348. }
  1349. /**
  1350. * 项目文件-上传
  1351. *
  1352. * @apiParam {Number} fileid 文件ID
  1353. */
  1354. public function files__download()
  1355. {
  1356. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1357. if (empty($fileDetail)) {
  1358. return abort(404, '文件不存在或已被删除!');
  1359. }
  1360. $filePath = public_path($fileDetail['path']);
  1361. if (!file_exists($filePath)) {
  1362. return abort(404, '文件不存在或已被删除。');
  1363. }
  1364. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  1365. Session::put('filesDownload:' . $fileDetail['id'], 1);
  1366. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  1367. }
  1368. return response()->download($filePath, $fileDetail['name']);
  1369. }
  1370. /**
  1371. * 项目文件-重命名
  1372. *
  1373. * @apiParam {Number} fileid 文件ID
  1374. * @apiParam {String} name 新文件名称
  1375. */
  1376. public function files__rename()
  1377. {
  1378. $user = Users::authE();
  1379. if (Base::isError($user)) {
  1380. return $user;
  1381. } else {
  1382. $user = $user['data'];
  1383. }
  1384. //
  1385. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1386. if (empty($fileDetail)) {
  1387. return Base::retError('文件不存在或已被删除!');
  1388. }
  1389. if ($fileDetail['username'] != $user['username']) {
  1390. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1391. if (Base::isError($inRes)) {
  1392. return Base::retError('此操作仅支持管理员或上传者!');
  1393. }
  1394. }
  1395. //
  1396. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  1397. if (empty($name)) {
  1398. return Base::retError('文件名称不能为空!');
  1399. } elseif (mb_strlen($name) > 32) {
  1400. return Base::retError('文件名称最多只能设置32个字!');
  1401. }
  1402. //
  1403. $name .= '.' . $fileDetail['ext'];
  1404. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  1405. DB::table('project_log')->insert([
  1406. 'type' => '日志',
  1407. 'projectid' => $fileDetail['projectid'],
  1408. 'taskid' => $fileDetail['taskid'],
  1409. 'username' => $user['username'],
  1410. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  1411. 'indate' => Base::time(),
  1412. 'other' => Base::array2string([
  1413. 'type' => 'file',
  1414. 'id' => $fileDetail['id'],
  1415. 'name' => $name,
  1416. ])
  1417. ]);
  1418. }
  1419. //
  1420. return Base::retSuccess('修改成功!', [
  1421. 'name' => $name,
  1422. ]);
  1423. }
  1424. /**
  1425. * 项目文件-删除
  1426. *
  1427. * @apiParam {Number} fileid 文件ID
  1428. */
  1429. public function files__delete()
  1430. {
  1431. $user = Users::authE();
  1432. if (Base::isError($user)) {
  1433. return $user;
  1434. } else {
  1435. $user = $user['data'];
  1436. }
  1437. //
  1438. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1439. if (empty($fileDetail)) {
  1440. return Base::retError('文件不存在或已被删除!');
  1441. }
  1442. if ($fileDetail['username'] != $user['username']) {
  1443. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1444. if (Base::isError($inRes)) {
  1445. return Base::retError('此操作仅支持管理员或上传者!');
  1446. }
  1447. }
  1448. //
  1449. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  1450. 'delete' => 1,
  1451. 'deletedate' => Base::time()
  1452. ]);
  1453. DB::table('project_log')->insert([
  1454. 'type' => '日志',
  1455. 'projectid' => $fileDetail['projectid'],
  1456. 'taskid' => $fileDetail['taskid'],
  1457. 'username' => $user['username'],
  1458. 'detail' => '删除文件',
  1459. 'indate' => Base::time(),
  1460. 'other' => Base::array2string([
  1461. 'type' => 'file',
  1462. 'id' => $fileDetail['id'],
  1463. 'name' => $fileDetail['name'],
  1464. ])
  1465. ]);
  1466. //
  1467. return Base::retSuccess('删除成功!');
  1468. }
  1469. /**
  1470. * 项目动态-列表
  1471. *
  1472. * @apiParam {Number} projectid 项目ID
  1473. * @apiParam {Number} [taskid] 任务ID
  1474. * @apiParam {String} [username] 用户名
  1475. * @apiParam {Number} [page] 当前页,默认:1
  1476. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1477. */
  1478. public function log__lists()
  1479. {
  1480. $user = Users::authE();
  1481. if (Base::isError($user)) {
  1482. return $user;
  1483. } else {
  1484. $user = $user['data'];
  1485. }
  1486. //
  1487. $projectid = intval(Request::input('projectid'));
  1488. $inRes = Project::inThe($projectid, $user['username']);
  1489. if (Base::isError($inRes)) {
  1490. return $inRes;
  1491. }
  1492. //
  1493. $whereArray = [];
  1494. $whereArray[] = ['projectid', '=', $projectid];
  1495. if (intval(Request::input('taskid')) > 0) {
  1496. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1497. }
  1498. if (trim(Request::input('username'))) {
  1499. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1500. }
  1501. //
  1502. $lists = DB::table('project_log')
  1503. ->where($whereArray)
  1504. ->orderByDesc('indate')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1505. $lists = Base::getPageList($lists);
  1506. if ($lists['total'] == 0) {
  1507. return Base::retError('未找到任何相关的记录', $lists);
  1508. }
  1509. foreach ($lists['lists'] AS $key => $item) {
  1510. $item = array_merge($item, Users::username2basic($item['username']));
  1511. $item['timeData'] = [
  1512. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  1513. 'hi' => date("h:i", $item['indate']) ,
  1514. 'week' => "周" . Base::getTimeWeek($item['indate']),
  1515. 'segment' => Base::getTimeDayeSegment($item['indate']),
  1516. ];
  1517. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  1518. $lists['lists'][$key] = $item;
  1519. }
  1520. return Base::retSuccess('success', $lists);
  1521. }
  1522. }