ProjectController.php 78 KB

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