Project.php 8.0 KB

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