ProjectController.php 126 KB

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