Api.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. if (in_array($checkState, [0, 1, 2])) {
  71. $where[] = ["step", "=", 1];
  72. }
  73. $where[] = ["project", "=", $project];
  74. $where[] = ["type", "=", $type];
  75. return json(["rows" => Db::table("new_common_filetype")->where($where)->order("sn " . $order)->select()]);
  76. }
  77. public function listTalentFile() {
  78. $param = $this->request->param();
  79. $mainId = $param["mainId"];
  80. $typeId = $param["fileTypeId"];
  81. $where = [["mainId", "=", $mainId], ["typeId", "=", $typeId]];
  82. $list = Db::table("new_talent_file")->where($where)->select()->toArray();
  83. foreach ($list as $key => $item) {
  84. $list[$key]["url"] = "/storage/" . $item["url"]; //获取系统配置无效,暂时这样
  85. }
  86. return json($list);
  87. }
  88. public function addTalentFile() {
  89. $backName = $this->request->param("backName");
  90. $fileId = $this->request->param("fileId");
  91. $mainId = $this->request->param("mainId");
  92. $fileTypeId = $this->request->param("fileTypeId");
  93. $index = $this->request->param("index");
  94. $type = $this->request->param("type");
  95. $upload = new \app\common\api\UploadApi();
  96. $file = $this->request->file("fileUrl");
  97. if (!TalentApi::checkIsEditable($mainId)) {
  98. $res = ["msg" => "当前状态不能修改附件", "obj" => $index];
  99. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  100. exit();
  101. }
  102. $filestd = $upload->uploadOne($file, "image", "talent_files");
  103. if ($fileId) {
  104. if (!$this->chkIsFileOwner($mainId, $type)) {
  105. $res = ["msg" => "删除失败", "obj" => $index];
  106. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  107. exit();
  108. }
  109. $old = Db::table("new_talent_file")->findOrEmpty($fileId);
  110. $old_filepath = "storage/" . $old["url"];
  111. if (file_exists($old_filepath))
  112. unlink($old_filepath);
  113. $data["id"] = $fileId;
  114. }
  115. $data["mainId"] = $mainId;
  116. $data["type"] = $type;
  117. $data["typeId"] = $fileTypeId;
  118. $data["orignName"] = $file->getOriginalName();
  119. $data["url"] = $filestd->filepath;
  120. $data["sn"] = $index;
  121. $data["createTime"] = time();
  122. Db::table("new_talent_file")->save($data);
  123. TalentLogApi::write($type, $mainId, 0, sprintf("添加附件,附件名为:%s", $data["orignName"]), 1, $fileTypeId);
  124. $res = ["code" => 200, "msg" => "上传附件成功", "obj" => $index, "info" => $url];
  125. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  126. }
  127. public function deleteFile() {
  128. $param = $this->request->param();
  129. $where = [["id", "=", $param["id"]]];
  130. $file = Db::table("new_talent_file")->where($where)->findOrEmpty();
  131. if (!TalentApi::checkIsEditable($file["mainId"]))
  132. return json(["msg" => "当前状态不能删除"]);
  133. if ($this->chkIsFileOwner($file["mainId"], $file["type"])) {
  134. $filepath = "storage/" . $file["url"];
  135. if (file_exists($filepath)) {
  136. unlink($filepath);
  137. }
  138. Db::table("new_talent_file")->delete($file["id"]);
  139. TalentLogApi::write($file["type"], $file["mainId"], 0, sprintf("删除附件,附件名为:%s", $file["orignName"]), 1, $file["typeId"]);
  140. return json(["code" => 200, "msg" => "删除成功"]);
  141. }
  142. return json(["msg" => "不能删除"]);
  143. }
  144. /**
  145. * 下载文件
  146. */
  147. public function downloadFile() {
  148. $param = $this->request->param();
  149. $type = $param["type"];
  150. $id = $param["id"];
  151. $where = [];
  152. $where[] = ["id", "=", $id];
  153. $where[] = ["type", "=", $type];
  154. $fileinfo = Db::table("new_talent_file")->where($where)->findOrEmpty();
  155. $filename = $fileinfo["orignName"];
  156. $filepath = "storage/" . $fileinfo["url"]; // 下载文件名
  157. if (!file_exists($filepath)) {
  158. header('HTTP/1.1 404 NOT FOUND');
  159. } else {
  160. $file = fopen($filepath, "rb");
  161. Header("Content-type: application/octet-stream");
  162. Header("Accept-Ranges: bytes");
  163. Header("Accept-Length: " . filesize($filepath));
  164. Header("Content-Disposition: attachment; filename=" . $filename);
  165. echo fread($file, filesize($filepath));
  166. fclose($file);
  167. exit();
  168. }
  169. }
  170. /**
  171. * 打包下载人才申请附件
  172. */
  173. public function downloadZip() {
  174. $param = $this->request->param();
  175. $type = $param["type"];
  176. $id = $param["id"];
  177. $where = [];
  178. $where[] = ["mainId", "=", $id];
  179. $where[] = ["type", "=", $type];
  180. $files = Db::table("new_talent_file")->where($where)->select()->toArray();
  181. if (!$files)
  182. die("没有附件不能打包下载");
  183. $talent_info = \app\enterprise\model\Talent::findOrEmpty($id);
  184. $enterprise_info = \app\common\model\Enterprise::findOrEmpty($talent_info["enterprise_id"]);
  185. $zip_filename = sprintf("%s(%s)人才申报材料.zip", $talent_info["name"], $enterprise_info["name"]);
  186. $tmp_path = "storage/temp/";
  187. $tmp_file_path = $tmp_path . $zip_filename;
  188. if (!file_exists($tmp_path)) {
  189. mkdir($tmp_path);
  190. }
  191. $zip = new \ZipArchive();
  192. if (!$zip->open($tmp_file_path, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) {
  193. header('HTTP/1.1 404 NOT FOUND');
  194. }
  195. foreach ($files as $file) {
  196. $filepath = "storage/" . $file["url"];
  197. $filename = $file["orignName"];
  198. $zip->addFile($filepath, $filename);
  199. }
  200. $zip->close();
  201. if (file_exists($tmp_file_path)) {
  202. header("Cache-Control: public");
  203. header("Content-Description: File Transfer");
  204. header('Content-disposition: attachment; filename=' . $zip_filename); //文件名
  205. header("Content-Type: application/octet-stream;charset=utf-8"); //zip格式的
  206. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  207. header('Content-Length: ' . filesize($tmp_file_path)); //告诉浏览器,文件大小
  208. @readfile($tmp_file_path);
  209. }
  210. //删除临时文件
  211. @unlink($tmp_file_path);
  212. }
  213. private function chkIsFileOwner($mainId, $type) {
  214. switch ($type) {
  215. case 1:
  216. if ($this->user["usertype"] == 2) {
  217. $user_id = $this->user["uid"];
  218. $talent_info = Db::table("new_talent_info")->findOrEmpty($mainId);
  219. if ($user_id == $talent_info["enterprise_id"])
  220. return true;
  221. }
  222. break;
  223. }
  224. return false;
  225. }
  226. public function getCompanyKvs() {
  227. $companys = \app\common\model\Company::field("name,id")->select();
  228. return json($companys);
  229. }
  230. }