ProjectController.php 79 KB

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