ProjectController.php 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Model\DBCache;
  5. use App\Module\Base;
  6. use App\Module\BillExport;
  7. use App\Module\Project;
  8. use App\Module\Users;
  9. use DB;
  10. use Madzipper;
  11. use Request;
  12. use Session;
  13. /**
  14. * @apiDefine project
  15. *
  16. * 项目
  17. */
  18. class ProjectController extends Controller
  19. {
  20. public function __invoke($method, $action = '')
  21. {
  22. $app = $method ? $method : 'main';
  23. if ($action) {
  24. $app .= "__" . $action;
  25. }
  26. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  27. }
  28. /**
  29. * 项目列表
  30. *
  31. * @apiParam {String} act 类型
  32. * - join: 加入的项目(默认)
  33. * - favor: 收藏的项目
  34. * - manage: 管理的项目
  35. * @apiParam {Number} [page] 当前页,默认:1
  36. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  37. */
  38. public function lists()
  39. {
  40. $user = Users::authE();
  41. if (Base::isError($user)) {
  42. return $user;
  43. } else {
  44. $user = $user['data'];
  45. }
  46. //
  47. $whereArray = [];
  48. $whereArray[] = ['project_lists.delete', '=', 0];
  49. $whereArray[] = ['project_users.username', '=', $user['username']];
  50. switch (Request::input('act')) {
  51. case "favor": {
  52. $whereArray[] = ['project_users.type', '=', '收藏'];
  53. break;
  54. }
  55. case "manage": {
  56. $whereArray[] = ['project_users.type', '=', '成员'];
  57. $whereArray[] = ['project_users.isowner', '=', 1];
  58. break;
  59. }
  60. default: {
  61. $whereArray[] = ['project_users.type', '=', '成员'];
  62. break;
  63. }
  64. }
  65. $lists = DB::table('project_lists')
  66. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  67. ->select(['project_lists.*', 'project_users.isowner', 'project_users.indate as uindate'])
  68. ->where($whereArray)
  69. ->orderByDesc('project_lists.id')->paginate(Base::getPaginate(100, 20));
  70. $lists = Base::getPageList($lists);
  71. if ($lists['total'] == 0) {
  72. return Base::retError('未找到任何相关的项目');
  73. }
  74. return Base::retSuccess('success', $lists);
  75. }
  76. /**
  77. * 项目详情
  78. *
  79. * @apiParam {Number} projectid 项目ID
  80. */
  81. public function detail()
  82. {
  83. $user = Users::authE();
  84. if (Base::isError($user)) {
  85. return $user;
  86. } else {
  87. $user = $user['data'];
  88. }
  89. //
  90. $projectid = trim(Request::input('projectid'));
  91. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  92. if (empty($projectDetail)) {
  93. return Base::retError('项目不存在或已被删除!');
  94. }
  95. $inRes = Project::inThe($projectid, $user['username']);
  96. if (Base::isError($inRes)) {
  97. return $inRes;
  98. }
  99. $projectSetting = Base::string2array($projectDetail['setting']);
  100. //子分类
  101. $label = Base::DBC2A(DB::table('project_label')->where('projectid', $projectid)->orderBy('inorder')->orderBy('id')->get());
  102. $simpleLabel = [];
  103. //任务
  104. $whereArray = [ 'projectid' => $projectid, 'delete' => 0, 'complete' => 0 ];
  105. if ($projectSetting['complete_show'] == 'show') {
  106. unset($whereArray['complete']);
  107. }
  108. $task = Base::DBC2A(DB::table('project_task')->where($whereArray)->orderByDesc('inorder')->orderByDesc('id')->get());
  109. //任务归类
  110. foreach ($label AS $index => $temp) {
  111. $taskLists = [];
  112. foreach ($task AS $info) {
  113. if ($temp['id'] == $info['labelid']) {
  114. $info['persons'] = Project::taskPersons($info);
  115. $info['overdue'] = Project::taskIsOverdue($info);
  116. $info['subtask'] = Base::string2array($info['subtask']);
  117. $info['follower'] = Base::string2array($info['follower']);
  118. $taskLists[] = array_merge($info, Users::username2basic($info['username']));
  119. }
  120. }
  121. $label[$index]['taskLists'] = $taskLists;
  122. $simpleLabel[] = ['id' => $temp['id'], 'title' => $temp['title']];
  123. }
  124. //
  125. return Base::retSuccess('success', [
  126. 'project' => $projectDetail,
  127. 'label' => $label,
  128. 'simpleLabel' => $simpleLabel,
  129. ]);
  130. }
  131. /**
  132. * 获取项目负责人
  133. *
  134. * @apiParam {Number} projectid 项目ID
  135. */
  136. public function leader()
  137. {
  138. $user = Users::authE();
  139. if (Base::isError($user)) {
  140. return $user;
  141. } else {
  142. $user = $user['data'];
  143. }
  144. //
  145. $projectid = trim(Request::input('projectid'));
  146. $projectDetail = Base::DBC2A(DB::table('project_lists')->select(['username'])->where('id', $projectid)->where('delete', 0)->first());
  147. if (empty($projectDetail)) {
  148. return Base::retError('项目不存在或已被删除!');
  149. }
  150. return Base::retSuccess('success', [
  151. 'username' => $projectDetail['username'],
  152. ]);
  153. }
  154. /**
  155. * 添加项目
  156. *
  157. * @apiParam {String} title 项目名称
  158. * @apiParam {Array} labels 流程,格式[流程1, 流程2]
  159. */
  160. public function add()
  161. {
  162. $user = Users::authE();
  163. if (Base::isError($user)) {
  164. return $user;
  165. } else {
  166. $user = $user['data'];
  167. }
  168. //项目名称
  169. $title = trim(Request::input('title'));
  170. if (mb_strlen($title) < 2) {
  171. return Base::retError('项目名称不可以少于2个字!');
  172. } elseif (mb_strlen($title) > 32) {
  173. return Base::retError('项目名称最多只能设置32个字!');
  174. }
  175. //流程
  176. $labels = Request::input('labels');
  177. if (!is_array($labels)) $labels = [];
  178. $insertLabels = [];
  179. $inorder = 0;
  180. foreach ($labels AS $label) {
  181. $label = trim($label);
  182. if ($label) {
  183. $insertLabels[] = [
  184. 'title' => $label,
  185. 'inorder' => $inorder++,
  186. ];
  187. }
  188. }
  189. if (empty($insertLabels)) {
  190. $insertLabels[] = [
  191. 'title' => '默认',
  192. 'inorder' => 0,
  193. ];
  194. }
  195. if (count($insertLabels) > 100) {
  196. return Base::retError(['项目流程最多不能超过%个!', 100]);
  197. }
  198. //开始创建
  199. $projectid = DB::table('project_lists')->insertGetId([
  200. 'title' => $title,
  201. 'username' => $user['username'],
  202. 'createuser' => $user['username'],
  203. 'indate' => Base::time()
  204. ]);
  205. if ($projectid) {
  206. foreach ($insertLabels AS $key => $label) {
  207. $insertLabels[$key]['projectid'] = $projectid;
  208. }
  209. DB::table('project_label')->insert($insertLabels);
  210. DB::table('project_log')->insert([
  211. 'type' => '日志',
  212. 'projectid' => $projectid,
  213. 'username' => $user['username'],
  214. 'detail' => '创建项目',
  215. 'indate' => Base::time()
  216. ]);
  217. DB::table('project_users')->insert([
  218. 'type' => '成员',
  219. 'projectid' => $projectid,
  220. 'isowner' => 1,
  221. 'username' => $user['username'],
  222. 'indate' => Base::time()
  223. ]);
  224. return Base::retSuccess('添加成功!');
  225. } else {
  226. return Base::retError('添加失败!');
  227. }
  228. }
  229. /**
  230. * 设置项目
  231. *
  232. * @apiParam {String} act 类型
  233. * - save: 保存
  234. * - other: 读取
  235. * @apiParam {Number} projectid 项目ID
  236. * @apiParam {Object} ... 其他保存参数
  237. *
  238. * @throws \Throwable
  239. */
  240. public function setting()
  241. {
  242. $user = Users::authE();
  243. if (Base::isError($user)) {
  244. return $user;
  245. } else {
  246. $user = $user['data'];
  247. }
  248. //
  249. $projectid = trim(Request::input('projectid'));
  250. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  251. if (empty($projectDetail)) {
  252. return Base::retError('项目不存在或已被删除!');
  253. }
  254. //
  255. $setting = Base::string2array($projectDetail['setting']);
  256. $act = trim(Request::input('act'));
  257. if ($act == 'save') {
  258. if ($projectDetail['username'] != $user['username']) {
  259. return Base::retError('你不是项目负责人!');
  260. }
  261. foreach (Request::input() AS $key => $value) {
  262. if (in_array($key, ['project_desc', 'add_role', 'edit_role', 'complete_role', 'archived_role', 'del_role', 'complete_show'])) {
  263. $setting[$key] = $value;
  264. }
  265. }
  266. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  267. 'setting' => Base::string2array($setting)
  268. ]);
  269. }
  270. //
  271. foreach (['edit_role', 'complete_role', 'archived_role', 'del_role'] AS $key) {
  272. $setting[$key] = is_array($setting[$key]) ? $setting[$key] : ['__', 'owner'];
  273. }
  274. $setting['add_role'] = is_array($setting['add_role']) ? $setting['add_role'] : ['__', 'member'];
  275. $setting['complete_show'] = $setting['complete_show'] ?: 'hide';
  276. //
  277. return Base::retSuccess($act == 'save' ? '修改成功!' : 'success', $setting ?: json_decode('{}'));
  278. }
  279. /**
  280. * 收藏项目
  281. *
  282. * @apiParam {String} act 类型
  283. * - cancel: 取消收藏
  284. * - else: 添加收藏
  285. * @apiParam {Number} projectid 项目ID
  286. *
  287. * @throws \Throwable
  288. */
  289. public function favor()
  290. {
  291. $user = Users::authE();
  292. if (Base::isError($user)) {
  293. return $user;
  294. } else {
  295. $user = $user['data'];
  296. }
  297. //
  298. $projectid = trim(Request::input('projectid'));
  299. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  300. if (empty($projectDetail)) {
  301. return Base::retError('项目不存在或已被删除!');
  302. }
  303. return DB::transaction(function () use ($projectDetail, $user) {
  304. switch (Request::input('act')) {
  305. case 'cancel': {
  306. if (DB::table('project_users')->where([
  307. 'type' => '收藏',
  308. 'projectid' => $projectDetail['id'],
  309. 'username' => $user['username'],
  310. ])->delete()) {
  311. DB::table('project_log')->insert([
  312. 'type' => '日志',
  313. 'projectid' => $projectDetail['id'],
  314. 'username' => $user['username'],
  315. 'detail' => '取消收藏',
  316. 'indate' => Base::time()
  317. ]);
  318. return Base::retSuccess('取消成功!');
  319. }
  320. return Base::retSuccess('已取消!');
  321. }
  322. default: {
  323. $row = Base::DBC2A(DB::table('project_users')->where([
  324. 'type' => '收藏',
  325. 'projectid' => $projectDetail['id'],
  326. 'username' => $user['username'],
  327. ])->lockForUpdate()->first());
  328. if (empty($row)) {
  329. DB::table('project_users')->insert([
  330. 'type' => '收藏',
  331. 'projectid' => $projectDetail['id'],
  332. 'isowner' => $projectDetail['username'] == $user['username'] ? 1 : 0,
  333. 'username' => $user['username'],
  334. 'indate' => Base::time()
  335. ]);
  336. DB::table('project_log')->insert([
  337. 'type' => '日志',
  338. 'projectid' => $projectDetail['id'],
  339. 'username' => $user['username'],
  340. 'detail' => '收藏项目',
  341. 'indate' => Base::time()
  342. ]);
  343. return Base::retSuccess('收藏成功!');
  344. }
  345. return Base::retSuccess('已收藏!');
  346. }
  347. }
  348. });
  349. }
  350. /**
  351. * 重命名项目
  352. *
  353. * @apiParam {Number} projectid 项目ID
  354. * @apiParam {String} title 项目新名称
  355. */
  356. public function rename()
  357. {
  358. $user = Users::authE();
  359. if (Base::isError($user)) {
  360. return $user;
  361. } else {
  362. $user = $user['data'];
  363. }
  364. //
  365. $projectid = trim(Request::input('projectid'));
  366. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  367. if (empty($projectDetail)) {
  368. return Base::retError('项目不存在或已被删除!');
  369. }
  370. if ($projectDetail['username'] != $user['username']) {
  371. return Base::retError('你不是项目负责人!');
  372. }
  373. //
  374. $title = trim(Request::input('title'));
  375. if (mb_strlen($title) < 2) {
  376. return Base::retError('项目名称不可以少于2个字!');
  377. } elseif (mb_strlen($title) > 32) {
  378. return Base::retError('项目名称最多只能设置32个字!');
  379. }
  380. //
  381. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  382. 'title' => $title
  383. ]);
  384. DB::table('project_log')->insert([
  385. 'type' => '日志',
  386. 'projectid' => $projectDetail['id'],
  387. 'username' => $user['username'],
  388. 'detail' => '【' . $projectDetail['title'] . '】重命名【' . $title . '】',
  389. 'indate' => Base::time()
  390. ]);
  391. //
  392. return Base::retSuccess('修改成功!');
  393. }
  394. /**
  395. * 移交项目
  396. *
  397. * @apiParam {Number} projectid 项目ID
  398. * @apiParam {String} username 项目新负责人用户名
  399. *
  400. * @throws \Throwable
  401. */
  402. public function transfer()
  403. {
  404. $user = Users::authE();
  405. if (Base::isError($user)) {
  406. return $user;
  407. } else {
  408. $user = $user['data'];
  409. }
  410. //
  411. $projectid = trim(Request::input('projectid'));
  412. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  413. if (empty($projectDetail)) {
  414. return Base::retError('项目不存在或已被删除!');
  415. }
  416. if ($projectDetail['username'] != $user['username']) {
  417. return Base::retError('你不是项目负责人!');
  418. }
  419. //
  420. $username = trim(Request::input('username'));
  421. if ($username == $projectDetail['username']) {
  422. return Base::retError('你已是项目负责人!');
  423. }
  424. $count = DB::table('users')->where('username', $username)->count();
  425. if ($count <= 0) {
  426. return Base::retError(['成员用户名(%)不存在!', $username]);
  427. }
  428. //判断是否已在项目成员内
  429. $inRes = Project::inThe($projectDetail['id'], $username);
  430. if (Base::isError($inRes)) {
  431. DB::table('project_users')->insert([
  432. 'type' => '成员',
  433. 'projectid' => $projectDetail['id'],
  434. 'isowner' => 0,
  435. 'username' => $username,
  436. 'indate' => Base::time()
  437. ]);
  438. DB::table('project_log')->insert([
  439. 'type' => '日志',
  440. 'projectid' => $projectDetail['id'],
  441. 'username' => $username,
  442. 'detail' => '自动加入项目',
  443. 'indate' => Base::time()
  444. ]);
  445. }
  446. //开始移交
  447. return DB::transaction(function () use ($user, $username, $projectDetail) {
  448. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  449. 'username' => $username
  450. ]);
  451. DB::table('project_log')->insert([
  452. 'type' => '日志',
  453. 'projectid' => $projectDetail['id'],
  454. 'username' => $user['username'],
  455. 'detail' => '【' . $projectDetail['username'] . '】移交给【' . $username . '】',
  456. 'indate' => Base::time()
  457. ]);
  458. DB::table('project_users')->where([
  459. 'projectid' => $projectDetail['id'],
  460. 'username' => $projectDetail['username'],
  461. ])->update([
  462. 'isowner' => 0
  463. ]);
  464. DB::table('project_users')->where([
  465. 'projectid' => $projectDetail['id'],
  466. 'username' => $username,
  467. ])->update([
  468. 'isowner' => 1
  469. ]);
  470. return Base::retSuccess('移交成功!');
  471. });
  472. }
  473. /**
  474. * 删除项目
  475. *
  476. * @apiParam {Number} projectid 项目ID
  477. */
  478. public function delete()
  479. {
  480. $user = Users::authE();
  481. if (Base::isError($user)) {
  482. return $user;
  483. } else {
  484. $user = $user['data'];
  485. }
  486. //
  487. $projectid = trim(Request::input('projectid'));
  488. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  489. if (empty($projectDetail)) {
  490. return Base::retError('项目不存在或已被删除!');
  491. }
  492. if ($projectDetail['username'] != $user['username']) {
  493. return Base::retError('你不是项目负责人!');
  494. }
  495. //
  496. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  497. 'delete' => 1,
  498. 'deletedate' => Base::time()
  499. ]);
  500. DB::table('project_task')->where('projectid', $projectDetail['id'])->update([
  501. 'delete' => 1,
  502. 'deletedate' => Base::time()
  503. ]);
  504. DB::table('project_files')->where('projectid', $projectDetail['id'])->update([
  505. 'delete' => 1,
  506. 'deletedate' => Base::time()
  507. ]);
  508. DB::table('project_log')->insert([
  509. 'type' => '日志',
  510. 'projectid' => $projectDetail['id'],
  511. 'username' => $user['username'],
  512. 'detail' => '删除项目',
  513. 'indate' => Base::time()
  514. ]);
  515. //
  516. return Base::retSuccess('删除成功!');
  517. }
  518. /**
  519. * 排序任务
  520. *
  521. * @apiParam {Number} projectid 项目ID
  522. * @apiParam {String} oldsort 旧排序数据
  523. * @apiParam {String} newsort 新排序数据
  524. * @apiParam {Number} label 赋值表示排序分类,否则排序任务(调整任务所属分类)
  525. */
  526. public function sort()
  527. {
  528. $user = Users::authE();
  529. if (Base::isError($user)) {
  530. return $user;
  531. } else {
  532. $user = $user['data'];
  533. }
  534. //
  535. $projectid = trim(Request::input('projectid'));
  536. $inRes = Project::inThe($projectid, $user['username']);
  537. if (Base::isError($inRes)) {
  538. return $inRes;
  539. }
  540. //
  541. $oldSort = explode(";", Request::input('oldsort'));
  542. $newSort = explode(";", Request::input('newsort'));
  543. if (count($oldSort) != count($newSort)) {
  544. return Base::retError('参数错误!');
  545. }
  546. if (intval(Request::input('label'))) {
  547. //排序分类
  548. foreach ($newSort AS $sort => $item) {
  549. list($newLabelid, $newTask) = explode(':', $item);
  550. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  551. if ($newLabelid != $oldLabelid) {
  552. DB::table('project_label')->where([
  553. 'id' => $newLabelid,
  554. 'projectid' => $projectid
  555. ])->update([
  556. 'inorder' => intval($sort)
  557. ]);
  558. }
  559. }
  560. $detail = '调整任务列表排序';
  561. $sortType = 'label';
  562. } else {
  563. //排序任务(调整任务归类)
  564. foreach ($newSort AS $sort => $item) {
  565. list($newLabelid, $newTask) = explode(':', $item);
  566. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  567. if ($newTask != $oldTask) {
  568. $newTask = explode('-', $newTask);
  569. $inorder = count($newTask);
  570. foreach ($newTask AS $taskid) {
  571. DB::table('project_task')->where([
  572. 'id' => $taskid,
  573. 'projectid' => $projectid
  574. ])->update([
  575. 'labelid' => $newLabelid,
  576. 'inorder' => $inorder
  577. ]);
  578. $inorder--;
  579. }
  580. }
  581. }
  582. $detail = '调整任务排序';
  583. $sortType = 'task';
  584. }
  585. //
  586. $row = Base::DBC2A(DB::table('project_log')->where([ 'type' => '日志', 'projectid' => $projectid ])->orderByDesc('id')->first());
  587. $continue = 1;
  588. if ($row && $row['username'] == $user['username'] && $row['indate'] + 300 > Base::time()) {
  589. $other = Base::string2array($row['other']);
  590. if ($other['sortType'] == $sortType) {
  591. $continue = intval($other['continue']) + 1;
  592. if ($continue <= 100) {
  593. DB::table('project_log')->where('id', $row['id'])->update([
  594. 'detail' => $detail . '(' . $continue . '次)',
  595. 'other' => Base::array2string([
  596. 'sortType' => $sortType,
  597. 'continue' => $continue,
  598. 'times' => $other['times'] . '|' . Base::time(),
  599. ])
  600. ]);
  601. }
  602. }
  603. }
  604. if ($continue == 1) {
  605. DB::table('project_log')->insert([
  606. 'type' => '日志',
  607. 'projectid' => $projectid,
  608. 'username' => $user['username'],
  609. 'detail' => $detail,
  610. 'indate' => Base::time(),
  611. 'other' => Base::array2string([
  612. 'sortType' => $sortType,
  613. 'continue' => $continue,
  614. 'times' => Base::time(),
  615. ])
  616. ]);
  617. }
  618. return Base::retSuccess('保存成功!');
  619. }
  620. /**
  621. * 排序任务(todo待办)
  622. *
  623. * @apiParam {String} oldsort 旧排序数据
  624. * @apiParam {String} newsort 新排序数据
  625. */
  626. public function sort__todo()
  627. {
  628. $user = Users::authE();
  629. if (Base::isError($user)) {
  630. return $user;
  631. } else {
  632. $user = $user['data'];
  633. }
  634. //
  635. $oldSort = explode(";", Request::input('oldsort'));
  636. $newSort = explode(";", Request::input('newsort'));
  637. if (count($oldSort) != count($newSort)) {
  638. return Base::retError('参数错误!');
  639. }
  640. //
  641. $levels = [];
  642. $logArray = [];
  643. $taskLevel = [];
  644. foreach ($newSort AS $sort => $item) {
  645. list($newLevel, $newTask) = explode(':', $item);
  646. list($oldLevel, $oldTask) = explode(':', $oldSort[$sort]);
  647. if ($newTask != $oldTask) {
  648. $newTask = explode('-', $newTask);
  649. $oldTask = explode('-', $oldTask);
  650. $userorder = intval(DB::table('project_task')->select('userorder')->where([
  651. 'delete' => 0,
  652. 'archived' => 0,
  653. 'level' => $newLevel,
  654. 'username' => $user['username'],
  655. ])->orderByDesc('userorder')->value('userorder'));
  656. if (count($newTask) < count($oldTask)) {
  657. $userorder--;
  658. } else {
  659. $userorder++;
  660. }
  661. foreach ($newTask AS $taskid) {
  662. $task = Base::DBC2A(DB::table('project_task')->select(['id', 'title', 'projectid', 'level', 'userorder'])->where([
  663. 'id' => $taskid,
  664. 'username' => $user['username']
  665. ])->first());
  666. $upArray = [];
  667. if ($task) {
  668. if ($task['level'] != $newLevel) {
  669. $upArray['level'] = $newLevel;
  670. $logArray[] = [
  671. 'type' => '日志',
  672. 'projectid' => $task['projectid'],
  673. 'taskid' => $task['id'],
  674. 'username' => $user['username'],
  675. 'detail' => '调整任务等级为【P' . $newLevel . '】',
  676. 'indate' => Base::time(),
  677. 'other' => Base::array2string([
  678. 'type' => 'task',
  679. 'id' => $task['id'],
  680. 'title' => $task['title'],
  681. ])
  682. ];
  683. $taskLevel[] = [
  684. 'id' => $task['id'],
  685. 'level' => $newLevel,
  686. ];
  687. }
  688. if ($task['userorder'] != $userorder) {
  689. $upArray['userorder'] = $userorder;
  690. }
  691. }
  692. if ($upArray) {
  693. DB::table('project_task')->where('id', $taskid)->update($upArray);
  694. }
  695. $userorder--;
  696. }
  697. $levels[] = $newLevel;
  698. }
  699. }
  700. if ($logArray) {
  701. DB::table('project_log')->insert($logArray);
  702. }
  703. //
  704. return Base::retSuccess('保存成功!', [
  705. 'levels' => $levels,
  706. 'taskLevel' => $taskLevel,
  707. ]);
  708. }
  709. /**
  710. * 退出项目
  711. *
  712. * @apiParam {Number} projectid 项目ID
  713. */
  714. public function out()
  715. {
  716. $user = Users::authE();
  717. if (Base::isError($user)) {
  718. return $user;
  719. } else {
  720. $user = $user['data'];
  721. }
  722. //
  723. $projectid = trim(Request::input('projectid'));
  724. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  725. if (empty($projectDetail)) {
  726. return Base::retError('项目不存在或已被删除!');
  727. }
  728. if ($projectDetail['username'] == $user['username']) {
  729. return Base::retError('你是项目负责人,不可退出项目!');
  730. }
  731. $inRes = Project::inThe($projectid, $user['username']);
  732. if (Base::isError($inRes)) {
  733. return $inRes;
  734. }
  735. //
  736. DB::table('project_users')->where([
  737. 'type' => '成员',
  738. 'projectid' => $projectDetail['id'],
  739. 'username' => $user['username'],
  740. ])->delete();
  741. DB::table('project_log')->insert([
  742. 'type' => '日志',
  743. 'projectid' => $projectDetail['id'],
  744. 'username' => $user['username'],
  745. 'detail' => '退出项目',
  746. 'indate' => Base::time()
  747. ]);
  748. //
  749. return Base::retSuccess('退出项目成功!');
  750. }
  751. /**
  752. * 项目成员-列表
  753. *
  754. * @apiParam {Number} projectid 项目ID
  755. * @apiParam {Number} [page] 当前页,默认:1
  756. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  757. */
  758. public function users__lists()
  759. {
  760. $user = Users::authE();
  761. if (Base::isError($user)) {
  762. return $user;
  763. } else {
  764. $user = $user['data'];
  765. }
  766. //
  767. $projectid = intval(Request::input('projectid'));
  768. $inRes = Project::inThe($projectid, $user['username']);
  769. if (Base::isError($inRes)) {
  770. return $inRes;
  771. }
  772. //
  773. $lists = DB::table('project_lists')
  774. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  775. ->select(['project_lists.title', 'project_users.*'])
  776. ->where([
  777. ['project_lists.id', $projectid],
  778. ['project_lists.delete', 0],
  779. ['project_users.type', '成员'],
  780. ])
  781. ->orderByDesc('project_users.isowner')->orderByDesc('project_users.id')->paginate(Base::getPaginate(100, 20));
  782. $lists = Base::getPageList($lists);
  783. if ($lists['total'] == 0) {
  784. return Base::retError('未找到任何相关的成员');
  785. }
  786. foreach ($lists['lists'] AS $key => $projectDetail) {
  787. $userInfo = Users::username2basic($projectDetail['username']);
  788. $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
  789. $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
  790. $lists['lists'][$key]['profession'] = $userInfo['profession'];
  791. }
  792. return Base::retSuccess('success', $lists);
  793. }
  794. /**
  795. * 项目成员-添加、删除
  796. *
  797. * @apiParam {String} act
  798. * - delete: 删除成员
  799. * - else: 添加成员
  800. * @apiParam {Number} projectid 项目ID
  801. * @apiParam {Array|String} username 用户名(或用户名组)
  802. */
  803. public function users__join()
  804. {
  805. $user = Users::authE();
  806. if (Base::isError($user)) {
  807. return $user;
  808. } else {
  809. $user = $user['data'];
  810. }
  811. //
  812. $projectid = trim(Request::input('projectid'));
  813. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  814. if (empty($projectDetail)) {
  815. return Base::retError('项目不存在或已被删除!');
  816. }
  817. $usernames = Request::input('username');
  818. if (empty($usernames)) {
  819. return Base::retError('参数错误!');
  820. }
  821. if (!is_array($usernames)) {
  822. if (Base::strExists($usernames, ',')) {
  823. $usernames = explode(',', $usernames);
  824. } else {
  825. $usernames = [$usernames];
  826. }
  827. }
  828. //
  829. $logArray = [];
  830. foreach ($usernames AS $username) {
  831. $inRes = Project::inThe($projectid, $username);
  832. switch (Request::input('act')) {
  833. case 'delete': {
  834. if (!Base::isError($inRes) && $projectDetail['username'] != $username) {
  835. DB::table('project_users')->where([
  836. 'type' => '成员',
  837. 'projectid' => $projectid,
  838. 'username' => $username,
  839. ])->delete();
  840. $logArray[] = [
  841. 'type' => '日志',
  842. 'projectid' => $projectDetail['id'],
  843. 'username' => $user['username'],
  844. 'detail' => '将成员移出项目',
  845. 'indate' => Base::time(),
  846. 'other' => Base::array2string([
  847. 'type' => 'username',
  848. 'username' => $username,
  849. ])
  850. ];
  851. }
  852. break;
  853. }
  854. default: {
  855. if (Base::isError($inRes)) {
  856. DB::table('project_users')->insert([
  857. 'type' => '成员',
  858. 'projectid' => $projectid,
  859. 'isowner' => 0,
  860. 'username' => $username,
  861. 'indate' => Base::time()
  862. ]);
  863. $logArray[] = [
  864. 'type' => '日志',
  865. 'projectid' => $projectDetail['id'],
  866. 'username' => $user['username'],
  867. 'detail' => '邀请成员加入项目',
  868. 'indate' => Base::time(),
  869. 'other' => Base::array2string([
  870. 'type' => 'username',
  871. 'username' => $username,
  872. ])
  873. ];
  874. }
  875. break;
  876. }
  877. }
  878. }
  879. if ($logArray) {
  880. DB::table('project_log')->insert($logArray);
  881. }
  882. return Base::retSuccess('操作完成!');
  883. }
  884. /**
  885. * 项目子分类-添加分类
  886. *
  887. * @apiParam {Number} projectid 项目ID
  888. * @apiParam {String} title 分类名称
  889. */
  890. public function label__add()
  891. {
  892. $user = Users::authE();
  893. if (Base::isError($user)) {
  894. return $user;
  895. } else {
  896. $user = $user['data'];
  897. }
  898. //
  899. $projectid = trim(Request::input('projectid'));
  900. $inRes = Project::inThe($projectid, $user['username']);
  901. if (Base::isError($inRes)) {
  902. return $inRes;
  903. }
  904. //
  905. $title = trim(Request::input('title'));
  906. if (empty($title)) {
  907. return Base::retError('列表名称不能为空!');
  908. } elseif (mb_strlen($title) > 32) {
  909. return Base::retError('列表名称最多只能设置32个字!');
  910. }
  911. //
  912. $count = DB::table('project_label')->where('projectid', $projectid)->where('title', $title)->count();
  913. if ($count > 0) {
  914. return Base::retError('列表名称已存在!');
  915. }
  916. if (DB::table('project_label')->where('projectid', $projectid)->count() + 1 >= 100) {
  917. return Base::retError(['列表最多不能超过%个!', 100]);
  918. }
  919. //
  920. $id = DB::table('project_label')->insertGetId([
  921. 'projectid' => $projectid,
  922. 'title' => $title,
  923. 'inorder' => intval(DB::table('project_label')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  924. ]);
  925. if (empty($id)) {
  926. return Base::retError('系统繁忙,请稍后再试!');
  927. }
  928. DB::table('project_log')->insert([
  929. 'type' => '日志',
  930. 'projectid' => $projectid,
  931. 'username' => $user['username'],
  932. 'detail' => '添加任务列表【' . $title . '】',
  933. 'indate' => Base::time()
  934. ]);
  935. //
  936. $row = Base::DBC2A(DB::table('project_label')->where('id', $id)->first());
  937. $row['taskLists'] = [];
  938. return Base::retSuccess('添加成功!', $row);
  939. }
  940. /**
  941. * 项目子分类-重命名分类
  942. *
  943. * @apiParam {Number} projectid 项目ID
  944. * @apiParam {Number} labelid 分类ID
  945. * @apiParam {String} title 新分类名称
  946. */
  947. public function label__rename()
  948. {
  949. $user = Users::authE();
  950. if (Base::isError($user)) {
  951. return $user;
  952. } else {
  953. $user = $user['data'];
  954. }
  955. //
  956. $projectid = trim(Request::input('projectid'));
  957. $inRes = Project::inThe($projectid, $user['username']);
  958. if (Base::isError($inRes)) {
  959. return $inRes;
  960. }
  961. //
  962. $title = trim(Request::input('title'));
  963. if (empty($title)) {
  964. return Base::retError('列表名称不能为空!');
  965. } elseif (mb_strlen($title) > 32) {
  966. return Base::retError('列表名称最多只能设置32个字!');
  967. }
  968. //
  969. $labelid = intval(Request::input('labelid'));
  970. $count = DB::table('project_label')->where('id', '!=', $labelid)->where('projectid', $projectid)->where('title', $title)->count();
  971. if ($count > 0) {
  972. return Base::retError('列表名称已存在!');
  973. }
  974. //
  975. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  976. if (empty($labelDetail)) {
  977. return Base::retError('列表不存在或已被删除!');
  978. }
  979. //
  980. if (DB::table('project_label')->where('id', $labelDetail['id'])->update([ 'title' => $title ])) {
  981. DB::table('project_log')->insert([
  982. 'type' => '日志',
  983. 'projectid' => $projectid,
  984. 'username' => $user['username'],
  985. 'detail' => '任务列表【' . $labelDetail['title'] . '】重命名【' . $title . '】',
  986. 'indate' => Base::time()
  987. ]);
  988. }
  989. //
  990. return Base::retSuccess('修改成功!');
  991. }
  992. /**
  993. * 项目子分类-删除分类
  994. *
  995. * @apiParam {Number} projectid 项目ID
  996. * @apiParam {Number} labelid 分类ID
  997. *
  998. * @throws \Throwable
  999. */
  1000. public function label__delete()
  1001. {
  1002. $user = Users::authE();
  1003. if (Base::isError($user)) {
  1004. return $user;
  1005. } else {
  1006. $user = $user['data'];
  1007. }
  1008. //
  1009. $projectid = trim(Request::input('projectid'));
  1010. $inRes = Project::inThe($projectid, $user['username']);
  1011. if (Base::isError($inRes)) {
  1012. return $inRes;
  1013. }
  1014. //
  1015. $labelid = intval(Request::input('labelid'));
  1016. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1017. if (empty($labelDetail)) {
  1018. return Base::retError('列表不存在或已被删除!');
  1019. }
  1020. //
  1021. return DB::transaction(function () use ($user, $projectid, $labelDetail) {
  1022. $taskLists = Base::DBC2A(DB::table('project_task')->where('labelid', $labelDetail['id'])->get());
  1023. $logArray = [];
  1024. foreach ($taskLists AS $task) {
  1025. $logArray[] = [
  1026. 'type' => '日志',
  1027. 'projectid' => $projectid,
  1028. 'taskid' => $task['id'],
  1029. 'username' => $user['username'],
  1030. 'detail' => '删除列表任务',
  1031. 'indate' => Base::time(),
  1032. 'other' => Base::array2string([
  1033. 'type' => 'task',
  1034. 'id' => $task['id'],
  1035. 'title' => $task['title'],
  1036. ])
  1037. ];
  1038. }
  1039. $logArray[] = [
  1040. 'type' => '日志',
  1041. 'projectid' => $projectid,
  1042. 'taskid' => 0,
  1043. 'username' => $user['username'],
  1044. 'detail' => '删除任务列表【' . $labelDetail['title'] . '】',
  1045. 'indate' => Base::time(),
  1046. 'other' => Base::array2string([])
  1047. ];
  1048. DB::table('project_task')->where('labelid', $labelDetail['id'])->update([
  1049. 'delete' => 1,
  1050. 'deletedate' => Base::time()
  1051. ]);
  1052. DB::table('project_label')->where('id', $labelDetail['id'])->delete();
  1053. DB::table('project_log')->insert($logArray);
  1054. Project::updateNum($projectid);
  1055. //
  1056. return Base::retSuccess('删除成功!');
  1057. });
  1058. }
  1059. /**
  1060. * 项目任务-列表
  1061. *
  1062. * @apiParam {Number} [projectid] 项目ID
  1063. * @apiParam {Number} [labelid] 项目子分类ID
  1064. * @apiParam {String} [username] 负责人用户名(如果项目ID为空时此参数无效只获取自己的任务)
  1065. * @apiParam {Number} [level] 任务等级(1~4)
  1066. * @apiParam {String} [archived] 任务是否归档
  1067. * - 未归档 (默认)
  1068. * - 已归档
  1069. * - 全部
  1070. * @apiParam {String} [type] 任务类型
  1071. * - 全部(默认)
  1072. * - 未完成
  1073. * - 已超期
  1074. * - 已完成
  1075. * @apiParam {Number} [createuser] 是否仅获取自己创建的项目(1:是;赋值时projectid和username不强制)
  1076. * @apiParam {Number} [attention] 是否仅获取关注数据(1:是;赋值时projectid和username不强制)
  1077. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  1078. * @apiParam {String} [startdate] 任务开始时间,格式:YYYY-MM-DD
  1079. * @apiParam {String} [enddate] 任务结束时间,格式:YYYY-MM-DD
  1080. *
  1081. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  1082. * - key: title|labelid|enddate|username|level|indate|type|inorder(默认)|userorder
  1083. * - order: asc|desc
  1084. * - 【archived=已归档】或【startdate和enddate赋值】时无效
  1085. *
  1086. * @apiParam {Number} [page] 当前页,默认:1
  1087. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1088. * @apiParam {Number} [export] 是否导出并返回下载地址(1:是;仅支持projectid赋值时)
  1089. */
  1090. public function task__lists()
  1091. {
  1092. $user = Users::authE();
  1093. if (Base::isError($user)) {
  1094. return $user;
  1095. } else {
  1096. $user = $user['data'];
  1097. }
  1098. //
  1099. $projectid = intval(Request::input('projectid'));
  1100. $export = intval(Request::input('export'));
  1101. if ($projectid > 0) {
  1102. $inRes = Project::inThe($projectid, $user['username']);
  1103. if (Base::isError($inRes)) {
  1104. return $inRes;
  1105. }
  1106. } else {
  1107. $export = 0;
  1108. }
  1109. //
  1110. $orderBy = '`inorder` DESC,`id` DESC';
  1111. $sorts = Base::json2array(Request::input('sorts'));
  1112. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1113. switch ($sorts['key']) {
  1114. case 'title':
  1115. case 'labelid':
  1116. case 'enddate':
  1117. case 'username':
  1118. case 'level':
  1119. case 'indate':
  1120. case 'inorder':
  1121. case 'userorder':
  1122. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1123. break;
  1124. case 'type':
  1125. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['order'] . ', `complete` ' . $sorts['order'] . ',`id` DESC';
  1126. break;
  1127. }
  1128. }
  1129. //
  1130. $builder = DB::table('project_task');
  1131. $selectArray = ['project_task.*'];
  1132. $whereRaw = null;
  1133. $whereFunc = null;
  1134. $whereArray = [];
  1135. $whereArray[] = ['project_task.delete', '=', 0];
  1136. if (intval(Request::input('createuser')) === 1) {
  1137. $whereArray[] = ['project_task.createuser', '=', $user['username']];
  1138. if ($projectid > 0) {
  1139. $whereArray[] = ['project_lists.id', '=', $projectid];
  1140. }
  1141. if (trim(Request::input('username'))) {
  1142. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1143. }
  1144. } else if (intval(Request::input('attention')) === 1) {
  1145. if ($projectid > 0) {
  1146. $whereArray[] = ['project_lists.id', '=', $projectid];
  1147. }
  1148. if (trim(Request::input('username'))) {
  1149. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1150. }
  1151. } else {
  1152. if ($projectid > 0) {
  1153. $whereArray[] = ['project_lists.id', '=', $projectid];
  1154. if (trim(Request::input('username'))) {
  1155. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1156. }
  1157. } else {
  1158. $builder->where(function ($query) use ($user) {
  1159. $query->where('project_task.username', $user['username']);
  1160. $query->orWhereIn('project_task.id', function ($inQuery) use ($user) {
  1161. $inQuery->from('project_users')
  1162. ->select('taskid')
  1163. ->where('username', $user['username'])
  1164. ->where('type', '负责人');
  1165. });
  1166. });
  1167. }
  1168. }
  1169. if (intval(Request::input('labelid')) > 0) {
  1170. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  1171. }
  1172. if (intval(Request::input('level')) > 0) {
  1173. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  1174. }
  1175. $archived = trim(Request::input('archived'));
  1176. if (empty($archived)) $archived = "未归档";
  1177. switch ($archived) {
  1178. case '已归档':
  1179. $whereArray[] = ['project_task.archived', '=', 1];
  1180. $orderBy = '`archiveddate` DESC';
  1181. break;
  1182. case '未归档':
  1183. $whereArray[] = ['project_task.archived', '=', 0];
  1184. break;
  1185. }
  1186. $type = trim(Request::input('type'));
  1187. switch ($type) {
  1188. case '未完成':
  1189. $whereArray[] = ['project_task.complete', '=', 0];
  1190. break;
  1191. case '已超期':
  1192. $whereArray[] = ['project_task.complete', '=', 0];
  1193. $whereArray[] = ['project_task.enddate', '>', 0];
  1194. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  1195. break;
  1196. case '已完成':
  1197. $whereArray[] = ['project_task.complete', '=', 1];
  1198. break;
  1199. }
  1200. $startdate = trim(Request::input('startdate'));
  1201. $enddate = trim(Request::input('enddate'));
  1202. if (Base::isDate($startdate) || Base::isDate($enddate)) {
  1203. $startdate = strtotime($startdate . ' 00:00:00');
  1204. $enddate = strtotime($enddate . ' 23:59:59');
  1205. $whereRaw.= $whereRaw ? ' AND ' : '';
  1206. $whereRaw.= "((`startdate` >= " . $startdate . " OR `startdate` = 0) AND (`enddate` <= " . $enddate . " OR `enddate` = 0))";
  1207. $orderBy = '`startdate` DESC';
  1208. }
  1209. //
  1210. if ($projectid > 0) {
  1211. $builder->join('project_lists', 'project_lists.id', '=', 'project_task.projectid');
  1212. }
  1213. if (intval(Request::input('attention')) === 1) {
  1214. $builder->join('project_users', 'project_users.taskid', '=', 'project_task.id');
  1215. $builder->where([
  1216. ['project_users.type', '=', '关注'],
  1217. ['project_users.username', '=', $user['username']],
  1218. ]);
  1219. $selectArray[] = 'project_users.indate AS attentiondate';
  1220. }
  1221. if ($whereRaw) {
  1222. $builder->whereRaw($whereRaw);
  1223. }
  1224. $builder->select($selectArray)->where($whereArray)->orderByRaw($orderBy);
  1225. if ($export) {
  1226. //导出excel
  1227. $checkRole = Project::role('project_role_export', $projectid);
  1228. if (Base::isError($checkRole)) {
  1229. return $checkRole;
  1230. }
  1231. $lists = Base::DBC2A($builder->get());
  1232. if (empty($lists)) {
  1233. return Base::retError('未找到任何相关的任务!');
  1234. }
  1235. $labels = DB::table('project_label')->select(['id', 'title'])->where('projectid', $projectid)->pluck('title', 'id');
  1236. $fileName = str_replace(['/', '\\', ':', '*', '"', '<', '>', '|', '?'], '_', $checkRole['data']['title'] ?: $projectid) . '_' . Base::time() . '.xls';
  1237. $filePath = "temp/task/export/" . date("Ym", Base::time());
  1238. $headings = [];
  1239. $headings[] = '任务ID';
  1240. $headings[] = '任务标题';
  1241. $headings[] = '任务阶段';
  1242. $headings[] = '计划时间(开始)';
  1243. $headings[] = '计划时间(结束)';
  1244. $headings[] = '负责人';
  1245. $headings[] = '负责人(昵称)';
  1246. $headings[] = '创建人';
  1247. $headings[] = '创建人(昵称)';
  1248. $headings[] = '优先级';
  1249. $headings[] = '状态';
  1250. $headings[] = '是否超期';
  1251. $headings[] = '创建时间';
  1252. $data = [];
  1253. foreach ($lists AS $info) {
  1254. $overdue = Project::taskIsOverdue($info);
  1255. $data[] = [
  1256. $info['id'],
  1257. $info['title'],
  1258. $labels[$info['labelid']] ?: '-',
  1259. $info['startdate'] ? date("Y-m-d H:i:s", $info['startdate']) : '-',
  1260. $info['enddate'] ? date("Y-m-d H:i:s", $info['enddate']) : '-',
  1261. $info['username'],
  1262. DBCache::table('users')->where('username', $info['username'])->value('nickname') ?: $info['username'],
  1263. $info['createuser'],
  1264. DBCache::table('users')->where('username', $info['createuser'])->value('nickname') ?: $info['createuser'],
  1265. 'P' . $info['level'],
  1266. ($overdue ? '已超期' : ($info['complete'] ? '已完成' : '未完成')),
  1267. $overdue ? '是' : '-',
  1268. date("Y-m-d H:i:s", $info['indate'])
  1269. ];
  1270. $subtask = Base::string2array($info['subtask']);
  1271. if ($subtask) {
  1272. foreach ($subtask AS $subInfo) {
  1273. $data[] = [
  1274. '',
  1275. $subInfo['detail'],
  1276. '',
  1277. '',
  1278. '',
  1279. $subInfo['uname'] ?: '',
  1280. $subInfo['uname'] ? (DBCache::table('users')->where('username', $subInfo['uname'])->value('nickname') ?: $subInfo['uname']) : $subInfo['uname'],
  1281. '',
  1282. '',
  1283. '',
  1284. $subInfo['status'] == 'complete' ? '已完成' : '未完成',
  1285. '',
  1286. date("Y-m-d H:i:s", $subInfo['time'])
  1287. ];
  1288. }
  1289. }
  1290. }
  1291. $res = BillExport::create()->setHeadings($headings)->setData($data)->store($filePath . "/" . $fileName);
  1292. if ($res != 1) {
  1293. return Base::retError(['导出失败,%!', $fileName]);
  1294. }
  1295. //
  1296. $xlsPath = storage_path("app/" . $filePath . "/" . $fileName);
  1297. $zipFile = "app/" . $filePath . "/" . Base::rightDelete($fileName, '.xls'). ".zip";
  1298. $zipPath = storage_path($zipFile);
  1299. if (file_exists($zipPath)) {
  1300. Base::deleteDirAndFile($zipPath, true);
  1301. }
  1302. try {
  1303. Madzipper::make($zipPath)->add($xlsPath)->close();
  1304. } catch (\Exception $e) { }
  1305. //
  1306. if (file_exists($zipPath)) {
  1307. $base64 = base64_encode(Base::array2string([
  1308. 'projectid' => $projectid,
  1309. 'file' => $zipFile,
  1310. ]));
  1311. Session::put('task::export:username', $user['username']);
  1312. return Base::retSuccess("success", [
  1313. 'size' => Base::twoFloat(filesize($zipPath) / 1024, true),
  1314. 'url' => Base::fillUrl('api/project/task/export?data=' . urlencode($base64)),
  1315. ]);
  1316. } else {
  1317. return Base::retError('打包失败,请稍后再试...');
  1318. }
  1319. }
  1320. $lists = $builder->paginate(Base::getPaginate(100, 20));
  1321. $lists = Base::getPageList($lists);
  1322. if (intval(Request::input('statistics')) == 1) {
  1323. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 0)->count();
  1324. $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();
  1325. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 1)->count();
  1326. }
  1327. if ($lists['total'] == 0) {
  1328. return Base::retError('未找到任何相关的任务!', $lists);
  1329. }
  1330. foreach ($lists['lists'] AS $key => $info) {
  1331. $info['persons'] = Project::taskPersons($info);
  1332. $info['overdue'] = Project::taskIsOverdue($info);
  1333. $info['subtask'] = Base::string2array($info['subtask']);
  1334. $info['follower'] = Base::string2array($info['follower']);
  1335. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  1336. }
  1337. return Base::retSuccess('success', $lists);
  1338. }
  1339. /**
  1340. * 项目任务-导出结果
  1341. *
  1342. * @apiParam {String} data base64
  1343. */
  1344. public function task__export()
  1345. {
  1346. $username = Session::get('task::export:username');
  1347. if (empty($username)) {
  1348. return Base::ajaxError("请求已过期,请重新导出!", [], 0, 502);
  1349. }
  1350. $array = Base::string2array(base64_decode(urldecode(Request::input('data'))));
  1351. $projectid = intval($array['projectid']);
  1352. $file = $array['file'];
  1353. if (empty($projectid)) {
  1354. return Base::ajaxError("参数错误!", [], 0, 502);
  1355. }
  1356. if (empty($file) || !file_exists(storage_path($file))) {
  1357. return Base::ajaxError("文件不存在!", [], 0, 502);
  1358. }
  1359. $checkRole = Project::role('project_role_export', $projectid, 0, $username);
  1360. if (Base::isError($checkRole)) {
  1361. return Base::ajaxError($checkRole['msg'], [], 0, 502);
  1362. }
  1363. return response()->download(storage_path($file), ($checkRole['data']['title'] ?: Base::time()) . '.zip');
  1364. }
  1365. /**
  1366. * 项目任务-详情(与任务有关系的用户(关注的、在项目里的、负责人、创建者)都可以查到)
  1367. *
  1368. * @apiParam {Number} taskid 任务ID
  1369. */
  1370. public function task__detail()
  1371. {
  1372. $user = Users::authE();
  1373. if (Base::isError($user)) {
  1374. return $user;
  1375. } else {
  1376. $user = $user['data'];
  1377. }
  1378. //
  1379. $taskid = intval(Request::input('taskid'));
  1380. $tmpLists = Project::taskSomeUsers($taskid);
  1381. if (!in_array($user['username'], $tmpLists)) {
  1382. return Base::retError('未能找到此任务或无法管理此任务!');
  1383. }
  1384. //
  1385. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1386. $task['persons'] = Project::taskPersons($task);
  1387. $task['overdue'] = Project::taskIsOverdue($task);
  1388. $task['subtask'] = Base::string2array($task['subtask']);
  1389. $task['follower'] = Base::string2array($task['follower']);
  1390. $task = array_merge($task, Users::username2basic($task['username']));
  1391. $task['projectTitle'] = $task['projectid'] > 0 ? DB::table('project_lists')->where('id', $task['projectid'])->value('title') : '';
  1392. return Base::retSuccess('success', $task);
  1393. }
  1394. /**
  1395. * 项目任务-描述(任务有关系的用户(关注的、在项目里的、负责人、创建者)都可以查到)
  1396. *
  1397. * @apiParam {Number} taskid 任务ID
  1398. */
  1399. public function task__desc()
  1400. {
  1401. $user = Users::authE();
  1402. if (Base::isError($user)) {
  1403. return $user;
  1404. } else {
  1405. $user = $user['data'];
  1406. }
  1407. //
  1408. $taskid = intval(Request::input('taskid'));
  1409. $tmpLists = Project::taskSomeUsers($taskid);
  1410. if (!in_array($user['username'], $tmpLists)) {
  1411. return Base::retError('未能找到此任务或无法管理此任务!');
  1412. }
  1413. //
  1414. $desc = DB::table('project_content')->where('taskid', $taskid)->value('content');
  1415. if (empty($desc)) {
  1416. $desc = DB::table('project_task')->where('id', $taskid)->value('desc');
  1417. }
  1418. return Base::retSuccess('success', [
  1419. 'taskid' => $taskid,
  1420. 'desc' => $desc
  1421. ]);
  1422. }
  1423. /**
  1424. * 项目任务-获取数量
  1425. *
  1426. * @apiParam {Number} [level] 任务等级(1~4,留空获取所有)
  1427. */
  1428. public function task__levelnum()
  1429. {
  1430. $user = Users::authE();
  1431. if (Base::isError($user)) {
  1432. return $user;
  1433. } else {
  1434. $user = $user['data'];
  1435. }
  1436. //
  1437. $whereArray = [];
  1438. $whereArray[] = ['project_task.delete', '=', 0];
  1439. $whereArray[] = ['project_task.username', '=', $user['username']];
  1440. $whereArray[] = ['project_task.archived', '=', 0];
  1441. $array = [
  1442. 'level_1' => 0,
  1443. 'level_2' => 0,
  1444. 'level_3' => 0,
  1445. 'level_4' => 0,
  1446. ];
  1447. $level = intval(Request::input('level'));
  1448. if ($level > 0) {
  1449. $array = [];
  1450. $array['level_' . $level] = 0;
  1451. }
  1452. foreach ($array AS $key => $val) {
  1453. $level = intval(Base::leftDelete($key, 'level_'));
  1454. $array[$key] = DB::table('project_task')->where($whereArray)->where('level', $level)->count();
  1455. }
  1456. return Base::retSuccess('success', $array);
  1457. }
  1458. /**
  1459. * 项目任务-添加任务
  1460. *
  1461. * @apiParam {String} title 任务标题
  1462. * @apiParam {Number} [projectid] 项目ID
  1463. * @apiParam {Number} [labelid] 项目子分类ID
  1464. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  1465. * @apiParam {String} [username] 任务负责人用户名(如果项目ID为空时此参数无效,负责人为自己)
  1466. * @apiParam {Number} [insertbottom] 是否添加至列表结尾(1:是,默认:0,仅适用于项目分类列表)
  1467. *
  1468. * @throws \Throwable
  1469. */
  1470. public function task__add()
  1471. {
  1472. $user = Users::authE();
  1473. if (Base::isError($user)) {
  1474. return $user;
  1475. } else {
  1476. $user = $user['data'];
  1477. }
  1478. //
  1479. $projectid = intval(Request::input('projectid'));
  1480. $labelid = intval(Request::input('labelid'));
  1481. $insertbottom = intval(Request::input('insertbottom'));
  1482. if ($projectid > 0) {
  1483. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  1484. if (empty($projectDetail)) {
  1485. return Base::retError('项目不存在或已被删除!');
  1486. }
  1487. //
  1488. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1489. if (empty($labelDetail)) {
  1490. return Base::retError('项目子分类不存在或已被删除!');
  1491. }
  1492. //
  1493. $inRes = Project::inThe($projectid, $user['username']);
  1494. if (Base::isError($inRes)) {
  1495. return $inRes;
  1496. }
  1497. $checkRole = Project::role('add_role', $projectid, 0);
  1498. if (Base::isError($checkRole)) {
  1499. return $checkRole;
  1500. }
  1501. //
  1502. $username = trim(Request::input('username'));
  1503. if (empty($username)) {
  1504. $username = $user['username'];
  1505. }
  1506. if ($username != $user['username']) {
  1507. $inRes = Project::inThe($projectid, $username);
  1508. if (Base::isError($inRes)) {
  1509. return Base::retError('负责人不在项目成员内!');
  1510. }
  1511. }
  1512. } else {
  1513. $username = $user['username'];
  1514. }
  1515. //
  1516. $title = trim(Request::input('title'));
  1517. if (empty($title)) {
  1518. return Base::retError('任务标题不能为空!');
  1519. } elseif (mb_strlen($title) > 255) {
  1520. return Base::retError('任务标题最多只能设置255个字!');
  1521. }
  1522. //
  1523. $level = max(1, min(4, intval(Request::input('level'))));
  1524. if (empty($projectid)) {
  1525. $inorder = 0;
  1526. } else {
  1527. $inorder = intval(DB::table('project_task')->where('projectid', $projectid)->orderBy('inorder', $insertbottom ? 'asc' : 'desc')->value('inorder')) + ($insertbottom ? -1 : 1);
  1528. }
  1529. $userorder = intval(DB::table('project_task')->where('username', $user['username'])->where('level', $level)->orderByDesc('userorder')->value('userorder')) + 1;
  1530. //
  1531. $inArray = [
  1532. 'projectid' => $projectid,
  1533. 'labelid' => $labelid,
  1534. 'createuser' => $user['username'],
  1535. 'username' => $username,
  1536. 'title' => $title,
  1537. 'level' => $level,
  1538. 'inorder' => $inorder,
  1539. 'userorder' => $userorder,
  1540. 'indate' => Base::time(),
  1541. 'startdate' => Base::time(),
  1542. 'subtask' => Base::array2string([]),
  1543. 'follower' => Base::array2string([]),
  1544. ];
  1545. return DB::transaction(function () use ($inArray) {
  1546. $taskid = DB::table('project_task')->insertGetId($inArray);
  1547. if (empty($taskid)) {
  1548. return Base::retError('系统繁忙,请稍后再试!');
  1549. }
  1550. DB::table('project_log')->insert([
  1551. 'type' => '日志',
  1552. 'projectid' => $inArray['projectid'],
  1553. 'taskid' => $taskid,
  1554. 'username' => $inArray['createuser'],
  1555. 'detail' => '添加任务',
  1556. 'indate' => Base::time(),
  1557. 'other' => Base::array2string([
  1558. 'type' => 'task',
  1559. 'id' => $taskid,
  1560. 'title' => $inArray['title'],
  1561. ])
  1562. ]);
  1563. Project::updateNum($inArray['projectid']);
  1564. //
  1565. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1566. $task['persons'] = Project::taskPersons($task);
  1567. $task['overdue'] = Project::taskIsOverdue($task);
  1568. $task['subtask'] = Base::string2array($task['subtask']);
  1569. $task['follower'] = Base::string2array($task['follower']);
  1570. $task = array_merge($task, Users::username2basic($task['username']));
  1571. return Base::retSuccess('添加成功!', $task);
  1572. });
  1573. }
  1574. /**
  1575. * {post} 项目任务-修改
  1576. *
  1577. * @apiParam {Number} taskid 任务ID
  1578. * @apiParam {String} act 修改字段|操作类型
  1579. * - title: 标题
  1580. * - desc: 描述
  1581. * - level: 优先级
  1582. * - username: 负责人
  1583. * - plannedtime: 设置计划时间
  1584. * - unplannedtime: 取消计划时间
  1585. * - complete: 标记完成
  1586. * - unfinished: 标记未完成
  1587. * - archived: 归档
  1588. * - unarchived: 取消归档
  1589. * - delete: 删除任务
  1590. * - comment: 评论
  1591. * - attention: 添加关注
  1592. * - subtask: 修改子任务
  1593. * @apiParam {String} [content] 内容数据
  1594. *
  1595. * @throws \Throwable
  1596. */
  1597. public function task__edit()
  1598. {
  1599. $user = Users::authE();
  1600. if (Base::isError($user)) {
  1601. return $user;
  1602. } else {
  1603. $user = $user['data'];
  1604. }
  1605. //
  1606. $act = trim(Base::getPostValue('act'));
  1607. $taskid = intval(Base::getPostValue('taskid'));
  1608. $task = Base::DBC2A(DB::table('project_task')
  1609. ->where([
  1610. ['delete', '=', 0],
  1611. ['id', '=', $taskid],
  1612. ])
  1613. ->first());
  1614. if (empty($task)) {
  1615. return Base::retError('任务不存在!');
  1616. }
  1617. if ($task['projectid'] > 0) {
  1618. if (!Project::isPersons($task, $user['username'])) {
  1619. $inRes = Project::inThe($task['projectid'], $user['username']);
  1620. if (Base::isError($inRes)) {
  1621. return $inRes;
  1622. }
  1623. }
  1624. if (!in_array($act, ['comment', 'attention'])) {
  1625. $checkRole = Project::role('edit_role', $task['projectid'], $task['id']);
  1626. if (Base::isError($checkRole)) {
  1627. return $checkRole;
  1628. }
  1629. switch ($act) {
  1630. case 'complete':
  1631. case 'unfinished':
  1632. $checkRole = Project::role('complete_role', $task['projectid'], $task['id']);
  1633. if (Base::isError($checkRole)) {
  1634. return $checkRole;
  1635. }
  1636. break;
  1637. case 'archived':
  1638. case 'unarchived':
  1639. $checkRole = Project::role('archived_role', $task['projectid'], $task['id']);
  1640. if (Base::isError($checkRole)) {
  1641. return $checkRole;
  1642. }
  1643. break;
  1644. case 'delete':
  1645. $checkRole = Project::role('del_role', $task['projectid'], $task['id']);
  1646. if (Base::isError($checkRole)) {
  1647. return $checkRole;
  1648. }
  1649. break;
  1650. }
  1651. }
  1652. } else {
  1653. if (!Project::isPersons($task, $user['username'])) {
  1654. return Base::retError('此操作只允许任务负责人!');
  1655. }
  1656. }
  1657. //
  1658. $content = Base::newTrim(Base::getPostValue('content'));
  1659. $message = "";
  1660. $upArray = [];
  1661. $logArray = [];
  1662. switch ($act) {
  1663. /**
  1664. * 修改标题
  1665. */
  1666. case 'title': {
  1667. if ($content == $task['title']) {
  1668. return Base::retError('标题未做改变!');
  1669. }
  1670. $upArray['title'] = $content;
  1671. $logArray[] = [
  1672. 'type' => '日志',
  1673. 'projectid' => $task['projectid'],
  1674. 'taskid' => $task['id'],
  1675. 'username' => $user['username'],
  1676. 'detail' => '修改任务标题',
  1677. 'indate' => Base::time(),
  1678. 'other' => Base::array2string([
  1679. 'type' => 'task',
  1680. 'id' => $task['id'],
  1681. 'title' => $content,
  1682. 'old_title' => $task['title'],
  1683. ])
  1684. ];
  1685. break;
  1686. }
  1687. /**
  1688. * 修改描述
  1689. */
  1690. case 'desc': {
  1691. preg_match_all("/<img\s*src=\"data:image\/(png|jpg|jpeg);base64,(.*?)\"/s", $content, $matchs);
  1692. foreach ($matchs[2] as $key => $text) {
  1693. $p = "uploads/projects/" . ($task['projectid'] ?: Users::token2userid()) . "/";
  1694. Base::makeDir(public_path($p));
  1695. $p.= md5($text) . "." . $matchs[1][$key];
  1696. $r = file_put_contents(public_path($p), base64_decode($text));
  1697. if ($r) {
  1698. $content = str_replace($matchs[0][$key], '<img src="' . Base::fillUrl($p) . '"', $content);
  1699. }
  1700. }
  1701. Base::DBUPIN('project_content', [
  1702. 'taskid' => $task['id'],
  1703. ], [
  1704. 'content' => $content,
  1705. ], [
  1706. 'projectid' => $task['projectid'],
  1707. 'content' => $content,
  1708. 'indate' => Base::time()
  1709. ]);
  1710. $upArray['desc'] = $content ? Base::time() : '';
  1711. $logArray[] = [
  1712. 'type' => '日志',
  1713. 'projectid' => $task['projectid'],
  1714. 'taskid' => $task['id'],
  1715. 'username' => $user['username'],
  1716. 'detail' => '修改任务描述',
  1717. 'indate' => Base::time(),
  1718. 'other' => Base::array2string([
  1719. 'type' => 'task',
  1720. 'id' => $task['id'],
  1721. 'title' => $task['title'],
  1722. 'old_desc' => $task['desc'],
  1723. ])
  1724. ];
  1725. break;
  1726. }
  1727. /**
  1728. * 调整任务等级
  1729. */
  1730. case 'level': {
  1731. $content = intval($content);
  1732. if ($content == $task['level']) {
  1733. return Base::retError('优先级未做改变!');
  1734. }
  1735. if ($content > 4 || $content < 1) {
  1736. return Base::retError('优先级参数错误!');
  1737. }
  1738. $upArray['level'] = $content;
  1739. $logArray[] = [
  1740. 'type' => '日志',
  1741. 'projectid' => $task['projectid'],
  1742. 'taskid' => $task['id'],
  1743. 'username' => $user['username'],
  1744. 'detail' => '调整任务等级为【P' . $content . '】',
  1745. 'indate' => Base::time(),
  1746. 'other' => Base::array2string([
  1747. 'type' => 'task',
  1748. 'id' => $task['id'],
  1749. 'title' => $task['title'],
  1750. 'old_level' => $task['level'],
  1751. ])
  1752. ];
  1753. break;
  1754. }
  1755. /**
  1756. * 修改任务负责人
  1757. */
  1758. case 'username': {
  1759. if ($content == $task['username']) {
  1760. return Base::retError('负责人未做改变!');
  1761. }
  1762. if ($task['projectid'] > 0) {
  1763. $inRes = Project::inThe($task['projectid'], $content);
  1764. if (Base::isError($inRes)) {
  1765. return Base::retError(['%不在成员列表内!', $content]);
  1766. }
  1767. }
  1768. $upArray['username'] = $content;
  1769. $logArray[] = [
  1770. 'type' => '日志',
  1771. 'projectid' => $task['projectid'],
  1772. 'taskid' => $task['id'],
  1773. 'username' => $user['username'],
  1774. 'detail' => '修改负责人',
  1775. 'indate' => Base::time(),
  1776. 'other' => Base::array2string([
  1777. 'type' => 'task',
  1778. 'id' => $task['id'],
  1779. 'title' => $task['title'],
  1780. 'old_username' => $task['username'],
  1781. ])
  1782. ];
  1783. break;
  1784. }
  1785. /**
  1786. * 修改计划时间
  1787. */
  1788. case 'plannedtime': {
  1789. list($startdate, $enddate) = explode(",", $content);
  1790. if (!Base::isDate($startdate) || !Base::isDate($enddate)) {
  1791. return Base::retError('计划时间参数错误!');
  1792. }
  1793. $startdate = strtotime($startdate);
  1794. $enddate = strtotime($enddate);
  1795. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1796. return Base::retError('与原计划时间一致!');
  1797. }
  1798. if ($startdate == $enddate) {
  1799. return Base::retError('开始时间与结束时间一致!');
  1800. }
  1801. $upArray['startdate'] = $startdate;
  1802. $upArray['enddate'] = $enddate;
  1803. $logArray[] = [
  1804. 'type' => '日志',
  1805. 'projectid' => $task['projectid'],
  1806. 'taskid' => $task['id'],
  1807. 'username' => $user['username'],
  1808. 'detail' => '设置计划时间',
  1809. 'indate' => Base::time(),
  1810. 'other' => Base::array2string([
  1811. 'type' => 'task',
  1812. 'id' => $task['id'],
  1813. 'title' => $task['title'],
  1814. 'old_startdate' => $task['startdate'],
  1815. 'old_enddate' => $task['enddate'],
  1816. ])
  1817. ];
  1818. break;
  1819. }
  1820. /**
  1821. * 取消计划时间
  1822. */
  1823. case 'unplannedtime': {
  1824. $startdate = 0;
  1825. $enddate = 0;
  1826. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1827. return Base::retError('与原计划时间一致!');
  1828. }
  1829. $upArray['startdate'] = $startdate;
  1830. $upArray['enddate'] = $enddate;
  1831. $logArray[] = [
  1832. 'type' => '日志',
  1833. 'projectid' => $task['projectid'],
  1834. 'taskid' => $task['id'],
  1835. 'username' => $user['username'],
  1836. 'detail' => '取消计划时间',
  1837. 'indate' => Base::time(),
  1838. 'other' => Base::array2string([
  1839. 'type' => 'task',
  1840. 'id' => $task['id'],
  1841. 'title' => $task['title'],
  1842. 'old_startdate' => $task['startdate'],
  1843. 'old_enddate' => $task['enddate'],
  1844. ])
  1845. ];
  1846. break;
  1847. }
  1848. /**
  1849. * 标记完成
  1850. */
  1851. case 'complete': {
  1852. if ($task['complete'] == 1) {
  1853. return Base::retError('任务已标记完成,请勿重复操作!');
  1854. }
  1855. $upArray['complete'] = 1;
  1856. $upArray['completedate'] = Base::time();
  1857. $logArray[] = [
  1858. 'type' => '日志',
  1859. 'projectid' => $task['projectid'],
  1860. 'taskid' => $task['id'],
  1861. 'username' => $user['username'],
  1862. 'detail' => '标记已完成',
  1863. 'indate' => Base::time(),
  1864. 'other' => Base::array2string([
  1865. 'type' => 'task',
  1866. 'id' => $task['id'],
  1867. 'title' => $task['title'],
  1868. ])
  1869. ];
  1870. break;
  1871. }
  1872. /**
  1873. * 标记未完成
  1874. */
  1875. case 'unfinished': {
  1876. if ($task['complete'] == 0) {
  1877. return Base::retError('任务未完成,无法标记未完成!');
  1878. }
  1879. $upArray['complete'] = 0;
  1880. $logArray[] = [
  1881. 'type' => '日志',
  1882. 'projectid' => $task['projectid'],
  1883. 'taskid' => $task['id'],
  1884. 'username' => $user['username'],
  1885. 'detail' => '标记未完成',
  1886. 'indate' => Base::time(),
  1887. 'other' => Base::array2string([
  1888. 'type' => 'task',
  1889. 'id' => $task['id'],
  1890. 'title' => $task['title'],
  1891. ])
  1892. ];
  1893. break;
  1894. }
  1895. /**
  1896. * 归档
  1897. */
  1898. case 'archived': {
  1899. if ($task['archived'] == 1) {
  1900. return Base::retError('任务已经归档,请勿重复操作!');
  1901. }
  1902. $upArray['archived'] = 1;
  1903. $upArray['archiveddate'] = Base::time();
  1904. $logArray[] = [
  1905. 'type' => '日志',
  1906. 'projectid' => $task['projectid'],
  1907. 'taskid' => $task['id'],
  1908. 'username' => $user['username'],
  1909. 'detail' => '任务归档',
  1910. 'indate' => Base::time(),
  1911. 'other' => Base::array2string([
  1912. 'type' => 'task',
  1913. 'id' => $task['id'],
  1914. 'title' => $task['title'],
  1915. ])
  1916. ];
  1917. break;
  1918. }
  1919. /**
  1920. * 取消归档
  1921. */
  1922. case 'unarchived': {
  1923. if ($task['archived'] == 0) {
  1924. return Base::retError('任务未归档,无法取消归档操作!');
  1925. }
  1926. $upArray['archived'] = 0;
  1927. $logArray[] = [
  1928. 'type' => '日志',
  1929. 'projectid' => $task['projectid'],
  1930. 'taskid' => $task['id'],
  1931. 'username' => $user['username'],
  1932. 'detail' => '取消归档',
  1933. 'indate' => Base::time(),
  1934. 'other' => Base::array2string([
  1935. 'type' => 'task',
  1936. 'id' => $task['id'],
  1937. 'title' => $task['title'],
  1938. ])
  1939. ];
  1940. break;
  1941. }
  1942. /**
  1943. * 删除任务
  1944. */
  1945. case 'delete': {
  1946. if ($task['delete'] == 1) {
  1947. return Base::retError('任务已删除,请勿重复操作!');
  1948. }
  1949. $upArray['delete'] = 1;
  1950. $upArray['deletedate'] = Base::time();
  1951. $logArray[] = [
  1952. 'type' => '日志',
  1953. 'projectid' => $task['projectid'],
  1954. 'taskid' => $task['id'],
  1955. 'username' => $user['username'],
  1956. 'detail' => '删除任务',
  1957. 'indate' => Base::time(),
  1958. 'other' => Base::array2string([
  1959. 'type' => 'task',
  1960. 'id' => $task['id'],
  1961. 'title' => $task['title'],
  1962. ])
  1963. ];
  1964. $message = "删除成功!";
  1965. break;
  1966. }
  1967. /**
  1968. * 评论任务
  1969. */
  1970. case 'comment': {
  1971. if (mb_strlen($content) < 2) {
  1972. return Base::retError('评论内容至少2个字!');
  1973. }
  1974. $logArray[] = [
  1975. 'type' => '评论',
  1976. 'projectid' => $task['projectid'],
  1977. 'taskid' => $task['id'],
  1978. 'username' => $user['username'],
  1979. 'detail' => $content,
  1980. 'indate' => Base::time(),
  1981. 'other' => Base::array2string([
  1982. 'type' => 'task',
  1983. 'id' => $task['id'],
  1984. 'title' => $task['title'],
  1985. ])
  1986. ];
  1987. $message = "评论成功!";
  1988. break;
  1989. }
  1990. /**
  1991. * 添加关注
  1992. */
  1993. case 'attention': {
  1994. $userArray = explode(",", $content);
  1995. DB::transaction(function () use ($user, $task, $userArray) {
  1996. foreach ($userArray AS $uname) {
  1997. $uid = Users::username2id($uname);
  1998. if (empty($uid)) {
  1999. continue;
  2000. }
  2001. $row = Base::DBC2A(DB::table('project_users')->where([
  2002. 'type' => '关注',
  2003. 'taskid' => $task['id'],
  2004. 'username' => $uname,
  2005. ])->lockForUpdate()->first());
  2006. if (empty($row)) {
  2007. DB::table('project_users')->insert([
  2008. 'type' => '关注',
  2009. 'projectid' => $task['projectid'],
  2010. 'taskid' => $task['id'],
  2011. 'isowner' => $task['username'] == $uname ? 1 : 0,
  2012. 'username' => $uname,
  2013. 'indate' => Base::time()
  2014. ]);
  2015. DB::table('project_log')->insert([
  2016. 'type' => '日志',
  2017. 'projectid' => $task['projectid'],
  2018. 'taskid' => $task['id'],
  2019. 'username' => $uname,
  2020. 'detail' => $uname == $user['username'] ? '关注任务' : '加入关注',
  2021. 'indate' => Base::time(),
  2022. 'other' => Base::array2string([
  2023. 'type' => 'task',
  2024. 'id' => $task['id'],
  2025. 'title' => $task['title'],
  2026. 'operator' => $user['username'],
  2027. 'action' => 'attention',
  2028. ])
  2029. ]);
  2030. }
  2031. }
  2032. });
  2033. $tempRow = Base::DBC2A(DB::table('project_users')->select(['username'])->where([ 'type' => '关注', 'taskid' => $task['id'] ])->get()->pluck('username'));
  2034. $upArray['follower'] = Base::array2string($tempRow);
  2035. $message = "保存成功!";
  2036. break;
  2037. }
  2038. /**
  2039. * 取消关注
  2040. */
  2041. case 'unattention': {
  2042. $userArray = explode(",", $content);
  2043. DB::transaction(function () use ($user, $task, $userArray) {
  2044. foreach ($userArray AS $uname) {
  2045. if (DB::table('project_users')->where([
  2046. 'type' => '关注',
  2047. 'taskid' => $task['id'],
  2048. 'username' => $uname,
  2049. ])->delete()) {
  2050. DB::table('project_log')->insert([
  2051. 'type' => '日志',
  2052. 'projectid' => $task['projectid'],
  2053. 'taskid' => $task['id'],
  2054. 'username' => $uname,
  2055. 'detail' => $uname == $user['username'] ? '取消关注' : '移出关注',
  2056. 'indate' => Base::time(),
  2057. 'other' => Base::array2string([
  2058. 'type' => 'task',
  2059. 'id' => $task['id'],
  2060. 'title' => $task['title'],
  2061. 'operator' => $user['username'],
  2062. 'action' => 'unattention',
  2063. ])
  2064. ]);
  2065. }
  2066. }
  2067. });
  2068. $tempRow = Base::DBC2A(DB::table('project_users')->select(['username'])->where([ 'type' => '关注', 'taskid' => $task['id'] ])->get()->pluck('username'));
  2069. $upArray['follower'] = Base::array2string($tempRow);
  2070. $message = "保存成功!";
  2071. break;
  2072. }
  2073. /**
  2074. * 修改子任务
  2075. */
  2076. case 'subtask': {
  2077. if (!is_array($content)) {
  2078. $content = [];
  2079. }
  2080. $subNames = [];
  2081. foreach ($content AS $tmp) {
  2082. if ($tmp['uname'] && !in_array($tmp['uname'], $subNames)) {
  2083. $subNames[] = $tmp['uname'];
  2084. }
  2085. }
  2086. $content = Base::array2string($content);
  2087. if ($content == $task['subtask']) {
  2088. return Base::retError('子任务未做改变!');
  2089. }
  2090. $upArray['subtask'] = $content;
  2091. //
  2092. $detail = '修改子任务';
  2093. $subtype = 'modify';
  2094. $new_count = count(Base::string2array($content));
  2095. $old_count = count(Base::string2array($task['subtask']));
  2096. if ($new_count > $old_count) {
  2097. $detail = '添加子任务';
  2098. $subtype = 'add';
  2099. } elseif ($new_count < $old_count) {
  2100. $detail = '删除子任务';
  2101. $subtype = 'del';
  2102. }
  2103. //
  2104. if ($subNames) {
  2105. DB::transaction(function() use ($task, $subNames) {
  2106. foreach ($subNames AS $uname) {
  2107. $row = Base::DBC2A(DB::table('project_users')->where([
  2108. 'type' => '负责人',
  2109. 'taskid' => $task['id'],
  2110. 'username' => $uname,
  2111. ])->lockForUpdate()->first());
  2112. if (empty($row)) {
  2113. DB::table('project_users')->insert([
  2114. 'type' => '负责人',
  2115. 'projectid' => $task['projectid'],
  2116. 'taskid' => $task['id'],
  2117. 'isowner' => $task['username'] == $uname ? 1 : 0,
  2118. 'username' => $uname,
  2119. 'indate' => Base::time()
  2120. ]);
  2121. }
  2122. }
  2123. DB::table('project_users')->where([
  2124. 'type' => '负责人',
  2125. 'taskid' => $task['id'],
  2126. ])->whereNotIn('username', $subNames)->delete();
  2127. });
  2128. } else {
  2129. DB::table('project_users')->where([
  2130. 'type' => '负责人',
  2131. 'taskid' => $task['id'],
  2132. ])->delete();
  2133. }
  2134. //
  2135. $logArray[] = [
  2136. 'type' => '日志',
  2137. 'projectid' => $task['projectid'],
  2138. 'taskid' => $task['id'],
  2139. 'username' => $user['username'],
  2140. 'detail' => $detail,
  2141. 'indate' => Base::time(),
  2142. 'other' => Base::array2string([
  2143. 'type' => 'task',
  2144. 'subtype' => $subtype,
  2145. 'id' => $task['id'],
  2146. 'title' => $task['title'],
  2147. 'subtask' => $content,
  2148. 'old_subtask' => $task['subtask'],
  2149. ])
  2150. ];
  2151. break;
  2152. }
  2153. default: {
  2154. return Base::retError('参数错误!');
  2155. }
  2156. }
  2157. //
  2158. if ($upArray) {
  2159. DB::table('project_task')->where('id', $taskid)->update($upArray);
  2160. }
  2161. if ($logArray) {
  2162. DB::table('project_log')->insert($logArray);
  2163. }
  2164. //
  2165. if (in_array($act, ['complete', 'unfinished'])) {
  2166. Project::updateNum($task['projectid']);
  2167. }
  2168. //
  2169. $task = array_merge($task, $upArray);
  2170. $task['persons'] = Project::taskPersons($task);
  2171. $task['overdue'] = Project::taskIsOverdue($task);
  2172. $task['subtask'] = Base::string2array($task['subtask']);
  2173. $task['follower'] = Base::string2array($task['follower']);
  2174. $task['projectTitle'] = $task['projectid'] > 0 ? DB::table('project_lists')->where('id', $task['projectid'])->value('title') : '';
  2175. $task = array_merge($task, Users::username2basic($task['username']));
  2176. return Base::retSuccess($message ?: '修改成功!', $task);
  2177. }
  2178. /**
  2179. * 项目任务-待推送日志
  2180. *
  2181. * @apiParam {Number} taskid 任务ID
  2182. * @apiParam {Number} [page] 当前页,默认:1
  2183. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  2184. * @throws \Throwable
  2185. */
  2186. public function task__pushlog()
  2187. {
  2188. $user = Users::authE();
  2189. if (Base::isError($user)) {
  2190. return $user;
  2191. } else {
  2192. $user = $user['data'];
  2193. }
  2194. //
  2195. return DB::transaction(function () {
  2196. $taskid = intval(Request::input('taskid'));
  2197. $task = Base::DBC2A(DB::table('project_task')
  2198. ->select(['pushlid', 'follower', 'username'])
  2199. ->where([
  2200. ['delete', '=', 0],
  2201. ['id', '=', $taskid],
  2202. ])
  2203. ->lockForUpdate()
  2204. ->first());
  2205. if (empty($task)) {
  2206. return Base::retError('任务不存在!');
  2207. }
  2208. $task['follower'] = Base::string2array($task['follower']);
  2209. $task['follower'][] = $task['username'];
  2210. //
  2211. $pushlid = $task['pushlid'];
  2212. $lists = DB::table('project_log')
  2213. ->select(['id', 'username', 'indate', 'detail', 'other'])
  2214. ->where([
  2215. ['id', '>', $pushlid],
  2216. ['taskid', '=', $taskid],
  2217. ['indate', '>', Base::time() - 60]
  2218. ])
  2219. ->orderBy('id')->paginate(Base::getPaginate(100, 20));
  2220. $lists = Base::getPageList($lists, false);
  2221. if (count($lists['lists']) == 0) {
  2222. return Base::retError('no lists');
  2223. }
  2224. $array = [];
  2225. foreach ($lists['lists'] AS $key => $item) {
  2226. $item = array_merge($item, Users::username2basic($item['username']));
  2227. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  2228. if (!in_array($item['other']['action'], ['attention', 'unattention'])) {
  2229. $array[] = $item;
  2230. }
  2231. $pushlid = $item['id'];
  2232. }
  2233. $lists['lists'] = $array;
  2234. if ($pushlid != $task['pushlid']) {
  2235. DB::table('project_task')->where('id', $taskid)->update([
  2236. 'pushlid' => $pushlid
  2237. ]);
  2238. }
  2239. return Base::retSuccess('success', array_merge($lists, $task));
  2240. });
  2241. }
  2242. /**
  2243. * 项目文件-列表
  2244. *
  2245. * @apiParam {Number} [projectid] 项目ID
  2246. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人或在任务所在的项目里)
  2247. * @apiParam {String} [name] 文件名称
  2248. * @apiParam {String} [username] 上传者用户名
  2249. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  2250. * - key: name|size|username|indate
  2251. * - order: asc|desc
  2252. * @apiParam {Number} [page] 当前页,默认:1
  2253. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  2254. */
  2255. public function files__lists()
  2256. {
  2257. $user = Users::authE();
  2258. if (Base::isError($user)) {
  2259. return $user;
  2260. } else {
  2261. $user = $user['data'];
  2262. }
  2263. //
  2264. $taskid = intval(Request::input('taskid'));
  2265. if ($taskid > 0) {
  2266. $projectid = intval(DB::table('project_task')->select(['projectid'])->where([ 'id' => $taskid])->value('projectid'));
  2267. } else {
  2268. $projectid = intval(Request::input('projectid'));
  2269. }
  2270. if ($projectid > 0) {
  2271. $inRes = Project::inThe($projectid, $user['username']);
  2272. if (Base::isError($inRes)) {
  2273. return $inRes;
  2274. }
  2275. }
  2276. //
  2277. $orderBy = '`id` DESC';
  2278. $sorts = Base::json2array(Request::input('sorts'));
  2279. if (in_array($sorts['order'], ['asc', 'desc'])) {
  2280. switch ($sorts['key']) {
  2281. case 'name':
  2282. case 'size':
  2283. case 'download':
  2284. case 'username':
  2285. case 'indate':
  2286. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  2287. break;
  2288. }
  2289. }
  2290. //
  2291. $whereArray = [];
  2292. if ($projectid > 0) {
  2293. $whereArray[] = ['projectid', '=', $projectid];
  2294. if ($taskid > 0) {
  2295. $whereArray[] = ['taskid', '=', $taskid];
  2296. }
  2297. } else {
  2298. if ($taskid <= 0) {
  2299. return Base::retError('参数错误!');
  2300. }
  2301. $tmpLists = Project::taskSomeUsers($taskid);
  2302. if (!in_array($user['username'], $tmpLists)) {
  2303. return Base::retError('未能找到此任务或无法管理此任务!');
  2304. }
  2305. $whereArray[] = ['taskid', '=', $taskid];
  2306. }
  2307. $whereArray[] = ['delete', '=', 0];
  2308. if (intval(Request::input('taskid')) > 0) {
  2309. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  2310. }
  2311. if (trim(Request::input('name'))) {
  2312. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  2313. }
  2314. if (trim(Request::input('username'))) {
  2315. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  2316. }
  2317. //
  2318. $lists = DB::table('project_files')
  2319. ->where($whereArray)
  2320. ->orderByRaw($orderBy)->paginate(Base::getPaginate(100, 20));
  2321. $lists = Base::getPageList($lists);
  2322. if ($lists['total'] == 0) {
  2323. return Base::retError('未找到任何相关的文件', $lists);
  2324. }
  2325. foreach ($lists['lists'] AS $key => $item) {
  2326. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  2327. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  2328. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  2329. }
  2330. return Base::retSuccess('success', $lists);
  2331. }
  2332. /**
  2333. * 项目文件-上传
  2334. *
  2335. * @apiParam {Number} [projectid] get-项目ID
  2336. * @apiParam {Number} [taskid] get-任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  2337. * @apiParam {String} [filename] post-文件名称
  2338. * @apiParam {String} [image64] post-base64图片(二选一)
  2339. * @apiParam {File} [files] post-文件对象(二选一)
  2340. */
  2341. public function files__upload()
  2342. {
  2343. $user = Users::authE();
  2344. if (Base::isError($user)) {
  2345. return $user;
  2346. } else {
  2347. $user = $user['data'];
  2348. }
  2349. //
  2350. $projectid = intval(Request::input('projectid'));
  2351. $taskid = intval(Request::input('taskid'));
  2352. if ($projectid > 0) {
  2353. $inRes = Project::inThe($projectid, $user['username']);
  2354. if (Base::isError($inRes)) {
  2355. return $inRes;
  2356. }
  2357. } else {
  2358. if ($taskid <= 0) {
  2359. return Base::retError('参数错误!');
  2360. }
  2361. $tmpLists = Project::taskSomeUsers($taskid);
  2362. if (!in_array($user['username'], $tmpLists)) {
  2363. return Base::retError('未能找到此任务或无法管理此任务!');
  2364. }
  2365. $projectid = DB::table('project_task')->where('id', $taskid)->value('projectid');
  2366. }
  2367. //
  2368. $path = "uploads/projects/" . ($projectid ?: Users::token2userid()) . "/";
  2369. $image64 = trim(Base::getPostValue('image64'));
  2370. $fileName = trim(Base::getPostValue('filename'));
  2371. if ($image64) {
  2372. $data = Base::image64save([
  2373. "image64" => $image64,
  2374. "path" => $path,
  2375. "fileName" => $fileName,
  2376. ]);
  2377. } else {
  2378. $data = Base::upload([
  2379. "file" => Request::file('files'),
  2380. "type" => 'file',
  2381. "path" => $path,
  2382. "fileName" => $fileName,
  2383. ]);
  2384. }
  2385. //
  2386. if (Base::isError($data)) {
  2387. return Base::retError($data['msg']);
  2388. } else {
  2389. $fileData = $data['data'];
  2390. $fileData['thumb'] = $fileData['thumb'] ?: 'images/files/file.png';
  2391. switch ($fileData['ext']) {
  2392. case "docx":
  2393. $fileData['thumb'] = 'images/files/doc.png';
  2394. break;
  2395. case "xlsx":
  2396. $fileData['thumb'] = 'images/files/xls.png';
  2397. break;
  2398. case "pptx":
  2399. $fileData['thumb'] = 'images/files/ppt.png';
  2400. break;
  2401. case "doc":
  2402. case "xls":
  2403. case "ppt":
  2404. case "txt":
  2405. case "esp":
  2406. case "gif":
  2407. $fileData['thumb'] = 'images/files/' . $fileData['ext'] . '.png';
  2408. break;
  2409. }
  2410. $array = [
  2411. 'projectid' => $projectid,
  2412. 'taskid' => $taskid,
  2413. 'name' => $fileData['name'],
  2414. 'size' => $fileData['size'] * 1024,
  2415. 'ext' => $fileData['ext'],
  2416. 'path' => $fileData['path'],
  2417. 'thumb' => $fileData['thumb'],
  2418. 'username' => $user['username'],
  2419. 'indate' => Base::time(),
  2420. ];
  2421. $id = DB::table('project_files')->insertGetId($array);
  2422. $array['id'] = $id;
  2423. $array['path'] = Base::fillUrl($array['path']);
  2424. $array['thumb'] = Base::fillUrl($array['thumb']);
  2425. $array['download'] = 0;
  2426. $array['yetdown'] = 0;
  2427. DB::table('project_log')->insert([
  2428. 'type' => '日志',
  2429. 'projectid' => $projectid,
  2430. 'taskid' => $taskid,
  2431. 'username' => $user['username'],
  2432. 'detail' => '上传文件',
  2433. 'indate' => Base::time(),
  2434. 'other' => Base::array2string([
  2435. 'type' => 'file',
  2436. 'id' => $id,
  2437. 'name' => $fileData['name'],
  2438. ])
  2439. ]);
  2440. if ($taskid > 0) {
  2441. DB::table('project_task')->where('id', $taskid)->increment('filenum');
  2442. }
  2443. return Base::retSuccess('success', $array);
  2444. }
  2445. }
  2446. /**
  2447. * 项目文件-下载
  2448. *
  2449. * @apiParam {Number} fileid 文件ID
  2450. */
  2451. public function files__download()
  2452. {
  2453. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2454. if (empty($fileDetail)) {
  2455. return abort(404, '文件不存在或已被删除!');
  2456. }
  2457. $filePath = public_path($fileDetail['path']);
  2458. if (!file_exists($filePath)) {
  2459. return abort(404, '文件不存在或已被删除。');
  2460. }
  2461. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  2462. Session::put('filesDownload:' . $fileDetail['id'], 1);
  2463. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  2464. }
  2465. return response()->download($filePath, $fileDetail['name']);
  2466. }
  2467. /**
  2468. * 项目文件-重命名
  2469. *
  2470. * @apiParam {Number} fileid 文件ID
  2471. * @apiParam {String} name 新文件名称
  2472. */
  2473. public function files__rename()
  2474. {
  2475. $user = Users::authE();
  2476. if (Base::isError($user)) {
  2477. return $user;
  2478. } else {
  2479. $user = $user['data'];
  2480. }
  2481. //
  2482. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2483. if (empty($fileDetail)) {
  2484. return Base::retError('文件不存在或已被删除!');
  2485. }
  2486. if ($fileDetail['username'] != $user['username']) {
  2487. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  2488. if (Base::isError($inRes)) {
  2489. return Base::retError('此操作仅支持管理员或上传者!');
  2490. }
  2491. }
  2492. //
  2493. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  2494. if (empty($name)) {
  2495. return Base::retError('文件名称不能为空!');
  2496. } elseif (mb_strlen($name) > 32) {
  2497. return Base::retError('文件名称最多只能设置32个字!');
  2498. }
  2499. //
  2500. $name .= '.' . $fileDetail['ext'];
  2501. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  2502. DB::table('project_log')->insert([
  2503. 'type' => '日志',
  2504. 'projectid' => $fileDetail['projectid'],
  2505. 'taskid' => $fileDetail['taskid'],
  2506. 'username' => $user['username'],
  2507. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  2508. 'indate' => Base::time(),
  2509. 'other' => Base::array2string([
  2510. 'type' => 'file',
  2511. 'id' => $fileDetail['id'],
  2512. 'name' => $name,
  2513. ])
  2514. ]);
  2515. }
  2516. //
  2517. return Base::retSuccess('修改成功!', [
  2518. 'name' => $name,
  2519. ]);
  2520. }
  2521. /**
  2522. * 项目文件-删除
  2523. *
  2524. * @apiParam {Number} fileid 文件ID
  2525. */
  2526. public function files__delete()
  2527. {
  2528. $user = Users::authE();
  2529. if (Base::isError($user)) {
  2530. return $user;
  2531. } else {
  2532. $user = $user['data'];
  2533. }
  2534. //
  2535. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2536. if (empty($fileDetail)) {
  2537. return Base::retError('文件不存在或已被删除!');
  2538. }
  2539. if ($fileDetail['username'] != $user['username']) {
  2540. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  2541. if (Base::isError($inRes)) {
  2542. return Base::retError('此操作仅支持管理员或上传者!');
  2543. }
  2544. }
  2545. //
  2546. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  2547. 'delete' => 1,
  2548. 'deletedate' => Base::time()
  2549. ]);
  2550. DB::table('project_log')->insert([
  2551. 'type' => '日志',
  2552. 'projectid' => $fileDetail['projectid'],
  2553. 'taskid' => $fileDetail['taskid'],
  2554. 'username' => $user['username'],
  2555. 'detail' => '删除文件',
  2556. 'indate' => Base::time(),
  2557. 'other' => Base::array2string([
  2558. 'type' => 'file',
  2559. 'id' => $fileDetail['id'],
  2560. 'name' => $fileDetail['name'],
  2561. ])
  2562. ]);
  2563. if ($fileDetail['taskid'] > 0) {
  2564. DB::table('project_task')->where('id', $fileDetail['taskid'])->decrement('filenum');
  2565. }
  2566. //
  2567. return Base::retSuccess('删除成功!');
  2568. }
  2569. /**
  2570. * 项目动态-列表
  2571. *
  2572. * @apiParam {Number} [projectid] 项目ID
  2573. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  2574. * @apiParam {String} [type] 类型
  2575. * - 全部: 日志+评论(默认)
  2576. * - 日志
  2577. * - 评论
  2578. * @apiParam {String} [username] 用户名
  2579. * @apiParam {Number} [page] 当前页,默认:1
  2580. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  2581. */
  2582. public function log__lists()
  2583. {
  2584. $user = Users::authE();
  2585. if (Base::isError($user)) {
  2586. return $user;
  2587. } else {
  2588. $user = $user['data'];
  2589. }
  2590. //
  2591. $projectid = intval(Request::input('projectid'));
  2592. if ($projectid > 0) {
  2593. $inRes = Project::inThe($projectid, $user['username']);
  2594. if (Base::isError($inRes)) {
  2595. return $inRes;
  2596. }
  2597. }
  2598. //
  2599. $taskid = intval(Request::input('taskid'));
  2600. $whereArray = [];
  2601. $whereFunc = null;
  2602. if ($projectid > 0) {
  2603. $whereArray[] = ['projectid', '=', $projectid];
  2604. if ($taskid > 0) {
  2605. $whereArray[] = ['taskid', '=', $taskid];
  2606. }
  2607. } else {
  2608. if ($taskid < 0) {
  2609. return Base::retError('参数错误!');
  2610. }
  2611. $tmpLists = Project::taskSomeUsers($taskid);
  2612. if (!in_array($user['username'], $tmpLists)) {
  2613. return Base::retError('未能找到此任务或无法管理此任务!');
  2614. }
  2615. $whereArray[] = ['taskid', '=', $taskid];
  2616. }
  2617. if (trim(Request::input('username'))) {
  2618. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  2619. }
  2620. switch (trim(Request::input('type'))) {
  2621. case '日志': {
  2622. $whereArray[] = ['type', '=', '日志'];
  2623. break;
  2624. }
  2625. case '评论': {
  2626. $whereArray[] = ['type', '=', '评论'];
  2627. break;
  2628. }
  2629. default: {
  2630. $whereFunc = function ($query) {
  2631. $query->whereIn('type', ['日志', '评论']);
  2632. };
  2633. break;
  2634. }
  2635. }
  2636. //
  2637. $lists = DB::table('project_log')
  2638. ->where($whereArray)
  2639. ->where($whereFunc)
  2640. ->orderByDesc('indate')->paginate(Base::getPaginate(100, 20));
  2641. $lists = Base::getPageList($lists);
  2642. if ($lists['total'] == 0) {
  2643. return Base::retError('未找到任何相关的记录', $lists);
  2644. }
  2645. foreach ($lists['lists'] AS $key => $item) {
  2646. $item = array_merge($item, Users::username2basic($item['username']));
  2647. if (empty($item['userimg'])) {
  2648. $item['userimg'] = Users::userimg($item['userimg']);
  2649. }
  2650. $item['timeData'] = [
  2651. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  2652. 'hi' => date("h:i", $item['indate']) ,
  2653. 'week' => "周" . Base::getTimeWeek($item['indate']),
  2654. 'segment' => Base::getTimeDayeSegment($item['indate']),
  2655. ];
  2656. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  2657. $lists['lists'][$key] = $item;
  2658. }
  2659. return Base::retSuccess('success', $lists);
  2660. }
  2661. }