Api.php 15 KB

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