ProjectController.php 135 KB

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