Api.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. /**
  10. * 需要权限的公共方法放这
  11. * Description of Tool
  12. *
  13. * @author sgq
  14. */
  15. class Api extends BaseController {
  16. protected $middleware = [Auth::class];
  17. protected $user;
  18. public function __construct(\think\App $app) {
  19. parent::__construct($app);
  20. $this->user = session("user");
  21. }
  22. public function findIdentifyConditionByLevel() {
  23. $params = $this->request->param();
  24. $id = $params["id"];
  25. if ($this->user["usertype"] == 2) {
  26. $type = $this->user["type"];
  27. } else {
  28. $talentInfo = TalentApi::getOne($id);
  29. $enterprise = \app\common\model\Enterprise::findOrEmpty($talentInfo["enterprise_id"]);
  30. $type = $enterprise["type"];
  31. }
  32. $list = \app\common\api\TalentConditionApi::getList($params["level"], $type);
  33. return json($list, 200);
  34. }
  35. public function getCheckLog() {
  36. $params = $this->request->param();
  37. $mainId = $params["mainId"];
  38. $type = $params["type"];
  39. $list = TalentLogApi::getList($type, $mainId);
  40. $new_list = [];
  41. foreach ($list as $item) {
  42. $new_item["stepName"] = DictApi::getTalentInfoStepByState($item["state"]);
  43. if ($item["state"] == 13) {
  44. $new_item["stateName"] = '<span class="label label-success">审核不通过</span>';
  45. } else if (in_array($item["state"], [3, 7, 9, 11])) {
  46. $new_item["stateName"] = '<span class="label label-primary">审核通过</span>';
  47. } else if (in_array($item["state"], [4, 8, 10, 12])) {
  48. $new_item["stateName"] = '<span class="label label-danger">审核驳回</span>';
  49. } else {
  50. $new_item["stateName"] = '<span class="label label-success">待审核</span>';
  51. }
  52. if ($item["last_state"] && $item["state"]) {
  53. $new_item["stateChange"] = sprintf("%s -> %s", DictApi::getTalentInfoStateName($item["last_state"]), DictApi::getTalentInfoStateName($item["state"]));
  54. } else {
  55. $new_item["stateChange"] = "";
  56. }
  57. $new_item["description"] = $item["description"];
  58. $new_item["createUser"] = $item["createUser"];
  59. $new_item["createTime"] = $item["createTime"];
  60. $new_list[] = $new_item;
  61. }
  62. return json(["rows" => $new_list]);
  63. }
  64. public function findCommonFileType() {
  65. $param = $this->request->param();
  66. $order = $param["order"];
  67. $project = $param["project"];
  68. $type = $param["type"];
  69. $checkState = $param["checkState"];
  70. $talent_condition = $param["talent_condition"];
  71. if (in_array($checkState, [0, 1, 2])) {
  72. $where[] = ["step", "=", 1]; //只查找人才第一步所需文件
  73. } else {
  74. $where[] = ["isConditionFile", "=", 0]; //排除人才条件上传文件
  75. }
  76. $where[] = ["project", "=", $project];
  77. $where[] = ["type", "=", $type];
  78. if ($talent_condition) {
  79. $condition_info = Db::table("new_talent_condition")->findOrEmpty($talent_condition);
  80. if ($condition_info["bindFileTypes"]) {
  81. $whr[] = ["id", "in", $condition_info["bindFileTypes"]];
  82. }
  83. }
  84. if ($whr) {
  85. $rows = Db::table("new_common_filetype")->whereOr([$where, $whr])->order("sn " . $order)->select();
  86. } else {
  87. $rows = Db::table("new_common_filetype")->where($where)->order("sn " . $order)->select();
  88. }
  89. return json(["rows" => $rows]);
  90. }
  91. public function listTalentFile() {
  92. $param = $this->request->param();
  93. $mainId = $param["mainId"];
  94. $typeId = $param["fileTypeId"];
  95. $where = [["mainId", "=", $mainId], ["typeId", "=", $typeId]];
  96. $list = Db::table("new_talent_file")->where($where)->select()->toArray();
  97. foreach ($list as $key => $item) {
  98. $list[$key]["url"] = "/storage/" . $item["url"]; //获取系统配置无效,暂时这样
  99. }
  100. return json($list);
  101. }
  102. public function addTalentFile() {
  103. $backName = $this->request->param("backName");
  104. $fileId = $this->request->param("fileId");
  105. $mainId = $this->request->param("mainId");
  106. $fileTypeId = $this->request->param("fileTypeId");
  107. $index = $this->request->param("index");
  108. $type = $this->request->param("type");
  109. $upload = new \app\common\api\UploadApi();
  110. $file = $this->request->file("fileUrl");
  111. if (!TalentApi::checkIsEditable($mainId)) {
  112. $res = ["msg" => "当前状态不能修改附件", "obj" => $index];
  113. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  114. exit();
  115. }
  116. $filestd = $upload->uploadOne($file, "image", "talent_files");
  117. if ($fileId) {
  118. if (!$this->chkIsFileOwner($mainId, $type)) {
  119. $res = ["msg" => "删除失败", "obj" => $index];
  120. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  121. exit();
  122. }
  123. $old = Db::table("new_talent_file")->findOrEmpty($fileId);
  124. $old_filepath = "storage/" . $old["url"];
  125. if (file_exists($old_filepath))
  126. unlink($old_filepath);
  127. $data["id"] = $fileId;
  128. }
  129. $data["mainId"] = $mainId;
  130. $data["type"] = $type;
  131. $data["typeId"] = $fileTypeId;
  132. $data["orignName"] = $file->getOriginalName();
  133. $data["url"] = $filestd->filepath;
  134. $data["sn"] = $index;
  135. $data["createTime"] = time();
  136. Db::table("new_talent_file")->save($data);
  137. TalentLogApi::write($type, $mainId, 0, sprintf("添加附件,附件名为:%s", $data["orignName"]), 1, $fileTypeId);
  138. $res = ["code" => 200, "msg" => "上传附件成功", "obj" => $index, "info" => $url];
  139. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  140. }
  141. public function deleteFile() {
  142. $param = $this->request->param();
  143. $where = [["id", "=", $param["id"]]];
  144. $file = Db::table("new_talent_file")->where($where)->findOrEmpty();
  145. if (!TalentApi::checkIsEditable($file["mainId"]))
  146. return json(["msg" => "当前状态不能删除"]);
  147. if ($this->chkIsFileOwner($file["mainId"], $file["type"])) {
  148. $filepath = "storage/" . $file["url"];
  149. if (file_exists($filepath)) {
  150. unlink($filepath);
  151. }
  152. Db::table("new_talent_file")->delete($file["id"]);
  153. TalentLogApi::write($file["type"], $file["mainId"], 0, sprintf("删除附件,附件名为:%s", $file["orignName"]), 1, $file["typeId"]);
  154. return json(["code" => 200, "msg" => "删除成功"]);
  155. }
  156. return json(["msg" => "不能删除"]);
  157. }
  158. /**
  159. * 下载文件
  160. */
  161. public function downloadFile() {
  162. $param = $this->request->param();
  163. $type = $param["type"];
  164. $id = $param["id"];
  165. $where = [];
  166. $where[] = ["id", "=", $id];
  167. $where[] = ["type", "=", $type];
  168. $fileinfo = Db::table("new_talent_file")->where($where)->findOrEmpty();
  169. $filename = $fileinfo["orignName"];
  170. $filepath = "storage/" . $fileinfo["url"]; // 下载文件名
  171. if (!file_exists($filepath)) {
  172. header('HTTP/1.1 404 NOT FOUND');
  173. } else {
  174. $file = fopen($filepath, "rb");
  175. Header("Content-type: application/octet-stream");
  176. Header("Accept-Ranges: bytes");
  177. Header("Accept-Length: " . filesize($filepath));
  178. Header("Content-Disposition: attachment; filename=" . $filename);
  179. echo fread($file, filesize($filepath));
  180. fclose($file);
  181. exit();
  182. }
  183. }
  184. /**
  185. * 打包下载人才申请附件
  186. */
  187. public function downloadZip() {
  188. $param = $this->request->param();
  189. $type = $param["type"];
  190. $id = $param["id"];
  191. $where = [];
  192. $where[] = ["mainId", "=", $id];
  193. $where[] = ["type", "=", $type];
  194. $files = Db::table("new_talent_file")->where($where)->select()->toArray();
  195. if (!$files)
  196. die("没有附件不能打包下载");
  197. $talent_info = \app\enterprise\model\Talent::findOrEmpty($id);
  198. $enterprise_info = \app\common\model\Enterprise::findOrEmpty($talent_info["enterprise_id"]);
  199. $zip_filename = sprintf("%s(%s)人才申报材料.zip", $talent_info["name"], $enterprise_info["name"]);
  200. $tmp_path = "storage/temp/";
  201. $tmp_file_path = $tmp_path . $zip_filename;
  202. if (!file_exists($tmp_path)) {
  203. mkdir($tmp_path);
  204. }
  205. $zip = new \ZipArchive();
  206. if (!$zip->open($tmp_file_path, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) {
  207. header('HTTP/1.1 404 NOT FOUND');
  208. }
  209. foreach ($files as $file) {
  210. $filepath = "storage/" . $file["url"];
  211. $filename = $file["orignName"];
  212. $zip->addFile($filepath, $filename);
  213. }
  214. $zip->close();
  215. if (file_exists($tmp_file_path)) {
  216. header("Cache-Control: public");
  217. header("Content-Description: File Transfer");
  218. header('Content-disposition: attachment; filename=' . $zip_filename); //文件名
  219. header("Content-Type: application/octet-stream;charset=utf-8"); //zip格式的
  220. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  221. header('Content-Length: ' . filesize($tmp_file_path)); //告诉浏览器,文件大小
  222. @readfile($tmp_file_path);
  223. }
  224. //删除临时文件
  225. @unlink($tmp_file_path);
  226. }
  227. private function chkIsFileOwner($mainId, $type) {
  228. switch ($type) {
  229. case 1:
  230. if ($this->user["usertype"] == 2) {
  231. $user_id = $this->user["uid"];
  232. $talent_info = Db::table("new_talent_info")->findOrEmpty($mainId);
  233. if ($user_id == $talent_info["enterprise_id"])
  234. return true;
  235. }
  236. break;
  237. }
  238. return false;
  239. }
  240. public function getCompanyKvs() {
  241. $companys = \app\common\model\Company::field("name,id")->select();
  242. return json($companys);
  243. }
  244. /**
  245. * 通过人才类别查找人才认定第二步骤支持的所有文件类型
  246. * 默认人才认定第二步骤,当前只有人才认定分了两步,所以此方法目前默认参数高度匹配人才认定第二阶段附件的查找
  247. */
  248. public function getConditionFileTypesByType() {
  249. $params = $this->request->param();
  250. $type = $params["type"]; //人才类型不默认,需要传
  251. $declare_type = $params["project"] ?: 1; //默认人才认定
  252. $active = $params["active"] ?: 1; //默认查找启用的附件
  253. $where[] = ["type", "=", $type];
  254. $where[] = ["project", "=", $declare_type];
  255. $where[] = ["active", "=", $active];
  256. $where[] = ["isConditionFile", "=", 1];
  257. $list = Db::table("new_common_filetype")->where($where)->order("sn " . $order)->select()->toArray();
  258. return json($list);
  259. }
  260. }