背景
项目的实际需求中既要用到阿里云oss产品又用到阿里云点播系统,实现完美的统一。设置两个地址downUrl,thirdCode。分别代表阿里云oss上传路径和阿里云点播系统vId。
实现
默认框架你已经集成好阿里云oss集成工作,前端html页面实现
<form id="edit-form" class="form-horizontal form-ajax" role="form" data-toggle="validator" method="POST" action=""><input name="row[id]" type="hidden" value="{$row.id}" ><div class="form-group" style="display: none"><label for="c-title" class="control-label col-xs-12 col-sm-2">{:__('作者')}:</label><div class="col-xs-12 col-sm-8"><input id="c-title" data-rule="" class="form-control" name="row[author]" type="text" value="{$row.author|htmlentities}" placeholder="请输作者"></div></div><div class="form-group"><label class="control-label col-xs-12 col-sm-2">{:__('音视频')}:</label><div class="col-xs-12 col-sm-8"><div class="input-group"><input id="c-downurl" data-rule="" class="form-control" size="255" name="row[downurl]" type="text" value="{$row.downurl}"><div class="input-group-addon no-border no-padding"><span><button type="button" id="plupload-downurl" class="btn btn-danger plupload" data-input-id="c-downurl" data-mimetype="*" data-multiple="false" data-preview-id="p-downurl"><i class="fa fa-upload"></i> {:__('Upload')}</button></span><span><button type="button" id="fachoose-downurl" class="btn btn-primary fachoose" data-input-id="c-downurl" data-mimetype="*" data-multiple="false"><i class="fa fa-list"></i> {:__('Choose')}</button></span></div><span class="msg-box n-right" for="c-downurl"></span></div><ul class="row list-inline plupload-preview" id="p-downurl"></ul></div></div><div class="form-group layer-footer"><label class="control-label col-xs-12 col-sm-2"></label><div class="col-xs-12 col-sm-8"><button type="submit" class="btn btn-success btn-embossed" id="submit">{:__('OK')}</button><button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button></div></div>
</form>
table.bootstrapTable({url: $.fn.bootstrapTable.defaults.extend.index_url,showToggle: false,showColumns: false,showExport: false,//commonSearch: false,search:false,fixedColumns: true,fixedRightNumber: 1,columns: [[{checkbox: true},{field: 'id', title: __('序号'),operate:false},{field: 'category.name', title: __('格式'),operate:false,formatter:Table.api.formatter.label},{field: 'categoryId', title: __('格式'),visible: false,searchList: $.getJSON("ajax/filecate")},{field: 'title',title: __('标题'),operate:'LIKE',cellStyle: {css: {"max-width": "500px","white-space":"nowrap","overflow":"hidden","text-overflow":"ellipsis"}}},{field: 'secondId', title: __('分类'), visible: false,searchList: $.getJSON("ajax/schooldata")},{field: 'thumb', title: __('缩略图'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},{field: 'third_code', title: __('三方平台'),operate: false},{field: 'createtime', title: __('提交时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true}, {field: 'updatetime', title: __('更新时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true}, {field: 'status', title: __('Status'), formatter: Controller.api.formatter.station, searchList: {0: __('草稿'), 1: __('发布')}},{field: 'operate', title: __('Operate'), buttons: [{name: '视频转码',text: '',title: __('转码'),classname: 'btn btn-xs btn-success btn-view btn-dialog',icon: 'fa fa-trademark',url: 'school/data/transfer?id={ids}',refresh:true ,visible: function (row) {//返回true时按钮显示,返回false隐藏if(row.categoryId===232){if(row.third_plat==0||!row.third_plat) return true;}return false}},{name: 'detail',text: '', title: '编辑',icon: 'fa fa-pencil',classname: 'btn btn-xs btn-info btn-addtabs',url: 'school/data/edit'}], table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}]]});
使用ajax异常实现上传
public function uploadV(){Config::set('default_return_type', 'json');$attachment = null;$res="";//默认普通上传文件$file = $this->request->post('url');//print_r($file->getMime());//print_r($file->getFilename());//print_r($file->getRealPath());require_once APP_PATH. DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';try {//$upload = new Upload($file);//print_r($upload->getFile());//$attachment = $upload->upload();$apiRegionId = 'cn-beijing';$uploader = new \AliyunVodUploader(Config::get('aliVod.accessKeyId'), Config::get('aliVod.accessKeySecret'), $apiRegionId);$fileURL="http://xxxxxx".$file;$uploadVideoRequest = new \UploadVideoRequest($fileURL, "课程");$res = $uploader->uploadWebVideo($uploadVideoRequest);if ($res) { // 根据你的逻辑,可能需要检查 $res 是否表示成功return json(['status' => 'success', 'data' => $res]);} else {// 处理 $res 为空或失败的情况return json(['status' => 'error', 'message' => '上传失败,未获取到有效响应']);}} catch (\Exception $e) {return json(['status' => 'error', 'message' => $e->getMessage()]);}}
可以完美完成。本功能实现大概用了一周,遇到很多问题,包括sdk引入层级问题,ajax异步格式返回等待