ProjectController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Module\Base;
  5. use App\Module\Users;
  6. use DB;
  7. use Request;
  8. use Session;
  9. /**
  10. * @apiDefine project
  11. *
  12. * 项目
  13. */
  14. class ProjectController extends Controller
  15. {
  16. public function __invoke($method, $action = '')
  17. {
  18. $app = $method ? $method : 'main';
  19. if ($action) {
  20. $app .= "__" . $action;
  21. }
  22. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  23. }
  24. /**
  25. * 项目列表
  26. */
  27. public function lists()
  28. {
  29. $user = Users::authE();
  30. if (Base::isError($user)) {
  31. return $user;
  32. } else {
  33. $user = $user['data'];
  34. }
  35. //
  36. $lists = DB::table('project_lists')
  37. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  38. ->select(['project_lists.*', 'project_users.isowner'])
  39. ->where([
  40. ['project_lists.delete', 0],
  41. ['project_users.type', '成员'],
  42. ['project_users.username', $user['username']]
  43. ])
  44. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  45. $lists = Base::getPageList($lists);
  46. if ($lists['total'] == 0) {
  47. return Base::retError('未找到任何相关的项目');
  48. }
  49. return Base::retSuccess('success', $lists);
  50. }
  51. /**
  52. * 添加项目
  53. */
  54. public function add()
  55. {
  56. $user = Users::authE();
  57. if (Base::isError($user)) {
  58. return $user;
  59. } else {
  60. $user = $user['data'];
  61. }
  62. //项目名称
  63. $title = trim(Request::input('title'));
  64. if (mb_strlen($title) < 2) {
  65. return Base::retError('项目名称不可以少于2个字!');
  66. } elseif (mb_strlen($title) > 32) {
  67. return Base::retError('项目名称最多只能设置32个字!');
  68. }
  69. //流程
  70. $labels = Request::input('labels');
  71. if (!is_array($labels)) $labels = [];
  72. $insertLabels = [];
  73. $inorder = 0;
  74. foreach ($labels AS $label) {
  75. $label = trim($label);
  76. if ($label) {
  77. $insertLabels[] = [
  78. 'title' => $label,
  79. 'inorder' => $inorder++,
  80. ];
  81. }
  82. }
  83. if (empty($insertLabels)) {
  84. $insertLabels[] = [
  85. 'title' => '默认',
  86. 'inorder' => 0,
  87. ];
  88. }
  89. //开始创建
  90. $projectid = DB::table('project_lists')->insertGetId([
  91. 'title' => $title,
  92. 'username' => $user['username'],
  93. 'createuser' => $user['username'],
  94. 'indate' => Base::time()
  95. ]);
  96. if ($projectid) {
  97. DB::table('project_label')->insert($insertLabels);
  98. DB::table('project_log')->insert([
  99. 'type' => '日志',
  100. 'projectid' => $projectid,
  101. 'username' => $user['username'],
  102. 'detail' => '创建项目',
  103. 'indate' => Base::time()
  104. ]);
  105. DB::table('project_users')->insert([
  106. 'type' => '成员',
  107. 'projectid' => $projectid,
  108. 'isowner' => 1,
  109. 'username' => $user['username'],
  110. 'indate' => Base::time()
  111. ]);
  112. return Base::retSuccess('添加成功!');
  113. } else {
  114. return Base::retError('添加失败!');
  115. }
  116. }
  117. /**
  118. * 收藏项目
  119. * @throws \Throwable
  120. */
  121. public function favor()
  122. {
  123. $user = Users::authE();
  124. if (Base::isError($user)) {
  125. return $user;
  126. } else {
  127. $user = $user['data'];
  128. }
  129. //
  130. $projectid = trim(Request::input('projectid'));
  131. $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
  132. if (empty($item)) {
  133. return Base::retError('项目不存在或已被删除!');
  134. }
  135. return DB::transaction(function () use ($item, $user) {
  136. $row = Base::DBC2A(DB::table('project_users')->where([
  137. 'type' => '收藏',
  138. 'projectid' => $item['id'],
  139. 'username' => $user['username'],
  140. ])->lockForUpdate()->first());
  141. if (empty($row)) {
  142. DB::table('project_users')->insert([
  143. 'type' => '收藏',
  144. 'projectid' => $item['id'],
  145. 'isowner' => $item['username'] == $user['username'] ? 1 : 0,
  146. 'username' => $user['username'],
  147. 'indate' => Base::time()
  148. ]);
  149. DB::table('project_log')->insert([
  150. 'type' => '日志',
  151. 'projectid' => $item['id'],
  152. 'username' => $user['username'],
  153. 'detail' => '收藏项目',
  154. 'indate' => Base::time()
  155. ]);
  156. return Base::retSuccess('收藏成功');
  157. }
  158. return Base::retSuccess('已收藏');
  159. });
  160. }
  161. /**
  162. * 重命名项目
  163. */
  164. public function rename()
  165. {
  166. $user = Users::authE();
  167. if (Base::isError($user)) {
  168. return $user;
  169. } else {
  170. $user = $user['data'];
  171. }
  172. //
  173. $projectid = trim(Request::input('projectid'));
  174. $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
  175. if (empty($item)) {
  176. return Base::retError('项目不存在或已被删除!');
  177. }
  178. if ($item['username'] != $user['username']) {
  179. return Base::retError('你不是项目负责人!');
  180. }
  181. //
  182. $title = trim(Request::input('title'));
  183. if (mb_strlen($title) < 2) {
  184. return Base::retError('项目名称不可以少于2个字!');
  185. } elseif (mb_strlen($title) > 32) {
  186. return Base::retError('项目名称最多只能设置32个字!');
  187. }
  188. //
  189. DB::table('project_lists')->where('id', $item['id'])->update([
  190. 'title' => $title
  191. ]);
  192. DB::table('project_log')->insert([
  193. 'type' => '日志',
  194. 'projectid' => $item['id'],
  195. 'username' => $user['username'],
  196. 'detail' => '【' . $item['title'] . '】重命名【' . $title . '】',
  197. 'indate' => Base::time()
  198. ]);
  199. //
  200. return Base::retSuccess('修改成功');
  201. }
  202. /**
  203. * 移交项目
  204. * @throws \Throwable
  205. */
  206. public function transfer()
  207. {
  208. $user = Users::authE();
  209. if (Base::isError($user)) {
  210. return $user;
  211. } else {
  212. $user = $user['data'];
  213. }
  214. //
  215. $projectid = trim(Request::input('projectid'));
  216. $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
  217. if (empty($item)) {
  218. return Base::retError('项目不存在或已被删除!');
  219. }
  220. if ($item['username'] != $user['username']) {
  221. return Base::retError('你不是项目负责人!');
  222. }
  223. //
  224. $username = trim(Request::input('username'));
  225. if ($username == $item['username']) {
  226. return Base::retError('你已是项目负责人!');
  227. }
  228. $count = DB::table('users')->where('username', $username)->count();
  229. if ($count <= 0) {
  230. return Base::retError('成员用户名(' . $username . ')不存在!');
  231. }
  232. //判断是否已在项目
  233. $count = DB::table('project_users')->where([
  234. 'type' => '成员',
  235. 'projectid' => $item['id'],
  236. 'username' => $username,
  237. ])->count();
  238. if ($count <= 0) {
  239. DB::table('project_log')->insert([
  240. 'type' => '日志',
  241. 'projectid' => $item['id'],
  242. 'username' => $username,
  243. 'detail' => '加入项目',
  244. 'indate' => Base::time()
  245. ]);
  246. DB::table('project_users')->insert([
  247. 'type' => '成员',
  248. 'projectid' => $item['id'],
  249. 'isowner' => 0,
  250. 'username' => $username,
  251. 'indate' => Base::time()
  252. ]);
  253. }
  254. //开始移交
  255. return DB::transaction(function () use ($user, $username, $item) {
  256. DB::table('project_lists')->where('id', $item['id'])->update([
  257. 'username' => $username
  258. ]);
  259. DB::table('project_log')->insert([
  260. 'type' => '日志',
  261. 'projectid' => $item['id'],
  262. 'username' => $user['username'],
  263. 'detail' => '【' . $item['username'] . '】移交给【' . $username . '】',
  264. 'indate' => Base::time()
  265. ]);
  266. DB::table('project_users')->where([
  267. 'projectid' => $item['id'],
  268. 'username' => $item['username'],
  269. ])->update([
  270. 'isowner' => 0
  271. ]);
  272. DB::table('project_users')->where([
  273. 'projectid' => $item['id'],
  274. 'username' => $username,
  275. ])->update([
  276. 'isowner' => 1
  277. ]);
  278. return Base::retSuccess('移交成功');
  279. });
  280. }
  281. /**
  282. * 删除项目
  283. */
  284. public function delete()
  285. {
  286. $user = Users::authE();
  287. if (Base::isError($user)) {
  288. return $user;
  289. } else {
  290. $user = $user['data'];
  291. }
  292. //
  293. $projectid = trim(Request::input('projectid'));
  294. $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
  295. if (empty($item)) {
  296. return Base::retError('项目不存在或已被删除!');
  297. }
  298. if ($item['username'] != $user['username']) {
  299. return Base::retError('你不是项目负责人!');
  300. }
  301. //
  302. DB::table('project_lists')->where('id', $item['id'])->update([
  303. 'delete' => 1,
  304. 'deletedate' => Base::time()
  305. ]);
  306. DB::table('project_log')->insert([
  307. 'type' => '日志',
  308. 'projectid' => $item['id'],
  309. 'username' => $user['username'],
  310. 'detail' => '删除项目',
  311. 'indate' => Base::time()
  312. ]);
  313. //
  314. return Base::retSuccess('删除成功');
  315. }
  316. /**
  317. * 退出项目
  318. */
  319. public function out()
  320. {
  321. $user = Users::authE();
  322. if (Base::isError($user)) {
  323. return $user;
  324. } else {
  325. $user = $user['data'];
  326. }
  327. //
  328. $projectid = trim(Request::input('projectid'));
  329. $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
  330. if (empty($item)) {
  331. return Base::retError('项目不存在或已被删除!');
  332. }
  333. if ($item['username'] == $user['username']) {
  334. return Base::retError('你是项目负责人,不可退出项目!');
  335. }
  336. $count = DB::table('project_users')->where([
  337. 'type' => '成员',
  338. 'projectid' => $item['id'],
  339. 'username' => $user['username'],
  340. ])->count();
  341. if ($count <= 0) {
  342. return Base::retError('你不在项目成员内!');
  343. }
  344. //
  345. DB::table('project_users')->where([
  346. 'type' => '成员',
  347. 'projectid' => $item['id'],
  348. 'username' => $user['username'],
  349. ])->delete();
  350. DB::table('project_log')->insert([
  351. 'type' => '日志',
  352. 'projectid' => $item['id'],
  353. 'username' => $user['username'],
  354. 'detail' => '退出项目',
  355. 'indate' => Base::time()
  356. ]);
  357. //
  358. return Base::retSuccess('退出项目成功');
  359. }
  360. /**
  361. * 项目成员
  362. */
  363. public function users()
  364. {
  365. $user = Users::authE();
  366. if (Base::isError($user)) {
  367. return $user;
  368. } else {
  369. $user = $user['data'];
  370. }
  371. //
  372. $projectid = intval(Request::input('projectid'));
  373. $count = DB::table('project_users')->where([
  374. 'type' => '成员',
  375. 'projectid' => $projectid,
  376. 'username' => $user['username'],
  377. ])->count();
  378. if ($count <= 0) {
  379. return Base::retError('你不在项目成员内!');
  380. }
  381. //
  382. $lists = DB::table('project_lists')
  383. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  384. ->select(['project_lists.title', 'project_users.*'])
  385. ->where([
  386. ['project_lists.id', $projectid],
  387. ['project_lists.delete', 0],
  388. ['project_users.type', '成员'],
  389. ])
  390. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  391. $lists = Base::getPageList($lists);
  392. if ($lists['total'] == 0) {
  393. return Base::retError('未找到任何相关的成员');
  394. }
  395. foreach ($lists['lists'] AS $key => $item) {
  396. $userInfo = Users::username2basic($item['username']);
  397. $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
  398. $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
  399. $lists['lists'][$key]['profession'] = $userInfo['profession'];
  400. }
  401. return Base::retSuccess('success', $lists);
  402. }
  403. /**
  404. * 任务-列表
  405. */
  406. public function task__lists()
  407. {
  408. $user = Users::authE();
  409. if (Base::isError($user)) {
  410. return $user;
  411. } else {
  412. $user = $user['data'];
  413. }
  414. //
  415. $whereArray = [];
  416. $whereArray[] = ['project_lists.delete', '=', 0];
  417. if (Request::input('projectid') > 0) $whereArray[] = ['project_lists.id', '=', intval(Request::input('projectid'))];
  418. if (in_array(intval(Request::input('archived')), [0, 1])) $whereArray[] = ['project_task.archived', '=', Request::input('archived')];
  419. //
  420. $orderBy = 'project_task.id';
  421. if (intval(Request::input('archived')) === 1) {
  422. $orderBy = 'project_task.archiveddate';
  423. }
  424. //
  425. $lists = DB::table('project_lists')
  426. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  427. ->select(['project_task.*'])
  428. ->where($whereArray)
  429. ->orderByDesc($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  430. $lists = Base::getPageList($lists);
  431. if ($lists['total'] == 0) {
  432. return Base::retError('未找到任何相关的任务');
  433. }
  434. return Base::retSuccess('success', $lists);
  435. }
  436. }