CommonFile.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\common\AdminController;
  4. use app\common\api\CommonFileApi;
  5. /**
  6. * Description of System
  7. *
  8. * @author sgq
  9. */
  10. class CommonFile extends AdminController {
  11. /**
  12. * @auth {{/commonFile/}}
  13. * @return type
  14. */
  15. public function index() {
  16. return view();
  17. }
  18. public function list() {
  19. $params = $this->request->param();
  20. $res = CommonFileApi::getList($params);
  21. return json($res);
  22. }
  23. public function add() {
  24. $params = $this->request->param();
  25. $returnObj = new \stdClass();
  26. $returnObj->obj = $params["index"];
  27. try {
  28. if (!$this->request->file())
  29. throw new \think\Exception("请上传附件");
  30. $file = $this->request->file("fileUrl");
  31. $upload = new \app\common\api\UploadApi();
  32. $result = $upload->uploadOne($file, "system", "common/file");
  33. if ($result->code != 200)
  34. throw new \think\Exception($result->msg);
  35. $data["url"] = $result->filepath;
  36. $data["originalName"] = $file->getOriginalName();
  37. $data["description"] = $params["description"];
  38. if (CommonFileApi::edit($data)) {
  39. $returnObj->msg = "上传附件成功";
  40. $returnObj->code = 200;
  41. echo sprintf("<script>parent.CommonFile.callback(%s);</script>", json_encode($returnObj));
  42. exit();
  43. }
  44. throw new \think\Exception("上传附件失败");
  45. } catch (\think\exception $e) {
  46. $returnObj->msg = $e->getMessage();
  47. $returnObj->code = 500;
  48. echo sprintf("<script>parent.CommonFile.callback(%s);</script>", json_encode($returnObj));
  49. exit();
  50. }
  51. }
  52. public function delete() {
  53. $id = $this->request->param("id");
  54. $file = CommonFileApi::getOne($id);
  55. if ($file && CommonFileApi::delete($id)) {
  56. $url = "storage/" . $file["url"];
  57. //@unlink($url);
  58. return json(["code" => 200, "msg" => "删除成功"]);
  59. }
  60. return json(["msg" => "找不到对应附件"]);
  61. }
  62. }