ProjectController.php 77 KB

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