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 100
| import Vue from 'vue' module.exports = { uploadImage: function (success) { this.select_img(1, success) }, select_img: function (way, success) { uni.chooseImage({ count: 1, success: function (res) { if (res.tempFilePaths.length > 0) { let filePath = res.tempFilePaths[0] let ext = '' ext = this.get_suffix(res.tempFiles[0].name) ext = this.get_suffix(res.tempFiles[0].path) this.get_sign(way, filePath, ext, success) } else { console.log('选择文件失败') } }.bind(this), }) }, get_sign: function (way, filePath, ext, success) { var url = getApp().globalData.baseUrl + '/sp-home/alioss/getSign' uni.request({ url: url, header: this.get_header(), data: { way: way, ext: ext }, success: function (res) { if (res.data.code == 200) { var sign_obj = res.data.data this.upload_to_oss(sign_obj, filePath, success) } else { console.log('错误:' + res.data.msg) } }.bind(this), }) }, upload_to_oss: function (sign_obj, filePath, success) { var formData = { OSSAccessKeyId: sign_obj.OSSAccessKeyId, policy: sign_obj.policy, Signature: sign_obj.Signature, key: sign_obj.key, success_action_status: sign_obj.success_action_status, 'x-oss-object-acl': sign_obj['x-oss-object-acl'], } uni.showLoading({ title: '上传中', mask: false, }) uni.uploadFile({ url: sign_obj.host, name: 'file', filePath: filePath, formData: formData, success: function (res) { console.log('上传成功') uni.hideLoading() success(sign_obj.file_url) }, }) }, get_suffix: function (filename) { var pos = filename.lastIndexOf('.') let suffix = '' if (pos != -1) { suffix = filename.substring(pos + 1) } return suffix }, get_header: function () { var tokenName = 'satoken' var tokenValue = uni.getStorageSync('satoken') var header = { 'content-type': 'application/x-www-form-urlencoded', } if (tokenName != undefined && tokenName != '') { header[tokenName] = tokenValue } return header }, }
|