Api.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. namespace app\common\controller;
  3. use app\BaseController;
  4. use app\common\middleware\Auth;
  5. use think\facade\Db;
  6. use app\enterprise\api\TalentApi;
  7. use app\common\api\TalentLogApi;
  8. use app\common\api\DictApi;
  9. use app\common\model\CurrentcyFileType;
  10. use app\common\model\TalentCommonFile;
  11. use app\common\api\UploadApi;
  12. use app\common\api\TalentConditionApi;
  13. use app\common\api\CompanyApi;
  14. /**
  15. * 需要权限的公共方法放这
  16. * Description of Tool
  17. *
  18. * @author sgq
  19. */
  20. class Api extends BaseController {
  21. protected $middleware = [Auth::class];
  22. protected $user;
  23. public function __construct(\think\App $app) {
  24. parent::__construct($app);
  25. $this->user = session("user");
  26. }
  27. public function findIdentifyConditionByLevel() {
  28. $params = $this->request->param();
  29. $id = $params["id"];
  30. if ($this->user["usertype"] == 2) {
  31. $type = $this->user["type"];
  32. } else {
  33. $talentInfo = TalentApi::getOne($id);
  34. $enterprise = \app\common\model\Enterprise::findOrEmpty($talentInfo["enterprise_id"]);
  35. $type = $enterprise["type"];
  36. }
  37. $list = TalentConditionApi::getList($params["level"], $type);
  38. return json($list, 200);
  39. }
  40. public function getTalentCondtionUploadFile() {
  41. $param = $this->request->param();
  42. $id = $param["mainId"];
  43. $order = $param["order"];
  44. $project = $param["project"];
  45. $type = $param["type"];
  46. $talent_condition = $param["talent_condition"];
  47. $condition_info = Db::table("new_talent_condition")->findOrEmpty($talent_condition);
  48. if (!$condition_info["bindFileTypes"])
  49. return json(["rows" => null]);
  50. $whr[] = ["id", "in", $condition_info["bindFileTypes"]];
  51. $rows = Db::table("new_common_filetype")->where($whr)->order("sn " . $order)->select()->toArray();
  52. if ($id) {
  53. foreach ($rows as $key => $row) {
  54. $where = [];
  55. $where[] = ["mainId", "=", $id];
  56. $where[] = ["typeId", "=", $row["id"]];
  57. $files = Db::table("new_talent_file")->where($where)->field("id,typeId,orignName,url")->order("sn asc")->select()->toArray();
  58. foreach ($files as &$file) {
  59. $file["url"] = "/storage/" . $file["url"];
  60. }
  61. $rows[$key]["files"] = $files;
  62. }
  63. }
  64. return json(["rows" => $rows]);
  65. }
  66. public function getCheckLog() {
  67. $params = $this->request->param();
  68. $mainId = $params["mainId"];
  69. $type = $params["type"];
  70. $talentInfo = TalentApi::getOne($mainId);
  71. $condition = TalentConditionApi::getOne($talentInfo["talent_condition"]);
  72. $needDeptVerify = false;
  73. if ($condition["companyIds"])
  74. $needDeptVerify = true;
  75. $list = TalentLogApi::getList($type, $mainId);
  76. $new_list = [];
  77. foreach ($list as $item) {
  78. if($item['type'] == 10){//姑且按type还区分
  79. switch ($item['state']){
  80. case -2:
  81. $new_item["stateName"] = "<span class='label label-success'>待提交</span>";
  82. break;
  83. case -1:
  84. $new_item["stateName"] = "<span class='label label-success'>审核不通过</span>";
  85. break;
  86. case 1:
  87. $new_item["stateName"] = "<span class='label label-success'>待审核</span>";
  88. break;
  89. case 2:
  90. $new_item["stateName"] = "<span class='label label-danger'>审核驳回</span>";
  91. break;
  92. case 3:
  93. $new_item["stateName"] = "<span class='label label-primary'>审核通过</span>";
  94. break;
  95. case 4:
  96. $new_item["stateName"] = "<span class='label label-warm'>上级驳回</span>";
  97. break;
  98. case 8:
  99. $new_item["stateName"] = "<span class='label label-primary'>提交审核</span>";
  100. break;
  101. case 9:
  102. $new_item["stateName"] = "<span class='label label-primary'>重新提交</span>";
  103. break;
  104. case 10:
  105. $new_item["stateName"] = "<span class='label label-default'>冻结</span>";
  106. break;
  107. case 11:
  108. $new_item["stateName"] = "<span class='label label-primary'>撤销审核</span>";
  109. break;
  110. }
  111. }else{
  112. $new_item["stepName"] = DictApi::getTalentInfoStepByState($item["new_state"]);
  113. if ($item["state"] == 13) {
  114. $new_item["stateName"] = '<span class="label label-success">审核不通过</span>';
  115. } else if (in_array($item["state"], [3, 7, 9, 11])) {
  116. $new_item["stateName"] = '<span class="label label-primary">审核通过</span>';
  117. } else if (in_array($item["state"], [4, 8, 10, 12])) {
  118. $new_item["stateName"] = '<span class="label label-danger">审核驳回</span>';
  119. } else {
  120. $new_item["stateName"] = '<span class="label label-success">待审核</span>';
  121. }
  122. }
  123. if ($item["step"] == 3) {
  124. $company = CompanyApi::getOne($item["companyId"]);
  125. if ($item["active"] == 0) {
  126. $new_item["stateChange"] = str_replace("部门", '"' . $company["name"] . '"', DictApi::getTalentInfoStateName($item["last_state"], $item["step"], $needDeptVerify));
  127. } else {
  128. $new_item["stateChange"] = sprintf("%s -> %s", str_replace("部门", '"' . $company["name"] . '"', DictApi::getTalentInfoStateName($item["last_state"], $item["step"], $needDeptVerify)), DictApi::getTalentInfoStateName($item["new_state"], $item["step"], $needDeptVerify));
  129. }
  130. } else {
  131. if ($item["last_state"] && $item["new_state"]) {
  132. $new_item["stateChange"] = sprintf("%s -> %s", DictApi::getTalentInfoStateName($item["last_state"], $item["step"], $needDeptVerify), DictApi::getTalentInfoStateName($item["new_state"], $item["step"], $needDeptVerify));
  133. } else {
  134. $new_item["stateChange"] = $item['stateChange'];
  135. }
  136. }
  137. $new_item["description"] = $item["description"];
  138. $new_item["createUser"] = $item["updateUser"] ?: $item["createUser"];
  139. $new_item["createTime"] = $item["updateTime"] ?: $item["createTime"];
  140. $new_list[] = $new_item;
  141. }
  142. return json(["rows" => $new_list]);
  143. }
  144. public function findCommonFileType() {
  145. $param = $this->request->param();
  146. $id = $param["mainId"];
  147. $order = $param["order"];
  148. $project = $param["project"];
  149. $type = $param["type"];
  150. $checkState = $param["checkState"];
  151. $talent_condition = $param["talent_condition"];
  152. if (in_array($checkState, [0, 1, 2])) {
  153. $where[] = ["step", "=", 1]; //只查找人才第一步所需文件
  154. } else {
  155. $where[] = ["isConditionFile", "=", 0]; //排除人才条件上传文件
  156. }
  157. $where[] = ["project", "=", $project];
  158. $where[] = ["type", "=", $type];
  159. if ($talent_condition) {
  160. $condition_info = Db::table("new_talent_condition")->findOrEmpty($talent_condition);
  161. if ($condition_info["bindFileTypes"]) {
  162. $whr[] = ["id", "in", $condition_info["bindFileTypes"]];
  163. }
  164. }
  165. if ($whr) {
  166. $rows = Db::table("new_common_filetype")->whereOr([$where, $whr])->order("sn " . $order)->select()->toArray();
  167. } else {
  168. $rows = Db::table("new_common_filetype")->where($where)->order("sn " . $order)->select()->toArray();
  169. }
  170. if ($id) {
  171. foreach ($rows as $key => $row) {
  172. $where = [];
  173. $where[] = ["mainId", "=", $id];
  174. $where[] = ["typeId", "=", $row["id"]];
  175. $files = Db::table("new_talent_file")->where($where)->field("id,typeId,orignName,url")->order("sn asc")->select()->toArray();
  176. foreach ($files as &$file) {
  177. $file["url"] = "/storage/" . $file["url"];
  178. }
  179. $rows[$key]["files"] = $files;
  180. }
  181. }
  182. return json(["rows" => $rows]);
  183. }
  184. public function listTalentFile() {
  185. $param = $this->request->param();
  186. $mainId = $param["mainId"];
  187. $typeId = $param["fileTypeId"];
  188. $where = [["mainId", "=", $mainId], ["typeId", "=", $typeId]];
  189. $list = Db::table("new_talent_file")->where($where)->select()->toArray();
  190. foreach ($list as $key => $item) {
  191. $list[$key]["url"] = "/storage/" . $item["url"]; //获取系统配置无效,暂时这样
  192. }
  193. return json($list);
  194. }
  195. public function addTalentFile() {
  196. $backName = $this->request->param("backName");
  197. $fileId = $this->request->param("fileId");
  198. $mainId = $this->request->param("mainId");
  199. $fileTypeId = $this->request->param("fileTypeId");
  200. $index = $this->request->param("index");
  201. $type = $this->request->param("type");
  202. $upload = new \app\common\api\UploadApi();
  203. $file = $this->request->file("fileUrl");
  204. if (!TalentApi::checkIsEditable($mainId)) {
  205. $res = ["msg" => "当前状态不能修改附件", "obj" => $index];
  206. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  207. exit();
  208. }
  209. $mime = $file->getMime();
  210. switch ($mime) {
  211. case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"://xlsx
  212. case "application/pdf"://pdf
  213. case "application/vnd.ms-excel"://xls
  214. $filestd = $upload->uploadOne($file, "file", "talent_files");
  215. break;
  216. case "image/jpg":
  217. case "image/jpeg":
  218. case "image/png":
  219. case "image/gif":
  220. $filestd = $upload->uploadOne($file, "image", "talent_files");
  221. break;
  222. default:
  223. $res = ["msg" => "不支持的附件类型", "obj" => $index];
  224. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  225. exit();
  226. break;
  227. }
  228. $change = false;
  229. if ($fileId) {
  230. if (!$this->chkIsFileOwner($mainId, $type)) {
  231. $res = ["msg" => "删除失败", "obj" => $index];
  232. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  233. exit();
  234. }
  235. $old = Db::table("new_talent_file")->findOrEmpty($fileId);
  236. $old_filepath = "storage/" . $old["url"];
  237. if (file_exists($old_filepath))
  238. unlink($old_filepath);
  239. $data["id"] = $fileId;
  240. $change = true;
  241. }
  242. $data["mainId"] = $mainId;
  243. $data["type"] = $type;
  244. $data["typeId"] = $fileTypeId;
  245. $data["orignName"] = $file->getOriginalName();
  246. $data["url"] = $filestd->filepath;
  247. $data["sn"] = $index;
  248. $data["createTime"] = time();
  249. if ($fileId) {
  250. Db::table("new_talent_file")->save($data);
  251. } else {
  252. $fileId = Db::table("new_talent_file")->insertGetId($data);
  253. }
  254. TalentLogApi::write($type, $mainId, 0, sprintf("%s附件,附件名为:%s", $change ? "修改" : "添加", $data["orignName"]), 1, $fileTypeId, $fileId);
  255. $res = ["code" => 200, "msg" => "上传附件成功", "obj" => $index, "info" => "/storage/" . $filestd->filepath, "typeId" => $fileTypeId, "id" => $fileId, "orignName" => $data["orignName"]];
  256. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  257. }
  258. public function deleteFile() {
  259. $param = $this->request->param();
  260. $where = [["id", "=", $param["id"]]];
  261. $file = Db::table("new_talent_file")->where($where)->findOrEmpty();
  262. if (!TalentApi::checkIsEditable($file["mainId"]))
  263. return json(["msg" => "当前状态不能删除"]);
  264. if ($this->chkIsFileOwner($file["mainId"], $file["type"])) {
  265. $filepath = "storage/" . $file["url"];
  266. if (file_exists($filepath)) {
  267. unlink($filepath);
  268. }
  269. Db::table("new_talent_file")->delete($file["id"]);
  270. TalentLogApi::write($file["type"], $file["mainId"], 0, sprintf("删除附件,附件名为:%s", $file["orignName"]), 1, $file["typeId"], $param["id"]);
  271. return json(["code" => 200, "msg" => "删除成功"]);
  272. }
  273. return json(["msg" => "不能删除"]);
  274. }
  275. /**
  276. * 下载文件
  277. */
  278. public function downloadFile() {
  279. $param = $this->request->param();
  280. $type = $param["type"];
  281. $id = $param["id"];
  282. $where = [];
  283. $where[] = ["id", "=", $id];
  284. $where[] = ["type", "=", $type];
  285. $fileinfo = Db::table("new_talent_file")->where($where)->findOrEmpty();
  286. $filename = $fileinfo["orignName"];
  287. $filepath = "storage/" . $fileinfo["url"]; // 下载文件名
  288. if (!file_exists($filepath)) {
  289. header('HTTP/1.1 404 NOT FOUND');
  290. } else {
  291. $file = fopen($filepath, "rb");
  292. Header("Content-type: application/octet-stream");
  293. Header("Accept-Ranges: bytes");
  294. Header("Accept-Length: " . filesize($filepath));
  295. Header("Content-Disposition: attachment; filename=" . $filename);
  296. echo fread($file, filesize($filepath));
  297. fclose($file);
  298. exit();
  299. }
  300. }
  301. /**
  302. * 打包下载人才申请附件
  303. */
  304. public function downloadZip() {
  305. $param = $this->request->param();
  306. $type = $param["type"];
  307. $id = $param["id"];
  308. $where = [];
  309. $where[] = ["mainId", "=", $id];
  310. $where[] = ["type", "=", $type];
  311. $files = Db::table("new_talent_file")->where($where)->select()->toArray();
  312. if (!$files)
  313. die("没有附件不能打包下载");
  314. $talent_info = \app\enterprise\model\Talent::findOrEmpty($id);
  315. $enterprise_info = \app\common\model\Enterprise::findOrEmpty($talent_info["enterprise_id"]);
  316. $zip_filename = sprintf("%s(%s)人才申报材料.zip", $talent_info["name"], $enterprise_info["name"]);
  317. $tmp_path = "storage/temp/";
  318. $tmp_file_path = $tmp_path . $zip_filename;
  319. if (!file_exists($tmp_path)) {
  320. mkdir($tmp_path);
  321. }
  322. $zip = new \ZipArchive();
  323. if (!$zip->open($tmp_file_path, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) {
  324. header('HTTP/1.1 404 NOT FOUND');
  325. }
  326. foreach ($files as $file) {
  327. $filepath = "storage/" . $file["url"];
  328. $filename = $file["orignName"];
  329. $zip->addFile($filepath, $filename);
  330. }
  331. $zip->close();
  332. if (file_exists($tmp_file_path)) {
  333. header("Cache-Control: public");
  334. header("Content-Description: File Transfer");
  335. header('Content-disposition: attachment; filename=' . $zip_filename); //文件名
  336. header("Content-Type: application/octet-stream;charset=utf-8"); //zip格式的
  337. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  338. header('Content-Length: ' . filesize($tmp_file_path)); //告诉浏览器,文件大小
  339. @readfile($tmp_file_path);
  340. }
  341. //删除临时文件
  342. @unlink($tmp_file_path);
  343. }
  344. private function chkIsFileOwner($mainId, $type) {
  345. if (!$mainId)
  346. return true;
  347. switch ($type) {
  348. case 1:
  349. if ($this->user["usertype"] == 2) {
  350. $user_id = $this->user["uid"];
  351. $talent_info = Db::table("new_talent_info")->findOrEmpty($mainId);
  352. if ($user_id == $talent_info["enterprise_id"])
  353. return true;
  354. }
  355. break;
  356. }
  357. return false;
  358. }
  359. public function getCompanyKvs() {
  360. $companys = \app\common\model\Company::field("name,id")->select();
  361. return json($companys);
  362. }
  363. /**
  364. * 通过人才类别查找人才认定第二步骤支持的所有文件类型
  365. * 默认人才认定第二步骤,当前只有人才认定分了两步,所以此方法目前默认参数高度匹配人才认定第二阶段附件的查找
  366. */
  367. public function getConditionFileTypesByType() {
  368. $params = $this->request->param();
  369. $type = $params["type"]; //人才类型不默认,需要传
  370. $declare_type = $params["project"] ?: 1; //默认人才认定
  371. $active = $params["active"] ?: 1; //默认查找启用的附件
  372. $where[] = ["type", "=", $type];
  373. $where[] = ["project", "=", $declare_type];
  374. $where[] = ["active", "=", $active];
  375. $where[] = ["isConditionFile", "=", 1];
  376. $list = Db::table("new_common_filetype")->where($where)->order("sn " . $order)->select()->toArray();
  377. return json($list);
  378. }
  379. public function listCurrencyFileType() {
  380. $where = [
  381. 'type' => $this->request['type'],
  382. 'active' => 1
  383. ];
  384. $rows = CurrentcyFileType::where($where)->select();
  385. return json(["rows" => $rows, 'total' => count($rows)]);
  386. }
  387. public function listTalentCommonFile() {
  388. $where = [];
  389. if (\StrUtil::isNotEmpAndNull($this->request['mainId'])) {
  390. $where[] = ['mainId', '=', $this->request['mainId']];
  391. }
  392. if (\StrUtil::isNotEmpAndNull($this->request['typeId'])) {
  393. $where[] = ['typeId', '=', $this->request['typeId']];
  394. }
  395. $res = TalentCommonFile::where($where)->order('sn')->select();
  396. if ($res) {
  397. foreach ($res as $k => &$v) {
  398. $v['url'] = "/storage/" . $v['url'];
  399. }
  400. }
  401. return json($res);
  402. }
  403. public function addTalentCommonFile() {
  404. $backName = \StrUtil::getRequestDecodeParam($this->request, 'backName');
  405. $id = \StrUtil::getRequestDecodeParam($this->request, "fileId");
  406. $mainId = \StrUtil::getRequestDecodeParam($this->request, "mainId");
  407. $typeId = \StrUtil::getRequestDecodeParam($this->request, "typeId");
  408. $index = \StrUtil::getRequestDecodeParam($this->request, "index");
  409. if ($backName == "EpChangeEdit.callBack") {
  410. $type = 1;
  411. $error = "文件格式不正确,只能上传图片";
  412. } else {
  413. $type = 4;
  414. $error = "文件格式不正确,只能上传pdf和图片";
  415. }
  416. $uploadapi = new UploadApi();
  417. $file_check_res = $uploadapi->uploadOne($this->request->file('fileUrl'), 'system');
  418. if ($file_check_res->code == 500) {
  419. return \StrUtil::back($file_check_res, "Register.epCallBack");
  420. }
  421. $file_data = [
  422. 'id' => getStringId(),
  423. 'mainId' => $mainId,
  424. 'typeId' => $typeId,
  425. 'orignName' => $this->request->file('fileUrl')->getOriginalName(),
  426. 'url' => $file_check_res->filepath
  427. ];
  428. if (\StrUtil::isEmpOrNull($id)) {
  429. $tc = TalentCommonFile::where('mainId', $mainId)->where('typeId', $typeId)->order('sn', 'desc')->findOrEmpty();
  430. if ($tc) {
  431. $file_data['sn'] = $tc['sn'] + 1;
  432. } else {
  433. $file_data['sn'] = 1;
  434. }
  435. $file_data['createTime'] = date("Y-m-d H:i:s");
  436. TalentCommonFile::create($file_data);
  437. $response_object = new \StdClass();
  438. $response_object->code = 200;
  439. $response_object->msg = "附件上传成功!";
  440. $response_object->obj = $index;
  441. return \StrUtil::back($response_object, $backName);
  442. } else {
  443. $tf = TalentCommonFile::findOrEmpty($id);
  444. $tf->originalName = $file_data['orignName'];
  445. $tf->updateTime = date("Y-m-d H:i:s");
  446. $tf->url = $file_check_res->filepath;
  447. $tf->save();
  448. $response_object = new \StdClass();
  449. $response_object->code = 200;
  450. $response_object->msg = "附件修改成功!";
  451. $response_object->obj = $index;
  452. return \StrUtil::back($response_object, $backName);
  453. }
  454. }
  455. }