Api.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\common\controller;
  3. use app\BaseController;
  4. use app\common\middleware\Auth;
  5. use think\facade\Db;
  6. use think\facade\Config;
  7. /**
  8. * 需要权限的公共方法放这
  9. * Description of Tool
  10. *
  11. * @author sgq
  12. */
  13. class Api extends BaseController {
  14. protected $middleware = [Auth::class];
  15. protected $user;
  16. public function __construct(\think\App $app) {
  17. parent::__construct($app);
  18. $this->user = session("user");
  19. }
  20. public function findCommonFileType() {
  21. $param = $this->request->param();
  22. $order = $param["order"];
  23. $project = $param["project"];
  24. $type = $param["type"];
  25. $checkState = $param["checkState"];
  26. if ($checkState == 0) {
  27. $where[] = ["step", "=", 1];
  28. }
  29. $where[] = ["project", "=", $project];
  30. $where[] = ["type", "=", $type];
  31. return json(["rows" => Db::table("new_common_filetype")->where($where)->order("sn " . $order)->select()]);
  32. }
  33. public function listTalentFile() {
  34. $param = $this->request->param();
  35. $mainId = $param["mainId"];
  36. $typeId = $param["fileTypeId"];
  37. $where = [["mainId", "=", $mainId], ["typeId", "=", $typeId]];
  38. $list = Db::table("new_talent_file")->where($where)->select()->toArray();
  39. foreach ($list as $key => $item) {
  40. $list[$key]["url"] = "/storage/" . $item["url"]; //获取系统配置无效,暂时这样
  41. }
  42. return json($list);
  43. }
  44. public function addTalentFile() {
  45. $backName = $this->request->param("backName");
  46. $fileId = $this->request->param("fileId");
  47. $mainId = $this->request->param("mainId");
  48. $fileTypeId = $this->request->param("fileTypeId");
  49. $index = $this->request->param("index");
  50. $type = $this->request->param("type");
  51. $upload = new \app\common\api\UploadApi();
  52. $file = $this->request->file("fileUrl");
  53. $url = $upload->uploadOne($file, "talent_files");
  54. if ($fileId) {
  55. if (!$this->chkIsFileOwner($mainId, $type))
  56. return json(["msg" => "删除失败"]);
  57. $old = Db::table("new_talent_file")->findOrEmpty($fileId);
  58. $old_filepath = "storage/" . $old["url"];
  59. if (file_exists($old_filepath))
  60. unlink($old_filepath);
  61. $data["id"] = $fileId;
  62. }
  63. $data["mainId"] = $mainId;
  64. $data["type"] = $type;
  65. $data["typeId"] = $fileTypeId;
  66. $data["orignName"] = $file->getOriginalName();
  67. $data["url"] = $url;
  68. $data["sn"] = $index;
  69. $data["createTime"] = time();
  70. Db::table("new_talent_file")->save($data);
  71. $res = ["code" => 200, "msg" => "上传附件成功", "obj" => $index, "info" => $url];
  72. echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
  73. }
  74. public function deleteFile() {
  75. $param = $this->request->param();
  76. $where = [["id", "=", $param["id"]]];
  77. $file = Db::table("new_talent_file")->where($where)->findOrEmpty();
  78. if ($this->chkIsFileOwner($file["mainId"], $file["type"])) {
  79. $filepath = "storage/" . $file["url"];
  80. if (file_exists($filepath)) {
  81. unlink($filepath);
  82. }
  83. Db::table("new_talent_file")->delete($file["id"]);
  84. return json(["code" => 200, "msg" => "删除成功"]);
  85. }
  86. return json(["msg" => "不能删除"]);
  87. }
  88. private function chkIsFileOwner($mainId, $type) {
  89. switch ($type) {
  90. case 1:
  91. if ($this->user["usertype"] == 2) {
  92. $user_id = $this->user["uid"];
  93. $talent_info = Db::table("new_talent_info")->findOrEmpty($mainId);
  94. if ($user_id == $talent_info["enterprise_id"])
  95. return true;
  96. }
  97. break;
  98. }
  99. return false;
  100. }
  101. }