wang jun 2 years ago
parent
commit
48aabac37a

+ 54 - 0
application/common/server/cosserver.php

@@ -0,0 +1,54 @@
+<?php
+//腾讯云链接用
+namespace app\common\server;
+
+use Qcloud\Cos\Client;
+
+class cosserver
+{
+    public function __construct()
+    {
+        require EXTEND_PATH . '/tencent-php/vendor/autoload.php';
+    }
+    public function getconfig()
+    {
+        $data = [
+            'secretId' => 'AKID2uVrwjGXWEfpjMjqhJejY6ML2TdV6SXL',
+            'secretKey' => 'Yqrb3EH4ZnlSs2xaw1qOffNoyrSnrfCU',
+            'region' => "ap-nanjing",
+        ];
+        return $data;
+    }
+    public function uploadfiles($path, $dir, $filename, $bucket)
+    {
+        $config = $this->getconfig();
+        //$dirname = "workcard/" . $dir;
+        $key = $dir . '/' . $filename;
+        $cosClient = new Client(
+            array(
+                'region' => $config['region'],
+                'schema' => 'https', //协议头部,默认为http
+                'credentials' => array(
+                    'secretId' => $config['secretId'],
+                    'secretKey' => $config['secretKey'],
+                ),
+            )
+        );
+        //key可以设置路径
+        try {
+            $result = $cosClient->putObject(array(
+                //'Bucket' => 'backup-1306866048', //格式:BucketName-APPID
+                'Bucket' => $bucket,
+                'Key' => $key,
+                'Body' => fopen($path, 'rb'),
+                //'Body' => $base64,
+            ));
+            // 请求成功
+            $path = $result['Location'];
+            return backarr(1, 'success', ['path' => 'http://' . $path]);
+        } catch (\Exception $e) {
+            // 请求失败
+            return backarr(0, $e->getMessage());
+        }
+    }
+}

+ 30 - 0
application/index/controller/Fileoper.php

@@ -9,6 +9,7 @@
 
 namespace app\index\controller;
 
+use app\common\server\cosserver;
 use think\Controller;
 use think\facade\Log;
 use think\Request;
@@ -107,4 +108,33 @@ class Fileoper extends Controller
             $this->error($file->getError());
         }
     }
+
+    public function savephoto()
+    {
+        Log::info($_FILES);
+        $file = request()->file('file');
+        // 移动到框架应用根目录/uploads/ 目录下
+        $info = $file->move('Uploads/cosfile');
+        if (!$info) {
+            return backjson2(0, $file->getError());
+        }
+        $path = $info->getPathName();
+        $filename = $info->getFilename();
+        $houzhui = explode('.', $filename);
+        $ext = end($houzhui);
+        $allowtype = ['png', 'jpg', 'jpeg'];
+        if (!in_array($ext, $allowtype)) {
+            return backjson2(0, "格式错误");
+        }
+        $dir = "workercard/" . date('Ymd') . "/";
+        $filename = uniqid() . '.' . $ext;
+        $bucket = "backup-1306866048";
+        $s_cos = new cosserver();
+        $result = $s_cos->uploadfiles($path, $dir, $filename, $bucket);
+        if (1 != $result['status']) {
+            return backjson2(0, $result['msg']);
+        }
+        unlink($path);
+        return backjson2(1, "操作成功", $result['data']);
+    }
 }

+ 1 - 0
public/index.php

@@ -15,6 +15,7 @@ namespace think;
 define('DS', DIRECTORY_SEPARATOR);
 defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . DS);
 defined('ROOT_PATH') or define('ROOT_PATH', dirname(realpath(APP_PATH)) . DS);
+defined('EXTEND_PATH') or define('EXTEND_PATH', __DIR__ . '/../extend/');
 // 加载基础文件
 require __DIR__ . '/../thinkphp/base.php';