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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
| <template> <div> <div class="box-img" v-if="fileType===1"> <div class="box el-icon-circle-plus" :id="'selectfiles'+index" @dblclick.native="alert(index)"> <div class="img_del el-icon-circle-close" v-if="Url!==''" @click.stop="del(index)"></div> <img :src="Url" alt="" v-if="Url!==null"> <div class="jindu_box" v-if="jintu"> <el-progress type="circle" :percentage="percentage" color="#67C23A" v-if="percentage!==100"></el-progress> <el-progress type="circle" :percentage="percentage" status="success" v-if="percentage===100"></el-progress> </div> </div> <div class="img_name" v-if="fileType===1"> {{name}} </div> </div> <div class="file-box" v-if="fileType!==1"> <div> {{Url===''?'请选择文件':pushName}} <div v-if="jintu"> <el-progress :percentage="percentage" color="#67C23A" v-if="percentage!==100"></el-progress> <el-progress :percentage="percentage" status="success" v-if="percentage===100"></el-progress> </div> </div> <el-button type="primary" :id="'selectfiles'+index"> {{Url===''?'选择文件':'重新选择'}} </el-button> <el-button type="primary" v-if="Url!==''" @click="inspect(Url)"> 检查 </el-button> </div> </div> </template> <script type="text/javascript"> import '@/utils/crypto1/crypto/crypto.js' import '@/utils/crypto1/hmac/hmac.js' import '@/utils/crypto1/sha1/sha1.js' import Base64 from '@/utils/base64.js' import plupload from 'plupload' let accessid = 'LTAIShfs0pmAbRkb' let accesskey = 'qlx4Fp0yLI8fbA9zQmVIcPsCCdGBBh' let host = 'http://sougematch.oss-cn-beijing.aliyuncs.com' let bucket = 'image' let g_dirname = '' let g_object_name = '' let g_object_name_type = '' var timestamp let now = timestamp = Date.parse(new Date()) / 1000 let pos = '' let suffix = '' var policyText = { 'expiration': '2020-01-01T12:00:00.000Z', 'conditions': [ ['content-length-range', 0, 1048576000] ] } var policyBase64 = Base64.encode(JSON.stringify(policyText)) let message = policyBase64 var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, {asBytes: true}) var signature = Crypto.util.bytesToBase64(bytes) function check_object_radio () { g_object_name_type = 'random_name' } function get_dirname () { g_dirname = 'image/' } function random_string (len) { len = len || 32 var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' var maxPos = chars.length var pwd = '' for (var i = 0; i < len; i++) { pwd += chars.charAt(Math.floor(Math.random() * maxPos)) } return pwd } function get_suffix (filename) { pos = filename.lastIndexOf('.') suffix = '' if (pos !== -1) { suffix = filename.substring(pos) } return suffix } function calculate_object_name (filename) { if (g_object_name_type === 'local_name') { g_object_name += filename } else if (g_object_name_type === 'random_name') { suffix = get_suffix(filename) g_object_name = random_string(10) + new Date().getTime() + suffix } return '' } function get_uploaded_object_name (filename) { if (g_object_name_type === 'local_name') { var tmp_name = g_object_name tmp_name = tmp_name.replace(filename, filename) return tmp_name } else if (g_object_name_type === 'random_name') { return g_object_name } } function set_upload_param (up, filename, ret) { if (filename !== '') { suffix = get_suffix(filename) calculate_object_name(filename) } let new_multipart_params = { 'Filename': g_dirname + g_object_name, 'key': g_dirname + g_object_name, 'policy': policyBase64, 'OSSAccessKeyId': accessid, 'success_action_status': '200', 'signature': signature } up.setOption({ 'url': host, 'multipart_params': new_multipart_params }) up.start() } export default { name: 'ImgUpload', data () { return { percentage: 0, jintu: false, pushName:'' } }, props: { name:{ type: String, required: true }, fileType:{ type: Number, required: true }, Url:String, type: { type: Number, default:1 }, index: { type: Number, default:0 }, add_img: Function, del_img:Function }, methods: { del (index) { this.del_img(index) }, inspect (url) { window.open(url); } }, mounted () { var that = this var uploader = new plupload.Uploader({ runtimes: 'html5,flash,silverlight,html4', browse_button: 'selectfiles'+that.index, multi_selection: false, flash_swf_url: './../assets/plupload-2.1.2/js/Moxie.swf', silverlight_xap_url: './../assets/plupload-2.1.2/js/Moxie.xap', url: host, init: { PostInit: function () {}, QueueChanged: function (up) { }, FileFiltered: function (up, files) { var fileSize = (Math.round(files.size * 100 / (1024 * 1024)) / 100).toString() if (fileSize > 10) { uploader.removeFile(files) that.$message.error('文件不能大于10M') } }, FilesAdded: function (up, files) { set_upload_param(uploader, '', false) }, BeforeUpload: function (up, file) { check_object_radio() get_dirname() set_upload_param(up, file.name, true) }, UploadProgress: function (up, file) { that.jintu = true that.percentage = file.percent }, FileUploaded: function (up, file, info) { if (info.status === 200) { that.$message({ message: '上传成功', type: 'success' }) var Url = host + '/' + g_dirname + get_uploaded_object_name(file.name) that.pushName = file.name that.add_img(that.type, Url, that.index,that.fileType) that.jintu = false that.percentage = 0 } else { that.jintu = false that.percentage = 0 } }, Error: function (up, err) { } } }) uploader.init() } } </script> <style type="text/css"> .uploadImg-main { width: 100px; height: 130px; text-align: center; float: left; font-size: 14px; color: #C0C4CC; line-height: 30px; margin-right: 54px; } .uploadImg-main .uploadImg { width: 100px; height: 100px; position: relative; font-size: 12px; line-height: 20px; border: 1px dashed #C0C4CC; cursor: pointer; } .uploadImg-main .uploadImg i { font-size: 40px; line-height: 70px } .uploadImg-main .uploadImg input { width: 100%; height: 100%; position: absolute; left: 0; top: 0; filter: alpha(opacity:0); opacity: 0; cursor: pointer; } .uploadImg-main .uploadImg img { width: 100%; height: 100%; position: absolute; left: 0; top: 0; } .box { width: 130px; height: 130px; background: #F9F9F9; color: #D8D8D8; text-align: center; line-height: 130px; font-size: 28px; border: 1px solid #D8D8D8; } .box img { display: block; width: 100%; height: 100%; position: absolute; top: 0; left: 0; } .box-img{ width: 130px; } .img_name{ width: 100%; height: 16px; line-height: 16px; font-size: 14px; text-align: center; margin-top: 15px; } .jindu_box{ width: 100%; position: absolute; top: 0; left: 0; } .img_del{ position: absolute; width: 20px; height: 20px; color: #989898; top: 5px; right: 5px; z-index: 111; font-size: 20px; } .file-box{ width: 100%; } </style>
|