Common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace app\controller\admin;
  3. use app\service\admin\admin\UserServiceFacade;
  4. use app\service\ConfServiceFacade;
  5. use laytp\library\UploadDomain;
  6. use plugin\ali_oss\service\Oss;
  7. use plugin\qiniu_kodo\service\Kodo;
  8. use laytp\controller\Backend;
  9. use think\facade\Env;
  10. use think\facade\Filesystem;
  11. use think\File;
  12. class Common extends Backend
  13. {
  14. protected $noNeedAuth = ['*'];
  15. protected $noNeedLogin = ['getLoginNeedCaptchaConf'];
  16. /**
  17. * 获取登录后台是否需要验证码的配置
  18. * 这个接口是后台登录界面使用的,给这个接口独立的访问权限,无需登录,无需鉴权
  19. */
  20. public function getLoginNeedCaptchaConf()
  21. {
  22. return $this->success('获取成功', ConfServiceFacade::get('system.basic.loginNeedCaptcha', 0));
  23. }
  24. //上传接口
  25. public function upload()
  26. {
  27. global $_W;
  28. try {
  29. $defaultType = ConfServiceFacade::get('system.upload.defaultType', 'local');
  30. $uploadType = $this->request->param('upload_type', 'default');
  31. $isGetImageInfo = $this->request->param('is_get_image_info', 0);
  32. if ($uploadType == 'default') $uploadType = $defaultType;
  33. if (!in_array($uploadType, ['local', 'ali-oss', 'qiniu-kodo'])) {
  34. return $this->error($uploadType . '上传方式未定义');
  35. }
  36. $file = $this->request->file('laytpUploadFile'); // 获取上传的文件
  37. if (!$file) {
  38. return $this->error('上传失败,请选择需要上传的文件');
  39. }
  40. $fileExt = strtolower($file->getOriginalExtension());
  41. $uploadDomain = new UploadDomain();
  42. if (!$uploadDomain->check($file->getOriginalName(), $file->getSize(), $fileExt, $file->getMime())) {
  43. return $this->error($uploadDomain->getError());
  44. }
  45. $saveName = date("Ymd") . "/" . md5(uniqid(mt_rand())) . ".{$fileExt}";
  46. /**
  47. * 不能以斜杆开头
  48. * - 因为OSS存储时,不允许以/开头
  49. */
  50. $uploadDir = $this->request->param('dir');
  51. print_r($uploadDir);
  52. $object = $uploadDir ? $uploadDir . '/' . $saveName : $saveName;//设置了上传目录的上传文件名
  53. $filePath = $object; //保存到lt_files中的path
  54. //如果上传的是图片,验证图片的宽和高
  55. $accept = $this->request->param('accept');
  56. if ($accept == "image") {
  57. $width = $this->request->param('width');
  58. $height = $this->request->param('height');
  59. if ($width || $height) {
  60. $imageInfo = getimagesize($file->getFileInfo());
  61. if (($width && $imageInfo[0] > $width) || ($height && $imageInfo[1] > $height)) {
  62. return $this->error('上传失败,图片尺寸要求宽:' . $width . 'px,高:' . $height . 'px,实际上传文件[ ' . $file->getOriginalName() . ' ]的尺寸为宽' . $imageInfo[0] . 'px,高:' . $imageInfo[1] . 'px');
  63. }
  64. }
  65. }
  66. $inputValue = "";
  67. //上传至七牛云
  68. if ($uploadType == 'qiniu-kodo') {
  69. if (ConfServiceFacade::get('plugin.qiniu_kodo.switch') != 1) {
  70. return $this->error('未开启七牛云KODO存储,请到七牛云KODO配置中开启,如果未安装七牛云KODO存储插件,请先到插件市场进行安装');
  71. }
  72. $kodoConf = [
  73. 'accessKey' => ConfServiceFacade::get('plugin.qiniu_kodo.accessKeyID'),
  74. 'secretKey' => ConfServiceFacade::get('plugin.qiniu_kodo.secretKey'),
  75. 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'),
  76. 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'),
  77. ];
  78. $kodo = Kodo::instance();
  79. $kodoRes = $kodo->upload($file->getPathname(), $object, $kodoConf);
  80. if ($kodoRes) {
  81. $inputValue = $kodoRes;
  82. } else {
  83. return $this->error($kodo->getError());
  84. }
  85. }
  86. //上传至阿里云
  87. if ($uploadType == 'ali-oss') {
  88. if (ConfServiceFacade::get('plugin.ali_oss.switch') != 1) {
  89. return $this->error('未开启阿里云OSS存储,请到阿里云OSS配置中开启');
  90. }
  91. $ossConf = [
  92. 'accessKeyID' => ConfServiceFacade::get('plugin.ali_oss.accessKeyID'),
  93. 'accessKeySecret' => ConfServiceFacade::get('plugin.ali_oss.accessKeySecret'),
  94. 'bucket' => ConfServiceFacade::get('plugin.ali_oss.bucket'),
  95. 'endpoint' => ConfServiceFacade::get('plugin.ali_oss.endpoint'),
  96. 'domain' => ConfServiceFacade::get('plugin.ali_oss.domain'),
  97. ];
  98. $oss = Oss::instance();
  99. $ossUploadRes = $oss->upload($file->getPathname(), $object, $ossConf);
  100. if ($ossUploadRes) {
  101. $inputValue = $ossUploadRes;
  102. } else {
  103. return $this->error($oss->getError());
  104. }
  105. }
  106. //本地上传
  107. if ($uploadType == 'local') {
  108. $uploadDir = ltrim('/', $uploadDir);
  109. $saveName = Filesystem::putFileAs('/' . $uploadDir, $file, '/' . $object);
  110. $filePath = $saveName;
  111. $staticDomain = Env::get('domain.static');
  112. if ($accept == "client"){
  113. // print_r($accept);
  114. $inputValue = request()->domain() . STATIC_PATH . '/admin/client/' . $saveName;
  115. } else {
  116. $inputValue = request()->domain() . STATIC_PATH . '/storage/' . $saveName;
  117. }
  118. }
  119. //将inputValue存入lt_files表中
  120. $filesModel = new \app\model\Files();
  121. $fileId = $filesModel->insertGetId([
  122. 'category_id' => 0,
  123. 'name' => $accept == "client"?$saveName:$file->getOriginalName(),
  124. 'file_type' => $this->request->param('accept'),
  125. 'path' => $filePath,
  126. 'upload_type' => $uploadType,
  127. 'size' => $file->getSize(),
  128. 'ext' => $file->getExtension(),
  129. 'create_admin_user_id' => UserServiceFacade::getUser()->id,
  130. 'update_admin_user_id' => UserServiceFacade::getUser()->id,
  131. 'create_time' => date('Y-m-d H:i:s'),
  132. 'update_time' => date('Y-m-d H:i:s'),
  133. 'uniacid' => $_W['uniacid']
  134. ]);
  135. $returnData = [
  136. 'id' => $fileId,
  137. 'path' => $inputValue,
  138. 'name' => $file->getOriginalName(),
  139. ];
  140. if($isGetImageInfo){
  141. $url = str_replace("https://", "http://", $inputValue);
  142. $url = trim($url);
  143. list($width, $height, $type, $attr) = getimagesize($url);
  144. if(!empty($width)){
  145. $returnData['width'] = $width;
  146. $returnData['height'] = $height;
  147. }
  148. }
  149. return $this->success('上传成功', $returnData);
  150. } catch (\Exception $e) {
  151. return $this->exceptionError($e);
  152. }
  153. }
  154. // 获取阿里云sts的临时凭证,目前仅用于客户端直接上传到oss前获取到临时凭证进行上传
  155. public function aliSts()
  156. {
  157. if (ConfServiceFacade::get('plugin.ali_oss_sts.switch') != 1) {
  158. return $this->error('阿里云OSS存储的STS方式,请到阿里云STS配置中开启');
  159. }
  160. $uploadDomain = new UploadDomain();
  161. $fileName = $this->request->param('name');
  162. $fileSize = $this->request->param('size');
  163. $fileExt = $this->request->param('ext');
  164. if (!$uploadDomain->check($fileName, $fileSize, $fileExt, '')) {
  165. return $this->error($uploadDomain->getError());
  166. }
  167. $stsConf = [
  168. 'accessKeyID' => ConfServiceFacade::get('plugin.ali_oss_sts.accessKeyID'),
  169. 'accessKeySecret' => ConfServiceFacade::get('plugin.ali_oss_sts.accessKeySecret'),
  170. 'ARN' => ConfServiceFacade::get('plugin.ali_oss_sts.ARN'),
  171. 'endpoint' => ConfServiceFacade::get('plugin.ali_oss_sts.endpoint'),
  172. 'domain' => ConfServiceFacade::get('plugin.ali_oss_sts.domain'),
  173. 'bucket' => ConfServiceFacade::get('plugin.ali_oss_sts.bucket'),
  174. ];
  175. $oss = Oss::instance();
  176. $sts = $oss->sts($stsConf);
  177. $file = new File('', false);
  178. if ($sts) {
  179. return $this->success('sts获取成功', [
  180. 'sts' => $sts,
  181. 'path' => str_replace('\\', '/', $file->hashName()) . $fileExt,
  182. 'index' => $this->request->param('index'),
  183. ]);
  184. } else {
  185. return $this->error('sts获取失败,' . $oss->getError());
  186. }
  187. }
  188. // 获取kodo上传凭证token
  189. public function kodoToken()
  190. {
  191. if (ConfServiceFacade::get('plugin.qiniu_kodo.switch') != 1) {
  192. return $this->error('未开启七牛云KODO存储,请到七牛云KODO配置中开启');
  193. }
  194. $uploadDomain = new UploadDomain();
  195. $fileName = $this->request->param('name');
  196. $fileSize = $this->request->param('size');
  197. $fileExt = $this->request->param('ext');
  198. if (!$uploadDomain->check($fileName, $fileSize, $fileExt, '')) {
  199. return $this->error($uploadDomain->getError());
  200. }
  201. $kodoConf = [
  202. 'accessKey' => ConfServiceFacade::get('plugin.qiniu_kodo.accessKey'),
  203. 'secretKey' => ConfServiceFacade::get('plugin.qiniu_kodo.secretKey'),
  204. 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'),
  205. 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'),
  206. ];
  207. $kodo = Kodo::instance();
  208. $token = $kodo->token($kodoConf);
  209. $file = new File('', false);
  210. if ($token) {
  211. return $this->success('KodoToken获取成功', [
  212. 'token' => $token,
  213. 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'),
  214. 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'),
  215. 'path' => str_replace('\\', '/', $file->hashName()) . $fileExt,
  216. 'index' => $this->request->param('index'),
  217. ]);
  218. } else {
  219. return $this->error('KodoToken获取失败,' . $kodo->getError());
  220. }
  221. }
  222. //解锁屏幕
  223. function unLockScreen()
  224. {
  225. $password = $this->request->post('password');
  226. $userId = UserServiceFacade::getUser()->id;
  227. $passwordHash = User::where('id', '=', $userId)->value('password');
  228. if (!password_verify(md5($password), $passwordHash)) {
  229. return $this->error('解锁失败,密码错误');
  230. } else {
  231. return $this->success('解锁成功');
  232. }
  233. }
  234. }