| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use App\Module\Base;
- use App\Module\Users;
- use DB;
- use Request;
- use Session;
- /**
- * @apiDefine project
- *
- * 项目
- */
- class ProjectController extends Controller
- {
- public function __invoke($method, $action = '')
- {
- $app = $method ? $method : 'main';
- if ($action) {
- $app .= "__" . $action;
- }
- return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
- }
- /**
- * 项目列表
- */
- public function lists()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $lists = DB::table('project_lists')
- ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
- ->select(['project_lists.*', 'project_users.isowner'])
- ->where([
- ['project_lists.delete', 0],
- ['project_users.type', '成员'],
- ['project_users.username', $user['username']]
- ])
- ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
- $lists = Base::getPageList($lists);
- if ($lists['total'] == 0) {
- return Base::retError('未找到任何相关的项目');
- }
- return Base::retSuccess('success', $lists);
- }
- /**
- * 添加项目
- */
- public function add()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //项目名称
- $title = trim(Request::input('title'));
- if (mb_strlen($title) < 2) {
- return Base::retError('项目名称不可以少于2个字!');
- } elseif (mb_strlen($title) > 32) {
- return Base::retError('项目名称最多只能设置32个字!');
- }
- //流程
- $labels = Request::input('labels');
- if (!is_array($labels)) $labels = [];
- $insertLabels = [];
- $inorder = 0;
- foreach ($labels AS $label) {
- $label = trim($label);
- if ($label) {
- $insertLabels[] = [
- 'title' => $label,
- 'inorder' => $inorder++,
- ];
- }
- }
- if (empty($insertLabels)) {
- $insertLabels[] = [
- 'title' => '默认',
- 'inorder' => 0,
- ];
- }
- //开始创建
- $projectid = DB::table('project_lists')->insertGetId([
- 'title' => $title,
- 'username' => $user['username'],
- 'createuser' => $user['username'],
- 'indate' => Base::time()
- ]);
- if ($projectid) {
- DB::table('project_label')->insert($insertLabels);
- DB::table('project_log')->insert([
- 'type' => '日志',
- 'projectid' => $projectid,
- 'username' => $user['username'],
- 'detail' => '创建项目',
- 'indate' => Base::time()
- ]);
- DB::table('project_users')->insert([
- 'type' => '成员',
- 'projectid' => $projectid,
- 'isowner' => 1,
- 'username' => $user['username'],
- 'indate' => Base::time()
- ]);
- return Base::retSuccess('添加成功!');
- } else {
- return Base::retError('添加失败!');
- }
- }
- /**
- * 收藏项目
- * @throws \Throwable
- */
- public function favor()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $projectid = trim(Request::input('projectid'));
- $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
- if (empty($item)) {
- return Base::retError('项目不存在或已被删除!');
- }
- return DB::transaction(function () use ($item, $user) {
- $row = Base::DBC2A(DB::table('project_users')->where([
- 'type' => '收藏',
- 'projectid' => $item['id'],
- 'username' => $user['username'],
- ])->lockForUpdate()->first());
- if (empty($row)) {
- DB::table('project_users')->insert([
- 'type' => '收藏',
- 'projectid' => $item['id'],
- 'isowner' => $item['username'] == $user['username'] ? 1 : 0,
- 'username' => $user['username'],
- 'indate' => Base::time()
- ]);
- DB::table('project_log')->insert([
- 'type' => '日志',
- 'projectid' => $item['id'],
- 'username' => $user['username'],
- 'detail' => '收藏项目',
- 'indate' => Base::time()
- ]);
- return Base::retSuccess('收藏成功');
- }
- return Base::retSuccess('已收藏');
- });
- }
- /**
- * 重命名项目
- */
- public function rename()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $projectid = trim(Request::input('projectid'));
- $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
- if (empty($item)) {
- return Base::retError('项目不存在或已被删除!');
- }
- if ($item['username'] != $user['username']) {
- return Base::retError('你不是项目负责人!');
- }
- //
- $title = trim(Request::input('title'));
- if (mb_strlen($title) < 2) {
- return Base::retError('项目名称不可以少于2个字!');
- } elseif (mb_strlen($title) > 32) {
- return Base::retError('项目名称最多只能设置32个字!');
- }
- //
- DB::table('project_lists')->where('id', $item['id'])->update([
- 'title' => $title
- ]);
- DB::table('project_log')->insert([
- 'type' => '日志',
- 'projectid' => $item['id'],
- 'username' => $user['username'],
- 'detail' => '【' . $item['title'] . '】重命名【' . $title . '】',
- 'indate' => Base::time()
- ]);
- //
- return Base::retSuccess('修改成功');
- }
- /**
- * 移交项目
- * @throws \Throwable
- */
- public function transfer()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $projectid = trim(Request::input('projectid'));
- $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
- if (empty($item)) {
- return Base::retError('项目不存在或已被删除!');
- }
- if ($item['username'] != $user['username']) {
- return Base::retError('你不是项目负责人!');
- }
- //
- $username = trim(Request::input('username'));
- if ($username == $item['username']) {
- return Base::retError('你已是项目负责人!');
- }
- $count = DB::table('users')->where('username', $username)->count();
- if ($count <= 0) {
- return Base::retError('成员用户名(' . $username . ')不存在!');
- }
- //判断是否已在项目
- $count = DB::table('project_users')->where([
- 'type' => '成员',
- 'projectid' => $item['id'],
- 'username' => $username,
- ])->count();
- if ($count <= 0) {
- DB::table('project_log')->insert([
- 'type' => '日志',
- 'projectid' => $item['id'],
- 'username' => $username,
- 'detail' => '加入项目',
- 'indate' => Base::time()
- ]);
- DB::table('project_users')->insert([
- 'type' => '成员',
- 'projectid' => $item['id'],
- 'isowner' => 0,
- 'username' => $username,
- 'indate' => Base::time()
- ]);
- }
- //开始移交
- return DB::transaction(function () use ($user, $username, $item) {
- DB::table('project_lists')->where('id', $item['id'])->update([
- 'username' => $username
- ]);
- DB::table('project_log')->insert([
- 'type' => '日志',
- 'projectid' => $item['id'],
- 'username' => $user['username'],
- 'detail' => '【' . $item['username'] . '】移交给【' . $username . '】',
- 'indate' => Base::time()
- ]);
- DB::table('project_users')->where([
- 'projectid' => $item['id'],
- 'username' => $item['username'],
- ])->update([
- 'isowner' => 0
- ]);
- DB::table('project_users')->where([
- 'projectid' => $item['id'],
- 'username' => $username,
- ])->update([
- 'isowner' => 1
- ]);
- return Base::retSuccess('移交成功');
- });
- }
- /**
- * 删除项目
- */
- public function delete()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $projectid = trim(Request::input('projectid'));
- $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
- if (empty($item)) {
- return Base::retError('项目不存在或已被删除!');
- }
- if ($item['username'] != $user['username']) {
- return Base::retError('你不是项目负责人!');
- }
- //
- DB::table('project_lists')->where('id', $item['id'])->update([
- 'delete' => 1,
- 'deletedate' => Base::time()
- ]);
- DB::table('project_log')->insert([
- 'type' => '日志',
- 'projectid' => $item['id'],
- 'username' => $user['username'],
- 'detail' => '删除项目',
- 'indate' => Base::time()
- ]);
- //
- return Base::retSuccess('删除成功');
- }
- /**
- * 退出项目
- */
- public function out()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $projectid = trim(Request::input('projectid'));
- $item = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->first());
- if (empty($item)) {
- return Base::retError('项目不存在或已被删除!');
- }
- if ($item['username'] == $user['username']) {
- return Base::retError('你是项目负责人,不可退出项目!');
- }
- $count = DB::table('project_users')->where([
- 'type' => '成员',
- 'projectid' => $item['id'],
- 'username' => $user['username'],
- ])->count();
- if ($count <= 0) {
- return Base::retError('你不在项目成员内!');
- }
- //
- DB::table('project_users')->where([
- 'type' => '成员',
- 'projectid' => $item['id'],
- 'username' => $user['username'],
- ])->delete();
- DB::table('project_log')->insert([
- 'type' => '日志',
- 'projectid' => $item['id'],
- 'username' => $user['username'],
- 'detail' => '退出项目',
- 'indate' => Base::time()
- ]);
- //
- return Base::retSuccess('退出项目成功');
- }
- /**
- * 项目成员
- */
- public function users()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $projectid = intval(Request::input('projectid'));
- $count = DB::table('project_users')->where([
- 'type' => '成员',
- 'projectid' => $projectid,
- 'username' => $user['username'],
- ])->count();
- if ($count <= 0) {
- return Base::retError('你不在项目成员内!');
- }
- //
- $lists = DB::table('project_lists')
- ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
- ->select(['project_lists.title', 'project_users.*'])
- ->where([
- ['project_lists.id', $projectid],
- ['project_lists.delete', 0],
- ['project_users.type', '成员'],
- ])
- ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
- $lists = Base::getPageList($lists);
- if ($lists['total'] == 0) {
- return Base::retError('未找到任何相关的成员');
- }
- foreach ($lists['lists'] AS $key => $item) {
- $userInfo = Users::username2basic($item['username']);
- $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
- $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
- $lists['lists'][$key]['profession'] = $userInfo['profession'];
- }
- return Base::retSuccess('success', $lists);
- }
- /**
- * 任务-列表
- */
- public function task__lists()
- {
- $user = Users::authE();
- if (Base::isError($user)) {
- return $user;
- } else {
- $user = $user['data'];
- }
- //
- $whereArray = [];
- $whereArray[] = ['project_lists.delete', '=', 0];
- if (Request::input('projectid') > 0) $whereArray[] = ['project_lists.id', '=', intval(Request::input('projectid'))];
- if (in_array(intval(Request::input('archived')), [0, 1])) $whereArray[] = ['project_task.archived', '=', Request::input('archived')];
- //
- $orderBy = 'project_task.id';
- if (intval(Request::input('archived')) === 1) {
- $orderBy = 'project_task.archiveddate';
- }
- //
- $lists = DB::table('project_lists')
- ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
- ->select(['project_task.*'])
- ->where($whereArray)
- ->orderByDesc($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
- $lists = Base::getPageList($lists);
- if ($lists['total'] == 0) {
- return Base::retError('未找到任何相关的任务');
- }
- return Base::retSuccess('success', $lists);
- }
- }
|