服务端获取签名
客服端获取签名 直传 到 阿里云oss
vue + axios + aliyunOSS + php
主要代码
data(){
return:{
status:[],
}
}
async upaliyunOss(ID, filePath,index) {
const url = "服务器返回签名接口地址"
let ossSign = ""
let timeStamp = Date.parse(new Date()) + "YC" + this.RandomCode()
this.RandomCode
await this.$axios.get(url + ID).then(function (res) {
ossSign = res.data
}).catch(err => {
console.log(err);
})
console.log(ossSign);
const host = ossSign.host;
const signature = ossSign.signature;
const ossAccessKeyId = ossSign.accessid;
const policy = ossSign.policy;
const callback = ossSign.callback;
const key = ossSign.dir + "Le" + timeStamp + ".jpg";
let fromData = new FormData();
fromData.append("name", 'file')
fromData.append("key", key)
fromData.append("policy", policy)
fromData.append("OSSAccessKeyId", ossAccessKeyId)
fromData.append("success_action_status", "200")
fromData.append("callback", callback)
fromData.append("signature", signature)
fromData.append("file", filePath)
await this.$axios.post(host, fromData).then(res => {
console.log(res);
this.status[index] = 200
}).catch(err => {
console.log(err);
this.status[index] = false
})
},
async onsubmit() {
if (this.empty(this.data)) {
if (this.inputVerificationCode === this.verificationCode && this.verificationCode != "") {
console.log("验证通过")
this.isToastShow = true
this.isShowDialog = true
this.toastPicUrl = "https://****.oss-cn-chengdu.aliyuncs.com/ossStatic/staff-end-static/dialog.png"
this.toastTitle = "提交资料中..."
let index = 0
for (const item of this.filePath) {
await this.upaliyunOss(1, item,index)
index ++
}
this.success()
} else {
console.log("验证码错误")
}
} else {
console.log("请完善信息");
}
},
success(){
let statu = false
for (let item of this.status) {
console.log(statu)
if(item == 200){
statu = true
console.log(statu)
}else {
statu = false
console.log(statu)
break
}
}
console.log(this.status)
console.log(statu)
this.isToastShow = true
if(statu){
this.isShowDialog = false
this.toastPicUrl = "https://****.oss-cn-chengdu.aliyuncs.com/ossStatic/staff-end-static/success.png"
this.toastTitle = "提交成功"
setTimeout(()=>{
window.location.href = "SuccessfulRegistration.html"
},2000)
}
},
php主要代码。PHP7.2.9
<?php
namespace app\controller\wx;
use app\traits\ControllerTrait;
use think\annotation\Route;
use think\annotation\route\Group;
use think\App;
use think\facade\Env;
class Oss extends ControllerTrait {
protected $id;
protected $key;
public function __construct(App $app)
{
parent::__construct($app);
$this->id= Env::get("aliyun.access_key_id");
$this->key= Env::get("aliyun.access_key_secret");
}
public function complaints(){
$id=$this->id;
$key=$this->key;
$host="https://****.aliyuncs.com";
$dir="wx-applet/";
$res=app("ossSign")->signedUrl($id,$key,$host,$dir);
$path="";
$res["path"]=$path;
$this->success($res);
}
}