|
| 1 | +Vue.component('zone-list', { |
| 2 | + props: ['hostMap', 'currentZone', 'switchZone', 'selectedHost'], |
| 3 | + template: '<div class="uphosts-list"><ul><li v-for="item in hostMap" :class="{on: item.zone == currentZone}" @click="switchZone(item.zone)">{{item.zoneZh}}</li></ul><ul><li v-for="item in hostMap" :class="{on: item.zone == currentZone}"><label v-for="uphost in item.uphosts"><input type="radio" v-model="selectedHost.host" :value="uphost"/>{{uphost}}</label></li></ul></div>' |
| 4 | +}); |
| 5 | +Vue.component('upload-performance', { |
| 6 | + props: ['per'], |
| 7 | + template: '<div class="up-performance"><div class="per-title">上传耗时:</div>' + |
| 8 | + '<table>' + |
| 9 | + '<tr><th>类型</th><th>耗时 / ms</th></tr>' + |
| 10 | + '<tr><td>重定向:</td><td>{{per.redirect | tofixed(2)}}</td></tr>' + |
| 11 | + '<tr><td>DNS 查询:</td><td>{{per.domainLookup | tofixed(2)}}</td></tr>' + |
| 12 | + '<tr><td>建立连接:</td><td>{{per.connect | tofixed(2)}}</td></tr>' + |
| 13 | + '<tr><td>发送数据到开始响应:</td><td>{{per.request | tofixed(2)}}</td></tr>' + |
| 14 | + '<tr><td>接收响应:</td><td>{{per.response | tofixed(2)}}</td></tr>' + |
| 15 | + '<tr><td>总耗时:</td><td>{{per.duration | tofixed(2)}}</td></tr></table>' + |
| 16 | + '</div>' |
| 17 | +}); |
| 18 | +Vue.component('up-headers', { |
| 19 | + props: ['headers'], |
| 20 | + template: '<div class="up-headers"><div class="per-title">响应头:</div><table><tr><th>类型</th><th>值</th></tr><tr v-for="header in headers"><td>{{header.key}}</td><td>{{header.val}}</td></tr></table></div>' |
| 21 | +}); |
| 22 | +Vue.filter('tofixed', function (val, size) { |
| 23 | + return val.toFixed(size); |
| 24 | +}); |
| 25 | + |
| 26 | +var app = new Vue({ |
| 27 | + el: '#app', |
| 28 | + data: { |
| 29 | + currentZone: 'z0', |
| 30 | + selectedHost: { |
| 31 | + host: '' |
| 32 | + }, |
| 33 | + currentToken: '', |
| 34 | + loadMessage: '', |
| 35 | + hostMap: [ |
| 36 | + { |
| 37 | + zone: 'z0', |
| 38 | + zoneZh: '华东', |
| 39 | + token: 'xozWSPMxkMjIVoHg2JyXq4-7-oJaEADLOKHVR0vU:hzD116IsDOpb4gjzyPg7ngg4Qjs=:eyJzY29wZSI6Impzc2RrOmEuanBnIiwiZGVhZGxpbmUiOjIxMTQzODA4MDAwMDAsImZzaXplTGltaXQiOjEwMDAwMDB9', |
| 40 | + uphosts: [ |
| 41 | + 'http://up.qiniu.com', |
| 42 | + 'http://upload.qiniu.com', |
| 43 | + 'https://up.qbox.me', |
| 44 | + 'https://upload.qbox.me' |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + zone: 'z1', |
| 49 | + zoneZh: '华北', |
| 50 | + token: 'xozWSPMxkMjIVoHg2JyXq4-7-oJaEADLOKHVR0vU:9RFuA13c6pp-O2aIOe1UfTI3rlo=:eyJzY29wZSI6Impzc2RrLXoxOmEuanBnIiwiZGVhZGxpbmUiOjIxMTQzODA4MDAwMDAsImZzaXplTGltaXQiOjEwMDAwMDB9', |
| 51 | + uphosts: [ |
| 52 | + 'http://up-z1.qiniu.com', |
| 53 | + 'http://upload-z1.qiniu.com', |
| 54 | + 'https://up-z1.qbox.me', |
| 55 | + 'https://upload-z1.qbox.me' |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + zone: 'z2', |
| 60 | + zoneZh: '华南', |
| 61 | + token: 'xozWSPMxkMjIVoHg2JyXq4-7-oJaEADLOKHVR0vU:qmDBxiYviu0tg2hZzCFHjCZM3-w=:eyJzY29wZSI6Impzc2RrLXoyOmEuanBnIiwiZGVhZGxpbmUiOjIxMTQzODA4MDAwMDAsImZzaXplTGltaXQiOjEwMDAwMDB9', |
| 62 | + uphosts: [ |
| 63 | + 'http://up-z2.qiniu.com', |
| 64 | + 'http://upload-z2.qiniu.com', |
| 65 | + 'https://up-z2.qbox.me', |
| 66 | + 'https://upload-z2.qbox.me' |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + zone: 'na0', |
| 71 | + zoneZh: '北美', |
| 72 | + token: 'xozWSPMxkMjIVoHg2JyXq4-7-oJaEADLOKHVR0vU:DMPzTGGhX3HY0ph99YfAK_y-0XM=:eyJzY29wZSI6Impzc2RrLW5hMDphLmpwZyIsImRlYWRsaW5lIjoyMTE0MzgwODAwMDAwLCJmc2l6ZUxpbWl0IjoxMDAwMDAwfQ==', |
| 73 | + uphosts: [ |
| 74 | + 'http://up-na0.qiniu.com', |
| 75 | + 'http://upload-na0.qiniu.com', |
| 76 | + 'https://up-na0.qbox.me', |
| 77 | + 'https://upload-na0.qbox.me' |
| 78 | + ] |
| 79 | + }, |
| 80 | + ], |
| 81 | + isPerformanceSupported: true, |
| 82 | + performance: null, |
| 83 | + headers: null |
| 84 | + }, |
| 85 | + methods: { |
| 86 | + renderHtml: function() {}, |
| 87 | + post: function(opt) { |
| 88 | + var xmlHttp = new XMLHttpRequest(); |
| 89 | + xmlHttp.open('POST', opt.url, true); |
| 90 | + xmlHttp.setRequestHeader('X-Qiniu-Performance', 'true'); |
| 91 | + xmlHttp.onreadystatechange = function () { |
| 92 | + if (xmlHttp.readyState == 4) { |
| 93 | + if (xmlHttp.status == 200) { |
| 94 | + opt.success(xmlHttp); |
| 95 | + } else { |
| 96 | + opt.error(xmlHttp.responseText); |
| 97 | + } |
| 98 | + } |
| 99 | + }; |
| 100 | + xmlHttp.upload.onprogress = opt.progress; |
| 101 | + xmlHttp.send(opt.data); |
| 102 | + }, |
| 103 | + uploadTest: function() { |
| 104 | + if(!this.selectedHost.host) return; |
| 105 | + this.resetResult(); |
| 106 | + var self = this; |
| 107 | + this.post({ |
| 108 | + url: this.selectedHost.host, |
| 109 | + data: this.mockDate(), |
| 110 | + progress: function(e) { |
| 111 | + if (e.lengthComputable) { |
| 112 | + var percent = e.loaded/e.total*100; |
| 113 | + self.loadMessage = '模拟数据上传:' + e.loaded + " / " + e.total+" bytes 完成:" + percent.toFixed(2) + "%"; |
| 114 | + } |
| 115 | + }, |
| 116 | + success: function(xhr) { |
| 117 | + self.formateHeader(xhr.getAllResponseHeaders()); |
| 118 | + self.getPerformance(); |
| 119 | + }, |
| 120 | + error: function(res) { |
| 121 | + self.loadMessage = '上传失败:' + res; |
| 122 | + } |
| 123 | + }); |
| 124 | + }, |
| 125 | + mockDate: function(size) { |
| 126 | + var f = new FormData(document.getElementById("testform")); |
| 127 | + f.append('file', this.dataURLtoBlob(this.randomBase64(size))); |
| 128 | + f.append('token', this.currentToken); |
| 129 | + return f; |
| 130 | + }, |
| 131 | + randomBase64: function() { |
| 132 | + var dataurl = 'data:image/jpeg;base64,'; |
| 133 | + var len = parseInt(Math.random()*(1330000-1100000+1)+1100000, 10); |
| 134 | + while (dataurl.length < len) { |
| 135 | + dataurl += Math.random > 0.5 ? '/9j/4AAQSkZJRgABAQAASABIAAD/4Q' : '/9j/4AAQSkZJRgABAQAASABIAAD/4W'; |
| 136 | + } |
| 137 | + return dataurl; |
| 138 | + }, |
| 139 | + dataURLtoBlob: function(dataurl) { |
| 140 | + var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], |
| 141 | + bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); |
| 142 | + while(n--){ |
| 143 | + u8arr[n] = bstr.charCodeAt(n); |
| 144 | + } |
| 145 | + return new Blob([u8arr], {type:mime}); |
| 146 | + }, |
| 147 | + resetResult: function() { |
| 148 | + for (var i = 0; i < this.hostMap.length; i++) { |
| 149 | + if (this.hostMap[i].zone === this.currentZone) { |
| 150 | + this.currentToken = this.hostMap[i].token; |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + this.performance = null; |
| 155 | + this.headers = null; |
| 156 | + }, |
| 157 | + switchZone: function(zone) { |
| 158 | + this.currentZone = zone; |
| 159 | + this.selectedHost.host = ''; |
| 160 | + }, |
| 161 | + getPerformance: function() { |
| 162 | + var per = httpPerformance.getByName(this.selectedHost.host + '/'); |
| 163 | + if(per.length == 2) { |
| 164 | + per[1].redirect = per[0].redirect; |
| 165 | + per[1].domainLookup = per[0].domainLookup; |
| 166 | + per[1].connect = per[0].connect; |
| 167 | + } |
| 168 | + this.performance = per[1] || per[0]; // 跨域有时候会先发送一个 option 请求,并不是真的上传请求。 |
| 169 | + }, |
| 170 | + formateHeader: function(headers) { |
| 171 | + var list = []; |
| 172 | + headers.match(/.+/mg).map(function(item) { |
| 173 | + var index = item.indexOf(':'); |
| 174 | + var o = { |
| 175 | + key: item.substr(0, index), |
| 176 | + val: item.substr(index + 1) |
| 177 | + }; |
| 178 | + list.push(o); |
| 179 | + }); |
| 180 | + this.headers = list; |
| 181 | + } |
| 182 | + }, |
| 183 | + created: function() { |
| 184 | + this.isPerformanceSupported = !!(window.performance && window.performance.getEntries); |
| 185 | + } |
| 186 | +}); |
| 187 | + |
| 188 | + |
0 commit comments