Api.php 23 KB

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