多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
``` 安装sdk composer require qiniu/php-sdk ``` ``` <?php namespace lib; use think\Db; use Qiniu\Auth; use Qiniu\Storage\BucketManager; use Qiniu\Storage\UploadManager; class qiniu{ private $auth; public function __construct(){ $accessKey = 'lUyTn0p6tRiFBShEMHlHWSrEP45zlNQrKfLwFYSs'; $secretKey = 'R9LfJXAvQIUPNfTQWhhdanhqSfVi9jonsuCO0Q7q'; $this->auth = new Auth($accessKey,$secretKey); } //获取空间token public function gettoken($bucket){ $token = $this->auth->uploadToken($bucket,null,3600); return $token; } //获取操作七牛资源对象 public function getBucketManager(){ return new BucketManager($this->auth); } //获取上传七牛资源对象 public function getUploadManager(){ return new UploadManager(); } } ``` 前端上传--jquery代码: ``` <script type="text/javascript"> var app={}; var tokens={}; $(document).ready(function(){ app=parent.app; $('#app_name').val(app.app_name); $('#app_img').attr('src',app.app_logo); $('#version').val(app.version); $('#size').val(app.size); $('#bid').val(app.bid); $('#app_id').val(app.app_id); //app.package--file get_token(); }) //获取token function get_token(){ $.ajax({ type: "POST", url: "{:url('app/token')}", dataType: "json", success: function(data) { tokens=data; } }); } //表单提交 function submit() { $.ajax({ type: "POST", url: "{:url('app/app_adds')}", data: $("#form-app-add").serialize(), dataType: "json", success: function(data){ alert(data.message); parent.close(); //调用父页面方法 } }); } //base64上传 function base64(){ var pic = app.app_logo.replace(/^data[\S]+;base64,/, ''); var url = "http://upload-z2.qiniup.com/putb64/-1"; //非华东空间需要根据注意事项 1 修改上传域名 var xhr = new XMLHttpRequest(); xhr.onreadystatechange=function(){ if (xhr.readyState==4){ logo = JSON.parse(xhr.responseText).key; $("#app_logo").val(logo); upload(); } } xhr.open("POST", url, true); xhr.setRequestHeader("Content-Type", "application/octet-stream"); xhr.setRequestHeader("Authorization","UpToken " +tokens.logo_token); xhr.send(pic); } 文件上传 function upload(){ var observer = { next(res) { layui.use('element', function(){ var element = layui.element; element.progress('app_progress',res.total.percent+"%"); }); }, error(err) { console.log(err); }, complete(res) { $("#app_file").val(res.key); submit(); } }; var putExtra = { fname: "", params: {}, mimeType: null }; var config = { useCdnDomain: true, region: 'z2', concurrentRequestLimit: 3 }; var observable = qiniu.upload(app.package,app.app_name,tokens.app_token,putExtra,config); observable.subscribe(observer); } ```