Api.php 19 KB

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