Api.php 21 KB

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