ProjectController.php 138 KB

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