ProjectController.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  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. foreach ($newSort AS $sort => $item) {
  549. list($newLevel, $newTask) = explode(':', $item);
  550. list($oldLevel, $oldTask) = explode(':', $oldSort[$sort]);
  551. if ($newTask != $oldTask) {
  552. $newTask = explode('-', $newTask);
  553. $oldTask = explode('-', $oldTask);
  554. $userorder = intval(DB::table('project_task')->select('userorder')->where([
  555. 'delete' => 0,
  556. 'archived' => 0,
  557. 'level' => $newLevel,
  558. 'username' => $user['username'],
  559. ])->orderByDesc('userorder')->value('userorder'));
  560. if (count($newTask) < count($oldTask)) {
  561. $userorder--;
  562. } else {
  563. $userorder++;
  564. }
  565. foreach ($newTask AS $taskid) {
  566. $task = Base::DBC2A(DB::table('project_task')->select(['id', 'title', 'projectid', 'level', 'userorder'])->where([
  567. 'id' => $taskid,
  568. 'username' => $user['username']
  569. ])->first());
  570. $upArray = [];
  571. if ($task) {
  572. if ($task['level'] != $newLevel) {
  573. $upArray['level'] = $newLevel;
  574. $logArray[] = [
  575. 'type' => '日志',
  576. 'projectid' => $task['projectid'],
  577. 'taskid' => $task['id'],
  578. 'username' => $user['username'],
  579. 'detail' => '调整任务等级为【P' . $newLevel . '】',
  580. 'indate' => Base::time(),
  581. 'other' => Base::array2string([
  582. 'type' => 'task',
  583. 'id' => $task['id'],
  584. 'title' => $task['title'],
  585. ])
  586. ];
  587. }
  588. if ($task['userorder'] != $userorder) {
  589. $upArray['userorder'] = $userorder;
  590. }
  591. }
  592. if ($upArray) {
  593. DB::table('project_task')->where('id', $taskid)->update($upArray);
  594. }
  595. $userorder--;
  596. }
  597. $levels[] = $newLevel;
  598. }
  599. }
  600. if ($logArray) {
  601. DB::table('project_log')->insert($logArray);
  602. }
  603. //
  604. return Base::retSuccess('保存成功!', $levels);
  605. }
  606. /**
  607. * 退出项目
  608. *
  609. * @apiParam {Number} projectid 项目ID
  610. */
  611. public function out()
  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. $inRes = Project::inThe($projectid, $user['username']);
  629. if (Base::isError($inRes)) {
  630. return $inRes;
  631. }
  632. //
  633. DB::table('project_users')->where([
  634. 'type' => '成员',
  635. 'projectid' => $projectDetail['id'],
  636. 'username' => $user['username'],
  637. ])->delete();
  638. DB::table('project_log')->insert([
  639. 'type' => '日志',
  640. 'projectid' => $projectDetail['id'],
  641. 'username' => $user['username'],
  642. 'detail' => '退出项目',
  643. 'indate' => Base::time()
  644. ]);
  645. //
  646. return Base::retSuccess('退出项目成功!');
  647. }
  648. /**
  649. * 项目成员-列表
  650. *
  651. * @apiParam {Number} projectid 项目ID
  652. * @apiParam {Number} [page] 当前页,默认:1
  653. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  654. */
  655. public function users__lists()
  656. {
  657. $user = Users::authE();
  658. if (Base::isError($user)) {
  659. return $user;
  660. } else {
  661. $user = $user['data'];
  662. }
  663. //
  664. $projectid = intval(Request::input('projectid'));
  665. $inRes = Project::inThe($projectid, $user['username']);
  666. if (Base::isError($inRes)) {
  667. return $inRes;
  668. }
  669. //
  670. $lists = DB::table('project_lists')
  671. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  672. ->select(['project_lists.title', 'project_users.*'])
  673. ->where([
  674. ['project_lists.id', $projectid],
  675. ['project_lists.delete', 0],
  676. ['project_users.type', '成员'],
  677. ])
  678. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  679. $lists = Base::getPageList($lists);
  680. if ($lists['total'] == 0) {
  681. return Base::retError('未找到任何相关的成员');
  682. }
  683. foreach ($lists['lists'] AS $key => $projectDetail) {
  684. $userInfo = Users::username2basic($projectDetail['username']);
  685. $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
  686. $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
  687. $lists['lists'][$key]['profession'] = $userInfo['profession'];
  688. }
  689. return Base::retSuccess('success', $lists);
  690. }
  691. /**
  692. * 项目成员-添加、删除
  693. *
  694. * @apiParam {String} act
  695. * - delete: 删除成员
  696. * - else: 添加成员
  697. * @apiParam {Number} projectid 项目ID
  698. * @apiParam {Array|String} username 用户名(或用户名组)
  699. */
  700. public function users__join()
  701. {
  702. $user = Users::authE();
  703. if (Base::isError($user)) {
  704. return $user;
  705. } else {
  706. $user = $user['data'];
  707. }
  708. //
  709. $projectid = trim(Request::input('projectid'));
  710. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  711. if (empty($projectDetail)) {
  712. return Base::retError('项目不存在或已被删除!');
  713. }
  714. if ($projectDetail['username'] != $user['username']) {
  715. return Base::retError('你是不是项目负责人!');
  716. }
  717. $usernames = Request::input('username');
  718. if (empty($usernames)) {
  719. return Base::retError('参数错误!');
  720. }
  721. if (!is_array($usernames)) {
  722. if (Base::strExists($usernames, ',')) {
  723. $usernames = explode(',', $usernames);
  724. } else {
  725. $usernames = [$usernames];
  726. }
  727. }
  728. //
  729. $logArray = [];
  730. foreach ($usernames AS $username) {
  731. $inRes = Project::inThe($projectid, $username);
  732. switch (Request::input('act')) {
  733. case 'delete': {
  734. if (!Base::isError($inRes) && $projectDetail['username'] != $username) {
  735. DB::table('project_users')->where([
  736. 'type' => '成员',
  737. 'projectid' => $projectid,
  738. 'username' => $username,
  739. ])->delete();
  740. $logArray[] = [
  741. 'type' => '日志',
  742. 'projectid' => $projectDetail['id'],
  743. 'username' => $user['username'],
  744. 'detail' => '将成员【' . $username . '】移出项目',
  745. 'indate' => Base::time()
  746. ];
  747. }
  748. break;
  749. }
  750. default: {
  751. if (Base::isError($inRes)) {
  752. DB::table('project_users')->insert([
  753. 'type' => '成员',
  754. 'projectid' => $projectid,
  755. 'isowner' => 0,
  756. 'username' => $username,
  757. 'indate' => Base::time()
  758. ]);
  759. $logArray[] = [
  760. 'type' => '日志',
  761. 'projectid' => $projectDetail['id'],
  762. 'username' => $username,
  763. 'detail' => '将成员【' . $username . '】加入项目',
  764. 'indate' => Base::time()
  765. ];
  766. }
  767. break;
  768. }
  769. }
  770. }
  771. return Base::retSuccess('操作完成!');
  772. }
  773. /**
  774. * 项目子分类-添加分类
  775. *
  776. * @apiParam {Number} projectid 项目ID
  777. * @apiParam {String} title 分类名称
  778. */
  779. public function label__add()
  780. {
  781. $user = Users::authE();
  782. if (Base::isError($user)) {
  783. return $user;
  784. } else {
  785. $user = $user['data'];
  786. }
  787. //
  788. $projectid = trim(Request::input('projectid'));
  789. $inRes = Project::inThe($projectid, $user['username']);
  790. if (Base::isError($inRes)) {
  791. return $inRes;
  792. }
  793. //
  794. $title = trim(Request::input('title'));
  795. if (empty($title)) {
  796. return Base::retError('列表名称不能为空!');
  797. } elseif (mb_strlen($title) > 32) {
  798. return Base::retError('列表名称最多只能设置32个字!');
  799. }
  800. //
  801. $count = DB::table('project_label')->where('projectid', $projectid)->where('title', $title)->count();
  802. if ($count > 0) {
  803. return Base::retError('列表名称已存在!');
  804. }
  805. //
  806. $id = DB::table('project_label')->insertGetId([
  807. 'projectid' => $projectid,
  808. 'title' => $title,
  809. 'inorder' => intval(DB::table('project_label')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  810. ]);
  811. if (empty($id)) {
  812. return Base::retError('系统繁忙,请稍后再试!');
  813. }
  814. DB::table('project_log')->insert([
  815. 'type' => '日志',
  816. 'projectid' => $projectid,
  817. 'username' => $user['username'],
  818. 'detail' => '添加任务列表【' . $title . '】',
  819. 'indate' => Base::time()
  820. ]);
  821. //
  822. $row = Base::DBC2A(DB::table('project_label')->where('id', $id)->first());
  823. $row['taskLists'] = [];
  824. return Base::retSuccess('添加成功!', $row);
  825. }
  826. /**
  827. * 项目子分类-重命名分类
  828. *
  829. * @apiParam {Number} projectid 项目ID
  830. * @apiParam {Number} labelid 分类ID
  831. * @apiParam {String} title 新分类名称
  832. */
  833. public function label__rename()
  834. {
  835. $user = Users::authE();
  836. if (Base::isError($user)) {
  837. return $user;
  838. } else {
  839. $user = $user['data'];
  840. }
  841. //
  842. $projectid = trim(Request::input('projectid'));
  843. $inRes = Project::inThe($projectid, $user['username']);
  844. if (Base::isError($inRes)) {
  845. return $inRes;
  846. }
  847. //
  848. $title = trim(Request::input('title'));
  849. if (empty($title)) {
  850. return Base::retError('列表名称不能为空!');
  851. } elseif (mb_strlen($title) > 32) {
  852. return Base::retError('列表名称最多只能设置32个字!');
  853. }
  854. //
  855. $labelid = intval(Request::input('labelid'));
  856. $count = DB::table('project_label')->where('id', '!=', $labelid)->where('projectid', $projectid)->where('title', $title)->count();
  857. if ($count > 0) {
  858. return Base::retError('列表名称已存在!');
  859. }
  860. //
  861. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  862. if (empty($labelDetail)) {
  863. return Base::retError('列表不存在或已被删除!');
  864. }
  865. //
  866. if (DB::table('project_label')->where('id', $labelDetail['id'])->update([ 'title' => $title ])) {
  867. DB::table('project_log')->insert([
  868. 'type' => '日志',
  869. 'projectid' => $projectid,
  870. 'username' => $user['username'],
  871. 'detail' => '任务列表【' . $labelDetail['title'] . '】重命名【' . $title . '】',
  872. 'indate' => Base::time()
  873. ]);
  874. }
  875. //
  876. return Base::retSuccess('修改成功!');
  877. }
  878. /**
  879. * 项目子分类-删除分类
  880. *
  881. * @apiParam {Number} projectid 项目ID
  882. * @apiParam {Number} labelid 分类ID
  883. *
  884. * @throws \Throwable
  885. */
  886. public function label__delete()
  887. {
  888. $user = Users::authE();
  889. if (Base::isError($user)) {
  890. return $user;
  891. } else {
  892. $user = $user['data'];
  893. }
  894. //
  895. $projectid = trim(Request::input('projectid'));
  896. $inRes = Project::inThe($projectid, $user['username']);
  897. if (Base::isError($inRes)) {
  898. return $inRes;
  899. }
  900. //
  901. $labelid = intval(Request::input('labelid'));
  902. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  903. if (empty($labelDetail)) {
  904. return Base::retError('列表不存在或已被删除!');
  905. }
  906. //
  907. return DB::transaction(function () use ($user, $projectid, $labelDetail) {
  908. $taskLists = Base::DBC2A(DB::table('project_task')->where('labelid', $labelDetail['id'])->get());
  909. $logArray = [];
  910. foreach ($taskLists AS $task) {
  911. $logArray[] = [
  912. 'type' => '日志',
  913. 'projectid' => $projectid,
  914. 'taskid' => $task['id'],
  915. 'username' => $user['username'],
  916. 'detail' => '删除列表任务',
  917. 'indate' => Base::time(),
  918. 'other' => Base::array2string([
  919. 'type' => 'task',
  920. 'id' => $task['id'],
  921. 'title' => $task['title'],
  922. ])
  923. ];
  924. }
  925. $logArray[] = [
  926. 'type' => '日志',
  927. 'projectid' => $projectid,
  928. 'taskid' => 0,
  929. 'username' => $user['username'],
  930. 'detail' => '删除任务列表【' . $labelDetail['title'] . '】',
  931. 'indate' => Base::time()
  932. ];
  933. DB::table('project_task')->where('labelid', $labelDetail['id'])->update([
  934. 'delete' => 1,
  935. 'deletedate' => Base::time()
  936. ]);
  937. DB::table('project_label')->where('id', $labelDetail['id'])->delete();
  938. DB::table('project_log')->insert($logArray);
  939. //
  940. return Base::retSuccess('删除成功!');
  941. });
  942. }
  943. /**
  944. * 项目任务-列表
  945. *
  946. * @apiParam {Number} [projectid] 项目ID
  947. * @apiParam {String} [username] 负责人用户名(如果项目ID为空时此参数无效只获取自己的任务)
  948. * @apiParam {Number} [labelid] 项目子分类ID
  949. * @apiParam {Number} [taskid] 任务ID (赋值返回详细数据,不返回列表数据)
  950. * @apiParam {Number} [level] 任务等级(1~4)
  951. * @apiParam {String} [archived] 任务是否归档
  952. * - 未归档 (默认)
  953. * - 已归档
  954. * - 全部
  955. * @apiParam {String} [type] 任务类型
  956. * - 全部(默认)
  957. * - 未完成
  958. * - 已超期
  959. * - 已完成
  960. * @apiParam {Number} [attention] 是否仅获取关注数据(1:是)
  961. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  962. * @apiParam {String} [startdate] 任务开始时间,格式:YYYY-MM-DD
  963. * @apiParam {String} [enddate] 任务结束时间,格式:YYYY-MM-DD
  964. *
  965. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  966. * - key: title|labelid|enddate|username|level|indate|type|inorder(默认)|userorder
  967. * - order: asc|desc
  968. * - 【archived=已归档】或【startdate和enddate赋值】时无效
  969. *
  970. * @apiParam {Number} [page] 当前页,默认:1
  971. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  972. */
  973. public function task__lists()
  974. {
  975. $user = Users::authE();
  976. if (Base::isError($user)) {
  977. return $user;
  978. } else {
  979. $user = $user['data'];
  980. }
  981. //
  982. $projectid = intval(Request::input('projectid'));
  983. if ($projectid > 0) {
  984. $inRes = Project::inThe($projectid, $user['username']);
  985. if (Base::isError($inRes)) {
  986. return $inRes;
  987. }
  988. }
  989. //
  990. $orderBy = '`inorder` DESC,`id` DESC';
  991. $sorts = Base::json2array(Request::input('sorts'));
  992. if (in_array($sorts['order'], ['asc', 'desc'])) {
  993. switch ($sorts['key']) {
  994. case 'title':
  995. case 'labelid':
  996. case 'enddate':
  997. case 'username':
  998. case 'level':
  999. case 'indate':
  1000. case 'inorder':
  1001. case 'userorder':
  1002. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1003. break;
  1004. case 'type':
  1005. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['order'] . ', `complete` ' . $sorts['order'] . ',`id` DESC';
  1006. break;
  1007. }
  1008. }
  1009. //
  1010. $taskid = intval(Request::input('taskid'));
  1011. $whereRaw = null;
  1012. $whereFunc = null;
  1013. $whereArray = [];
  1014. $whereArray[] = ['project_task.delete', '=', 0];
  1015. if ($projectid > 0) {
  1016. $whereArray[] = ['project_lists.id', '=', $projectid];
  1017. $whereArray[] = ['project_lists.delete', '=', 0];
  1018. if (trim(Request::input('username'))) {
  1019. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1020. }
  1021. } else {
  1022. $whereArray[] = ['project_task.username', '=', $user['username']];
  1023. }
  1024. if ($taskid > 0) {
  1025. $whereArray[] = ['project_task.id', '=', intval(Request::input('taskid'))];
  1026. }
  1027. if (intval(Request::input('labelid')) > 0) {
  1028. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  1029. }
  1030. if (intval(Request::input('level')) > 0) {
  1031. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  1032. }
  1033. if (intval(Request::input('attention')) === 1) {
  1034. $whereRaw.= $whereRaw ? ' AND ' : '';
  1035. $whereRaw.= "`username` in (select username from `" . env('DB_PREFIX') . "project_users` where `type`='关注' AND `username`='" . $user['username'] . "')";
  1036. }
  1037. $archived = trim(Request::input('archived'));
  1038. if (empty($archived)) $archived = "未归档";
  1039. switch ($archived) {
  1040. case '已归档':
  1041. $whereArray[] = ['project_task.archived', '=', 1];
  1042. $orderBy = '`archiveddate` DESC';
  1043. break;
  1044. case '未归档':
  1045. $whereArray[] = ['project_task.archived', '=', 0];
  1046. break;
  1047. }
  1048. $type = trim(Request::input('type'));
  1049. switch ($type) {
  1050. case '未完成':
  1051. $whereArray[] = ['project_task.complete', '=', 0];
  1052. break;
  1053. case '已超期':
  1054. $whereArray[] = ['project_task.complete', '=', 0];
  1055. $whereArray[] = ['project_task.enddate', '>', 0];
  1056. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  1057. break;
  1058. case '已完成':
  1059. $whereArray[] = ['project_task.complete', '=', 1];
  1060. break;
  1061. }
  1062. $startdate = trim(Request::input('startdate'));
  1063. $enddate = trim(Request::input('enddate'));
  1064. if (Base::isDate($startdate) || Base::isDate($enddate)) {
  1065. $startdate = strtotime($startdate . ' 00:00:00');
  1066. $enddate = strtotime($enddate . ' 23:59:59');
  1067. $whereRaw.= $whereRaw ? ' AND ' : '';
  1068. $whereRaw.= "((`startdate` >= " . $startdate . " OR `startdate` = 0) AND (`enddate` <= " . $enddate . " OR `enddate` = 0))";
  1069. $orderBy = '`startdate` DESC';
  1070. }
  1071. //
  1072. $builder = DB::table('project_task');
  1073. if ($projectid > 0) {
  1074. $builder->join('project_lists', 'project_lists.id', '=', 'project_task.projectid');
  1075. }
  1076. if ($whereRaw) {
  1077. $builder->whereRaw($whereRaw);
  1078. }
  1079. $lists = $builder->select(['project_task.*'])
  1080. ->where($whereArray)
  1081. ->orderByRaw($orderBy)
  1082. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1083. $lists = Base::getPageList($lists, $taskid > 0 ? false : true);
  1084. if (intval(Request::input('statistics')) == 1) {
  1085. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->count();
  1086. $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();
  1087. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 1)->count();
  1088. }
  1089. if ($lists['total'] == 0) {
  1090. return Base::retError('未找到任何相关的任务', $lists);
  1091. }
  1092. foreach ($lists['lists'] AS $key => $info) {
  1093. $info['overdue'] = Project::taskIsOverdue($info);
  1094. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  1095. }
  1096. if ($taskid > 0) {
  1097. return Base::retSuccess('success', $lists['lists'][0]);
  1098. } else {
  1099. return Base::retSuccess('success', $lists);
  1100. }
  1101. }
  1102. /**
  1103. * 项目任务-添加任务
  1104. *
  1105. * @apiParam {String} title 任务标题
  1106. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  1107. * @apiParam {String} [username] 任务负责人用户名(如果项目ID为空时此参数无效复制人为自己)
  1108. * @apiParam {Number} [projectid] 项目ID
  1109. * @apiParam {Number} [labelid] 项目子分类ID
  1110. *
  1111. * @throws \Throwable
  1112. */
  1113. public function task__add()
  1114. {
  1115. $user = Users::authE();
  1116. if (Base::isError($user)) {
  1117. return $user;
  1118. } else {
  1119. $user = $user['data'];
  1120. }
  1121. //
  1122. $projectid = intval(Request::input('projectid'));
  1123. $labelid = intval(Request::input('labelid'));
  1124. if ($projectid > 0) {
  1125. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  1126. if (empty($projectDetail)) {
  1127. return Base::retError('项目不存在或已被删除!');
  1128. }
  1129. //
  1130. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1131. if (empty($labelDetail)) {
  1132. return Base::retError('项目子分类不存在或已被删除!');
  1133. }
  1134. //
  1135. $inRes = Project::inThe($projectid, $user['username']);
  1136. if (Base::isError($inRes)) {
  1137. return $inRes;
  1138. }
  1139. //
  1140. $username = trim(Request::input('username'));
  1141. if (empty($username)) {
  1142. $username = $user['username'];
  1143. }
  1144. if ($username != $user['username']) {
  1145. $inRes = Project::inThe($projectid, $username);
  1146. if (Base::isError($inRes)) {
  1147. return Base::retError('负责人不在项目成员内!');
  1148. }
  1149. }
  1150. } else {
  1151. $username = $user['username'];
  1152. }
  1153. //
  1154. $title = trim(Request::input('title'));
  1155. if (empty($title)) {
  1156. return Base::retError('任务标题不能为空!');
  1157. } elseif (mb_strlen($title) > 255) {
  1158. return Base::retError('任务标题最多只能设置255个字!');
  1159. }
  1160. //
  1161. $level = max(1, min(4, intval(Request::input('level'))));
  1162. $inArray = [
  1163. 'projectid' => $projectid,
  1164. 'labelid' => $labelid,
  1165. 'createuser' => $user['username'],
  1166. 'username' => $username,
  1167. 'title' => $title,
  1168. 'level' => $level,
  1169. 'inorder' => empty($projectid) ? 0 : intval(DB::table('project_task')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  1170. 'userorder' => intval(DB::table('project_task')->where('username', $user['username'])->where('level', $level)->orderByDesc('userorder')->value('userorder')) + 1,
  1171. 'indate' => Base::time(),
  1172. 'startdate' => Base::time(),
  1173. 'subtask' => Base::array2string([]),
  1174. 'follower' => Base::array2string([]),
  1175. ];
  1176. return DB::transaction(function () use ($inArray) {
  1177. $taskid = DB::table('project_task')->insertGetId($inArray);
  1178. if (empty($taskid)) {
  1179. return Base::retError('系统繁忙,请稍后再试!');
  1180. }
  1181. DB::table('project_log')->insert([
  1182. 'type' => '日志',
  1183. 'projectid' => $inArray['projectid'],
  1184. 'taskid' => $taskid,
  1185. 'username' => $inArray['createuser'],
  1186. 'detail' => '添加任务',
  1187. 'indate' => Base::time(),
  1188. 'other' => Base::array2string([
  1189. 'type' => 'task',
  1190. 'id' => $taskid,
  1191. 'title' => $inArray['title'],
  1192. ])
  1193. ]);
  1194. Project::updateNum($inArray['projectid']);
  1195. //
  1196. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1197. $task['overdue'] = Project::taskIsOverdue($task);
  1198. $task = array_merge($task, Users::username2basic($task['username']));
  1199. return Base::retSuccess('添加成功!', $task);
  1200. });
  1201. }
  1202. /**
  1203. * 项目任务-修改
  1204. *
  1205. * @apiParam {Number} taskid 任务ID
  1206. * @apiParam {String} act 修改字段|操作类型
  1207. * - title: 标题
  1208. * - desc: 描述
  1209. * - level: 优先级
  1210. * - username: 负责人
  1211. * - plannedtime: 设置计划时间
  1212. * - unplannedtime: 取消计划时间
  1213. * - complete: 标记完成
  1214. * - unfinished: 标记未完成
  1215. * - archived: 归档
  1216. * - unarchived: 取消归档
  1217. * - delete: 删除任务
  1218. * - comment: 评论
  1219. * @apiParam {String} [content] 内容数据
  1220. */
  1221. public function task__edit()
  1222. {
  1223. $user = Users::authE();
  1224. if (Base::isError($user)) {
  1225. return $user;
  1226. } else {
  1227. $user = $user['data'];
  1228. }
  1229. //
  1230. $taskid = intval(Request::input('taskid'));
  1231. $task = Base::DBC2A(DB::table('project_task')
  1232. ->where([
  1233. ['delete', '=', 0],
  1234. ['id', '=', $taskid],
  1235. ])
  1236. ->first());
  1237. if (empty($task)) {
  1238. return Base::retError('任务不存在!');
  1239. }
  1240. if ($task['projectid'] > 0) {
  1241. $inRes = Project::inThe($task['projectid'], $user['username']);
  1242. if (Base::isError($inRes)) {
  1243. return $inRes;
  1244. }
  1245. if (!$inRes['data']['isowner'] && $task['username'] != $user['username']) {
  1246. return Base::retError('此操作只允许项目管理员或者任务负责人!');
  1247. }
  1248. } else {
  1249. if ($task['username'] != $user['username']) {
  1250. return Base::retError('此操作只允许任务负责人!');
  1251. }
  1252. }
  1253. //
  1254. $act = trim(Request::input('act'));
  1255. $content = trim(Request::input('content'));
  1256. $message = "";
  1257. $upArray = [];
  1258. $logArray = [];
  1259. switch ($act) {
  1260. /**
  1261. * 修改标题
  1262. */
  1263. case 'title': {
  1264. if ($content == $task['title']) {
  1265. return Base::retError('标题未做改变!');
  1266. }
  1267. $upArray['title'] = $content;
  1268. $logArray[] = [
  1269. 'type' => '日志',
  1270. 'projectid' => $task['projectid'],
  1271. 'taskid' => $task['id'],
  1272. 'username' => $user['username'],
  1273. 'detail' => '修改任务标题',
  1274. 'indate' => Base::time(),
  1275. 'other' => Base::array2string([
  1276. 'type' => 'task',
  1277. 'id' => $task['id'],
  1278. 'title' => $content,
  1279. 'old_title' => $task['title'],
  1280. ])
  1281. ];
  1282. break;
  1283. }
  1284. /**
  1285. * 修改描述
  1286. */
  1287. case 'desc': {
  1288. if ($content == $task['desc']) {
  1289. return Base::retError('描述未做改变!');
  1290. }
  1291. $upArray['desc'] = $content;
  1292. $logArray[] = [
  1293. 'type' => '日志',
  1294. 'projectid' => $task['projectid'],
  1295. 'taskid' => $task['id'],
  1296. 'username' => $user['username'],
  1297. 'detail' => '修改任务描述',
  1298. 'indate' => Base::time(),
  1299. 'other' => Base::array2string([
  1300. 'type' => 'task',
  1301. 'id' => $task['id'],
  1302. 'title' => $task['title'],
  1303. 'old_desc' => $task['desc'],
  1304. ])
  1305. ];
  1306. break;
  1307. }
  1308. /**
  1309. * 调整任务等级
  1310. */
  1311. case 'level': {
  1312. $content = intval($content);
  1313. if ($content == $task['level']) {
  1314. return Base::retError('优先级未做改变!');
  1315. }
  1316. if ($content > 4 || $content < 1) {
  1317. return Base::retError('优先级参数错误!');
  1318. }
  1319. $upArray['level'] = $content;
  1320. $logArray[] = [
  1321. 'type' => '日志',
  1322. 'projectid' => $task['projectid'],
  1323. 'taskid' => $task['id'],
  1324. 'username' => $user['username'],
  1325. 'detail' => '调整任务等级为【P' . $content . '】',
  1326. 'indate' => Base::time(),
  1327. 'other' => Base::array2string([
  1328. 'type' => 'task',
  1329. 'id' => $task['id'],
  1330. 'title' => $task['title'],
  1331. 'old_level' => $task['level'],
  1332. ])
  1333. ];
  1334. break;
  1335. }
  1336. /**
  1337. * 修改任务负责人
  1338. */
  1339. case 'username': {
  1340. if ($content == $task['username']) {
  1341. return Base::retError('负责人未做改变!');
  1342. }
  1343. if ($task['projectid'] > 0) {
  1344. $inRes = Project::inThe($task['projectid'], $content);
  1345. if (Base::isError($inRes)) {
  1346. return Base::retError($content . '不在成员列表内!');
  1347. }
  1348. }
  1349. $upArray['username'] = $content;
  1350. $logArray[] = [
  1351. 'type' => '日志',
  1352. 'projectid' => $task['projectid'],
  1353. 'taskid' => $task['id'],
  1354. 'username' => $user['username'],
  1355. 'detail' => '修改负责人',
  1356. 'indate' => Base::time(),
  1357. 'other' => Base::array2string([
  1358. 'type' => 'task',
  1359. 'id' => $task['id'],
  1360. 'title' => $task['title'],
  1361. 'old_username' => $task['username'],
  1362. ])
  1363. ];
  1364. break;
  1365. }
  1366. /**
  1367. * 修改计划时间
  1368. */
  1369. case 'plannedtime': {
  1370. list($startdate, $enddate) = explode(",", $content);
  1371. if (!Base::isDate($startdate) || !Base::isDate($enddate)) {
  1372. return Base::retError('计划时间参数错误!');
  1373. }
  1374. $startdate = strtotime($startdate);
  1375. $enddate = strtotime($enddate);
  1376. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1377. return Base::retError('与原计划时间一致!');
  1378. }
  1379. $upArray['startdate'] = $startdate;
  1380. $upArray['enddate'] = $enddate;
  1381. $logArray[] = [
  1382. 'type' => '日志',
  1383. 'projectid' => $task['projectid'],
  1384. 'taskid' => $task['id'],
  1385. 'username' => $user['username'],
  1386. 'detail' => '设置计划时间',
  1387. 'indate' => Base::time(),
  1388. 'other' => Base::array2string([
  1389. 'type' => 'task',
  1390. 'id' => $task['id'],
  1391. 'title' => $task['title'],
  1392. 'old_startdate' => $task['startdate'],
  1393. 'old_enddate' => $task['enddate'],
  1394. ])
  1395. ];
  1396. break;
  1397. }
  1398. /**
  1399. * 取消计划时间
  1400. */
  1401. case 'unplannedtime': {
  1402. $startdate = 0;
  1403. $enddate = 0;
  1404. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1405. return Base::retError('与原计划时间一致!');
  1406. }
  1407. $upArray['startdate'] = $startdate;
  1408. $upArray['enddate'] = $enddate;
  1409. $logArray[] = [
  1410. 'type' => '日志',
  1411. 'projectid' => $task['projectid'],
  1412. 'taskid' => $task['id'],
  1413. 'username' => $user['username'],
  1414. 'detail' => '取消计划时间',
  1415. 'indate' => Base::time(),
  1416. 'other' => Base::array2string([
  1417. 'type' => 'task',
  1418. 'id' => $task['id'],
  1419. 'title' => $task['title'],
  1420. 'old_startdate' => $task['startdate'],
  1421. 'old_enddate' => $task['enddate'],
  1422. ])
  1423. ];
  1424. break;
  1425. }
  1426. /**
  1427. * 标记完成
  1428. */
  1429. case 'complete': {
  1430. if ($task['complete'] == 1) {
  1431. return Base::retError('任务已标记完成,请勿重复操作!');
  1432. }
  1433. $upArray['complete'] = 1;
  1434. $upArray['completedate'] = Base::time();
  1435. $logArray[] = [
  1436. 'type' => '日志',
  1437. 'projectid' => $task['projectid'],
  1438. 'taskid' => $task['id'],
  1439. 'username' => $user['username'],
  1440. 'detail' => '标记已完成',
  1441. 'indate' => Base::time(),
  1442. 'other' => Base::array2string([
  1443. 'type' => 'task',
  1444. 'id' => $task['id'],
  1445. 'title' => $task['title'],
  1446. ])
  1447. ];
  1448. break;
  1449. }
  1450. /**
  1451. * 标记未完成
  1452. */
  1453. case 'unfinished': {
  1454. if ($task['complete'] == 0) {
  1455. return Base::retError('任务未完成,无法标记未完成!');
  1456. }
  1457. $upArray['complete'] = 0;
  1458. $logArray[] = [
  1459. 'type' => '日志',
  1460. 'projectid' => $task['projectid'],
  1461. 'taskid' => $task['id'],
  1462. 'username' => $user['username'],
  1463. 'detail' => '标记未完成',
  1464. 'indate' => Base::time(),
  1465. 'other' => Base::array2string([
  1466. 'type' => 'task',
  1467. 'id' => $task['id'],
  1468. 'title' => $task['title'],
  1469. ])
  1470. ];
  1471. break;
  1472. }
  1473. /**
  1474. * 归档
  1475. */
  1476. case 'archived': {
  1477. if ($task['archived'] == 1) {
  1478. return Base::retError('任务已经归档,请勿重复操作!');
  1479. }
  1480. $upArray['archived'] = 1;
  1481. $upArray['archiveddate'] = Base::time();
  1482. $logArray[] = [
  1483. 'type' => '日志',
  1484. 'projectid' => $task['projectid'],
  1485. 'taskid' => $task['id'],
  1486. 'username' => $user['username'],
  1487. 'detail' => '任务归档',
  1488. 'indate' => Base::time(),
  1489. 'other' => Base::array2string([
  1490. 'type' => 'task',
  1491. 'id' => $task['id'],
  1492. 'title' => $task['title'],
  1493. ])
  1494. ];
  1495. break;
  1496. }
  1497. /**
  1498. * 取消归档
  1499. */
  1500. case 'unarchived': {
  1501. if ($task['archived'] == 0) {
  1502. return Base::retError('任务未归档,无法取消归档操作!');
  1503. }
  1504. $upArray['archived'] = 0;
  1505. $logArray[] = [
  1506. 'type' => '日志',
  1507. 'projectid' => $task['projectid'],
  1508. 'taskid' => $task['id'],
  1509. 'username' => $user['username'],
  1510. 'detail' => '取消归档',
  1511. 'indate' => Base::time(),
  1512. 'other' => Base::array2string([
  1513. 'type' => 'task',
  1514. 'id' => $task['id'],
  1515. 'title' => $task['title'],
  1516. ])
  1517. ];
  1518. break;
  1519. }
  1520. /**
  1521. * 删除任务
  1522. */
  1523. case 'delete': {
  1524. if ($task['delete'] == 1) {
  1525. return Base::retError('任务已删除,请勿重复操作!');
  1526. }
  1527. $upArray['delete'] = 1;
  1528. $upArray['deletedate'] = Base::time();
  1529. $logArray[] = [
  1530. 'type' => '日志',
  1531. 'projectid' => $task['projectid'],
  1532. 'taskid' => $task['id'],
  1533. 'username' => $user['username'],
  1534. 'detail' => '删除任务',
  1535. 'indate' => Base::time(),
  1536. 'other' => Base::array2string([
  1537. 'type' => 'task',
  1538. 'id' => $task['id'],
  1539. 'title' => $task['title'],
  1540. ])
  1541. ];
  1542. $message = "删除成功!";
  1543. break;
  1544. }
  1545. /**
  1546. * 评论任务
  1547. */
  1548. case 'comment': {
  1549. if (mb_strlen($content) < 2) {
  1550. return Base::retError('评论内容至少2个字!');
  1551. }
  1552. $logArray[] = [
  1553. 'type' => '评论',
  1554. 'projectid' => $task['projectid'],
  1555. 'taskid' => $task['id'],
  1556. 'username' => $user['username'],
  1557. 'detail' => $content,
  1558. 'indate' => Base::time(),
  1559. 'other' => Base::array2string([
  1560. 'type' => 'task',
  1561. 'id' => $task['id'],
  1562. 'title' => $task['title'],
  1563. ])
  1564. ];
  1565. $message = "评论成功!";
  1566. break;
  1567. }
  1568. default: {
  1569. return Base::retError('参数错误!');
  1570. break;
  1571. }
  1572. }
  1573. //
  1574. if ($upArray) {
  1575. DB::table('project_task')->where('id', $taskid)->update($upArray);
  1576. }
  1577. if ($logArray) {
  1578. DB::table('project_log')->insert($logArray);
  1579. }
  1580. //
  1581. $task = array_merge($task, $upArray);
  1582. $task['overdue'] = Project::taskIsOverdue($task);
  1583. $task = array_merge($task, Users::username2basic($task['username']));
  1584. return Base::retSuccess($message ?: '修改成功!', $task);
  1585. }
  1586. /**
  1587. * 项目文件-列表
  1588. *
  1589. * @apiParam {Number} [projectid] 项目ID
  1590. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  1591. * @apiParam {String} [name] 文件名称
  1592. * @apiParam {String} [username] 上传者用户名
  1593. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  1594. * - key: name|size|username|indate
  1595. * - order: asc|desc
  1596. * @apiParam {Number} [page] 当前页,默认:1
  1597. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1598. */
  1599. public function files__lists()
  1600. {
  1601. $user = Users::authE();
  1602. if (Base::isError($user)) {
  1603. return $user;
  1604. } else {
  1605. $user = $user['data'];
  1606. }
  1607. //
  1608. $projectid = intval(Request::input('projectid'));
  1609. if ($projectid > 0) {
  1610. $inRes = Project::inThe($projectid, $user['username']);
  1611. if (Base::isError($inRes)) {
  1612. return $inRes;
  1613. }
  1614. }
  1615. //
  1616. $orderBy = '`id` DESC';
  1617. $sorts = Base::json2array(Request::input('sorts'));
  1618. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1619. switch ($sorts['key']) {
  1620. case 'name':
  1621. case 'size':
  1622. case 'download':
  1623. case 'username':
  1624. case 'indate':
  1625. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1626. break;
  1627. }
  1628. }
  1629. //
  1630. $taskid = intval(Request::input('taskid'));
  1631. $whereArray = [];
  1632. if ($projectid > 0) {
  1633. $whereArray[] = ['projectid', '=', $projectid];
  1634. if ($taskid > 0) {
  1635. $whereArray[] = ['taskid', '=', $taskid];
  1636. }
  1637. } else {
  1638. if ($taskid < 0) {
  1639. return Base::retError('参数错误!');
  1640. }
  1641. $count = DB::table('project_task')->where([ 'id' => $taskid, 'username' => $user['username']])->count();
  1642. if ($count <= 0) {
  1643. return Base::retError('你不是任务负责人!');
  1644. }
  1645. $whereArray[] = ['taskid', '=', $taskid];
  1646. }
  1647. $whereArray[] = ['delete', '=', 0];
  1648. if (intval(Request::input('taskid')) > 0) {
  1649. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1650. }
  1651. if (trim(Request::input('name'))) {
  1652. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  1653. }
  1654. if (trim(Request::input('username'))) {
  1655. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1656. }
  1657. //
  1658. $lists = DB::table('project_files')
  1659. ->where($whereArray)
  1660. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1661. $lists = Base::getPageList($lists);
  1662. if ($lists['total'] == 0) {
  1663. return Base::retError('未找到任何相关的文件', $lists);
  1664. }
  1665. foreach ($lists['lists'] AS $key => $item) {
  1666. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  1667. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  1668. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  1669. }
  1670. return Base::retSuccess('success', $lists);
  1671. }
  1672. /**
  1673. * 项目文件-上传
  1674. *
  1675. * @apiParam {Number} [projectid] 项目ID
  1676. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  1677. */
  1678. public function files__upload()
  1679. {
  1680. $user = Users::authE();
  1681. if (Base::isError($user)) {
  1682. return $user;
  1683. } else {
  1684. $user = $user['data'];
  1685. }
  1686. //
  1687. $projectid = intval(Request::input('projectid'));
  1688. $taskid = intval(Request::input('taskid'));
  1689. if ($projectid > 0) {
  1690. $inRes = Project::inThe($projectid, $user['username']);
  1691. if (Base::isError($inRes)) {
  1692. return $inRes;
  1693. }
  1694. } else {
  1695. if ($taskid <= 0) {
  1696. return Base::retError('参数错误!');
  1697. }
  1698. $row = Base::DBC2A(DB::table('project_task')->select(['projectid'])->where([ 'id' => $taskid, 'username' => $user['username']])->first());
  1699. if (empty($row)) {
  1700. return Base::retError('你不是任务负责人!');
  1701. }
  1702. $projectid = $row['projectid'];
  1703. }
  1704. //
  1705. $data = Base::upload([
  1706. "file" => Request::file('files'),
  1707. "type" => 'file',
  1708. "path" => "uploads/projects/" . $projectid . "/",
  1709. ]);
  1710. if (Base::isError($data)) {
  1711. return Base::retError($data['msg']);
  1712. } else {
  1713. $fileData = $data['data'];
  1714. $thumb = 'images/files/file.png';
  1715. switch ($fileData['ext']) {
  1716. case "docx":
  1717. $thumb = 'images/files/doc.png';
  1718. break;
  1719. case "xlsx":
  1720. $thumb = 'images/files/xls.png';
  1721. break;
  1722. case "pptx":
  1723. $thumb = 'images/files/ppt.png';
  1724. break;
  1725. case "doc":
  1726. case "xls":
  1727. case "ppt":
  1728. case "txt":
  1729. case "esp":
  1730. case "gif":
  1731. $thumb = 'images/files/' . $fileData['ext'] . '.png';
  1732. break;
  1733. case "jpg":
  1734. case "jpeg":
  1735. case "png":
  1736. if (Base::imgThumb($fileData['file'], $fileData['file'] . "_thumb.jpg", 64, 0)) {
  1737. $thumb = $fileData['path'] . "_thumb.jpg";
  1738. }
  1739. break;
  1740. }
  1741. $array = [
  1742. 'projectid' => $projectid,
  1743. 'taskid' => $taskid,
  1744. 'name' => $fileData['name'],
  1745. 'size' => $fileData['size'] * 1024,
  1746. 'ext' => $fileData['ext'],
  1747. 'path' => $fileData['path'],
  1748. 'thumb' => $thumb,
  1749. 'username' => $user['username'],
  1750. 'indate' => Base::time(),
  1751. ];
  1752. $id = DB::table('project_files')->insertGetId($array);
  1753. $array['id'] = $id;
  1754. $array['path'] = Base::fillUrl($array['path']);
  1755. $array['thumb'] = Base::fillUrl($array['thumb']);
  1756. $array['download'] = 0;
  1757. $array['yetdown'] = 0;
  1758. DB::table('project_log')->insert([
  1759. 'type' => '日志',
  1760. 'projectid' => $projectid,
  1761. 'taskid' => $taskid,
  1762. 'username' => $user['username'],
  1763. 'detail' => '上传文件',
  1764. 'indate' => Base::time(),
  1765. 'other' => Base::array2string([
  1766. 'type' => 'file',
  1767. 'id' => $id,
  1768. 'name' => $fileData['name'],
  1769. ])
  1770. ]);
  1771. if ($taskid > 0) {
  1772. DB::table('project_task')->where('id', $taskid)->increment('filenum');
  1773. }
  1774. return Base::retSuccess('success', $array);
  1775. }
  1776. }
  1777. /**
  1778. * 项目文件-下载
  1779. *
  1780. * @apiParam {Number} fileid 文件ID
  1781. */
  1782. public function files__download()
  1783. {
  1784. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1785. if (empty($fileDetail)) {
  1786. return abort(404, '文件不存在或已被删除!');
  1787. }
  1788. $filePath = public_path($fileDetail['path']);
  1789. if (!file_exists($filePath)) {
  1790. return abort(404, '文件不存在或已被删除。');
  1791. }
  1792. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  1793. Session::put('filesDownload:' . $fileDetail['id'], 1);
  1794. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  1795. }
  1796. return response()->download($filePath, $fileDetail['name']);
  1797. }
  1798. /**
  1799. * 项目文件-重命名
  1800. *
  1801. * @apiParam {Number} fileid 文件ID
  1802. * @apiParam {String} name 新文件名称
  1803. */
  1804. public function files__rename()
  1805. {
  1806. $user = Users::authE();
  1807. if (Base::isError($user)) {
  1808. return $user;
  1809. } else {
  1810. $user = $user['data'];
  1811. }
  1812. //
  1813. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1814. if (empty($fileDetail)) {
  1815. return Base::retError('文件不存在或已被删除!');
  1816. }
  1817. if ($fileDetail['username'] != $user['username']) {
  1818. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1819. if (Base::isError($inRes)) {
  1820. return Base::retError('此操作仅支持管理员或上传者!');
  1821. }
  1822. }
  1823. //
  1824. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  1825. if (empty($name)) {
  1826. return Base::retError('文件名称不能为空!');
  1827. } elseif (mb_strlen($name) > 32) {
  1828. return Base::retError('文件名称最多只能设置32个字!');
  1829. }
  1830. //
  1831. $name .= '.' . $fileDetail['ext'];
  1832. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  1833. DB::table('project_log')->insert([
  1834. 'type' => '日志',
  1835. 'projectid' => $fileDetail['projectid'],
  1836. 'taskid' => $fileDetail['taskid'],
  1837. 'username' => $user['username'],
  1838. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  1839. 'indate' => Base::time(),
  1840. 'other' => Base::array2string([
  1841. 'type' => 'file',
  1842. 'id' => $fileDetail['id'],
  1843. 'name' => $name,
  1844. ])
  1845. ]);
  1846. }
  1847. //
  1848. return Base::retSuccess('修改成功!', [
  1849. 'name' => $name,
  1850. ]);
  1851. }
  1852. /**
  1853. * 项目文件-删除
  1854. *
  1855. * @apiParam {Number} fileid 文件ID
  1856. */
  1857. public function files__delete()
  1858. {
  1859. $user = Users::authE();
  1860. if (Base::isError($user)) {
  1861. return $user;
  1862. } else {
  1863. $user = $user['data'];
  1864. }
  1865. //
  1866. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  1867. if (empty($fileDetail)) {
  1868. return Base::retError('文件不存在或已被删除!');
  1869. }
  1870. if ($fileDetail['username'] != $user['username']) {
  1871. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  1872. if (Base::isError($inRes)) {
  1873. return Base::retError('此操作仅支持管理员或上传者!');
  1874. }
  1875. }
  1876. //
  1877. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  1878. 'delete' => 1,
  1879. 'deletedate' => Base::time()
  1880. ]);
  1881. DB::table('project_log')->insert([
  1882. 'type' => '日志',
  1883. 'projectid' => $fileDetail['projectid'],
  1884. 'taskid' => $fileDetail['taskid'],
  1885. 'username' => $user['username'],
  1886. 'detail' => '删除文件',
  1887. 'indate' => Base::time(),
  1888. 'other' => Base::array2string([
  1889. 'type' => 'file',
  1890. 'id' => $fileDetail['id'],
  1891. 'name' => $fileDetail['name'],
  1892. ])
  1893. ]);
  1894. if ($fileDetail['taskid'] > 0) {
  1895. DB::table('project_task')->where('id', $fileDetail['taskid'])->decrement('filenum');
  1896. }
  1897. //
  1898. return Base::retSuccess('删除成功!');
  1899. }
  1900. /**
  1901. * 项目动态-列表
  1902. *
  1903. * @apiParam {Number} [projectid] 项目ID
  1904. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  1905. * @apiParam {String} [type] 类型
  1906. * - 全部: 日志+评论(默认)
  1907. * - 日志
  1908. * - 评论
  1909. * @apiParam {String} [username] 用户名
  1910. * @apiParam {Number} [page] 当前页,默认:1
  1911. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1912. */
  1913. public function log__lists()
  1914. {
  1915. $user = Users::authE();
  1916. if (Base::isError($user)) {
  1917. return $user;
  1918. } else {
  1919. $user = $user['data'];
  1920. }
  1921. //
  1922. $projectid = intval(Request::input('projectid'));
  1923. if ($projectid > 0) {
  1924. $inRes = Project::inThe($projectid, $user['username']);
  1925. if (Base::isError($inRes)) {
  1926. return $inRes;
  1927. }
  1928. }
  1929. //
  1930. $taskid = intval(Request::input('taskid'));
  1931. $whereArray = [];
  1932. $whereFunc = null;
  1933. if ($projectid > 0) {
  1934. $whereArray[] = ['projectid', '=', $projectid];
  1935. if ($taskid > 0) {
  1936. $whereArray[] = ['taskid', '=', $taskid];
  1937. }
  1938. } else {
  1939. if ($taskid < 0) {
  1940. return Base::retError('参数错误!');
  1941. }
  1942. $count = DB::table('project_task')->where([ 'id' => $taskid, 'username' => $user['username']])->count();
  1943. if ($count <= 0) {
  1944. return Base::retError('你不是任务负责人!');
  1945. }
  1946. $whereArray[] = ['taskid', '=', $taskid];
  1947. }
  1948. if (trim(Request::input('username'))) {
  1949. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1950. }
  1951. switch (trim(Request::input('type'))) {
  1952. case '日志': {
  1953. $whereArray[] = ['type', '=', '日志'];
  1954. break;
  1955. }
  1956. case '评论': {
  1957. $whereArray[] = ['type', '=', '评论'];
  1958. break;
  1959. }
  1960. default: {
  1961. $whereFunc = function ($query) {
  1962. $query->whereIn('type', ['日志', '评论']);
  1963. };
  1964. break;
  1965. }
  1966. }
  1967. //
  1968. $lists = DB::table('project_log')
  1969. ->where($whereArray)
  1970. ->where($whereFunc)
  1971. ->orderByDesc('indate')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1972. $lists = Base::getPageList($lists);
  1973. if ($lists['total'] == 0) {
  1974. return Base::retError('未找到任何相关的记录', $lists);
  1975. }
  1976. foreach ($lists['lists'] AS $key => $item) {
  1977. $item = array_merge($item, Users::username2basic($item['username']));
  1978. $item['timeData'] = [
  1979. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  1980. 'hi' => date("h:i", $item['indate']) ,
  1981. 'week' => "周" . Base::getTimeWeek($item['indate']),
  1982. 'segment' => Base::getTimeDayeSegment($item['indate']),
  1983. ];
  1984. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  1985. $lists['lists'][$key] = $item;
  1986. }
  1987. return Base::retSuccess('success', $lists);
  1988. }
  1989. }