123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace app\common\controller;
- use app\BaseController;
- use app\common\middleware\Auth;
- use think\facade\Db;
- use think\facade\Config;
- /**
- * 需要权限的公共方法放这
- * Description of Tool
- *
- * @author sgq
- */
- class Api extends BaseController {
- protected $middleware = [Auth::class];
- protected $user;
- public function __construct(\think\App $app) {
- parent::__construct($app);
- $this->user = session("user");
- }
- public function findCommonFileType() {
- $param = $this->request->param();
- $order = $param["order"];
- $project = $param["project"];
- $type = $param["type"];
- $checkState = $param["checkState"];
- if ($checkState == 0) {
- $where[] = ["step", "=", 1];
- }
- $where[] = ["project", "=", $project];
- $where[] = ["type", "=", $type];
- return json(["rows" => Db::table("new_common_filetype")->where($where)->order("sn " . $order)->select()]);
- }
- public function listTalentFile() {
- $param = $this->request->param();
- $mainId = $param["mainId"];
- $typeId = $param["fileTypeId"];
- $where = [["mainId", "=", $mainId], ["typeId", "=", $typeId]];
- $list = Db::table("new_talent_file")->where($where)->select()->toArray();
- foreach ($list as $key => $item) {
- $list[$key]["url"] = "/storage/" . $item["url"]; //获取系统配置无效,暂时这样
- }
- return json($list);
- }
- public function addTalentFile() {
- $backName = $this->request->param("backName");
- $fileId = $this->request->param("fileId");
- $mainId = $this->request->param("mainId");
- $fileTypeId = $this->request->param("fileTypeId");
- $index = $this->request->param("index");
- $type = $this->request->param("type");
- $upload = new \app\common\api\UploadApi();
- $file = $this->request->file("fileUrl");
- $url = $upload->uploadOne($file, "talent_files");
- if ($fileId) {
- if (!$this->chkIsFileOwner($mainId, $type))
- return json(["msg" => "删除失败"]);
- $old = Db::table("new_talent_file")->findOrEmpty($fileId);
- $old_filepath = "storage/" . $old["url"];
- if (file_exists($old_filepath))
- unlink($old_filepath);
- $data["id"] = $fileId;
- }
- $data["mainId"] = $mainId;
- $data["type"] = $type;
- $data["typeId"] = $fileTypeId;
- $data["orignName"] = $file->getOriginalName();
- $data["url"] = $url;
- $data["sn"] = $index;
- $data["createTime"] = time();
- Db::table("new_talent_file")->save($data);
- $res = ["code" => 200, "msg" => "上传附件成功", "obj" => $index, "info" => $url];
- echo sprintf("<script>parent.%s(%s);</script>", $backName, json_encode($res));
- }
- public function deleteFile() {
- $param = $this->request->param();
- $where = [["id", "=", $param["id"]]];
- $file = Db::table("new_talent_file")->where($where)->findOrEmpty();
- if ($this->chkIsFileOwner($file["mainId"], $file["type"])) {
- $filepath = "storage/" . $file["url"];
- if (file_exists($filepath)) {
- unlink($filepath);
- }
- Db::table("new_talent_file")->delete($file["id"]);
- return json(["code" => 200, "msg" => "删除成功"]);
- }
- return json(["msg" => "不能删除"]);
- }
- private function chkIsFileOwner($mainId, $type) {
- switch ($type) {
- case 1:
- if ($this->user["usertype"] == 2) {
- $user_id = $this->user["uid"];
- $talent_info = Db::table("new_talent_info")->findOrEmpty($mainId);
- if ($user_id == $talent_info["enterprise_id"])
- return true;
- }
- break;
- }
- return false;
- }
- }
|