ProjectController.php 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691
  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. }
  1271. $res = BillExport::create()->setHeadings($headings)->setData($data)->store($filePath . "/" . $fileName);
  1272. if ($res != 1) {
  1273. return Base::retError(['导出失败,%!', $fileName]);
  1274. }
  1275. //
  1276. $xlsPath = storage_path("app/" . $filePath . "/" . $fileName);
  1277. $zipFile = "app/" . $filePath . "/" . Base::rightDelete($fileName, '.xls'). ".zip";
  1278. $zipPath = storage_path($zipFile);
  1279. if (file_exists($zipPath)) {
  1280. Base::deleteDirAndFile($zipPath, true);
  1281. }
  1282. try {
  1283. Madzipper::make($zipPath)->add($xlsPath)->close();
  1284. } catch (\Exception $e) { }
  1285. //
  1286. if (file_exists($zipPath)) {
  1287. $base64 = base64_encode(Base::array2string([
  1288. 'projectid' => $projectid,
  1289. 'file' => $zipFile,
  1290. ]));
  1291. Session::put('task::export:username', $user['username']);
  1292. return Base::retSuccess("success", [
  1293. 'size' => Base::twoFloat(filesize($zipPath) / 1024, true),
  1294. 'url' => Base::fillUrl('api/project/task/export?data=' . urlencode($base64)),
  1295. ]);
  1296. } else {
  1297. return Base::retError('打包失败,请稍后再试...');
  1298. }
  1299. }
  1300. $lists = $builder->paginate(Base::getPaginate(100, 20));
  1301. $lists = Base::getPageList($lists);
  1302. if (intval(Request::input('statistics')) == 1) {
  1303. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 0)->count();
  1304. $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();
  1305. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 1)->count();
  1306. }
  1307. if ($lists['total'] == 0) {
  1308. return Base::retError('未找到任何相关的任务!', $lists);
  1309. }
  1310. foreach ($lists['lists'] AS $key => $info) {
  1311. $info['persons'] = Project::taskPersons($info);
  1312. $info['overdue'] = Project::taskIsOverdue($info);
  1313. $info['subtask'] = Base::string2array($info['subtask']);
  1314. $info['follower'] = Base::string2array($info['follower']);
  1315. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  1316. }
  1317. return Base::retSuccess('success', $lists);
  1318. }
  1319. /**
  1320. * 项目任务-导出结果
  1321. *
  1322. * @apiParam {String} data base64
  1323. */
  1324. public function task__export()
  1325. {
  1326. $username = Session::get('task::export:username');
  1327. if (empty($username)) {
  1328. return Base::ajaxError("请求已过期,请重新导出!", [], 0, 502);
  1329. }
  1330. $array = Base::string2array(base64_decode(urldecode(Request::input('data'))));
  1331. $projectid = intval($array['projectid']);
  1332. $file = $array['file'];
  1333. if (empty($projectid)) {
  1334. return Base::ajaxError("参数错误!", [], 0, 502);
  1335. }
  1336. if (empty($file) || !file_exists(storage_path($file))) {
  1337. return Base::ajaxError("文件不存在!", [], 0, 502);
  1338. }
  1339. $checkRole = Project::role('project_role_export', $projectid, 0, $username);
  1340. if (Base::isError($checkRole)) {
  1341. return Base::ajaxError($checkRole['msg'], [], 0, 502);
  1342. }
  1343. return response()->download(storage_path($file), ($checkRole['data']['title'] ?: Base::time()) . '.zip');
  1344. }
  1345. /**
  1346. * 项目任务-详情(与任务有关系的用户(关注的、在项目里的、负责人、创建者)都可以查到)
  1347. *
  1348. * @apiParam {Number} taskid 任务ID
  1349. */
  1350. public function task__detail()
  1351. {
  1352. $user = Users::authE();
  1353. if (Base::isError($user)) {
  1354. return $user;
  1355. } else {
  1356. $user = $user['data'];
  1357. }
  1358. //
  1359. $taskid = intval(Request::input('taskid'));
  1360. $tmpLists = Project::taskSomeUsers($taskid);
  1361. if (!in_array($user['username'], $tmpLists)) {
  1362. return Base::retError('未能找到此任务或无法管理此任务!');
  1363. }
  1364. //
  1365. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1366. $task['persons'] = Project::taskPersons($task);
  1367. $task['overdue'] = Project::taskIsOverdue($task);
  1368. $task['subtask'] = Base::string2array($task['subtask']);
  1369. $task['follower'] = Base::string2array($task['follower']);
  1370. $task = array_merge($task, Users::username2basic($task['username']));
  1371. $task['projectTitle'] = $task['projectid'] > 0 ? DB::table('project_lists')->where('id', $task['projectid'])->value('title') : '';
  1372. return Base::retSuccess('success', $task);
  1373. }
  1374. /**
  1375. * 项目任务-描述(任务有关系的用户(关注的、在项目里的、负责人、创建者)都可以查到)
  1376. *
  1377. * @apiParam {Number} taskid 任务ID
  1378. */
  1379. public function task__desc()
  1380. {
  1381. $user = Users::authE();
  1382. if (Base::isError($user)) {
  1383. return $user;
  1384. } else {
  1385. $user = $user['data'];
  1386. }
  1387. //
  1388. $taskid = intval(Request::input('taskid'));
  1389. $tmpLists = Project::taskSomeUsers($taskid);
  1390. if (!in_array($user['username'], $tmpLists)) {
  1391. return Base::retError('未能找到此任务或无法管理此任务!');
  1392. }
  1393. //
  1394. $desc = DB::table('project_content')->where('taskid', $taskid)->value('content');
  1395. if (empty($desc)) {
  1396. $desc = DB::table('project_task')->where('id', $taskid)->value('desc');
  1397. }
  1398. return Base::retSuccess('success', [
  1399. 'taskid' => $taskid,
  1400. 'desc' => $desc
  1401. ]);
  1402. }
  1403. /**
  1404. * 项目任务-获取数量
  1405. *
  1406. * @apiParam {Number} [level] 任务等级(1~4,留空获取所有)
  1407. */
  1408. public function task__levelnum()
  1409. {
  1410. $user = Users::authE();
  1411. if (Base::isError($user)) {
  1412. return $user;
  1413. } else {
  1414. $user = $user['data'];
  1415. }
  1416. //
  1417. $whereArray = [];
  1418. $whereArray[] = ['project_task.delete', '=', 0];
  1419. $whereArray[] = ['project_task.username', '=', $user['username']];
  1420. $whereArray[] = ['project_task.archived', '=', 0];
  1421. $array = [
  1422. 'level_1' => 0,
  1423. 'level_2' => 0,
  1424. 'level_3' => 0,
  1425. 'level_4' => 0,
  1426. ];
  1427. $level = intval(Request::input('level'));
  1428. if ($level > 0) {
  1429. $array = [];
  1430. $array['level_' . $level] = 0;
  1431. }
  1432. foreach ($array AS $key => $val) {
  1433. $level = intval(Base::leftDelete($key, 'level_'));
  1434. $array[$key] = DB::table('project_task')->where($whereArray)->where('level', $level)->count();
  1435. }
  1436. return Base::retSuccess('success', $array);
  1437. }
  1438. /**
  1439. * 项目任务-添加任务
  1440. *
  1441. * @apiParam {String} title 任务标题
  1442. * @apiParam {Number} [projectid] 项目ID
  1443. * @apiParam {Number} [labelid] 项目子分类ID
  1444. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  1445. * @apiParam {String} [username] 任务负责人用户名(如果项目ID为空时此参数无效,负责人为自己)
  1446. * @apiParam {Number} [insertbottom] 是否添加至列表结尾(1:是,默认:0,仅适用于项目分类列表)
  1447. *
  1448. * @throws \Throwable
  1449. */
  1450. public function task__add()
  1451. {
  1452. $user = Users::authE();
  1453. if (Base::isError($user)) {
  1454. return $user;
  1455. } else {
  1456. $user = $user['data'];
  1457. }
  1458. //
  1459. $projectid = intval(Request::input('projectid'));
  1460. $labelid = intval(Request::input('labelid'));
  1461. $insertbottom = intval(Request::input('insertbottom'));
  1462. if ($projectid > 0) {
  1463. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  1464. if (empty($projectDetail)) {
  1465. return Base::retError('项目不存在或已被删除!');
  1466. }
  1467. //
  1468. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1469. if (empty($labelDetail)) {
  1470. return Base::retError('项目子分类不存在或已被删除!');
  1471. }
  1472. //
  1473. $inRes = Project::inThe($projectid, $user['username']);
  1474. if (Base::isError($inRes)) {
  1475. return $inRes;
  1476. }
  1477. $checkRole = Project::role('add_role', $projectid, 0);
  1478. if (Base::isError($checkRole)) {
  1479. return $checkRole;
  1480. }
  1481. //
  1482. $username = trim(Request::input('username'));
  1483. if (empty($username)) {
  1484. $username = $user['username'];
  1485. }
  1486. if ($username != $user['username']) {
  1487. $inRes = Project::inThe($projectid, $username);
  1488. if (Base::isError($inRes)) {
  1489. return Base::retError('负责人不在项目成员内!');
  1490. }
  1491. }
  1492. } else {
  1493. $username = $user['username'];
  1494. }
  1495. //
  1496. $title = trim(Request::input('title'));
  1497. if (empty($title)) {
  1498. return Base::retError('任务标题不能为空!');
  1499. } elseif (mb_strlen($title) > 255) {
  1500. return Base::retError('任务标题最多只能设置255个字!');
  1501. }
  1502. //
  1503. $level = max(1, min(4, intval(Request::input('level'))));
  1504. if (empty($projectid)) {
  1505. $inorder = 0;
  1506. } else {
  1507. $inorder = intval(DB::table('project_task')->where('projectid', $projectid)->orderBy('inorder', $insertbottom ? 'asc' : 'desc')->value('inorder')) + ($insertbottom ? -1 : 1);
  1508. }
  1509. $userorder = intval(DB::table('project_task')->where('username', $user['username'])->where('level', $level)->orderByDesc('userorder')->value('userorder')) + 1;
  1510. //
  1511. $inArray = [
  1512. 'projectid' => $projectid,
  1513. 'labelid' => $labelid,
  1514. 'createuser' => $user['username'],
  1515. 'username' => $username,
  1516. 'title' => $title,
  1517. 'level' => $level,
  1518. 'inorder' => $inorder,
  1519. 'userorder' => $userorder,
  1520. 'indate' => Base::time(),
  1521. 'startdate' => Base::time(),
  1522. 'subtask' => Base::array2string([]),
  1523. 'follower' => Base::array2string([]),
  1524. ];
  1525. return DB::transaction(function () use ($inArray) {
  1526. $taskid = DB::table('project_task')->insertGetId($inArray);
  1527. if (empty($taskid)) {
  1528. return Base::retError('系统繁忙,请稍后再试!');
  1529. }
  1530. DB::table('project_log')->insert([
  1531. 'type' => '日志',
  1532. 'projectid' => $inArray['projectid'],
  1533. 'taskid' => $taskid,
  1534. 'username' => $inArray['createuser'],
  1535. 'detail' => '添加任务',
  1536. 'indate' => Base::time(),
  1537. 'other' => Base::array2string([
  1538. 'type' => 'task',
  1539. 'id' => $taskid,
  1540. 'title' => $inArray['title'],
  1541. ])
  1542. ]);
  1543. Project::updateNum($inArray['projectid']);
  1544. //
  1545. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1546. $task['persons'] = Project::taskPersons($task);
  1547. $task['overdue'] = Project::taskIsOverdue($task);
  1548. $task['subtask'] = Base::string2array($task['subtask']);
  1549. $task['follower'] = Base::string2array($task['follower']);
  1550. $task = array_merge($task, Users::username2basic($task['username']));
  1551. return Base::retSuccess('添加成功!', $task);
  1552. });
  1553. }
  1554. /**
  1555. * {post} 项目任务-修改
  1556. *
  1557. * @apiParam {Number} taskid 任务ID
  1558. * @apiParam {String} act 修改字段|操作类型
  1559. * - title: 标题
  1560. * - desc: 描述
  1561. * - level: 优先级
  1562. * - username: 负责人
  1563. * - plannedtime: 设置计划时间
  1564. * - unplannedtime: 取消计划时间
  1565. * - complete: 标记完成
  1566. * - unfinished: 标记未完成
  1567. * - archived: 归档
  1568. * - unarchived: 取消归档
  1569. * - delete: 删除任务
  1570. * - comment: 评论
  1571. * - attention: 添加关注
  1572. * - subtask: 修改子任务
  1573. * @apiParam {String} [content] 内容数据
  1574. *
  1575. * @throws \Throwable
  1576. */
  1577. public function task__edit()
  1578. {
  1579. $user = Users::authE();
  1580. if (Base::isError($user)) {
  1581. return $user;
  1582. } else {
  1583. $user = $user['data'];
  1584. }
  1585. //
  1586. $act = trim(Base::getPostValue('act'));
  1587. $taskid = intval(Base::getPostValue('taskid'));
  1588. $task = Base::DBC2A(DB::table('project_task')
  1589. ->where([
  1590. ['delete', '=', 0],
  1591. ['id', '=', $taskid],
  1592. ])
  1593. ->first());
  1594. if (empty($task)) {
  1595. return Base::retError('任务不存在!');
  1596. }
  1597. if ($task['projectid'] > 0) {
  1598. if (!Project::isPersons($task, $user['username'])) {
  1599. $inRes = Project::inThe($task['projectid'], $user['username']);
  1600. if (Base::isError($inRes)) {
  1601. return $inRes;
  1602. }
  1603. }
  1604. if (!in_array($act, ['comment', 'attention'])) {
  1605. $checkRole = Project::role('edit_role', $task['projectid'], $task['id']);
  1606. if (Base::isError($checkRole)) {
  1607. return $checkRole;
  1608. }
  1609. switch ($act) {
  1610. case 'complete':
  1611. case 'unfinished':
  1612. $checkRole = Project::role('complete_role', $task['projectid'], $task['id']);
  1613. if (Base::isError($checkRole)) {
  1614. return $checkRole;
  1615. }
  1616. break;
  1617. case 'archived':
  1618. case 'unarchived':
  1619. $checkRole = Project::role('archived_role', $task['projectid'], $task['id']);
  1620. if (Base::isError($checkRole)) {
  1621. return $checkRole;
  1622. }
  1623. break;
  1624. case 'delete':
  1625. $checkRole = Project::role('del_role', $task['projectid'], $task['id']);
  1626. if (Base::isError($checkRole)) {
  1627. return $checkRole;
  1628. }
  1629. break;
  1630. }
  1631. }
  1632. } else {
  1633. if (!Project::isPersons($task, $user['username'])) {
  1634. return Base::retError('此操作只允许任务负责人!');
  1635. }
  1636. }
  1637. //
  1638. $content = Base::newTrim(Base::getPostValue('content'));
  1639. $message = "";
  1640. $upArray = [];
  1641. $logArray = [];
  1642. switch ($act) {
  1643. /**
  1644. * 修改标题
  1645. */
  1646. case 'title': {
  1647. if ($content == $task['title']) {
  1648. return Base::retError('标题未做改变!');
  1649. }
  1650. $upArray['title'] = $content;
  1651. $logArray[] = [
  1652. 'type' => '日志',
  1653. 'projectid' => $task['projectid'],
  1654. 'taskid' => $task['id'],
  1655. 'username' => $user['username'],
  1656. 'detail' => '修改任务标题',
  1657. 'indate' => Base::time(),
  1658. 'other' => Base::array2string([
  1659. 'type' => 'task',
  1660. 'id' => $task['id'],
  1661. 'title' => $content,
  1662. 'old_title' => $task['title'],
  1663. ])
  1664. ];
  1665. break;
  1666. }
  1667. /**
  1668. * 修改描述
  1669. */
  1670. case 'desc': {
  1671. preg_match_all("/<img\s*src=\"data:image\/(png|jpg|jpeg);base64,(.*?)\"/s", $content, $matchs);
  1672. foreach ($matchs[2] as $key => $text) {
  1673. $p = "uploads/projects/" . ($task['projectid'] ?: Users::token2userid()) . "/";
  1674. Base::makeDir(public_path($p));
  1675. $p.= md5($text) . "." . $matchs[1][$key];
  1676. $r = file_put_contents(public_path($p), base64_decode($text));
  1677. if ($r) {
  1678. $content = str_replace($matchs[0][$key], '<img src="' . Base::fillUrl($p) . '"', $content);
  1679. }
  1680. }
  1681. Base::DBUPIN('project_content', [
  1682. 'taskid' => $task['id'],
  1683. ], [
  1684. 'content' => $content,
  1685. ], [
  1686. 'projectid' => $task['projectid'],
  1687. 'content' => $content,
  1688. 'indate' => Base::time()
  1689. ]);
  1690. $upArray['desc'] = $content ? Base::time() : '';
  1691. $logArray[] = [
  1692. 'type' => '日志',
  1693. 'projectid' => $task['projectid'],
  1694. 'taskid' => $task['id'],
  1695. 'username' => $user['username'],
  1696. 'detail' => '修改任务描述',
  1697. 'indate' => Base::time(),
  1698. 'other' => Base::array2string([
  1699. 'type' => 'task',
  1700. 'id' => $task['id'],
  1701. 'title' => $task['title'],
  1702. 'old_desc' => $task['desc'],
  1703. ])
  1704. ];
  1705. break;
  1706. }
  1707. /**
  1708. * 调整任务等级
  1709. */
  1710. case 'level': {
  1711. $content = intval($content);
  1712. if ($content == $task['level']) {
  1713. return Base::retError('优先级未做改变!');
  1714. }
  1715. if ($content > 4 || $content < 1) {
  1716. return Base::retError('优先级参数错误!');
  1717. }
  1718. $upArray['level'] = $content;
  1719. $logArray[] = [
  1720. 'type' => '日志',
  1721. 'projectid' => $task['projectid'],
  1722. 'taskid' => $task['id'],
  1723. 'username' => $user['username'],
  1724. 'detail' => '调整任务等级为【P' . $content . '】',
  1725. 'indate' => Base::time(),
  1726. 'other' => Base::array2string([
  1727. 'type' => 'task',
  1728. 'id' => $task['id'],
  1729. 'title' => $task['title'],
  1730. 'old_level' => $task['level'],
  1731. ])
  1732. ];
  1733. break;
  1734. }
  1735. /**
  1736. * 修改任务负责人
  1737. */
  1738. case 'username': {
  1739. if ($content == $task['username']) {
  1740. return Base::retError('负责人未做改变!');
  1741. }
  1742. if ($task['projectid'] > 0) {
  1743. $inRes = Project::inThe($task['projectid'], $content);
  1744. if (Base::isError($inRes)) {
  1745. return Base::retError(['%不在成员列表内!', $content]);
  1746. }
  1747. }
  1748. $upArray['username'] = $content;
  1749. $logArray[] = [
  1750. 'type' => '日志',
  1751. 'projectid' => $task['projectid'],
  1752. 'taskid' => $task['id'],
  1753. 'username' => $user['username'],
  1754. 'detail' => '修改负责人',
  1755. 'indate' => Base::time(),
  1756. 'other' => Base::array2string([
  1757. 'type' => 'task',
  1758. 'id' => $task['id'],
  1759. 'title' => $task['title'],
  1760. 'old_username' => $task['username'],
  1761. ])
  1762. ];
  1763. break;
  1764. }
  1765. /**
  1766. * 修改计划时间
  1767. */
  1768. case 'plannedtime': {
  1769. list($startdate, $enddate) = explode(",", $content);
  1770. if (!Base::isDate($startdate) || !Base::isDate($enddate)) {
  1771. return Base::retError('计划时间参数错误!');
  1772. }
  1773. $startdate = strtotime($startdate);
  1774. $enddate = strtotime($enddate);
  1775. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1776. return Base::retError('与原计划时间一致!');
  1777. }
  1778. if ($startdate == $enddate) {
  1779. return Base::retError('开始时间与结束时间一致!');
  1780. }
  1781. $upArray['startdate'] = $startdate;
  1782. $upArray['enddate'] = $enddate;
  1783. $logArray[] = [
  1784. 'type' => '日志',
  1785. 'projectid' => $task['projectid'],
  1786. 'taskid' => $task['id'],
  1787. 'username' => $user['username'],
  1788. 'detail' => '设置计划时间',
  1789. 'indate' => Base::time(),
  1790. 'other' => Base::array2string([
  1791. 'type' => 'task',
  1792. 'id' => $task['id'],
  1793. 'title' => $task['title'],
  1794. 'old_startdate' => $task['startdate'],
  1795. 'old_enddate' => $task['enddate'],
  1796. ])
  1797. ];
  1798. break;
  1799. }
  1800. /**
  1801. * 取消计划时间
  1802. */
  1803. case 'unplannedtime': {
  1804. $startdate = 0;
  1805. $enddate = 0;
  1806. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1807. return Base::retError('与原计划时间一致!');
  1808. }
  1809. $upArray['startdate'] = $startdate;
  1810. $upArray['enddate'] = $enddate;
  1811. $logArray[] = [
  1812. 'type' => '日志',
  1813. 'projectid' => $task['projectid'],
  1814. 'taskid' => $task['id'],
  1815. 'username' => $user['username'],
  1816. 'detail' => '取消计划时间',
  1817. 'indate' => Base::time(),
  1818. 'other' => Base::array2string([
  1819. 'type' => 'task',
  1820. 'id' => $task['id'],
  1821. 'title' => $task['title'],
  1822. 'old_startdate' => $task['startdate'],
  1823. 'old_enddate' => $task['enddate'],
  1824. ])
  1825. ];
  1826. break;
  1827. }
  1828. /**
  1829. * 标记完成
  1830. */
  1831. case 'complete': {
  1832. if ($task['complete'] == 1) {
  1833. return Base::retError('任务已标记完成,请勿重复操作!');
  1834. }
  1835. $upArray['complete'] = 1;
  1836. $upArray['completedate'] = Base::time();
  1837. $logArray[] = [
  1838. 'type' => '日志',
  1839. 'projectid' => $task['projectid'],
  1840. 'taskid' => $task['id'],
  1841. 'username' => $user['username'],
  1842. 'detail' => '标记已完成',
  1843. 'indate' => Base::time(),
  1844. 'other' => Base::array2string([
  1845. 'type' => 'task',
  1846. 'id' => $task['id'],
  1847. 'title' => $task['title'],
  1848. ])
  1849. ];
  1850. break;
  1851. }
  1852. /**
  1853. * 标记未完成
  1854. */
  1855. case 'unfinished': {
  1856. if ($task['complete'] == 0) {
  1857. return Base::retError('任务未完成,无法标记未完成!');
  1858. }
  1859. $upArray['complete'] = 0;
  1860. $logArray[] = [
  1861. 'type' => '日志',
  1862. 'projectid' => $task['projectid'],
  1863. 'taskid' => $task['id'],
  1864. 'username' => $user['username'],
  1865. 'detail' => '标记未完成',
  1866. 'indate' => Base::time(),
  1867. 'other' => Base::array2string([
  1868. 'type' => 'task',
  1869. 'id' => $task['id'],
  1870. 'title' => $task['title'],
  1871. ])
  1872. ];
  1873. break;
  1874. }
  1875. /**
  1876. * 归档
  1877. */
  1878. case 'archived': {
  1879. if ($task['archived'] == 1) {
  1880. return Base::retError('任务已经归档,请勿重复操作!');
  1881. }
  1882. $upArray['archived'] = 1;
  1883. $upArray['archiveddate'] = Base::time();
  1884. $logArray[] = [
  1885. 'type' => '日志',
  1886. 'projectid' => $task['projectid'],
  1887. 'taskid' => $task['id'],
  1888. 'username' => $user['username'],
  1889. 'detail' => '任务归档',
  1890. 'indate' => Base::time(),
  1891. 'other' => Base::array2string([
  1892. 'type' => 'task',
  1893. 'id' => $task['id'],
  1894. 'title' => $task['title'],
  1895. ])
  1896. ];
  1897. break;
  1898. }
  1899. /**
  1900. * 取消归档
  1901. */
  1902. case 'unarchived': {
  1903. if ($task['archived'] == 0) {
  1904. return Base::retError('任务未归档,无法取消归档操作!');
  1905. }
  1906. $upArray['archived'] = 0;
  1907. $logArray[] = [
  1908. 'type' => '日志',
  1909. 'projectid' => $task['projectid'],
  1910. 'taskid' => $task['id'],
  1911. 'username' => $user['username'],
  1912. 'detail' => '取消归档',
  1913. 'indate' => Base::time(),
  1914. 'other' => Base::array2string([
  1915. 'type' => 'task',
  1916. 'id' => $task['id'],
  1917. 'title' => $task['title'],
  1918. ])
  1919. ];
  1920. break;
  1921. }
  1922. /**
  1923. * 删除任务
  1924. */
  1925. case 'delete': {
  1926. if ($task['delete'] == 1) {
  1927. return Base::retError('任务已删除,请勿重复操作!');
  1928. }
  1929. $upArray['delete'] = 1;
  1930. $upArray['deletedate'] = Base::time();
  1931. $logArray[] = [
  1932. 'type' => '日志',
  1933. 'projectid' => $task['projectid'],
  1934. 'taskid' => $task['id'],
  1935. 'username' => $user['username'],
  1936. 'detail' => '删除任务',
  1937. 'indate' => Base::time(),
  1938. 'other' => Base::array2string([
  1939. 'type' => 'task',
  1940. 'id' => $task['id'],
  1941. 'title' => $task['title'],
  1942. ])
  1943. ];
  1944. $message = "删除成功!";
  1945. break;
  1946. }
  1947. /**
  1948. * 评论任务
  1949. */
  1950. case 'comment': {
  1951. if (mb_strlen($content) < 2) {
  1952. return Base::retError('评论内容至少2个字!');
  1953. }
  1954. $logArray[] = [
  1955. 'type' => '评论',
  1956. 'projectid' => $task['projectid'],
  1957. 'taskid' => $task['id'],
  1958. 'username' => $user['username'],
  1959. 'detail' => $content,
  1960. 'indate' => Base::time(),
  1961. 'other' => Base::array2string([
  1962. 'type' => 'task',
  1963. 'id' => $task['id'],
  1964. 'title' => $task['title'],
  1965. ])
  1966. ];
  1967. $message = "评论成功!";
  1968. break;
  1969. }
  1970. /**
  1971. * 添加关注
  1972. */
  1973. case 'attention': {
  1974. $userArray = explode(",", $content);
  1975. DB::transaction(function () use ($user, $task, $userArray) {
  1976. foreach ($userArray AS $uname) {
  1977. $uid = Users::username2id($uname);
  1978. if (empty($uid)) {
  1979. continue;
  1980. }
  1981. $row = Base::DBC2A(DB::table('project_users')->where([
  1982. 'type' => '关注',
  1983. 'taskid' => $task['id'],
  1984. 'username' => $uname,
  1985. ])->lockForUpdate()->first());
  1986. if (empty($row)) {
  1987. DB::table('project_users')->insert([
  1988. 'type' => '关注',
  1989. 'projectid' => $task['projectid'],
  1990. 'taskid' => $task['id'],
  1991. 'isowner' => $task['username'] == $uname ? 1 : 0,
  1992. 'username' => $uname,
  1993. 'indate' => Base::time()
  1994. ]);
  1995. DB::table('project_log')->insert([
  1996. 'type' => '日志',
  1997. 'projectid' => $task['projectid'],
  1998. 'taskid' => $task['id'],
  1999. 'username' => $uname,
  2000. 'detail' => $uname == $user['username'] ? '关注任务' : '加入关注',
  2001. 'indate' => Base::time(),
  2002. 'other' => Base::array2string([
  2003. 'type' => 'task',
  2004. 'id' => $task['id'],
  2005. 'title' => $task['title'],
  2006. 'operator' => $user['username'],
  2007. 'action' => 'attention',
  2008. ])
  2009. ]);
  2010. }
  2011. }
  2012. });
  2013. $tempRow = Base::DBC2A(DB::table('project_users')->select(['username'])->where([ 'type' => '关注', 'taskid' => $task['id'] ])->get()->pluck('username'));
  2014. $upArray['follower'] = Base::array2string($tempRow);
  2015. $message = "保存成功!";
  2016. break;
  2017. }
  2018. /**
  2019. * 取消关注
  2020. */
  2021. case 'unattention': {
  2022. $userArray = explode(",", $content);
  2023. DB::transaction(function () use ($user, $task, $userArray) {
  2024. foreach ($userArray AS $uname) {
  2025. if (DB::table('project_users')->where([
  2026. 'type' => '关注',
  2027. 'taskid' => $task['id'],
  2028. 'username' => $uname,
  2029. ])->delete()) {
  2030. DB::table('project_log')->insert([
  2031. 'type' => '日志',
  2032. 'projectid' => $task['projectid'],
  2033. 'taskid' => $task['id'],
  2034. 'username' => $uname,
  2035. 'detail' => $uname == $user['username'] ? '取消关注' : '移出关注',
  2036. 'indate' => Base::time(),
  2037. 'other' => Base::array2string([
  2038. 'type' => 'task',
  2039. 'id' => $task['id'],
  2040. 'title' => $task['title'],
  2041. 'operator' => $user['username'],
  2042. 'action' => 'unattention',
  2043. ])
  2044. ]);
  2045. }
  2046. }
  2047. });
  2048. $tempRow = Base::DBC2A(DB::table('project_users')->select(['username'])->where([ 'type' => '关注', 'taskid' => $task['id'] ])->get()->pluck('username'));
  2049. $upArray['follower'] = Base::array2string($tempRow);
  2050. $message = "保存成功!";
  2051. break;
  2052. }
  2053. /**
  2054. * 修改子任务
  2055. */
  2056. case 'subtask': {
  2057. if (!is_array($content)) {
  2058. $content = [];
  2059. }
  2060. $subNames = [];
  2061. foreach ($content AS $tmp) {
  2062. if ($tmp['uname'] && !in_array($tmp['uname'], $subNames)) {
  2063. $subNames[] = $tmp['uname'];
  2064. }
  2065. }
  2066. $content = Base::array2string($content);
  2067. if ($content == $task['subtask']) {
  2068. return Base::retError('子任务未做改变!');
  2069. }
  2070. $upArray['subtask'] = $content;
  2071. //
  2072. $detail = '修改子任务';
  2073. $subtype = 'modify';
  2074. $new_count = count(Base::string2array($content));
  2075. $old_count = count(Base::string2array($task['subtask']));
  2076. if ($new_count > $old_count) {
  2077. $detail = '添加子任务';
  2078. $subtype = 'add';
  2079. } elseif ($new_count < $old_count) {
  2080. $detail = '删除子任务';
  2081. $subtype = 'del';
  2082. }
  2083. //
  2084. if ($subNames) {
  2085. DB::transaction(function() use ($task, $subNames) {
  2086. foreach ($subNames AS $uname) {
  2087. $row = Base::DBC2A(DB::table('project_users')->where([
  2088. 'type' => '负责人',
  2089. 'taskid' => $task['id'],
  2090. 'username' => $uname,
  2091. ])->lockForUpdate()->first());
  2092. if (empty($row)) {
  2093. DB::table('project_users')->insert([
  2094. 'type' => '负责人',
  2095. 'projectid' => $task['projectid'],
  2096. 'taskid' => $task['id'],
  2097. 'isowner' => $task['username'] == $uname ? 1 : 0,
  2098. 'username' => $uname,
  2099. 'indate' => Base::time()
  2100. ]);
  2101. }
  2102. }
  2103. DB::table('project_users')->where([
  2104. 'type' => '负责人',
  2105. 'taskid' => $task['id'],
  2106. ])->whereNotIn('username', $subNames)->delete();
  2107. });
  2108. } else {
  2109. DB::table('project_users')->where([
  2110. 'type' => '负责人',
  2111. 'taskid' => $task['id'],
  2112. ])->delete();
  2113. }
  2114. //
  2115. $logArray[] = [
  2116. 'type' => '日志',
  2117. 'projectid' => $task['projectid'],
  2118. 'taskid' => $task['id'],
  2119. 'username' => $user['username'],
  2120. 'detail' => $detail,
  2121. 'indate' => Base::time(),
  2122. 'other' => Base::array2string([
  2123. 'type' => 'task',
  2124. 'subtype' => $subtype,
  2125. 'id' => $task['id'],
  2126. 'title' => $task['title'],
  2127. 'subtask' => $content,
  2128. 'old_subtask' => $task['subtask'],
  2129. ])
  2130. ];
  2131. break;
  2132. }
  2133. default: {
  2134. return Base::retError('参数错误!');
  2135. }
  2136. }
  2137. //
  2138. if ($upArray) {
  2139. DB::table('project_task')->where('id', $taskid)->update($upArray);
  2140. }
  2141. if ($logArray) {
  2142. DB::table('project_log')->insert($logArray);
  2143. }
  2144. //
  2145. if (in_array($act, ['complete', 'unfinished'])) {
  2146. Project::updateNum($task['projectid']);
  2147. }
  2148. //
  2149. $task = array_merge($task, $upArray);
  2150. $task['persons'] = Project::taskPersons($task);
  2151. $task['overdue'] = Project::taskIsOverdue($task);
  2152. $task['subtask'] = Base::string2array($task['subtask']);
  2153. $task['follower'] = Base::string2array($task['follower']);
  2154. $task['projectTitle'] = $task['projectid'] > 0 ? DB::table('project_lists')->where('id', $task['projectid'])->value('title') : '';
  2155. $task = array_merge($task, Users::username2basic($task['username']));
  2156. return Base::retSuccess($message ?: '修改成功!', $task);
  2157. }
  2158. /**
  2159. * 项目任务-待推送日志
  2160. *
  2161. * @apiParam {Number} taskid 任务ID
  2162. * @apiParam {Number} [page] 当前页,默认:1
  2163. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  2164. * @throws \Throwable
  2165. */
  2166. public function task__pushlog()
  2167. {
  2168. $user = Users::authE();
  2169. if (Base::isError($user)) {
  2170. return $user;
  2171. } else {
  2172. $user = $user['data'];
  2173. }
  2174. //
  2175. return DB::transaction(function () {
  2176. $taskid = intval(Request::input('taskid'));
  2177. $task = Base::DBC2A(DB::table('project_task')
  2178. ->select(['pushlid', 'follower', 'username'])
  2179. ->where([
  2180. ['delete', '=', 0],
  2181. ['id', '=', $taskid],
  2182. ])
  2183. ->lockForUpdate()
  2184. ->first());
  2185. if (empty($task)) {
  2186. return Base::retError('任务不存在!');
  2187. }
  2188. $task['follower'] = Base::string2array($task['follower']);
  2189. $task['follower'][] = $task['username'];
  2190. //
  2191. $pushlid = $task['pushlid'];
  2192. $lists = DB::table('project_log')
  2193. ->select(['id', 'username', 'indate', 'detail', 'other'])
  2194. ->where([
  2195. ['id', '>', $pushlid],
  2196. ['taskid', '=', $taskid],
  2197. ['indate', '>', Base::time() - 60]
  2198. ])
  2199. ->orderBy('id')->paginate(Base::getPaginate(100, 20));
  2200. $lists = Base::getPageList($lists, false);
  2201. if (count($lists['lists']) == 0) {
  2202. return Base::retError('no lists');
  2203. }
  2204. $array = [];
  2205. foreach ($lists['lists'] AS $key => $item) {
  2206. $item = array_merge($item, Users::username2basic($item['username']));
  2207. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  2208. if (!in_array($item['other']['action'], ['attention', 'unattention'])) {
  2209. $array[] = $item;
  2210. }
  2211. $pushlid = $item['id'];
  2212. }
  2213. $lists['lists'] = $array;
  2214. if ($pushlid != $task['pushlid']) {
  2215. DB::table('project_task')->where('id', $taskid)->update([
  2216. 'pushlid' => $pushlid
  2217. ]);
  2218. }
  2219. return Base::retSuccess('success', array_merge($lists, $task));
  2220. });
  2221. }
  2222. /**
  2223. * 项目文件-列表
  2224. *
  2225. * @apiParam {Number} [projectid] 项目ID
  2226. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人或在任务所在的项目里)
  2227. * @apiParam {String} [name] 文件名称
  2228. * @apiParam {String} [username] 上传者用户名
  2229. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  2230. * - key: name|size|username|indate
  2231. * - order: asc|desc
  2232. * @apiParam {Number} [page] 当前页,默认:1
  2233. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  2234. */
  2235. public function files__lists()
  2236. {
  2237. $user = Users::authE();
  2238. if (Base::isError($user)) {
  2239. return $user;
  2240. } else {
  2241. $user = $user['data'];
  2242. }
  2243. //
  2244. $taskid = intval(Request::input('taskid'));
  2245. if ($taskid > 0) {
  2246. $projectid = intval(DB::table('project_task')->select(['projectid'])->where([ 'id' => $taskid])->value('projectid'));
  2247. } else {
  2248. $projectid = intval(Request::input('projectid'));
  2249. }
  2250. if ($projectid > 0) {
  2251. $inRes = Project::inThe($projectid, $user['username']);
  2252. if (Base::isError($inRes)) {
  2253. return $inRes;
  2254. }
  2255. }
  2256. //
  2257. $orderBy = '`id` DESC';
  2258. $sorts = Base::json2array(Request::input('sorts'));
  2259. if (in_array($sorts['order'], ['asc', 'desc'])) {
  2260. switch ($sorts['key']) {
  2261. case 'name':
  2262. case 'size':
  2263. case 'download':
  2264. case 'username':
  2265. case 'indate':
  2266. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  2267. break;
  2268. }
  2269. }
  2270. //
  2271. $whereArray = [];
  2272. if ($projectid > 0) {
  2273. $whereArray[] = ['projectid', '=', $projectid];
  2274. if ($taskid > 0) {
  2275. $whereArray[] = ['taskid', '=', $taskid];
  2276. }
  2277. } else {
  2278. if ($taskid <= 0) {
  2279. return Base::retError('参数错误!');
  2280. }
  2281. $tmpLists = Project::taskSomeUsers($taskid);
  2282. if (!in_array($user['username'], $tmpLists)) {
  2283. return Base::retError('未能找到此任务或无法管理此任务!');
  2284. }
  2285. $whereArray[] = ['taskid', '=', $taskid];
  2286. }
  2287. $whereArray[] = ['delete', '=', 0];
  2288. if (intval(Request::input('taskid')) > 0) {
  2289. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  2290. }
  2291. if (trim(Request::input('name'))) {
  2292. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  2293. }
  2294. if (trim(Request::input('username'))) {
  2295. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  2296. }
  2297. //
  2298. $lists = DB::table('project_files')
  2299. ->where($whereArray)
  2300. ->orderByRaw($orderBy)->paginate(Base::getPaginate(100, 20));
  2301. $lists = Base::getPageList($lists);
  2302. if ($lists['total'] == 0) {
  2303. return Base::retError('未找到任何相关的文件', $lists);
  2304. }
  2305. foreach ($lists['lists'] AS $key => $item) {
  2306. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  2307. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  2308. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  2309. }
  2310. return Base::retSuccess('success', $lists);
  2311. }
  2312. /**
  2313. * 项目文件-上传
  2314. *
  2315. * @apiParam {Number} [projectid] get-项目ID
  2316. * @apiParam {Number} [taskid] get-任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  2317. * @apiParam {String} [filename] post-文件名称
  2318. * @apiParam {String} [image64] post-base64图片(二选一)
  2319. * @apiParam {File} [files] post-文件对象(二选一)
  2320. */
  2321. public function files__upload()
  2322. {
  2323. $user = Users::authE();
  2324. if (Base::isError($user)) {
  2325. return $user;
  2326. } else {
  2327. $user = $user['data'];
  2328. }
  2329. //
  2330. $projectid = intval(Request::input('projectid'));
  2331. $taskid = intval(Request::input('taskid'));
  2332. if ($projectid > 0) {
  2333. $inRes = Project::inThe($projectid, $user['username']);
  2334. if (Base::isError($inRes)) {
  2335. return $inRes;
  2336. }
  2337. } else {
  2338. if ($taskid <= 0) {
  2339. return Base::retError('参数错误!');
  2340. }
  2341. $tmpLists = Project::taskSomeUsers($taskid);
  2342. if (!in_array($user['username'], $tmpLists)) {
  2343. return Base::retError('未能找到此任务或无法管理此任务!');
  2344. }
  2345. $projectid = DB::table('project_task')->where('id', $taskid)->value('projectid');
  2346. }
  2347. //
  2348. $path = "uploads/projects/" . ($projectid ?: Users::token2userid()) . "/";
  2349. $image64 = trim(Base::getPostValue('image64'));
  2350. $fileName = trim(Base::getPostValue('filename'));
  2351. if ($image64) {
  2352. $data = Base::image64save([
  2353. "image64" => $image64,
  2354. "path" => $path,
  2355. "fileName" => $fileName,
  2356. ]);
  2357. } else {
  2358. $data = Base::upload([
  2359. "file" => Request::file('files'),
  2360. "type" => 'file',
  2361. "path" => $path,
  2362. "fileName" => $fileName,
  2363. ]);
  2364. }
  2365. //
  2366. if (Base::isError($data)) {
  2367. return Base::retError($data['msg']);
  2368. } else {
  2369. $fileData = $data['data'];
  2370. $fileData['thumb'] = $fileData['thumb'] ?: 'images/files/file.png';
  2371. switch ($fileData['ext']) {
  2372. case "docx":
  2373. $fileData['thumb'] = 'images/files/doc.png';
  2374. break;
  2375. case "xlsx":
  2376. $fileData['thumb'] = 'images/files/xls.png';
  2377. break;
  2378. case "pptx":
  2379. $fileData['thumb'] = 'images/files/ppt.png';
  2380. break;
  2381. case "doc":
  2382. case "xls":
  2383. case "ppt":
  2384. case "txt":
  2385. case "esp":
  2386. case "gif":
  2387. $fileData['thumb'] = 'images/files/' . $fileData['ext'] . '.png';
  2388. break;
  2389. }
  2390. $array = [
  2391. 'projectid' => $projectid,
  2392. 'taskid' => $taskid,
  2393. 'name' => $fileData['name'],
  2394. 'size' => $fileData['size'] * 1024,
  2395. 'ext' => $fileData['ext'],
  2396. 'path' => $fileData['path'],
  2397. 'thumb' => $fileData['thumb'],
  2398. 'username' => $user['username'],
  2399. 'indate' => Base::time(),
  2400. ];
  2401. $id = DB::table('project_files')->insertGetId($array);
  2402. $array['id'] = $id;
  2403. $array['path'] = Base::fillUrl($array['path']);
  2404. $array['thumb'] = Base::fillUrl($array['thumb']);
  2405. $array['download'] = 0;
  2406. $array['yetdown'] = 0;
  2407. DB::table('project_log')->insert([
  2408. 'type' => '日志',
  2409. 'projectid' => $projectid,
  2410. 'taskid' => $taskid,
  2411. 'username' => $user['username'],
  2412. 'detail' => '上传文件',
  2413. 'indate' => Base::time(),
  2414. 'other' => Base::array2string([
  2415. 'type' => 'file',
  2416. 'id' => $id,
  2417. 'name' => $fileData['name'],
  2418. ])
  2419. ]);
  2420. if ($taskid > 0) {
  2421. DB::table('project_task')->where('id', $taskid)->increment('filenum');
  2422. }
  2423. return Base::retSuccess('success', $array);
  2424. }
  2425. }
  2426. /**
  2427. * 项目文件-下载
  2428. *
  2429. * @apiParam {Number} fileid 文件ID
  2430. */
  2431. public function files__download()
  2432. {
  2433. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2434. if (empty($fileDetail)) {
  2435. return abort(404, '文件不存在或已被删除!');
  2436. }
  2437. $filePath = public_path($fileDetail['path']);
  2438. if (!file_exists($filePath)) {
  2439. return abort(404, '文件不存在或已被删除。');
  2440. }
  2441. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  2442. Session::put('filesDownload:' . $fileDetail['id'], 1);
  2443. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  2444. }
  2445. return response()->download($filePath, $fileDetail['name']);
  2446. }
  2447. /**
  2448. * 项目文件-重命名
  2449. *
  2450. * @apiParam {Number} fileid 文件ID
  2451. * @apiParam {String} name 新文件名称
  2452. */
  2453. public function files__rename()
  2454. {
  2455. $user = Users::authE();
  2456. if (Base::isError($user)) {
  2457. return $user;
  2458. } else {
  2459. $user = $user['data'];
  2460. }
  2461. //
  2462. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2463. if (empty($fileDetail)) {
  2464. return Base::retError('文件不存在或已被删除!');
  2465. }
  2466. if ($fileDetail['username'] != $user['username']) {
  2467. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  2468. if (Base::isError($inRes)) {
  2469. return Base::retError('此操作仅支持管理员或上传者!');
  2470. }
  2471. }
  2472. //
  2473. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  2474. if (empty($name)) {
  2475. return Base::retError('文件名称不能为空!');
  2476. } elseif (mb_strlen($name) > 32) {
  2477. return Base::retError('文件名称最多只能设置32个字!');
  2478. }
  2479. //
  2480. $name .= '.' . $fileDetail['ext'];
  2481. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  2482. DB::table('project_log')->insert([
  2483. 'type' => '日志',
  2484. 'projectid' => $fileDetail['projectid'],
  2485. 'taskid' => $fileDetail['taskid'],
  2486. 'username' => $user['username'],
  2487. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  2488. 'indate' => Base::time(),
  2489. 'other' => Base::array2string([
  2490. 'type' => 'file',
  2491. 'id' => $fileDetail['id'],
  2492. 'name' => $name,
  2493. ])
  2494. ]);
  2495. }
  2496. //
  2497. return Base::retSuccess('修改成功!', [
  2498. 'name' => $name,
  2499. ]);
  2500. }
  2501. /**
  2502. * 项目文件-删除
  2503. *
  2504. * @apiParam {Number} fileid 文件ID
  2505. */
  2506. public function files__delete()
  2507. {
  2508. $user = Users::authE();
  2509. if (Base::isError($user)) {
  2510. return $user;
  2511. } else {
  2512. $user = $user['data'];
  2513. }
  2514. //
  2515. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2516. if (empty($fileDetail)) {
  2517. return Base::retError('文件不存在或已被删除!');
  2518. }
  2519. if ($fileDetail['username'] != $user['username']) {
  2520. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  2521. if (Base::isError($inRes)) {
  2522. return Base::retError('此操作仅支持管理员或上传者!');
  2523. }
  2524. }
  2525. //
  2526. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  2527. 'delete' => 1,
  2528. 'deletedate' => Base::time()
  2529. ]);
  2530. DB::table('project_log')->insert([
  2531. 'type' => '日志',
  2532. 'projectid' => $fileDetail['projectid'],
  2533. 'taskid' => $fileDetail['taskid'],
  2534. 'username' => $user['username'],
  2535. 'detail' => '删除文件',
  2536. 'indate' => Base::time(),
  2537. 'other' => Base::array2string([
  2538. 'type' => 'file',
  2539. 'id' => $fileDetail['id'],
  2540. 'name' => $fileDetail['name'],
  2541. ])
  2542. ]);
  2543. if ($fileDetail['taskid'] > 0) {
  2544. DB::table('project_task')->where('id', $fileDetail['taskid'])->decrement('filenum');
  2545. }
  2546. //
  2547. return Base::retSuccess('删除成功!');
  2548. }
  2549. /**
  2550. * 项目动态-列表
  2551. *
  2552. * @apiParam {Number} [projectid] 项目ID
  2553. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  2554. * @apiParam {String} [type] 类型
  2555. * - 全部: 日志+评论(默认)
  2556. * - 日志
  2557. * - 评论
  2558. * @apiParam {String} [username] 用户名
  2559. * @apiParam {Number} [page] 当前页,默认:1
  2560. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  2561. */
  2562. public function log__lists()
  2563. {
  2564. $user = Users::authE();
  2565. if (Base::isError($user)) {
  2566. return $user;
  2567. } else {
  2568. $user = $user['data'];
  2569. }
  2570. //
  2571. $projectid = intval(Request::input('projectid'));
  2572. if ($projectid > 0) {
  2573. $inRes = Project::inThe($projectid, $user['username']);
  2574. if (Base::isError($inRes)) {
  2575. return $inRes;
  2576. }
  2577. }
  2578. //
  2579. $taskid = intval(Request::input('taskid'));
  2580. $whereArray = [];
  2581. $whereFunc = null;
  2582. if ($projectid > 0) {
  2583. $whereArray[] = ['projectid', '=', $projectid];
  2584. if ($taskid > 0) {
  2585. $whereArray[] = ['taskid', '=', $taskid];
  2586. }
  2587. } else {
  2588. if ($taskid < 0) {
  2589. return Base::retError('参数错误!');
  2590. }
  2591. $tmpLists = Project::taskSomeUsers($taskid);
  2592. if (!in_array($user['username'], $tmpLists)) {
  2593. return Base::retError('未能找到此任务或无法管理此任务!');
  2594. }
  2595. $whereArray[] = ['taskid', '=', $taskid];
  2596. }
  2597. if (trim(Request::input('username'))) {
  2598. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  2599. }
  2600. switch (trim(Request::input('type'))) {
  2601. case '日志': {
  2602. $whereArray[] = ['type', '=', '日志'];
  2603. break;
  2604. }
  2605. case '评论': {
  2606. $whereArray[] = ['type', '=', '评论'];
  2607. break;
  2608. }
  2609. default: {
  2610. $whereFunc = function ($query) {
  2611. $query->whereIn('type', ['日志', '评论']);
  2612. };
  2613. break;
  2614. }
  2615. }
  2616. //
  2617. $lists = DB::table('project_log')
  2618. ->where($whereArray)
  2619. ->where($whereFunc)
  2620. ->orderByDesc('indate')->paginate(Base::getPaginate(100, 20));
  2621. $lists = Base::getPageList($lists);
  2622. if ($lists['total'] == 0) {
  2623. return Base::retError('未找到任何相关的记录', $lists);
  2624. }
  2625. foreach ($lists['lists'] AS $key => $item) {
  2626. $item = array_merge($item, Users::username2basic($item['username']));
  2627. if (empty($item['userimg'])) {
  2628. $item['userimg'] = Users::userimg($item['userimg']);
  2629. }
  2630. $item['timeData'] = [
  2631. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  2632. 'hi' => date("h:i", $item['indate']) ,
  2633. 'week' => "周" . Base::getTimeWeek($item['indate']),
  2634. 'segment' => Base::getTimeDayeSegment($item['indate']),
  2635. ];
  2636. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  2637. $lists['lists'][$key] = $item;
  2638. }
  2639. return Base::retSuccess('success', $lists);
  2640. }
  2641. }