ProjectController.php 138 KB

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