shop_uniapp/js_sdk/jason-alioss-upload/oss.js

69 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Base64 } from './base64.js'
import { util } from './crypto.js'
import { HMAC } from './hmac.js'
import { SHA1 } from './sha1.js'
// 下面这3个信息必填
export const OSSUrl = 'https://zjeloong.oss-cn-hangzhou.aliyuncs.com/'
const OSSAccessKeyId = 'LTAI5tEo7FnNzc6ubbEdXhn8'
const OssAccesskeySercet= '8xdKsFQSwQXSXHMsU7vCmpxcXIt3dr';
const policyText = {
"expiration": "2034-01-01T12:00:00.000Z", // 设置Policy的有效期,格式为UTC时间。如果Policy失效,将无法上传文件。
"conditions": [
["content-length-range", 0, 1048576000] // 限制上传文件的大小,单位为字节,此处限制文件大小为1 GB。如果上传的文件大小超过此限制,文件将无法上传到OSS。如果未设置该限制,则默认文件大小最大为5 GB。
]
}
const policy = Base64.encode(JSON.stringify(policyText))
const bytes = HMAC(SHA1, policy, OssAccesskeySercet, { asBytes: true })
const signature = util.bytesToBase64(bytes)
// 生成文件名随机字符串
function random_string(len) {
  const strLeng = len || 32;
  const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  const maxPos = chars.length;
  let pwd = '';
  for (let i = 0; i < strLeng; i++) {
  pwd += chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
// 获取文件后缀
function get_suffix(filename) {
const pos = filename.lastIndexOf('.')
let suffix = ''
if (pos != -1) {
suffix = filename.substring(pos)
}
return suffix;
}
// dir格式:'img/'
export const ossUpload = (filePath, name, dir) => {
const key = dir + random_string(10) + get_suffix(name)
return new Promise((resolve, reject) => {
uni.uploadFile({
url: OSSUrl,
filePath,
name: 'file',
formData: {
name,
key,
policy,
OSSAccessKeyId,
success_action_status: '200',
signature
},
success: () => {
resolve({success: true, data: OSSUrl + key})
},
fail: () => {
reject({success: false, data: '上传失败'})
}
})
})
}