success('获取成功', ConfServiceFacade::get('system.basic.loginNeedCaptcha', 0)); } //上传接口 public function upload() { global $_W; try { $defaultType = ConfServiceFacade::get('system.upload.defaultType', 'local'); $uploadType = $this->request->param('upload_type', 'default'); $isGetImageInfo = $this->request->param('is_get_image_info', 0); if ($uploadType == 'default') $uploadType = $defaultType; if (!in_array($uploadType, ['local', 'ali-oss', 'qiniu-kodo'])) { return $this->error($uploadType . '上传方式未定义'); } $file = $this->request->file('laytpUploadFile'); // 获取上传的文件 if (!$file) { return $this->error('上传失败,请选择需要上传的文件'); } $fileExt = strtolower($file->getOriginalExtension()); $uploadDomain = new UploadDomain(); if (!$uploadDomain->check($file->getOriginalName(), $file->getSize(), $fileExt, $file->getMime())) { return $this->error($uploadDomain->getError()); } $saveName = date("Ymd") . "/" . md5(uniqid(mt_rand())) . ".{$fileExt}"; /** * 不能以斜杆开头 * - 因为OSS存储时,不允许以/开头 */ $uploadDir = $this->request->param('dir'); print_r($uploadDir); $object = $uploadDir ? $uploadDir . '/' . $saveName : $saveName;//设置了上传目录的上传文件名 $filePath = $object; //保存到lt_files中的path //如果上传的是图片,验证图片的宽和高 $accept = $this->request->param('accept'); if ($accept == "image") { $width = $this->request->param('width'); $height = $this->request->param('height'); if ($width || $height) { $imageInfo = getimagesize($file->getFileInfo()); if (($width && $imageInfo[0] > $width) || ($height && $imageInfo[1] > $height)) { return $this->error('上传失败,图片尺寸要求宽:' . $width . 'px,高:' . $height . 'px,实际上传文件[ ' . $file->getOriginalName() . ' ]的尺寸为宽' . $imageInfo[0] . 'px,高:' . $imageInfo[1] . 'px'); } } } $inputValue = ""; //上传至七牛云 if ($uploadType == 'qiniu-kodo') { if (ConfServiceFacade::get('plugin.qiniu_kodo.switch') != 1) { return $this->error('未开启七牛云KODO存储,请到七牛云KODO配置中开启,如果未安装七牛云KODO存储插件,请先到插件市场进行安装'); } $kodoConf = [ 'accessKey' => ConfServiceFacade::get('plugin.qiniu_kodo.accessKeyID'), 'secretKey' => ConfServiceFacade::get('plugin.qiniu_kodo.secretKey'), 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'), 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'), ]; $kodo = Kodo::instance(); $kodoRes = $kodo->upload($file->getPathname(), $object, $kodoConf); if ($kodoRes) { $inputValue = $kodoRes; } else { return $this->error($kodo->getError()); } } //上传至阿里云 if ($uploadType == 'ali-oss') { if (ConfServiceFacade::get('plugin.ali_oss.switch') != 1) { return $this->error('未开启阿里云OSS存储,请到阿里云OSS配置中开启'); } $ossConf = [ 'accessKeyID' => ConfServiceFacade::get('plugin.ali_oss.accessKeyID'), 'accessKeySecret' => ConfServiceFacade::get('plugin.ali_oss.accessKeySecret'), 'bucket' => ConfServiceFacade::get('plugin.ali_oss.bucket'), 'endpoint' => ConfServiceFacade::get('plugin.ali_oss.endpoint'), 'domain' => ConfServiceFacade::get('plugin.ali_oss.domain'), ]; $oss = Oss::instance(); $ossUploadRes = $oss->upload($file->getPathname(), $object, $ossConf); if ($ossUploadRes) { $inputValue = $ossUploadRes; } else { return $this->error($oss->getError()); } } //本地上传 if ($uploadType == 'local') { $uploadDir = ltrim('/', $uploadDir); $saveName = Filesystem::putFileAs('/' . $uploadDir, $file, '/' . $object); $filePath = $saveName; $staticDomain = Env::get('domain.static'); if ($accept == "client"){ // print_r($accept); $inputValue = request()->domain() . STATIC_PATH . '/admin/client/' . $saveName; } else { $inputValue = request()->domain() . STATIC_PATH . '/storage/' . $saveName; } } //将inputValue存入lt_files表中 $filesModel = new \app\model\Files(); $fileId = $filesModel->insertGetId([ 'category_id' => 0, 'name' => $accept == "client"?$saveName:$file->getOriginalName(), 'file_type' => $this->request->param('accept'), 'path' => $filePath, 'upload_type' => $uploadType, 'size' => $file->getSize(), 'ext' => $file->getExtension(), 'create_admin_user_id' => UserServiceFacade::getUser()->id, 'update_admin_user_id' => UserServiceFacade::getUser()->id, 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s'), 'uniacid' => $_W['uniacid'] ]); $returnData = [ 'id' => $fileId, 'path' => $inputValue, 'name' => $file->getOriginalName(), ]; if($isGetImageInfo){ $url = str_replace("https://", "http://", $inputValue); $url = trim($url); list($width, $height, $type, $attr) = getimagesize($url); if(!empty($width)){ $returnData['width'] = $width; $returnData['height'] = $height; } } return $this->success('上传成功', $returnData); } catch (\Exception $e) { return $this->exceptionError($e); } } // 获取阿里云sts的临时凭证,目前仅用于客户端直接上传到oss前获取到临时凭证进行上传 public function aliSts() { if (ConfServiceFacade::get('plugin.ali_oss_sts.switch') != 1) { return $this->error('阿里云OSS存储的STS方式,请到阿里云STS配置中开启'); } $uploadDomain = new UploadDomain(); $fileName = $this->request->param('name'); $fileSize = $this->request->param('size'); $fileExt = $this->request->param('ext'); if (!$uploadDomain->check($fileName, $fileSize, $fileExt, '')) { return $this->error($uploadDomain->getError()); } $stsConf = [ 'accessKeyID' => ConfServiceFacade::get('plugin.ali_oss_sts.accessKeyID'), 'accessKeySecret' => ConfServiceFacade::get('plugin.ali_oss_sts.accessKeySecret'), 'ARN' => ConfServiceFacade::get('plugin.ali_oss_sts.ARN'), 'endpoint' => ConfServiceFacade::get('plugin.ali_oss_sts.endpoint'), 'domain' => ConfServiceFacade::get('plugin.ali_oss_sts.domain'), 'bucket' => ConfServiceFacade::get('plugin.ali_oss_sts.bucket'), ]; $oss = Oss::instance(); $sts = $oss->sts($stsConf); $file = new File('', false); if ($sts) { return $this->success('sts获取成功', [ 'sts' => $sts, 'path' => str_replace('\\', '/', $file->hashName()) . $fileExt, 'index' => $this->request->param('index'), ]); } else { return $this->error('sts获取失败,' . $oss->getError()); } } // 获取kodo上传凭证token public function kodoToken() { if (ConfServiceFacade::get('plugin.qiniu_kodo.switch') != 1) { return $this->error('未开启七牛云KODO存储,请到七牛云KODO配置中开启'); } $uploadDomain = new UploadDomain(); $fileName = $this->request->param('name'); $fileSize = $this->request->param('size'); $fileExt = $this->request->param('ext'); if (!$uploadDomain->check($fileName, $fileSize, $fileExt, '')) { return $this->error($uploadDomain->getError()); } $kodoConf = [ 'accessKey' => ConfServiceFacade::get('plugin.qiniu_kodo.accessKey'), 'secretKey' => ConfServiceFacade::get('plugin.qiniu_kodo.secretKey'), 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'), 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'), ]; $kodo = Kodo::instance(); $token = $kodo->token($kodoConf); $file = new File('', false); if ($token) { return $this->success('KodoToken获取成功', [ 'token' => $token, 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'), 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'), 'path' => str_replace('\\', '/', $file->hashName()) . $fileExt, 'index' => $this->request->param('index'), ]); } else { return $this->error('KodoToken获取失败,' . $kodo->getError()); } } //解锁屏幕 function unLockScreen() { $password = $this->request->post('password'); $userId = UserServiceFacade::getUser()->id; $passwordHash = User::where('id', '=', $userId)->value('password'); if (!password_verify(md5($password), $passwordHash)) { return $this->error('解锁失败,密码错误'); } else { return $this->success('解锁成功'); } } }