作者 杨育虎

小程序上传图片

@@ -7,11 +7,30 @@ @@ -7,11 +7,30 @@
7 namespace api\user\controller; 7 namespace api\user\controller;
8 8
9 use cmf\controller\RestUserBaseController; 9 use cmf\controller\RestUserBaseController;
  10 +use Qiniu\Auth;
  11 +use Qiniu\Storage\UploadManager;
10 use think\Db; 12 use think\Db;
11 13
  14 +/**
  15 + * @title 文件上传
  16 + * @description 文件上传
  17 + * @package api\wxapp\controller
  18 + */
12 class UploadController extends RestUserBaseController 19 class UploadController extends RestUserBaseController
13 { 20 {
14 - // 用户密码修改 21 +
  22 + /**
  23 + * @title 上传单个文件
  24 + * @description 上传单个文件
  25 + * @author Tiger Yang
  26 + * @url /user/upload/one
  27 + * @method POST
  28 + *
  29 + * @header name:XX-Token require:1 default: desc:登录标识
  30 + * @header name:XX-Device-Type require:0 default:wxapp desc:设备类型
  31 + *
  32 + * @param name:file type:file require:1 other: desc:上传文件
  33 + */
15 public function one() 34 public function one()
16 { 35 {
17 $file = $this->request->file('file'); 36 $file = $this->request->file('file');
@@ -26,7 +45,7 @@ class UploadController extends RestUserBaseController @@ -26,7 +45,7 @@ class UploadController extends RestUserBaseController
26 $findFile = Db::name("asset")->where('file_md5', $fileMd5)->where('file_sha1', $fileSha1)->find(); 45 $findFile = Db::name("asset")->where('file_md5', $fileMd5)->where('file_sha1', $fileSha1)->find();
27 46
28 if (!empty($findFile)) { 47 if (!empty($findFile)) {
29 - $this->success("上传成功!", ['url' => $findFile['file_path'], 'filename' => $findFile['filename']]); 48 + $this->success("上传成功!", ['url' => cmf_get_asset_url($findFile['file_path']), 'filename' => $findFile['filename']]);
30 } 49 }
31 $info = $info->move(ROOT_PATH . 'public' . DS . 'upload'); 50 $info = $info->move(ROOT_PATH . 'public' . DS . 'upload');
32 if ($info) { 51 if ($info) {
@@ -43,14 +62,20 @@ class UploadController extends RestUserBaseController @@ -43,14 +62,20 @@ class UploadController extends RestUserBaseController
43 'file_key' => $fileKey, 62 'file_key' => $fileKey,
44 'filename' => $originalName, 63 'filename' => $originalName,
45 'file_size' => $fileSize, 64 'file_size' => $fileSize,
46 - 'file_path' => $saveName, 65 + 'file_path' => cmf_get_asset_url($saveName),
47 'file_md5' => $fileMd5, 66 'file_md5' => $fileMd5,
48 'file_sha1' => $fileSha1, 67 'file_sha1' => $fileSha1,
49 'create_time' => time(), 68 'create_time' => time(),
50 'suffix' => $suffix 69 'suffix' => $suffix
51 ]); 70 ]);
52 71
53 - $this->success("上传成功!", ['url' => $saveName, 'filename' => $originalName]); 72 + $storage = cmf_get_option('storage');
  73 +
  74 + if (isset($storage['type'])&&$storage['type']=='Qiniu') {
  75 + $this->uploadToQiniu($saveName);
  76 + }
  77 +
  78 + $this->success("上传成功!", ['url' => cmf_get_asset_url($saveName), 'filename' => $originalName]);
54 } else { 79 } else {
55 // 上传失败获取错误信息 80 // 上传失败获取错误信息
56 $this->error($file->getError()); 81 $this->error($file->getError());
@@ -59,4 +84,45 @@ class UploadController extends RestUserBaseController @@ -59,4 +84,45 @@ class UploadController extends RestUserBaseController
59 } 84 }
60 85
61 86
  87 + /**
  88 + * 上传到七牛云
  89 + * @param $save_name
  90 + * @param bool $is_del_local
  91 + * @return array
  92 + * @throws \think\db\exception\DataNotFoundException
  93 + * @throws \think\db\exception\ModelNotFoundException
  94 + * @throws \think\exception\DbException
  95 + */
  96 + public function uploadToQiniu($save_name,$is_del_local=true){
  97 + $plugin=Db::name('plugin')->field('config')->where(['name'=>'Qiniu'])->find();
  98 + $config=json_decode($plugin['config'],true);
  99 +
  100 + $filePath = './../upload/'.$save_name;
  101 + // 上传到七牛后保存的文件名
  102 + $key =$save_name;
  103 + require_once VENDOR_PATH . 'qiniu/php-sdk/autoload.php';
  104 + // 需要填写你的 Access Key 和 Secret Key
  105 + $accessKey = $config['accessKey'];
  106 + $secretKey = $config['secretKey'];
  107 + // 构建鉴权对象
  108 + $auth = new Auth($accessKey,$secretKey);
  109 + // 要上传的空间
  110 + $bucket = $config['bucket'];
  111 +
  112 + $token = $auth->uploadToken($bucket);
  113 + // 初始化 UploadManager 对象并进行文件的上传
  114 + $uploadMgr = new UploadManager();
  115 + // 调用 UploadManager 的 putFile 方法进行文件的上传
  116 + list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
  117 + if($is_del_local){
  118 + unlink($filePath);
  119 + }
  120 + if ($err !== null) {
  121 + return ["err"=>1,"msg"=>$err,"data"=>""];
  122 + } else {
  123 + return ["err"=>0,"msg"=>"上传完成","data"=>cmf_get_image_url($ret['key'])];
  124 + }
  125 + }
  126 +
  127 +
62 } 128 }