ProjectController.php 110 KB

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