ProjectController.php 108 KB

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