ProjectController.php 77 KB

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