1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| module.exports = { getUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16) }) }, beforeUpload({ file, module }) { let _self = this; return new Promise((resolve, reject) => { this.policy(module) .then(response => { resolve({ policy: response.data.policy, signature: response.data.signature, ossaccessKeyId: response.data.accessid, key: response.data.dir + this.getUUID() + "${filename}", dir: response.data.dir, host: response.data.host }); }) .catch(err => { reject(err); }); }); }, policy(module) { return new Promise((resolve, reject) => { uni.request({ url: 'http://获取签证的接口/' + 'oss', data: { module }, success: (res) => { if (res.data.success) { resolve(res.data) } else { reject(res) } }, fail: (err) => { reject(err) } }); }) }, uploadFile({ file, module, response, limit = 5242880 }) { console.log("uploadFile: ", { file, module, response }) if (!file) { uni.showToast({ title: "读取文件失败", icon: 'none' }); return false; } if (file.size > limit) { uni.showToast({ title: "超过限制大小", icon: 'none' }); return false; } let filename = file.path.substr(file.path.lastIndexOf("/") + 1) this.beforeUpload({ file, module }).then(res => { let options = { url: res.host, name: 'file', filePath: file.path, formData: res, success: ossCallBack => { console.log({ ossCallBack, filename }) response(res.host + "/" + res.key.replace("${filename}", filename)) }, fail: console.error } console.log(options) uni.uploadFile(options) }).catch(console.error) } }
|