upload.wxml
<view bindtap='onChooseImage'>上传头像</view>
upload.js
Page({
onChooseImage: function () {
var that = this;
wx.chooseImage({
success(res) {
const tempFilePaths = res.tempFilePaths
that.getCloudCdnParams(tempFilePaths[0])
}
})
},
//获取签名oss鉴权信息
getCloudCdnParams: function (filePath) {
var that = this;
var dataUrl = '服务端获取签名接口';
var params = new Object();
wx.request({
url: dataUrl,
method: "post",
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
that.uploadFileFun(res.data, filePath)
}
})
},
// 上传图片
uploadFileFun: function (result, filePath) {
//console.log(result)
var that = this;
var aliyunFileKey = result.dir + new Date().getTime() + Math.floor(Math.random() * 150) + '.png';
console.log(aliyunFileKey)
wx.uploadFile({
url: result.host,
filePath: filePath,
name: 'file',
/**上传的参数**/
formData: {
name: filePath,
key: aliyunFileKey,
policy: result.policy,
OSSAccessKeyId: result.accessid,
success_action_status: "200",
signature: result.signature,
},
success: function (res) {
/**这边上传成功后要自己拼接文件的地址**/
console.log(result.cdnurl + aliyunFileKey)
},
fail: function (res) {
console.log(res)
}
})
},
})
2、后台只提供一个签名接口就够了,因为是客户端直接上传到阿里云oss
namespace App\Controller;
use Think\Controller;
class OssController extends Controller{
public function getSign(){
//此处三个参数需要修改
$ossConfig = C('UPLOAD_SITEIMG_OSS');
$jiaoanUpload = I('request.jiaoan_upload_maxSize','0','intval');
if($jiaoanUpload == 1){
$ossConfig['maxSize'] = 1024*1024*1024;
}
$id= $ossConfig['driverConfig']['AccessKeyId'];
$key= $ossConfig['driverConfig']['AccessKeySecret'];
$host = rtrim($ossConfig['driverConfig']['domain'],'/');
$now = time();
$expire = 600; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
$end = $now + $expire;
$expiration = $this->gmt_iso8601($end);
//自定义目录
$dir = 'wxupload/'.date('Ymd').'/';
//最大文件大小.用户可以自己设置
$condition = array(0=>'content-length-range', 1=>0, 2=>$ossConfig['maxSize']);
$conditions[] = $condition;
//表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
//此处语法为官方错误,单引号包含 $key,切勿改动,错误对错误才能验证成功
$start = array(0=>'starts-with', 1=>'$key', 2=>$dir);
$conditions[] = $start;
$arr = array('expiration'=>$expiration,'conditions'=>$conditions);
$policy = json_encode($arr);
$base64_policy = base64_encode($policy);
$string_to_sign = $base64_policy;
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true));
$response = array();
$response['accessid'] = $id;
$response['host'] = $host;
$response['policy'] = $base64_policy;
$response['signature'] = $signature;
$response['expire'] = $end;
//这个参数是设置用户上传指定的前缀
$response['dir'] = $dir;
//使用CDN URL,链接后可直接加@45p,进行图片缩放
$response['cdnurl'] =rtrim(C('file_url',null,$host),'/');
//使用OSS URL
//$response['cdnurl'] = $host;
echo json_encode($response);
exit;
}
public function gmt_iso8601($time) {
vendor('DateTime.DateTimea');
$dtStr = date("c", $time);
$mydatetime = new \DateTimea($dtStr);
$expiration = $mydatetime->format();
$pos = strpos($expiration, '+');
$expiration = substr($expiration, 0, $pos);
return $expiration."Z";
}
| 留言与评论(共有 0 条评论) |