123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace app\controller\admin;
- use app\service\admin\admin\UserServiceFacade;
- use app\service\ConfServiceFacade;
- use laytp\library\UploadDomain;
- use plugin\ali_oss\service\Oss;
- use plugin\qiniu_kodo\service\Kodo;
- use laytp\controller\Backend;
- use think\facade\Env;
- use think\facade\Filesystem;
- use think\File;
- class Common extends Backend
- {
- protected $noNeedAuth = ['*'];
- protected $noNeedLogin = ['getLoginNeedCaptchaConf'];
- /**
- * 获取登录后台是否需要验证码的配置
- * 这个接口是后台登录界面使用的,给这个接口独立的访问权限,无需登录,无需鉴权
- */
- public function getLoginNeedCaptchaConf()
- {
- return $this->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('解锁成功');
- }
- }
- }
|