ProjectController.php 139 KB

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