ProjectController.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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 {Number} [labelid] 项目子分类ID
  859. * @apiParam {String} [archived] 是否归档
  860. * - 未归档 (默认)
  861. * - 已归档
  862. * - 全部
  863. * @apiParam {String} [type] 任务类型
  864. * - 全部(默认)
  865. * - 未完成
  866. * - 已超期
  867. * - 已完成
  868. * @apiParam {String} [username] 负责人用户名
  869. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  870. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}("archived=已归档"时无效)
  871. * - key: title|labelid|enddate|username|level|indate|type
  872. * - order: asc|desc
  873. * @apiParam {Number} [page] 当前页,默认:1
  874. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  875. */
  876. public function task__lists()
  877. {
  878. $user = Users::authE();
  879. if (Base::isError($user)) {
  880. return $user;
  881. } else {
  882. $user = $user['data'];
  883. }
  884. //
  885. $projectid = intval(Request::input('projectid'));
  886. $inRes = Project::inThe($projectid, $user['username']);
  887. if (Base::isError($inRes)) {
  888. return $inRes;
  889. }
  890. //
  891. $orderBy = '`inorder` DESC,`id` DESC';
  892. if (intval(Request::input('labelid')) == 0) {
  893. $orderBy = '`indate` DESC,`id` DESC';
  894. }
  895. $sorts = Base::json2array(Request::input('sorts'));
  896. if (in_array($sorts['order'], ['asc', 'desc'])) {
  897. switch ($sorts['key']) {
  898. case 'title':
  899. case 'labelid':
  900. case 'enddate':
  901. case 'username':
  902. case 'level':
  903. case 'indate':
  904. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  905. break;
  906. case 'type':
  907. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['order'] . ', `complete` ' . $sorts['order'] . ',`id` DESC';
  908. break;
  909. }
  910. }
  911. //
  912. $whereArray = [];
  913. $whereArray[] = ['project_lists.id', '=', $projectid];
  914. $whereArray[] = ['project_lists.delete', '=', 0];
  915. $whereArray[] = ['project_task.delete', '=', 0];
  916. if (intval(Request::input('labelid')) > 0) {
  917. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  918. }
  919. if (intval(Request::input('level')) > 0) {
  920. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  921. }
  922. if (trim(Request::input('username'))) {
  923. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  924. }
  925. $archived = trim(Request::input('archived'));
  926. if (empty($archived)) $archived = "未归档";
  927. switch ($archived) {
  928. case '已归档':
  929. $whereArray[] = ['project_task.archived', '=', 1];
  930. $orderBy = '`archiveddate` DESC';
  931. break;
  932. case '未归档':
  933. $whereArray[] = ['project_task.archived', '=', 0];
  934. break;
  935. }
  936. $type = trim(Request::input('type'));
  937. switch ($type) {
  938. case '未完成':
  939. $whereArray[] = ['project_task.complete', '=', 0];
  940. break;
  941. case '已超期':
  942. $whereArray[] = ['project_task.complete', '=', 0];
  943. $whereArray[] = ['project_task.enddate', '>', 0];
  944. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  945. break;
  946. case '已完成':
  947. $whereArray[] = ['project_task.complete', '=', 1];
  948. break;
  949. }
  950. //
  951. $lists = DB::table('project_lists')
  952. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  953. ->select(['project_task.*'])
  954. ->where($whereArray)
  955. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  956. $lists = Base::getPageList($lists);
  957. if (intval(Request::input('statistics')) == 1) {
  958. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->count();
  959. $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();
  960. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 1)->count();
  961. }
  962. if ($lists['total'] == 0) {
  963. return Base::retError('未找到任何相关的任务', $lists);
  964. }
  965. foreach ($lists['lists'] AS $key => $info) {
  966. $info['overdue'] = Project::taskIsOverdue($info);
  967. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  968. }
  969. return Base::retSuccess('success', $lists);
  970. }
  971. /**
  972. * 项目任务-添加任务
  973. *
  974. * @apiParam {Number} projectid 项目ID
  975. * @apiParam {Number} labelid 项目子分类ID
  976. * @apiParam {String} title 任务标题
  977. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  978. * @apiParam {String} [username] 任务负责人用户名
  979. * - 0: 未归档
  980. * - 1: 已归档
  981. *
  982. * @throws \Throwable
  983. */
  984. public function task__add()
  985. {
  986. $user = Users::authE();
  987. if (Base::isError($user)) {
  988. return $user;
  989. } else {
  990. $user = $user['data'];
  991. }
  992. //
  993. $projectid = intval(Request::input('projectid'));
  994. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  995. if (empty($projectDetail)) {
  996. return Base::retError('项目不存在或已被删除!');
  997. }
  998. //
  999. $labelid = intval(Request::input('labelid'));
  1000. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1001. if (empty($labelDetail)) {
  1002. return Base::retError('项目子分类不存在或已被删除!');
  1003. }
  1004. //
  1005. $inRes = Project::inThe($projectid, $user['username']);
  1006. if (Base::isError($inRes)) {
  1007. return $inRes;
  1008. }
  1009. //
  1010. $username = trim(Request::input('username'));
  1011. if (empty($username)) {
  1012. $username = $user['username'];
  1013. }
  1014. if ($username != $user['username']) {
  1015. $inRes = Project::inThe($projectid, $username);
  1016. if (Base::isError($inRes)) {
  1017. return Base::retError('负责人不在项目成员内!');
  1018. }
  1019. }
  1020. //
  1021. $title = trim(Request::input('title'));
  1022. if (empty($title)) {
  1023. return Base::retError('任务标题不能为空!');
  1024. } elseif (mb_strlen($title) > 255) {
  1025. return Base::retError('任务标题最多只能设置255个字!');
  1026. }
  1027. //
  1028. $inArray = [
  1029. 'projectid' => $projectid,
  1030. 'labelid' => $labelid,
  1031. 'createuser' => $user['username'],
  1032. 'username' => $username,
  1033. 'title' => $title,
  1034. 'level' => max(1, min(4, intval(Request::input('level')))),
  1035. 'inorder' => intval(DB::table('project_task')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  1036. 'indate' => Base::time(),
  1037. 'subtask' => Base::array2string([]),
  1038. 'files' => Base::array2string([]),
  1039. 'follower' => Base::array2string([]),
  1040. ];
  1041. return DB::transaction(function () use ($inArray) {
  1042. $taskid = DB::table('project_task')->insertGetId($inArray);
  1043. if (empty($taskid)) {
  1044. return Base::retError('系统繁忙,请稍后再试!');
  1045. }
  1046. DB::table('project_log')->insert([
  1047. 'type' => '日志',
  1048. 'projectid' => $inArray['projectid'],
  1049. 'taskid' => $taskid,
  1050. 'username' => $inArray['createuser'],
  1051. 'detail' => '添加任务',
  1052. 'indate' => Base::time(),
  1053. 'other' => Base::array2string([
  1054. 'type' => 'task',
  1055. 'id' => $taskid,
  1056. 'title' => $inArray['title'],
  1057. ])
  1058. ]);
  1059. Project::updateNum($inArray['projectid']);
  1060. //
  1061. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1062. $task['overdue'] = Project::taskIsOverdue($task);
  1063. $task = array_merge($task, Users::username2basic($task['username']));
  1064. return Base::retSuccess('添加成功!', $task);
  1065. });
  1066. }
  1067. /**
  1068. * 项目任务-归档、取消归档
  1069. *
  1070. * @apiParam {String} act
  1071. * - cancel: 取消归档
  1072. * - else: 加入归档
  1073. * @apiParam {Number} taskid 任务ID
  1074. */
  1075. public function task__archived()
  1076. {
  1077. $user = Users::authE();
  1078. if (Base::isError($user)) {
  1079. return $user;
  1080. } else {
  1081. $user = $user['data'];
  1082. }
  1083. //
  1084. $taskid = intval(Request::input('taskid'));
  1085. $task = Base::DBC2A(DB::table('project_lists')
  1086. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  1087. ->select(['project_task.projectid', 'project_task.title', 'project_task.archived'])
  1088. ->where([
  1089. ['project_lists.delete', '=', 0],
  1090. ['project_task.delete', '=', 0],
  1091. ['project_task.id', '=', $taskid],
  1092. ])
  1093. ->first());
  1094. if (empty($task)) {
  1095. return Base::retError('任务不存在!');
  1096. }
  1097. $inRes = Project::inThe($task['projectid'], $user['username']);
  1098. if (Base::isError($inRes)) {
  1099. return $inRes;
  1100. }
  1101. //
  1102. switch (Request::input('act')) {
  1103. case 'cancel': {
  1104. if ($task['archived'] == 0) {
  1105. return Base::retError('任务未归档!');
  1106. }
  1107. DB::table('project_task')->where('id', $taskid)->update([
  1108. 'archived' => 1,
  1109. 'archiveddate' => Base::time()
  1110. ]);
  1111. DB::table('project_log')->insert([
  1112. 'type' => '日志',
  1113. 'projectid' => $task['projectid'],
  1114. 'taskid' => $taskid,
  1115. 'username' => $user['username'],
  1116. 'detail' => '取消归档',
  1117. 'indate' => Base::time(),
  1118. 'other' => Base::array2string([
  1119. 'type' => 'task',
  1120. 'id' => $taskid,
  1121. 'title' => $task['title'],
  1122. ])
  1123. ]);
  1124. return Base::retSuccess('取消归档成功!');
  1125. }
  1126. default: {
  1127. if ($task['archived'] == 1) {
  1128. return Base::retError('任务已归档!');
  1129. }
  1130. DB::table('project_task')->where('id', $taskid)->update([
  1131. 'archived' => 0,
  1132. ]);
  1133. DB::table('project_log')->insert([
  1134. 'type' => '日志',
  1135. 'projectid' => $task['projectid'],
  1136. 'taskid' => $taskid,
  1137. 'username' => $user['username'],
  1138. 'detail' => '加入归档',
  1139. 'indate' => Base::time(),
  1140. 'other' => Base::array2string([
  1141. 'type' => 'task',
  1142. 'id' => $taskid,
  1143. 'title' => $task['title'],
  1144. ])
  1145. ]);
  1146. return Base::retSuccess('加入归档成功!');
  1147. }
  1148. }
  1149. }
  1150. /**
  1151. * 项目文件-列表
  1152. *
  1153. * @apiParam {Number} projectid 项目ID
  1154. * @apiParam {Number} [taskid] 任务ID
  1155. * @apiParam {String} [name] 文件名称
  1156. * @apiParam {String} [username] 上传者用户名
  1157. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  1158. * - key: name|size|username|indate
  1159. * - order: asc|desc
  1160. * @apiParam {Number} [page] 当前页,默认:1
  1161. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1162. */
  1163. public function files__lists()
  1164. {
  1165. $user = Users::authE();
  1166. if (Base::isError($user)) {
  1167. return $user;
  1168. } else {
  1169. $user = $user['data'];
  1170. }
  1171. //
  1172. $projectid = intval(Request::input('projectid'));
  1173. $inRes = Project::inThe($projectid, $user['username']);
  1174. if (Base::isError($inRes)) {
  1175. return $inRes;
  1176. }
  1177. //
  1178. $orderBy = '`id` DESC';
  1179. $sorts = Base::json2array(Request::input('sorts'));
  1180. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1181. switch ($sorts['key']) {
  1182. case 'name':
  1183. case 'size':
  1184. case 'download':
  1185. case 'username':
  1186. case 'indate':
  1187. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1188. break;
  1189. }
  1190. }
  1191. //
  1192. $whereArray = [];
  1193. $whereArray[] = ['projectid', '=', $projectid];
  1194. $whereArray[] = ['delete', '=', 0];
  1195. if (intval(Request::input('taskid')) > 0) {
  1196. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1197. }
  1198. if (trim(Request::input('name'))) {
  1199. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  1200. }
  1201. if (trim(Request::input('username'))) {
  1202. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1203. }
  1204. //
  1205. $lists = DB::table('project_files')
  1206. ->where($whereArray)
  1207. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1208. $lists = Base::getPageList($lists);
  1209. if ($lists['total'] == 0) {
  1210. return Base::retError('未找到任何相关的文件', $lists);
  1211. }
  1212. foreach ($lists['lists'] AS $key => $item) {
  1213. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  1214. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  1215. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  1216. }
  1217. return Base::retSuccess('success', $lists);
  1218. }
  1219. /**
  1220. * 项目文件-上传
  1221. *
  1222. * @apiParam {Number} projectid 项目ID
  1223. * @apiParam {Number} [taskid] 任务ID
  1224. */
  1225. public function files__upload()
  1226. {
  1227. $user = Users::authE();
  1228. if (Base::isError($user)) {
  1229. return $user;
  1230. } else {
  1231. $user = $user['data'];
  1232. }
  1233. //
  1234. $projectid = intval(Request::input('projectid'));
  1235. $taskid = intval(Request::input('taskid'));
  1236. $inRes = Project::inThe($projectid, $user['username']);
  1237. if (Base::isError($inRes)) {
  1238. return $inRes;
  1239. }
  1240. //
  1241. $data = Base::upload([
  1242. "file" => Request::file('files'),
  1243. "type" => 'file',
  1244. "path" => "uploads/projects/" . $projectid . "/",
  1245. ]);
  1246. if (Base::isError($data)) {
  1247. return Base::retError($data['msg']);
  1248. } else {
  1249. $fileData = $data['data'];
  1250. $thumb = 'images/files/file.png';
  1251. switch ($fileData['ext']) {
  1252. case "docx":
  1253. $thumb = 'images/files/doc.png';
  1254. break;
  1255. case "xlsx":
  1256. $thumb = 'images/files/xls.png';
  1257. break;
  1258. case "pptx":
  1259. $thumb = 'images/files/ppt.png';
  1260. break;
  1261. case "doc":
  1262. case "xls":
  1263. case "ppt":
  1264. case "txt":
  1265. case "esp":
  1266. case "gif":
  1267. $thumb = 'images/files/' . $fileData['ext'] . '.png';
  1268. break;
  1269. case "jpg":
  1270. case "jpeg":
  1271. case "png":
  1272. if (Base::imgThumb($fileData['file'], $fileData['file'] . "_thumb.jpg", 64, 0)) {
  1273. $thumb = $fileData['path'] . "_thumb.jpg";
  1274. }
  1275. break;
  1276. }
  1277. $array = [
  1278. 'projectid' => $projectid,
  1279. 'taskid' => $taskid,
  1280. 'name' => $fileData['name'],
  1281. 'size' => $fileData['size'] * 1024,
  1282. 'ext' => $fileData['ext'],
  1283. 'path' => $fileData['path'],
  1284. 'thumb' => $thumb,
  1285. 'username' => $user['username'],
  1286. 'indate' => Base::time(),
  1287. ];
  1288. $id = DB::table('project_files')->insertGetId($array);
  1289. $array['id'] = $id;
  1290. $array['path'] = Base::fillUrl($array['path']);
  1291. $array['thumb'] = Base::fillUrl($array['thumb']);
  1292. $array['download'] = 0;
  1293. $array['yetdown'] = 0;
  1294. DB::table('project_log')->insert([
  1295. 'type' => '日志',
  1296. 'projectid' => $projectid,
  1297. 'taskid' => $taskid,
  1298. 'username' => $user['username'],
  1299. 'detail' => '上传文件',
  1300. 'indate' => Base::time(),
  1301. 'other' => Base::array2string([
  1302. 'type' => 'file',
  1303. 'id' => $id,
  1304. 'name' => $fileData['name'],
  1305. ])
  1306. ]);
  1307. return Base::retSuccess('success', $array);
  1308. }
  1309. }
  1310. /**
  1311. * 项目文件-上传
  1312. *
  1313. * @apiParam {Number} fileid 文件ID
  1314. */
  1315. public function files__download()
  1316. {
  1317. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1318. if (empty($fileDetail)) {
  1319. return abort(404, '文件不存在或已被删除!');
  1320. }
  1321. $filePath = public_path($fileDetail['path']);
  1322. if (!file_exists($filePath)) {
  1323. return abort(404, '文件不存在或已被删除。');
  1324. }
  1325. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  1326. Session::put('filesDownload:' . $fileDetail['id'], 1);
  1327. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  1328. }
  1329. return response()->download($filePath, $fileDetail['name']);
  1330. }
  1331. /**
  1332. * 项目文件-重命名
  1333. *
  1334. * @apiParam {Number} fileid 文件ID
  1335. * @apiParam {String} name 新文件名称
  1336. */
  1337. public function files__rename()
  1338. {
  1339. $user = Users::authE();
  1340. if (Base::isError($user)) {
  1341. return $user;
  1342. } else {
  1343. $user = $user['data'];
  1344. }
  1345. //
  1346. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1347. if (empty($fileDetail)) {
  1348. return Base::retError('文件不存在或已被删除!');
  1349. }
  1350. if ($fileDetail['username'] != $user['username']) {
  1351. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1352. if (Base::isError($inRes)) {
  1353. return Base::retError('此操作仅支持管理员或上传者!');
  1354. }
  1355. }
  1356. //
  1357. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  1358. if (empty($name)) {
  1359. return Base::retError('文件名称不能为空!');
  1360. } elseif (mb_strlen($name) > 32) {
  1361. return Base::retError('文件名称最多只能设置32个字!');
  1362. }
  1363. //
  1364. $name .= '.' . $fileDetail['ext'];
  1365. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  1366. DB::table('project_log')->insert([
  1367. 'type' => '日志',
  1368. 'projectid' => $fileDetail['projectid'],
  1369. 'taskid' => $fileDetail['taskid'],
  1370. 'username' => $user['username'],
  1371. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  1372. 'indate' => Base::time(),
  1373. 'other' => Base::array2string([
  1374. 'type' => 'file',
  1375. 'id' => $fileDetail['id'],
  1376. 'name' => $name,
  1377. ])
  1378. ]);
  1379. }
  1380. //
  1381. return Base::retSuccess('修改成功!', [
  1382. 'name' => $name,
  1383. ]);
  1384. }
  1385. /**
  1386. * 项目文件-删除
  1387. *
  1388. * @apiParam {Number} fileid 文件ID
  1389. */
  1390. public function files__delete()
  1391. {
  1392. $user = Users::authE();
  1393. if (Base::isError($user)) {
  1394. return $user;
  1395. } else {
  1396. $user = $user['data'];
  1397. }
  1398. //
  1399. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1400. if (empty($fileDetail)) {
  1401. return Base::retError('文件不存在或已被删除!');
  1402. }
  1403. if ($fileDetail['username'] != $user['username']) {
  1404. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1405. if (Base::isError($inRes)) {
  1406. return Base::retError('此操作仅支持管理员或上传者!');
  1407. }
  1408. }
  1409. //
  1410. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  1411. 'delete' => 1,
  1412. 'deletedate' => Base::time()
  1413. ]);
  1414. DB::table('project_log')->insert([
  1415. 'type' => '日志',
  1416. 'projectid' => $fileDetail['projectid'],
  1417. 'taskid' => $fileDetail['taskid'],
  1418. 'username' => $user['username'],
  1419. 'detail' => '删除文件',
  1420. 'indate' => Base::time(),
  1421. 'other' => Base::array2string([
  1422. 'type' => 'file',
  1423. 'id' => $fileDetail['id'],
  1424. 'name' => $fileDetail['name'],
  1425. ])
  1426. ]);
  1427. //
  1428. return Base::retSuccess('删除成功!');
  1429. }
  1430. /**
  1431. * 项目动态-列表
  1432. *
  1433. * @apiParam {Number} projectid 项目ID
  1434. * @apiParam {Number} [taskid] 任务ID
  1435. * @apiParam {String} [username] 用户名
  1436. * @apiParam {Number} [page] 当前页,默认:1
  1437. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1438. */
  1439. public function log__lists()
  1440. {
  1441. $user = Users::authE();
  1442. if (Base::isError($user)) {
  1443. return $user;
  1444. } else {
  1445. $user = $user['data'];
  1446. }
  1447. //
  1448. $projectid = intval(Request::input('projectid'));
  1449. $inRes = Project::inThe($projectid, $user['username']);
  1450. if (Base::isError($inRes)) {
  1451. return $inRes;
  1452. }
  1453. //
  1454. $whereArray = [];
  1455. $whereArray[] = ['projectid', '=', $projectid];
  1456. if (intval(Request::input('taskid')) > 0) {
  1457. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1458. }
  1459. if (trim(Request::input('username'))) {
  1460. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1461. }
  1462. //
  1463. $lists = DB::table('project_log')
  1464. ->where($whereArray)
  1465. ->orderByDesc('indate')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1466. $lists = Base::getPageList($lists);
  1467. if ($lists['total'] == 0) {
  1468. return Base::retError('未找到任何相关的记录', $lists);
  1469. }
  1470. foreach ($lists['lists'] AS $key => $item) {
  1471. $item = array_merge($item, Users::username2basic($item['username']));
  1472. $item['timeData'] = [
  1473. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  1474. 'hi' => date("h:i", $item['indate']) ,
  1475. 'week' => "周" . Base::getTimeWeek($item['indate']),
  1476. 'segment' => Base::getTimeDayeSegment($item['indate']),
  1477. ];
  1478. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  1479. $lists['lists'][$key] = $item;
  1480. }
  1481. return Base::retSuccess('success', $lists);
  1482. }
  1483. }