ProjectController.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Module\Base;
  5. use App\Module\Project;
  6. use App\Module\Users;
  7. use DB;
  8. use Request;
  9. use Session;
  10. /**
  11. * @apiDefine project
  12. *
  13. * 项目
  14. */
  15. class ProjectController extends Controller
  16. {
  17. public function __invoke($method, $action = '')
  18. {
  19. $app = $method ? $method : 'main';
  20. if ($action) {
  21. $app .= "__" . $action;
  22. }
  23. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  24. }
  25. /**
  26. * 项目列表
  27. *
  28. * @apiParam {String} act 类型
  29. * - join: 加入的项目(默认)
  30. * - favor: 收藏的项目
  31. * - manage: 管理的项目
  32. * @apiParam {Number} [page] 当前页,默认:1
  33. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  34. */
  35. public function lists()
  36. {
  37. $user = Users::authE();
  38. if (Base::isError($user)) {
  39. return $user;
  40. } else {
  41. $user = $user['data'];
  42. }
  43. //
  44. $whereArray = [];
  45. $whereArray[] = ['project_lists.delete', '=', 0];
  46. $whereArray[] = ['project_users.username', '=', $user['username']];
  47. switch (Request::input('act')) {
  48. case "favor": {
  49. $whereArray[] = ['project_users.type', '=', '收藏'];
  50. break;
  51. }
  52. case "manage": {
  53. $whereArray[] = ['project_users.type', '=', '成员'];
  54. $whereArray[] = ['project_users.isowner', '=', 1];
  55. break;
  56. }
  57. default: {
  58. $whereArray[] = ['project_users.type', '=', '成员'];
  59. break;
  60. }
  61. }
  62. $lists = DB::table('project_lists')
  63. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  64. ->select(['project_lists.*', 'project_users.isowner', 'project_users.indate as uindate'])
  65. ->where($whereArray)
  66. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  67. $lists = Base::getPageList($lists);
  68. if ($lists['total'] == 0) {
  69. return Base::retError('未找到任何相关的项目');
  70. }
  71. return Base::retSuccess('success', $lists);
  72. }
  73. /**
  74. * 项目详情
  75. *
  76. * @apiParam {Number} projectid 项目ID
  77. */
  78. public function detail()
  79. {
  80. $user = Users::authE();
  81. if (Base::isError($user)) {
  82. return $user;
  83. } else {
  84. $user = $user['data'];
  85. }
  86. //
  87. $projectid = trim(Request::input('projectid'));
  88. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  89. if (empty($projectDetail)) {
  90. return Base::retError('项目不存在或已被删除!');
  91. }
  92. $inRes = Project::inThe($projectid, $user['username']);
  93. if (Base::isError($inRes)) {
  94. return $inRes;
  95. }
  96. //子分类
  97. $label = Base::DBC2A(DB::table('project_label')->where('projectid', $projectid)->orderBy('inorder')->orderBy('id')->get());
  98. $simpleLabel = [];
  99. //任务
  100. $task = Base::DBC2A(DB::table('project_task')->where([ 'projectid' => $projectid, 'delete' => 0, 'complete' => 0 ])->orderByDesc('inorder')->orderByDesc('id')->get());
  101. //任务归类
  102. foreach ($label AS $index => $temp) {
  103. $taskLists = [];
  104. foreach ($task AS $info) {
  105. if ($temp['id'] == $info['labelid']) {
  106. $info['overdue'] = Project::taskIsOverdue($info);
  107. $taskLists[] = array_merge($info, Users::username2basic($info['username']));
  108. }
  109. }
  110. $label[$index]['taskLists'] = $taskLists;
  111. $simpleLabel[] = ['id' => $temp['id'], 'title' => $temp['title']];
  112. }
  113. //
  114. return Base::retSuccess('success', [
  115. 'project' => $projectDetail,
  116. 'label' => $label,
  117. 'simpleLabel' => $simpleLabel,
  118. ]);
  119. }
  120. /**
  121. * 添加项目
  122. *
  123. * @apiParam {String} title 项目名称
  124. * @apiParam {Array} labels 流程,格式[流程1, 流程2]
  125. */
  126. public function add()
  127. {
  128. $user = Users::authE();
  129. if (Base::isError($user)) {
  130. return $user;
  131. } else {
  132. $user = $user['data'];
  133. }
  134. //项目名称
  135. $title = trim(Request::input('title'));
  136. if (mb_strlen($title) < 2) {
  137. return Base::retError('项目名称不可以少于2个字!');
  138. } elseif (mb_strlen($title) > 32) {
  139. return Base::retError('项目名称最多只能设置32个字!');
  140. }
  141. //流程
  142. $labels = Request::input('labels');
  143. if (!is_array($labels)) $labels = [];
  144. $insertLabels = [];
  145. $inorder = 0;
  146. foreach ($labels AS $label) {
  147. $label = trim($label);
  148. if ($label) {
  149. $insertLabels[] = [
  150. 'title' => $label,
  151. 'inorder' => $inorder++,
  152. ];
  153. }
  154. }
  155. if (empty($insertLabels)) {
  156. $insertLabels[] = [
  157. 'title' => '默认',
  158. 'inorder' => 0,
  159. ];
  160. }
  161. //开始创建
  162. $projectid = DB::table('project_lists')->insertGetId([
  163. 'title' => $title,
  164. 'username' => $user['username'],
  165. 'createuser' => $user['username'],
  166. 'indate' => Base::time()
  167. ]);
  168. if ($projectid) {
  169. foreach ($insertLabels AS $key => $label) {
  170. $insertLabels[$key]['projectid'] = $projectid;
  171. }
  172. DB::table('project_label')->insert($insertLabels);
  173. DB::table('project_log')->insert([
  174. 'type' => '日志',
  175. 'projectid' => $projectid,
  176. 'username' => $user['username'],
  177. 'detail' => '创建项目',
  178. 'indate' => Base::time()
  179. ]);
  180. DB::table('project_users')->insert([
  181. 'type' => '成员',
  182. 'projectid' => $projectid,
  183. 'isowner' => 1,
  184. 'username' => $user['username'],
  185. 'indate' => Base::time()
  186. ]);
  187. return Base::retSuccess('添加成功!');
  188. } else {
  189. return Base::retError('添加失败!');
  190. }
  191. }
  192. /**
  193. * 收藏项目
  194. *
  195. * @apiParam {String} act 类型
  196. * - cancel: 取消收藏
  197. * - else: 添加收藏
  198. * @apiParam {Number} projectid 项目ID
  199. *
  200. * @throws \Throwable
  201. */
  202. public function favor()
  203. {
  204. $user = Users::authE();
  205. if (Base::isError($user)) {
  206. return $user;
  207. } else {
  208. $user = $user['data'];
  209. }
  210. //
  211. $projectid = trim(Request::input('projectid'));
  212. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  213. if (empty($projectDetail)) {
  214. return Base::retError('项目不存在或已被删除!');
  215. }
  216. return DB::transaction(function () use ($projectDetail, $user) {
  217. switch (Request::input('act')) {
  218. case 'cancel': {
  219. if (DB::table('project_users')->where([
  220. 'type' => '收藏',
  221. 'projectid' => $projectDetail['id'],
  222. 'username' => $user['username'],
  223. ])->delete()) {
  224. DB::table('project_log')->insert([
  225. 'type' => '日志',
  226. 'projectid' => $projectDetail['id'],
  227. 'username' => $user['username'],
  228. 'detail' => '取消收藏',
  229. 'indate' => Base::time()
  230. ]);
  231. return Base::retSuccess('取消成功!');
  232. }
  233. return Base::retSuccess('已取消!');
  234. }
  235. default: {
  236. $row = Base::DBC2A(DB::table('project_users')->where([
  237. 'type' => '收藏',
  238. 'projectid' => $projectDetail['id'],
  239. 'username' => $user['username'],
  240. ])->lockForUpdate()->first());
  241. if (empty($row)) {
  242. DB::table('project_users')->insert([
  243. 'type' => '收藏',
  244. 'projectid' => $projectDetail['id'],
  245. 'isowner' => $projectDetail['username'] == $user['username'] ? 1 : 0,
  246. 'username' => $user['username'],
  247. 'indate' => Base::time()
  248. ]);
  249. DB::table('project_log')->insert([
  250. 'type' => '日志',
  251. 'projectid' => $projectDetail['id'],
  252. 'username' => $user['username'],
  253. 'detail' => '收藏项目',
  254. 'indate' => Base::time()
  255. ]);
  256. return Base::retSuccess('收藏成功!');
  257. }
  258. return Base::retSuccess('已收藏!');
  259. }
  260. }
  261. });
  262. }
  263. /**
  264. * 重命名项目
  265. *
  266. * @apiParam {Number} projectid 项目ID
  267. * @apiParam {String} title 项目新名称
  268. */
  269. public function rename()
  270. {
  271. $user = Users::authE();
  272. if (Base::isError($user)) {
  273. return $user;
  274. } else {
  275. $user = $user['data'];
  276. }
  277. //
  278. $projectid = trim(Request::input('projectid'));
  279. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  280. if (empty($projectDetail)) {
  281. return Base::retError('项目不存在或已被删除!');
  282. }
  283. if ($projectDetail['username'] != $user['username']) {
  284. return Base::retError('你不是项目负责人!');
  285. }
  286. //
  287. $title = trim(Request::input('title'));
  288. if (mb_strlen($title) < 2) {
  289. return Base::retError('项目名称不可以少于2个字!');
  290. } elseif (mb_strlen($title) > 32) {
  291. return Base::retError('项目名称最多只能设置32个字!');
  292. }
  293. //
  294. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  295. 'title' => $title
  296. ]);
  297. DB::table('project_log')->insert([
  298. 'type' => '日志',
  299. 'projectid' => $projectDetail['id'],
  300. 'username' => $user['username'],
  301. 'detail' => '【' . $projectDetail['title'] . '】重命名【' . $title . '】',
  302. 'indate' => Base::time()
  303. ]);
  304. //
  305. return Base::retSuccess('修改成功!');
  306. }
  307. /**
  308. * 移交项目
  309. *
  310. * @apiParam {Number} projectid 项目ID
  311. * @apiParam {String} username 项目新负责人用户名
  312. *
  313. * @throws \Throwable
  314. */
  315. public function transfer()
  316. {
  317. $user = Users::authE();
  318. if (Base::isError($user)) {
  319. return $user;
  320. } else {
  321. $user = $user['data'];
  322. }
  323. //
  324. $projectid = trim(Request::input('projectid'));
  325. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  326. if (empty($projectDetail)) {
  327. return Base::retError('项目不存在或已被删除!');
  328. }
  329. if ($projectDetail['username'] != $user['username']) {
  330. return Base::retError('你不是项目负责人!');
  331. }
  332. //
  333. $username = trim(Request::input('username'));
  334. if ($username == $projectDetail['username']) {
  335. return Base::retError('你已是项目负责人!');
  336. }
  337. $count = DB::table('users')->where('username', $username)->count();
  338. if ($count <= 0) {
  339. return Base::retError('成员用户名(' . $username . ')不存在!');
  340. }
  341. //判断是否已在项目成员内
  342. $inRes = Project::inThe($projectDetail['id'], $username);
  343. if (Base::isError($inRes)) {
  344. DB::table('project_users')->insert([
  345. 'type' => '成员',
  346. 'projectid' => $projectDetail['id'],
  347. 'isowner' => 0,
  348. 'username' => $username,
  349. 'indate' => Base::time()
  350. ]);
  351. DB::table('project_log')->insert([
  352. 'type' => '日志',
  353. 'projectid' => $projectDetail['id'],
  354. 'username' => $username,
  355. 'detail' => '自动加入项目',
  356. 'indate' => Base::time()
  357. ]);
  358. }
  359. //开始移交
  360. return DB::transaction(function () use ($user, $username, $projectDetail) {
  361. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  362. 'username' => $username
  363. ]);
  364. DB::table('project_log')->insert([
  365. 'type' => '日志',
  366. 'projectid' => $projectDetail['id'],
  367. 'username' => $user['username'],
  368. 'detail' => '【' . $projectDetail['username'] . '】移交给【' . $username . '】',
  369. 'indate' => Base::time()
  370. ]);
  371. DB::table('project_users')->where([
  372. 'projectid' => $projectDetail['id'],
  373. 'username' => $projectDetail['username'],
  374. ])->update([
  375. 'isowner' => 0
  376. ]);
  377. DB::table('project_users')->where([
  378. 'projectid' => $projectDetail['id'],
  379. 'username' => $username,
  380. ])->update([
  381. 'isowner' => 1
  382. ]);
  383. return Base::retSuccess('移交成功!');
  384. });
  385. }
  386. /**
  387. * 删除项目
  388. *
  389. * @apiParam {Number} projectid 项目ID
  390. */
  391. public function delete()
  392. {
  393. $user = Users::authE();
  394. if (Base::isError($user)) {
  395. return $user;
  396. } else {
  397. $user = $user['data'];
  398. }
  399. //
  400. $projectid = trim(Request::input('projectid'));
  401. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  402. if (empty($projectDetail)) {
  403. return Base::retError('项目不存在或已被删除!');
  404. }
  405. if ($projectDetail['username'] != $user['username']) {
  406. return Base::retError('你不是项目负责人!');
  407. }
  408. //
  409. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  410. 'delete' => 1,
  411. 'deletedate' => Base::time()
  412. ]);
  413. DB::table('project_log')->insert([
  414. 'type' => '日志',
  415. 'projectid' => $projectDetail['id'],
  416. 'username' => $user['username'],
  417. 'detail' => '删除项目',
  418. 'indate' => Base::time()
  419. ]);
  420. //
  421. return Base::retSuccess('删除成功!');
  422. }
  423. public function sort()
  424. {
  425. $user = Users::authE();
  426. if (Base::isError($user)) {
  427. return $user;
  428. } else {
  429. $user = $user['data'];
  430. }
  431. //
  432. $projectid = trim(Request::input('projectid'));
  433. $inRes = Project::inThe($projectid, $user['username']);
  434. if (Base::isError($inRes)) {
  435. return $inRes;
  436. }
  437. //
  438. $oldSort = explode(";", Request::input('oldsort'));
  439. $newSort = explode(";", Request::input('newsort'));
  440. if (count($oldSort) != count($newSort)) {
  441. return Base::retError('参数错误!');
  442. }
  443. if (intval(Request::input('label'))) {
  444. //排序分类
  445. foreach ($newSort AS $sort => $item) {
  446. list($newLabelid, $newTask) = explode(':', $item);
  447. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  448. if ($newLabelid != $oldLabelid) {
  449. DB::table('project_label')->where([
  450. 'id' => $newLabelid,
  451. 'projectid' => $projectid
  452. ])->update([
  453. 'inorder' => intval($sort)
  454. ]);
  455. }
  456. }
  457. $detail = '调整任务列表排序';
  458. $sortType = 'label';
  459. } else {
  460. //排序任务(调整任务归类)
  461. foreach ($newSort AS $sort => $item) {
  462. list($newLabelid, $newTask) = explode(':', $item);
  463. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  464. if ($newTask != $oldTask) {
  465. $newTask = explode('-', $newTask);
  466. $inorder = count($newTask);
  467. foreach ($newTask AS $taskid) {
  468. DB::table('project_task')->where([
  469. 'id' => $taskid,
  470. 'projectid' => $projectid
  471. ])->update([
  472. 'labelid' => $newLabelid,
  473. 'inorder' => $inorder
  474. ]);
  475. $inorder--;
  476. }
  477. }
  478. }
  479. $detail = '调整任务排序';
  480. $sortType = 'task';
  481. }
  482. //
  483. $row = Base::DBC2A(DB::table('project_log')->where([ 'type' => '日志', 'projectid' => $projectid ])->orderByDesc('id')->first());
  484. $continue = 1;
  485. if ($row && $row['username'] == $user['username'] && $row['indate'] + 300 > Base::time()) {
  486. $other = Base::string2array($row['other']);
  487. if ($other['sortType'] == $sortType) {
  488. $continue = intval($other['continue']) + 1;
  489. if ($continue <= 100) {
  490. DB::table('project_log')->where('id', $row['id'])->update([
  491. 'detail' => $detail . '(' . $continue . '次)',
  492. 'other' => Base::array2string([
  493. 'sortType' => $sortType,
  494. 'continue' => $continue,
  495. 'times' => $other['times'] . '|' . Base::time(),
  496. ])
  497. ]);
  498. }
  499. }
  500. }
  501. if ($continue == 1) {
  502. DB::table('project_log')->insert([
  503. 'type' => '日志',
  504. 'projectid' => $projectid,
  505. 'username' => $user['username'],
  506. 'detail' => $detail,
  507. 'indate' => Base::time(),
  508. 'other' => Base::array2string([
  509. 'sortType' => $sortType,
  510. 'continue' => $continue,
  511. 'times' => Base::time(),
  512. ])
  513. ]);
  514. }
  515. return Base::retSuccess('保存成功!');
  516. }
  517. /**
  518. * 退出项目
  519. *
  520. * @apiParam {Number} projectid 项目ID
  521. */
  522. public function out()
  523. {
  524. $user = Users::authE();
  525. if (Base::isError($user)) {
  526. return $user;
  527. } else {
  528. $user = $user['data'];
  529. }
  530. //
  531. $projectid = trim(Request::input('projectid'));
  532. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  533. if (empty($projectDetail)) {
  534. return Base::retError('项目不存在或已被删除!');
  535. }
  536. if ($projectDetail['username'] == $user['username']) {
  537. return Base::retError('你是项目负责人,不可退出项目!');
  538. }
  539. $inRes = Project::inThe($projectid, $user['username']);
  540. if (Base::isError($inRes)) {
  541. return $inRes;
  542. }
  543. //
  544. DB::table('project_users')->where([
  545. 'type' => '成员',
  546. 'projectid' => $projectDetail['id'],
  547. 'username' => $user['username'],
  548. ])->delete();
  549. DB::table('project_log')->insert([
  550. 'type' => '日志',
  551. 'projectid' => $projectDetail['id'],
  552. 'username' => $user['username'],
  553. 'detail' => '退出项目',
  554. 'indate' => Base::time()
  555. ]);
  556. //
  557. return Base::retSuccess('退出项目成功!');
  558. }
  559. /**
  560. * 项目成员-列表
  561. *
  562. * @apiParam {Number} projectid 项目ID
  563. * @apiParam {Number} [page] 当前页,默认:1
  564. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  565. */
  566. public function users__lists()
  567. {
  568. $user = Users::authE();
  569. if (Base::isError($user)) {
  570. return $user;
  571. } else {
  572. $user = $user['data'];
  573. }
  574. //
  575. $projectid = intval(Request::input('projectid'));
  576. $inRes = Project::inThe($projectid, $user['username']);
  577. if (Base::isError($inRes)) {
  578. return $inRes;
  579. }
  580. //
  581. $lists = DB::table('project_lists')
  582. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  583. ->select(['project_lists.title', 'project_users.*'])
  584. ->where([
  585. ['project_lists.id', $projectid],
  586. ['project_lists.delete', 0],
  587. ['project_users.type', '成员'],
  588. ])
  589. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  590. $lists = Base::getPageList($lists);
  591. if ($lists['total'] == 0) {
  592. return Base::retError('未找到任何相关的成员');
  593. }
  594. foreach ($lists['lists'] AS $key => $projectDetail) {
  595. $userInfo = Users::username2basic($projectDetail['username']);
  596. $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
  597. $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
  598. $lists['lists'][$key]['profession'] = $userInfo['profession'];
  599. }
  600. return Base::retSuccess('success', $lists);
  601. }
  602. /**
  603. * 项目成员-添加、删除
  604. *
  605. * @apiParam {String} act
  606. * - delete: 删除成员
  607. * - else: 添加成员
  608. * @apiParam {Number} projectid 项目ID
  609. * @apiParam {Array|String} username 用户名(或用户名组)
  610. */
  611. public function users__join()
  612. {
  613. $user = Users::authE();
  614. if (Base::isError($user)) {
  615. return $user;
  616. } else {
  617. $user = $user['data'];
  618. }
  619. //
  620. $projectid = trim(Request::input('projectid'));
  621. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  622. if (empty($projectDetail)) {
  623. return Base::retError('项目不存在或已被删除!');
  624. }
  625. if ($projectDetail['username'] != $user['username']) {
  626. return Base::retError('你是不是项目负责人!');
  627. }
  628. $usernames = Request::input('username');
  629. if (empty($usernames)) {
  630. return Base::retError('参数错误!');
  631. }
  632. if (!is_array($usernames)) {
  633. if (Base::strExists($usernames, ',')) {
  634. $usernames = explode(',', $usernames);
  635. } else {
  636. $usernames = [$usernames];
  637. }
  638. }
  639. //
  640. $logArray = [];
  641. foreach ($usernames AS $username) {
  642. $inRes = Project::inThe($projectid, $username);
  643. switch (Request::input('act')) {
  644. case 'delete': {
  645. if (!Base::isError($inRes) && $projectDetail['username'] != $username) {
  646. DB::table('project_users')->where([
  647. 'type' => '成员',
  648. 'projectid' => $projectid,
  649. 'username' => $username,
  650. ])->delete();
  651. $logArray[] = [
  652. 'type' => '日志',
  653. 'projectid' => $projectDetail['id'],
  654. 'username' => $user['username'],
  655. 'detail' => '将成员【' . $username . '】移出项目',
  656. 'indate' => Base::time()
  657. ];
  658. }
  659. break;
  660. }
  661. default: {
  662. if (Base::isError($inRes)) {
  663. DB::table('project_users')->insert([
  664. 'type' => '成员',
  665. 'projectid' => $projectid,
  666. 'isowner' => 0,
  667. 'username' => $username,
  668. 'indate' => Base::time()
  669. ]);
  670. $logArray[] = [
  671. 'type' => '日志',
  672. 'projectid' => $projectDetail['id'],
  673. 'username' => $username,
  674. 'detail' => '将成员【' . $username . '】加入项目',
  675. 'indate' => Base::time()
  676. ];
  677. }
  678. break;
  679. }
  680. }
  681. }
  682. return Base::retSuccess('操作完成!');
  683. }
  684. /**
  685. * 项目子分类-添加分类
  686. *
  687. * @apiParam {Number} projectid 项目ID
  688. * @apiParam {String} title 分类名称
  689. */
  690. public function label__add()
  691. {
  692. $user = Users::authE();
  693. if (Base::isError($user)) {
  694. return $user;
  695. } else {
  696. $user = $user['data'];
  697. }
  698. //
  699. $projectid = trim(Request::input('projectid'));
  700. $inRes = Project::inThe($projectid, $user['username']);
  701. if (Base::isError($inRes)) {
  702. return $inRes;
  703. }
  704. //
  705. $title = trim(Request::input('title'));
  706. if (empty($title)) {
  707. return Base::retError('列表名称不能为空!');
  708. } elseif (mb_strlen($title) > 32) {
  709. return Base::retError('列表名称最多只能设置32个字!');
  710. }
  711. //
  712. $count = DB::table('project_label')->where('projectid', $projectid)->where('title', $title)->count();
  713. if ($count > 0) {
  714. return Base::retError('列表名称已存在!');
  715. }
  716. //
  717. $id = DB::table('project_label')->insertGetId([
  718. 'projectid' => $projectid,
  719. 'title' => $title,
  720. 'inorder' => intval(DB::table('project_label')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  721. ]);
  722. if (empty($id)) {
  723. return Base::retError('系统繁忙,请稍后再试!');
  724. }
  725. DB::table('project_log')->insert([
  726. 'type' => '日志',
  727. 'projectid' => $projectid,
  728. 'username' => $user['username'],
  729. 'detail' => '添加任务列表【' . $title . '】',
  730. 'indate' => Base::time()
  731. ]);
  732. //
  733. $row = Base::DBC2A(DB::table('project_label')->where('id', $id)->first());
  734. $row['taskLists'] = [];
  735. return Base::retSuccess('添加成功!', $row);
  736. }
  737. /**
  738. * 项目子分类-重命名分类
  739. *
  740. * @apiParam {Number} projectid 项目ID
  741. * @apiParam {Number} labelid 分类ID
  742. * @apiParam {String} title 新分类名称
  743. */
  744. public function label__rename()
  745. {
  746. $user = Users::authE();
  747. if (Base::isError($user)) {
  748. return $user;
  749. } else {
  750. $user = $user['data'];
  751. }
  752. //
  753. $projectid = trim(Request::input('projectid'));
  754. $inRes = Project::inThe($projectid, $user['username']);
  755. if (Base::isError($inRes)) {
  756. return $inRes;
  757. }
  758. //
  759. $title = trim(Request::input('title'));
  760. if (empty($title)) {
  761. return Base::retError('列表名称不能为空!');
  762. } elseif (mb_strlen($title) > 32) {
  763. return Base::retError('列表名称最多只能设置32个字!');
  764. }
  765. //
  766. $labelid = intval(Request::input('labelid'));
  767. $count = DB::table('project_label')->where('id', '!=', $labelid)->where('projectid', $projectid)->where('title', $title)->count();
  768. if ($count > 0) {
  769. return Base::retError('列表名称已存在!');
  770. }
  771. //
  772. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  773. if (empty($labelDetail)) {
  774. return Base::retError('列表不存在或已被删除!');
  775. }
  776. //
  777. if (DB::table('project_label')->where('id', $labelDetail['id'])->update([ 'title' => $title ])) {
  778. DB::table('project_log')->insert([
  779. 'type' => '日志',
  780. 'projectid' => $projectid,
  781. 'username' => $user['username'],
  782. 'detail' => '任务列表【' . $labelDetail['title'] . '】重命名【' . $title . '】',
  783. 'indate' => Base::time()
  784. ]);
  785. }
  786. //
  787. return Base::retSuccess('修改成功!');
  788. }
  789. /**
  790. * 项目子分类-删除分类
  791. *
  792. * @apiParam {Number} projectid 项目ID
  793. * @apiParam {Number} labelid 分类ID
  794. *
  795. * @throws \Throwable
  796. */
  797. public function label__delete()
  798. {
  799. $user = Users::authE();
  800. if (Base::isError($user)) {
  801. return $user;
  802. } else {
  803. $user = $user['data'];
  804. }
  805. //
  806. $projectid = trim(Request::input('projectid'));
  807. $inRes = Project::inThe($projectid, $user['username']);
  808. if (Base::isError($inRes)) {
  809. return $inRes;
  810. }
  811. //
  812. $labelid = intval(Request::input('labelid'));
  813. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  814. if (empty($labelDetail)) {
  815. return Base::retError('列表不存在或已被删除!');
  816. }
  817. //
  818. return DB::transaction(function () use ($user, $projectid, $labelDetail) {
  819. $taskLists = Base::DBC2A(DB::table('project_task')->where('labelid', $labelDetail['id'])->get());
  820. $logArray = [];
  821. foreach ($taskLists AS $task) {
  822. $logArray[] = [
  823. 'type' => '日志',
  824. 'projectid' => $projectid,
  825. 'taskid' => $task['id'],
  826. 'username' => $user['username'],
  827. 'detail' => '删除列表任务',
  828. 'indate' => Base::time(),
  829. 'other' => Base::array2string([
  830. 'type' => 'task',
  831. 'id' => $task['id'],
  832. 'title' => $task['title'],
  833. ])
  834. ];
  835. }
  836. $logArray[] = [
  837. 'type' => '日志',
  838. 'projectid' => $projectid,
  839. 'taskid' => 0,
  840. 'username' => $user['username'],
  841. 'detail' => '删除任务列表【' . $labelDetail['title'] . '】',
  842. 'indate' => Base::time()
  843. ];
  844. DB::table('project_task')->where('labelid', $labelDetail['id'])->update([
  845. 'delete' => 1,
  846. 'deletedate' => Base::time()
  847. ]);
  848. DB::table('project_label')->where('id', $labelDetail['id'])->delete();
  849. DB::table('project_log')->insert($logArray);
  850. //
  851. return Base::retSuccess('删除成功!');
  852. });
  853. }
  854. /**
  855. * 项目任务-列表
  856. *
  857. * @apiParam {Number} [projectid] 项目ID
  858. * @apiParam {String} [username] 负责人用户名(如果项目ID为空时此参数无效只获取自己的任务)
  859. * @apiParam {Number} [labelid] 项目子分类ID
  860. * @apiParam {Number} [level] 任务等级(1~4)
  861. * @apiParam {String} [archived] 任务是否归档
  862. * - 未归档 (默认)
  863. * - 已归档
  864. * - 全部
  865. * @apiParam {String} [type] 任务类型
  866. * - 全部(默认)
  867. * - 未完成
  868. * - 已超期
  869. * - 已完成
  870. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  871. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}("archived=已归档"时无效)
  872. * - key: title|labelid|enddate|username|level|indate|type
  873. * - order: asc|desc
  874. * @apiParam {Number} [idlater] 获取数据ID之后的数据
  875. * @apiParam {Number} [page] 当前页,默认:1
  876. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  877. */
  878. public function task__lists()
  879. {
  880. $user = Users::authE();
  881. if (Base::isError($user)) {
  882. return $user;
  883. } else {
  884. $user = $user['data'];
  885. }
  886. //
  887. $projectid = intval(Request::input('projectid'));
  888. if ($projectid > 0) {
  889. $inRes = Project::inThe($projectid, $user['username']);
  890. if (Base::isError($inRes)) {
  891. return $inRes;
  892. }
  893. }
  894. //
  895. $orderBy = '`inorder` DESC,`id` DESC';
  896. if (intval(Request::input('labelid')) == 0) {
  897. $orderBy = '`indate` DESC,`id` DESC';
  898. }
  899. $sorts = Base::json2array(Request::input('sorts'));
  900. if (in_array($sorts['order'], ['asc', 'desc'])) {
  901. switch ($sorts['key']) {
  902. case 'title':
  903. case 'labelid':
  904. case 'enddate':
  905. case 'username':
  906. case 'level':
  907. case 'indate':
  908. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  909. break;
  910. case 'type':
  911. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['order'] . ', `complete` ' . $sorts['order'] . ',`id` DESC';
  912. break;
  913. }
  914. }
  915. //
  916. $whereArray = [];
  917. $whereArray[] = ['project_task.delete', '=', 0];
  918. if ($projectid > 0) {
  919. $whereArray[] = ['project_lists.id', '=', $projectid];
  920. $whereArray[] = ['project_lists.delete', '=', 0];
  921. if (trim(Request::input('username'))) {
  922. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  923. }
  924. } else {
  925. $whereArray[] = ['project_task.username', '=', $user['username']];
  926. }
  927. if (intval(Request::input('labelid')) > 0) {
  928. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  929. }
  930. if (intval(Request::input('level')) > 0) {
  931. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  932. }
  933. if (intval(Request::input('idlater')) > 0) {
  934. $whereArray[] = ['project_task.id', '<', intval(Request::input('idlater'))];
  935. }
  936. $archived = trim(Request::input('archived'));
  937. if (empty($archived)) $archived = "未归档";
  938. switch ($archived) {
  939. case '已归档':
  940. $whereArray[] = ['project_task.archived', '=', 1];
  941. $orderBy = '`archiveddate` DESC';
  942. break;
  943. case '未归档':
  944. $whereArray[] = ['project_task.archived', '=', 0];
  945. break;
  946. }
  947. $type = trim(Request::input('type'));
  948. switch ($type) {
  949. case '未完成':
  950. $whereArray[] = ['project_task.complete', '=', 0];
  951. break;
  952. case '已超期':
  953. $whereArray[] = ['project_task.complete', '=', 0];
  954. $whereArray[] = ['project_task.enddate', '>', 0];
  955. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  956. break;
  957. case '已完成':
  958. $whereArray[] = ['project_task.complete', '=', 1];
  959. break;
  960. }
  961. //
  962. $builder = DB::table('project_task');
  963. if ($projectid > 0) {
  964. $builder = $builder->join('project_lists', 'project_lists.id', '=', 'project_task.projectid');
  965. }
  966. $lists = $builder->select(['project_task.*'])
  967. ->where($whereArray)
  968. ->orderByRaw($orderBy)
  969. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  970. $lists = Base::getPageList($lists);
  971. if (intval(Request::input('statistics')) == 1) {
  972. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->count();
  973. $lists['statistics_overdue'] = $type === '已超期' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->whereBetween('enddate', [1, Base::time()])->count();
  974. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 1)->count();
  975. }
  976. if ($lists['total'] == 0) {
  977. return Base::retError('未找到任何相关的任务', $lists);
  978. }
  979. foreach ($lists['lists'] AS $key => $info) {
  980. $info['overdue'] = Project::taskIsOverdue($info);
  981. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  982. }
  983. return Base::retSuccess('success', $lists);
  984. }
  985. /**
  986. * 项目任务-添加任务
  987. *
  988. * @apiParam {String} title 任务标题
  989. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  990. * @apiParam {String} [username] 任务负责人用户名(如果项目ID为空时此参数无效复制人为自己)
  991. * @apiParam {Number} [projectid] 项目ID
  992. * @apiParam {Number} [labelid] 项目子分类ID
  993. *
  994. * @throws \Throwable
  995. */
  996. public function task__add()
  997. {
  998. $user = Users::authE();
  999. if (Base::isError($user)) {
  1000. return $user;
  1001. } else {
  1002. $user = $user['data'];
  1003. }
  1004. //
  1005. $projectid = intval(Request::input('projectid'));
  1006. $labelid = intval(Request::input('labelid'));
  1007. if ($projectid > 0) {
  1008. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  1009. if (empty($projectDetail)) {
  1010. return Base::retError('项目不存在或已被删除!');
  1011. }
  1012. //
  1013. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1014. if (empty($labelDetail)) {
  1015. return Base::retError('项目子分类不存在或已被删除!');
  1016. }
  1017. //
  1018. $inRes = Project::inThe($projectid, $user['username']);
  1019. if (Base::isError($inRes)) {
  1020. return $inRes;
  1021. }
  1022. //
  1023. $username = trim(Request::input('username'));
  1024. if (empty($username)) {
  1025. $username = $user['username'];
  1026. }
  1027. if ($username != $user['username']) {
  1028. $inRes = Project::inThe($projectid, $username);
  1029. if (Base::isError($inRes)) {
  1030. return Base::retError('负责人不在项目成员内!');
  1031. }
  1032. }
  1033. } else {
  1034. $username = $user['username'];
  1035. }
  1036. //
  1037. $title = trim(Request::input('title'));
  1038. if (empty($title)) {
  1039. return Base::retError('任务标题不能为空!');
  1040. } elseif (mb_strlen($title) > 255) {
  1041. return Base::retError('任务标题最多只能设置255个字!');
  1042. }
  1043. //
  1044. $inArray = [
  1045. 'projectid' => $projectid,
  1046. 'labelid' => $labelid,
  1047. 'createuser' => $user['username'],
  1048. 'username' => $username,
  1049. 'title' => $title,
  1050. 'level' => max(1, min(4, intval(Request::input('level')))),
  1051. 'inorder' => intval(DB::table('project_task')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  1052. 'indate' => Base::time(),
  1053. 'subtask' => Base::array2string([]),
  1054. 'files' => Base::array2string([]),
  1055. 'follower' => Base::array2string([]),
  1056. ];
  1057. return DB::transaction(function () use ($inArray) {
  1058. $taskid = DB::table('project_task')->insertGetId($inArray);
  1059. if (empty($taskid)) {
  1060. return Base::retError('系统繁忙,请稍后再试!');
  1061. }
  1062. DB::table('project_log')->insert([
  1063. 'type' => '日志',
  1064. 'projectid' => $inArray['projectid'],
  1065. 'taskid' => $taskid,
  1066. 'username' => $inArray['createuser'],
  1067. 'detail' => '添加任务',
  1068. 'indate' => Base::time(),
  1069. 'other' => Base::array2string([
  1070. 'type' => 'task',
  1071. 'id' => $taskid,
  1072. 'title' => $inArray['title'],
  1073. ])
  1074. ]);
  1075. Project::updateNum($inArray['projectid']);
  1076. //
  1077. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1078. $task['overdue'] = Project::taskIsOverdue($task);
  1079. $task = array_merge($task, Users::username2basic($task['username']));
  1080. return Base::retSuccess('添加成功!', $task);
  1081. });
  1082. }
  1083. /**
  1084. * 项目任务-归档、取消归档
  1085. *
  1086. * @apiParam {String} act
  1087. * - cancel: 取消归档
  1088. * - else: 加入归档
  1089. * @apiParam {Number} taskid 任务ID
  1090. */
  1091. public function task__archived()
  1092. {
  1093. $user = Users::authE();
  1094. if (Base::isError($user)) {
  1095. return $user;
  1096. } else {
  1097. $user = $user['data'];
  1098. }
  1099. //
  1100. $taskid = intval(Request::input('taskid'));
  1101. $task = Base::DBC2A(DB::table('project_lists')
  1102. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  1103. ->select(['project_task.projectid', 'project_task.title', 'project_task.archived'])
  1104. ->where([
  1105. ['project_lists.delete', '=', 0],
  1106. ['project_task.delete', '=', 0],
  1107. ['project_task.id', '=', $taskid],
  1108. ])
  1109. ->first());
  1110. if (empty($task)) {
  1111. return Base::retError('任务不存在!');
  1112. }
  1113. $inRes = Project::inThe($task['projectid'], $user['username']);
  1114. if (Base::isError($inRes)) {
  1115. return $inRes;
  1116. }
  1117. //
  1118. switch (Request::input('act')) {
  1119. case 'cancel': {
  1120. if ($task['archived'] == 0) {
  1121. return Base::retError('任务未归档!');
  1122. }
  1123. DB::table('project_task')->where('id', $taskid)->update([
  1124. 'archived' => 1,
  1125. 'archiveddate' => Base::time()
  1126. ]);
  1127. DB::table('project_log')->insert([
  1128. 'type' => '日志',
  1129. 'projectid' => $task['projectid'],
  1130. 'taskid' => $taskid,
  1131. 'username' => $user['username'],
  1132. 'detail' => '取消归档',
  1133. 'indate' => Base::time(),
  1134. 'other' => Base::array2string([
  1135. 'type' => 'task',
  1136. 'id' => $taskid,
  1137. 'title' => $task['title'],
  1138. ])
  1139. ]);
  1140. return Base::retSuccess('取消归档成功!');
  1141. }
  1142. default: {
  1143. if ($task['archived'] == 1) {
  1144. return Base::retError('任务已归档!');
  1145. }
  1146. DB::table('project_task')->where('id', $taskid)->update([
  1147. 'archived' => 0,
  1148. ]);
  1149. DB::table('project_log')->insert([
  1150. 'type' => '日志',
  1151. 'projectid' => $task['projectid'],
  1152. 'taskid' => $taskid,
  1153. 'username' => $user['username'],
  1154. 'detail' => '加入归档',
  1155. 'indate' => Base::time(),
  1156. 'other' => Base::array2string([
  1157. 'type' => 'task',
  1158. 'id' => $taskid,
  1159. 'title' => $task['title'],
  1160. ])
  1161. ]);
  1162. return Base::retSuccess('加入归档成功!');
  1163. }
  1164. }
  1165. }
  1166. /**
  1167. * 项目文件-列表
  1168. *
  1169. * @apiParam {Number} projectid 项目ID
  1170. * @apiParam {Number} [taskid] 任务ID
  1171. * @apiParam {String} [name] 文件名称
  1172. * @apiParam {String} [username] 上传者用户名
  1173. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  1174. * - key: name|size|username|indate
  1175. * - order: asc|desc
  1176. * @apiParam {Number} [page] 当前页,默认:1
  1177. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1178. */
  1179. public function files__lists()
  1180. {
  1181. $user = Users::authE();
  1182. if (Base::isError($user)) {
  1183. return $user;
  1184. } else {
  1185. $user = $user['data'];
  1186. }
  1187. //
  1188. $projectid = intval(Request::input('projectid'));
  1189. $inRes = Project::inThe($projectid, $user['username']);
  1190. if (Base::isError($inRes)) {
  1191. return $inRes;
  1192. }
  1193. //
  1194. $orderBy = '`id` DESC';
  1195. $sorts = Base::json2array(Request::input('sorts'));
  1196. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1197. switch ($sorts['key']) {
  1198. case 'name':
  1199. case 'size':
  1200. case 'download':
  1201. case 'username':
  1202. case 'indate':
  1203. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1204. break;
  1205. }
  1206. }
  1207. //
  1208. $whereArray = [];
  1209. $whereArray[] = ['projectid', '=', $projectid];
  1210. $whereArray[] = ['delete', '=', 0];
  1211. if (intval(Request::input('taskid')) > 0) {
  1212. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1213. }
  1214. if (trim(Request::input('name'))) {
  1215. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  1216. }
  1217. if (trim(Request::input('username'))) {
  1218. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1219. }
  1220. //
  1221. $lists = DB::table('project_files')
  1222. ->where($whereArray)
  1223. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1224. $lists = Base::getPageList($lists);
  1225. if ($lists['total'] == 0) {
  1226. return Base::retError('未找到任何相关的文件', $lists);
  1227. }
  1228. foreach ($lists['lists'] AS $key => $item) {
  1229. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  1230. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  1231. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  1232. }
  1233. return Base::retSuccess('success', $lists);
  1234. }
  1235. /**
  1236. * 项目文件-上传
  1237. *
  1238. * @apiParam {Number} projectid 项目ID
  1239. * @apiParam {Number} [taskid] 任务ID
  1240. */
  1241. public function files__upload()
  1242. {
  1243. $user = Users::authE();
  1244. if (Base::isError($user)) {
  1245. return $user;
  1246. } else {
  1247. $user = $user['data'];
  1248. }
  1249. //
  1250. $projectid = intval(Request::input('projectid'));
  1251. $taskid = intval(Request::input('taskid'));
  1252. $inRes = Project::inThe($projectid, $user['username']);
  1253. if (Base::isError($inRes)) {
  1254. return $inRes;
  1255. }
  1256. //
  1257. $data = Base::upload([
  1258. "file" => Request::file('files'),
  1259. "type" => 'file',
  1260. "path" => "uploads/projects/" . $projectid . "/",
  1261. ]);
  1262. if (Base::isError($data)) {
  1263. return Base::retError($data['msg']);
  1264. } else {
  1265. $fileData = $data['data'];
  1266. $thumb = 'images/files/file.png';
  1267. switch ($fileData['ext']) {
  1268. case "docx":
  1269. $thumb = 'images/files/doc.png';
  1270. break;
  1271. case "xlsx":
  1272. $thumb = 'images/files/xls.png';
  1273. break;
  1274. case "pptx":
  1275. $thumb = 'images/files/ppt.png';
  1276. break;
  1277. case "doc":
  1278. case "xls":
  1279. case "ppt":
  1280. case "txt":
  1281. case "esp":
  1282. case "gif":
  1283. $thumb = 'images/files/' . $fileData['ext'] . '.png';
  1284. break;
  1285. case "jpg":
  1286. case "jpeg":
  1287. case "png":
  1288. if (Base::imgThumb($fileData['file'], $fileData['file'] . "_thumb.jpg", 64, 0)) {
  1289. $thumb = $fileData['path'] . "_thumb.jpg";
  1290. }
  1291. break;
  1292. }
  1293. $array = [
  1294. 'projectid' => $projectid,
  1295. 'taskid' => $taskid,
  1296. 'name' => $fileData['name'],
  1297. 'size' => $fileData['size'] * 1024,
  1298. 'ext' => $fileData['ext'],
  1299. 'path' => $fileData['path'],
  1300. 'thumb' => $thumb,
  1301. 'username' => $user['username'],
  1302. 'indate' => Base::time(),
  1303. ];
  1304. $id = DB::table('project_files')->insertGetId($array);
  1305. $array['id'] = $id;
  1306. $array['path'] = Base::fillUrl($array['path']);
  1307. $array['thumb'] = Base::fillUrl($array['thumb']);
  1308. $array['download'] = 0;
  1309. $array['yetdown'] = 0;
  1310. DB::table('project_log')->insert([
  1311. 'type' => '日志',
  1312. 'projectid' => $projectid,
  1313. 'taskid' => $taskid,
  1314. 'username' => $user['username'],
  1315. 'detail' => '上传文件',
  1316. 'indate' => Base::time(),
  1317. 'other' => Base::array2string([
  1318. 'type' => 'file',
  1319. 'id' => $id,
  1320. 'name' => $fileData['name'],
  1321. ])
  1322. ]);
  1323. return Base::retSuccess('success', $array);
  1324. }
  1325. }
  1326. /**
  1327. * 项目文件-上传
  1328. *
  1329. * @apiParam {Number} fileid 文件ID
  1330. */
  1331. public function files__download()
  1332. {
  1333. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1334. if (empty($fileDetail)) {
  1335. return abort(404, '文件不存在或已被删除!');
  1336. }
  1337. $filePath = public_path($fileDetail['path']);
  1338. if (!file_exists($filePath)) {
  1339. return abort(404, '文件不存在或已被删除。');
  1340. }
  1341. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  1342. Session::put('filesDownload:' . $fileDetail['id'], 1);
  1343. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  1344. }
  1345. return response()->download($filePath, $fileDetail['name']);
  1346. }
  1347. /**
  1348. * 项目文件-重命名
  1349. *
  1350. * @apiParam {Number} fileid 文件ID
  1351. * @apiParam {String} name 新文件名称
  1352. */
  1353. public function files__rename()
  1354. {
  1355. $user = Users::authE();
  1356. if (Base::isError($user)) {
  1357. return $user;
  1358. } else {
  1359. $user = $user['data'];
  1360. }
  1361. //
  1362. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1363. if (empty($fileDetail)) {
  1364. return Base::retError('文件不存在或已被删除!');
  1365. }
  1366. if ($fileDetail['username'] != $user['username']) {
  1367. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1368. if (Base::isError($inRes)) {
  1369. return Base::retError('此操作仅支持管理员或上传者!');
  1370. }
  1371. }
  1372. //
  1373. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  1374. if (empty($name)) {
  1375. return Base::retError('文件名称不能为空!');
  1376. } elseif (mb_strlen($name) > 32) {
  1377. return Base::retError('文件名称最多只能设置32个字!');
  1378. }
  1379. //
  1380. $name .= '.' . $fileDetail['ext'];
  1381. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  1382. DB::table('project_log')->insert([
  1383. 'type' => '日志',
  1384. 'projectid' => $fileDetail['projectid'],
  1385. 'taskid' => $fileDetail['taskid'],
  1386. 'username' => $user['username'],
  1387. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  1388. 'indate' => Base::time(),
  1389. 'other' => Base::array2string([
  1390. 'type' => 'file',
  1391. 'id' => $fileDetail['id'],
  1392. 'name' => $name,
  1393. ])
  1394. ]);
  1395. }
  1396. //
  1397. return Base::retSuccess('修改成功!', [
  1398. 'name' => $name,
  1399. ]);
  1400. }
  1401. /**
  1402. * 项目文件-删除
  1403. *
  1404. * @apiParam {Number} fileid 文件ID
  1405. */
  1406. public function files__delete()
  1407. {
  1408. $user = Users::authE();
  1409. if (Base::isError($user)) {
  1410. return $user;
  1411. } else {
  1412. $user = $user['data'];
  1413. }
  1414. //
  1415. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1416. if (empty($fileDetail)) {
  1417. return Base::retError('文件不存在或已被删除!');
  1418. }
  1419. if ($fileDetail['username'] != $user['username']) {
  1420. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1421. if (Base::isError($inRes)) {
  1422. return Base::retError('此操作仅支持管理员或上传者!');
  1423. }
  1424. }
  1425. //
  1426. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  1427. 'delete' => 1,
  1428. 'deletedate' => Base::time()
  1429. ]);
  1430. DB::table('project_log')->insert([
  1431. 'type' => '日志',
  1432. 'projectid' => $fileDetail['projectid'],
  1433. 'taskid' => $fileDetail['taskid'],
  1434. 'username' => $user['username'],
  1435. 'detail' => '删除文件',
  1436. 'indate' => Base::time(),
  1437. 'other' => Base::array2string([
  1438. 'type' => 'file',
  1439. 'id' => $fileDetail['id'],
  1440. 'name' => $fileDetail['name'],
  1441. ])
  1442. ]);
  1443. //
  1444. return Base::retSuccess('删除成功!');
  1445. }
  1446. /**
  1447. * 项目动态-列表
  1448. *
  1449. * @apiParam {Number} projectid 项目ID
  1450. * @apiParam {Number} [taskid] 任务ID
  1451. * @apiParam {String} [username] 用户名
  1452. * @apiParam {Number} [page] 当前页,默认:1
  1453. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1454. */
  1455. public function log__lists()
  1456. {
  1457. $user = Users::authE();
  1458. if (Base::isError($user)) {
  1459. return $user;
  1460. } else {
  1461. $user = $user['data'];
  1462. }
  1463. //
  1464. $projectid = intval(Request::input('projectid'));
  1465. $inRes = Project::inThe($projectid, $user['username']);
  1466. if (Base::isError($inRes)) {
  1467. return $inRes;
  1468. }
  1469. //
  1470. $whereArray = [];
  1471. $whereArray[] = ['projectid', '=', $projectid];
  1472. if (intval(Request::input('taskid')) > 0) {
  1473. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1474. }
  1475. if (trim(Request::input('username'))) {
  1476. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1477. }
  1478. //
  1479. $lists = DB::table('project_log')
  1480. ->where($whereArray)
  1481. ->orderByDesc('indate')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1482. $lists = Base::getPageList($lists);
  1483. if ($lists['total'] == 0) {
  1484. return Base::retError('未找到任何相关的记录', $lists);
  1485. }
  1486. foreach ($lists['lists'] AS $key => $item) {
  1487. $item = array_merge($item, Users::username2basic($item['username']));
  1488. $item['timeData'] = [
  1489. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  1490. 'hi' => date("h:i", $item['indate']) ,
  1491. 'week' => "周" . Base::getTimeWeek($item['indate']),
  1492. 'segment' => Base::getTimeDayeSegment($item['indate']),
  1493. ];
  1494. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  1495. $lists['lists'][$key] = $item;
  1496. }
  1497. return Base::retSuccess('success', $lists);
  1498. }
  1499. }