request.uvue 6.9 KB
Newer Older
1
<template>
2
	<view style="flex: 1;">
3
		<view class="uni-padding-wrap uni-common-mt">
Y
yurj26 已提交
4 5
			<view class="uni-common-mt" style="border-width: 2rpx;border-radius: 4rpx;">
				<textarea :value="res" class="uni-textarea"></textarea>
6
			</view>
7 8 9 10
			<view>
				<text>地址 : {{ host + url}}</text>
				<text>请求方式 : {{method}}</text>
			</view>
11
			<view class="uni-btn-v uni-common-mt">
12
				<button type="primary" @click="sendRequest">发起请求</button>
13 14
			</view>
		</view>
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
		<scroll-view style="flex: 1;" show-scrollbar="true">
			<view style="padding: 20rpx;">
				<text>设置请求方式</text>
				<view class="uni-common-pb"></view>
				<view style="flex-direction: row;flex-wrap: wrap;">
					<button style="padding: 5rpx; margin-right: 10rpx;" type="primary" size="mini"
						@click="changeMethod('GET')">GET</button>
					<button style="padding: 5rpx; margin-right: 10rpx; " type="primary" size="mini"
						@click="changeMethod('POST')">POST</button>
					<button style="padding: 5rpx; margin-right: 10rpx; " type="primary" size="mini"
						@click="changeMethod('PUT')">PUT</button>
					<button style="padding: 5rpx; margin-right: 10rpx;" type="primary" size="mini"
						@click="changeMethod('DELETE')">DELETE</button>
					<button style="padding: 5rpx; margin-right: 10rpx; " type="primary" size="mini"
						@click="changeMethod('PATCH')">PATCH</button>
					<button style="padding: 5rpx;margin-right: 10rpx;" type="primary" size="mini"
						@click="changeMethod('OPTIONS')">OPTIONS</button>
					<button style="padding: 5rpx;" type="primary" size="mini"
						@click="changeMethod('HEAD')">HEAD</button>
				</view>
			</view>
			<view style="padding: 20rpx;">
				<text>请求返回错误码的接口(默认为GET)</text>
				<view class="uni-common-pb"></view>
				<view style="flex-direction: row;flex-wrap: wrap;">
					<button style="padding: 5rpx;" type="primary" size="mini" v-for="(item, index) in errorCodeUrls"
						:key="index" @click="changeUrl(item)">{{item}}</button>
				</view>
			</view>
			<view style="padding: 20rpx;">
				<text>请求不同header的接口(默认为GET)</text>
				<view class="uni-common-pb"></view>
				<view style="flex-direction: row;flex-wrap: wrap;">
					<button style="padding: 5rpx;" type="primary" size="mini" v-for="(item, index) in headerUrls"
						:key="index" @click="changeUrl(item)">{{item}}</button>
				</view>
			</view>
			<view style="padding: 20rpx;">
				<text>请求不同content-type的接口(默认为GET)</text>
				<view class="uni-common-pb"></view>
				<view style="flex-direction: row;flex-wrap: wrap;">
					<button style="padding: 5rpx;" type="primary" size="mini" v-for="(item, index) in contentTypeUrls"
						:key="index" @click="changeUrl(item)">{{item}}</button>
				</view>
			</view>

			<view style="padding: 20rpx;">
				<text>POST请求(有body)</text>
				<view class="uni-common-pb"></view>
				<view style="flex-direction: row;flex-wrap: wrap;">
					<button style="padding: 5rpx;" type="primary" size="mini" v-for="(item, index) in postUrls"
						:key="index" @click="changeUrlFromPost(item)">{{item}}</button>
				</view>
			</view>
		</scroll-view>
70 71 72 73
	</view>
</template>
<script>
	const duration = 2000
74 75 76 77 78 79 80 81 82 83 84
	const methodMap = {
		"GET": "/api/http/method/get",
		"POST": "/api/http/method/post",
		"PUT": "/api/http/method/put",
		"DELETE": "/api/http/method/delete",
		"PATCH": "/api/http/method/patch",
		"OPTIONS": "/api/http/method/options",
		"HEAD": "/api/http/method/head"
	}


85 86 87 88
	export default {
		data() {
			return {
				title: 'request',
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
89 90
				res: '',
				task: null as RequestTask | null,
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
				pageVisible: false,
				host: "http://request.dcloud.net.cn",
				url: "/api/http/method/get",
				method: "GET",
				data: null as any | null,
				header: null as UTSJSONObject | null,
				errorCodeUrls: [
					"/api/http/statusCode/200",
					"/api/http/statusCode/204",
					"/api/http/statusCode/301",
					"/api/http/statusCode/302",
					"/api/http/statusCode/307",
					"/api/http/statusCode/400",
					"/api/http/statusCode/401",
					"/api/http/statusCode/403",
					"/api/http/statusCode/404",
					"/api/http/statusCode/405",
					"/api/http/statusCode/500",
					"/api/http/statusCode/502",
					"/api/http/statusCode/503",
					"/api/http/statusCode/504",
				],
				headerUrls: [
					"/api/http/header/ua",
					"/api/http/header/referer",
					"/api/http/header/requestCookie",
					"/api/http/header/setCookie",
					"/api/http/header/deleteCookie"
				],
				contentTypeUrls: [
					"/api/http/contentType/text/plain",
					"/api/http/contentType/text/html",
					"/api/http/contentType/text/xml",
					"/api/http/contentType/image/gif",
					"/api/http/contentType/image/jpeg",
					"/api/http/contentType/image/png",
					"/api/http/contentType/application/json",
					"/api/http/contentType/application/octetStream",
				],
				postUrls: [
					"/api/http/contentType/json",
					"/api/http/contentType/xWwwFormUrlencoded",
				],
134 135
			}
		},
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
136 137 138 139
		onLoad() {
			this.pageVisible = true;
		},
		onUnload() {
140
			this.pageVisible = false;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
141 142 143
			uni.hideLoading();
			this.task?.abort();
		},
144
		methods: {
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
			changeMethod(e : string) {
				this.method = e;
				this.url = methodMap[e] as string;
				this.data = null;
				this.header = null;
			},
			changeUrl(e : string) {
				this.method = "GET";
				this.url = e;
				this.data = null;
				this.header = null;
			},
			changeUrlFromPost(e : string) {
				this.method = "POST";
				this.url = e;
				switch (e) {
					case "/api/http/contentType/json":
						this.header = {
							"Content-Type": "application/json"
						};
						this.data = {
							"hello": "world"
						};
						break;
					case "/api/http/contentType/xWwwFormUrlencoded":
						this.header = {
							"Content-Type": "application/x-www-form-urlencoded"
						};
						this.data = "hello=world";
						break;
				}
			},
177
			sendRequest() {
178 179
				uni.showLoading({
					title: "请求中..."
180
				})
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
181
				this.task = uni.request({
182 183 184 185 186 187
					url: this.host + this.url,
					// dataType: "json",
					// responseType: "json",
					method: this.method,
					data: this.data,
					header: this.header,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
188 189 190 191 192 193
					timeout: 6000,
					sslVerify: false,
					withCredentials: false,
					firstIpv4: false,
					success(res) {
						if (this.pageVisible) {
194
							console.log('request success', JSON.stringify(res.data))
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
							uni.showToast({
								title: '请求成功',
								icon: 'success',
								mask: true,
								duration: duration
							});
							this.res = '请求结果 : ' + JSON.stringify(res);
						}
					},
					fail(err) {
						if (this.pageVisible) {
							console.log('request fail', err);
							uni.showModal({
								content: err.errMsg,
								showCancel: false
							});
						}
					},
					complete() {
214
						uni.hideLoading()
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
215 216
					},
				});
217 218 219
			}
		}
	}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
220
</script>