职站学生端小程序版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.1 KiB

4 days ago
import Crypto from './crypto.js';
import './hmac.js';
import './sha1.js';
import Util from '@/libs/util'
import {Base64} from './base64.js';
let date=new Date()
date=date.setHours(date.getHours() + 24)
let extime=""+new Date(date).toISOString()
console.log(33, extime)
var policyText = {
"expiration": extime, //设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
"conditions": [
["content-length-range", 0, 1024*1024*100] // 设置上传文件的大小限制
]
};
var config={
accessid:'LTAI5tAyVqtz2ZTQwVBmGgfW',
accesskey:'S744EFNUPpcCcXM6ivGRz5qquKYHrF',
4 days ago
osshost:'https://huoran.oss-cn-shenzhen.aliyuncs.com',
policyBase64:Base64.encode(JSON.stringify(policyText))
}
var message = config.policyBase64;
var bytes = Crypto.HMAC(Crypto.SHA1, message, config.accesskey, { asBytes: true }) ;
var signature = Crypto.util.bytesToBase64(bytes);
var timetamp = new Date().getTime();
function random_string(len) {
  len = len || 32;
  var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  var maxPos = chars.length;
  var pwd = '';
  for (let i = 0; i < len; i++) {
  pwd += chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
var OSS={
name:'aliyun',
host:config.osshost,
accessid:config.accessid,
signature:signature,
policyBase64:config.policyBase64,
}
export default function(tempFilePaths, callback) {
const ext = Util.getFileExt(tempFilePaths)
const fileName = Date.now() + '.' + ext
uni.uploadFile({
   url: OSS.host,
   filePath: tempFilePaths,
4 days ago
fileType: '',
name: 'file',
formData: {
name: fileName,
key: fileName,//文件名
  policy: OSS.policyBase64, // 输入你获取的的policy
OSSAccessKeyId: OSS.accessid, // 输入你的AccessKeyId
success_action_status: '200', // 让服务端返回200,不然,默认会返回204
signature: OSS.signature, // 输入你获取的的signature
},
success: (res) => {
callback({
name: fileName,
url: OSS.host + '/' + fileName,
ext
})
},
fail: (res) => {
console.log(res);
}
})
};