Project.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Module;
  3. use DB;
  4. /**
  5. * Class Project
  6. * @package App\Module
  7. */
  8. class Project
  9. {
  10. /**
  11. * 是否在项目里
  12. * @param int $projectid
  13. * @param string $username
  14. * @param bool $isowner
  15. * @return array
  16. */
  17. public static function inThe($projectid, $username, $isowner = false)
  18. {
  19. $whereArray = [
  20. 'type' => '成员',
  21. 'projectid' => $projectid,
  22. 'username' => $username,
  23. ];
  24. if ($isowner) {
  25. $whereArray['isowner'] = 1;
  26. }
  27. $row = Base::DBC2A(DB::table('project_users')->select(['isowner', 'indate'])->where($whereArray)->first());
  28. if (empty($row)) {
  29. return Base::retError('你不在项目成员内!');
  30. } else {
  31. return Base::retSuccess('你在项目内', $row);
  32. }
  33. }
  34. /**
  35. * 更新项目(complete、unfinished)
  36. * @param int $projectid
  37. */
  38. public static function updateNum($projectid)
  39. {
  40. if ($projectid > 0) {
  41. DB::table('project_lists')->where('id', $projectid)->update([
  42. 'unfinished' => DB::table('project_task')->where('projectid', $projectid)->where('complete', 0)->where('delete', 0)->count(),
  43. 'complete' => DB::table('project_task')->where('projectid', $projectid)->where('complete', 1)->where('delete', 0)->count(),
  44. ]);
  45. }
  46. }
  47. /**
  48. * 任务负责人组
  49. * @param $task
  50. * @return array
  51. */
  52. public static function taskPersons($task)
  53. {
  54. $array = [];
  55. $array[] = Users::username2basic($task['username']);
  56. $persons = [$task['username']];
  57. $subtask = Base::string2array($task['subtask']);
  58. foreach ($subtask AS $item) {
  59. if ($item['uname'] && !in_array($item['uname'], $persons)) {
  60. $persons[] = $item['uname'];
  61. $basic = Users::username2basic($item['uname']);
  62. if ($basic) {
  63. $array[] = $basic;
  64. }
  65. }
  66. }
  67. return $array;
  68. }
  69. /**
  70. * 是否负责人(任务负责人、子任务负责人)
  71. * @param $task
  72. * @param $username
  73. * @return bool
  74. */
  75. public static function isPersons($task, $username)
  76. {
  77. $persons = [$task['username']];
  78. $subtask = Base::string2array($task['subtask']);
  79. foreach ($subtask AS $item) {
  80. if ($item['uname'] && !in_array($item['uname'], $persons)) {
  81. $persons[] = $item['uname'];
  82. }
  83. }
  84. return in_array($username, $persons) ? true : false;
  85. }
  86. /**
  87. * 任务是否过期
  88. * @param array $task
  89. * @return int
  90. */
  91. public static function taskIsOverdue($task)
  92. {
  93. return $task['complete'] == 0 && $task['enddate'] > 0 && $task['enddate'] <= Base::time() ? 1 : 0;
  94. }
  95. /**
  96. * 过期的排在前
  97. * @param array $taskLists
  98. * @return mixed
  99. */
  100. public static function sortTask($taskLists)
  101. {
  102. $inOrder = [];
  103. foreach ($taskLists as $key => $oitem) {
  104. $inOrder[$key] = $oitem['overdue'] ? -1 : $key;
  105. }
  106. array_multisort($inOrder, SORT_ASC, $taskLists);
  107. return $taskLists;
  108. }
  109. /**
  110. * 获取与任务有关系的用户(关注的、在项目里的、负责人、创建者)
  111. * @param $taskId
  112. * @return array
  113. */
  114. public static function taskSomeUsers($taskId)
  115. {
  116. $taskDeatil = Base::DBC2A(DB::table('project_task')->select(['follower', 'subtask', 'createuser', 'username', 'projectid'])->where('id', $taskId)->first());
  117. if (empty($taskDeatil)) {
  118. return [];
  119. }
  120. //关注的用户
  121. $userArray = Base::string2array($taskDeatil['follower']);
  122. //子任务负责人
  123. $subtask = Base::string2array($taskDeatil['subtask']);
  124. foreach ($subtask AS $item) {
  125. $userArray[] = $item['uname'];
  126. }
  127. //创建者
  128. $userArray[] = $taskDeatil['createuser'];
  129. //负责人
  130. $userArray[] = $taskDeatil['username'];
  131. //在项目里的用户
  132. if ($taskDeatil['projectid'] > 0) {
  133. $tempLists = Base::DBC2A(DB::table('project_users')->select(['username'])->where(['projectid' => $taskDeatil['projectid'], 'type' => '成员' ])->get());
  134. foreach ($tempLists AS $item) {
  135. $userArray[] = $item['username'];
  136. }
  137. }
  138. //
  139. return array_values(array_filter(array_unique($userArray)));
  140. }
  141. /**
  142. * 项目(任务)权限
  143. * @param $type
  144. * @param $projectid
  145. * @param int $taskid
  146. * @return array|mixed
  147. */
  148. public static function role($type, $projectid, $taskid = 0)
  149. {
  150. $user = Users::authE();
  151. if (Base::isError($user)) {
  152. return $user;
  153. } else {
  154. $user = $user['data'];
  155. }
  156. //
  157. $project = Base::DBC2A(DB::table('project_lists')->select(['username', 'setting'])->where('id', $projectid)->where('delete', 0)->first());
  158. if (empty($project)) {
  159. return Base::retError('项目不存在或已被删除!');
  160. }
  161. // 项目负责人最高权限
  162. if ($project['username'] == $user['username']) {
  163. return Base::retSuccess('success');
  164. }
  165. //
  166. $setting = Base::string2array($project['setting']);
  167. foreach (['edit_role', 'complete_role', 'archived_role', 'del_role'] AS $key) {
  168. $setting[$key] = is_array($setting[$key]) ? $setting[$key] : ['__', 'owner'];
  169. }
  170. $setting['add_role'] = is_array($setting['add_role']) ? $setting['add_role'] : ['__', 'member'];
  171. //
  172. $role = $setting[$type];
  173. if (empty($role) || !is_array($role)) {
  174. return Base::retError('操作权限不足!');
  175. }
  176. if (in_array('member', $role)) {
  177. $inRes = Project::inThe($projectid, $user['username']);
  178. if (Base::isError($inRes)) {
  179. return $inRes;
  180. }
  181. } elseif (in_array('owner', $role)) {
  182. if (empty($taskid)) {
  183. return Base::retError('任务不存在!');
  184. }
  185. $task = Base::DBC2A(DB::table('project_task')
  186. ->select(['username'])
  187. ->where([
  188. ['delete', '=', 0],
  189. ['id', '=', $taskid],
  190. ])
  191. ->first());
  192. if (empty($task)) {
  193. return Base::retError('任务不存在!');
  194. }
  195. if ($task['username'] != $user['username']) {
  196. return Base::retError('此操作只允许项目管理员或者任务负责人!');
  197. }
  198. } else {
  199. return Base::retError('此操作仅限项目负责人!');
  200. }
  201. //
  202. return Base::retSuccess('success');
  203. }
  204. }