request.uvue 14.5 KB
Newer Older
1
<template>
2 3
  <view style="flex: 1;">
    <view class="uni-padding-wrap uni-common-mt">
4
      <view class="uni-common-mt" style="border-width: 2px;border-style: solid; border-radius: 4px;">
5
        <textarea :value="res" class="uni-textarea" style="width: 100%;"></textarea>
6 7 8 9 10 11
      </view>
      <view>
        <text>地址 : {{ host + url}}</text>
        <text>请求方式 : {{method}}</text>
      </view>
      <view class="uni-btn-v uni-common-mt">
12
        <button type="primary" @click="jest_respone_with_404_and_string_generics">发起请求</button>
13 14
      </view>
    </view>
雪洛's avatar
雪洛 已提交
15
    <scroll-view style="flex: 1;" show-scrollbar="true">
16
      <view style="padding: 20px;">
17 18 19
        <text>设置请求方式</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
20
          <button style="padding: 5px; margin-right: 10px;" type="primary" size="mini"
21
            @click="changeMethod('GET')">GET</button>
22
          <button style="padding: 5px; margin-right: 10px; " type="primary" size="mini"
23
            @click="changeMethod('POST')">POST</button>
24
          <button style="padding: 5px; margin-right: 10px; " type="primary" size="mini"
25
            @click="changeMethod('PUT')">PUT</button>
26
          <button style="padding: 5px; margin-right: 10px;" type="primary" size="mini"
27
            @click="changeMethod('DELETE')">DELETE</button>
28
          <button style="padding: 5px; margin-right: 10px; " type="primary" size="mini"
29
            @click="changeMethod('PATCH')">PATCH</button>
30
          <button style="padding: 5px;margin-right: 10px;" type="primary" size="mini"
31
            @click="changeMethod('OPTIONS')">OPTIONS</button>
32
          <button style="padding: 5px;" type="primary" size="mini" @click="changeMethod('HEAD')">HEAD</button>
33 34
        </view>
      </view>
35
      <view style="padding: 20px;">
36 37 38
        <text>请求返回错误码的接口(默认为GET)</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
39
          <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in errorCodeUrls" :key="index"
40 41 42
            @click="changeUrl(item)">{{item}}</button>
        </view>
      </view>
43
      <view style="padding: 20px;">
44 45 46
        <text>请求不同header的接口(默认为GET)</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
47
          <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in headerUrls" :key="index"
48 49 50
            @click="changeUrl(item)">{{item}}</button>
        </view>
      </view>
51
      <view style="padding: 20px;">
52 53 54
        <text>请求不同content-type的接口(默认为GET)</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
H
hdx 已提交
55 56
          <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in contentTypeUrls" :key="index"
            @click="changeUrl(item)">{{item}}</button>
57 58
        </view>
      </view>
59

60
      <view style="padding: 20px;">
61 62 63
        <text>POST请求(有body)</text>
        <view class="uni-common-pb"></view>
        <view style="flex-direction: row;flex-wrap: wrap;">
64
          <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in postUrls" :key="index"
65 66 67 68 69
            @click="changeUrlFromPost(item)">{{item}}</button>
        </view>
      </view>
    </scroll-view>
  </view>
70
</template>
DCloud-WZF's avatar
DCloud-WZF 已提交
71 72 73 74 75 76 77 78 79 80 81
<script>
  // #ifdef APP
  import {
    testInovkeRequest,
    CommonOptions
  } from '@/uni_modules/test-invoke-network-api'
  // #endif

  class GETDataType {
    data : UTSJSONObject | null = null
  }
82

83 84 85 86 87 88 89 90 91 92
  const duration = 2000
  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"
  }
93 94


95 96 97 98 99 100
  export default {
    data() {
      return {
        title: 'request',
        res: '',
        task: null as RequestTask | null,
雪洛's avatar
雪洛 已提交
101
        host: "https://request.dcloud.net.cn",
102
        url: "/api/http/method/get",
103
        method: "GET" as RequestMethod | null,
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
        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",
        ],
        //自动化测试例专用
DCloud-WZF's avatar
DCloud-WZF 已提交
144
        jest_result: false,
145
        jest_result_data: ""
146 147 148 149 150 151 152 153 154
      }
    },
    onLoad() {
    },
    onUnload() {
      uni.hideLoading();
      this.task?.abort();
    },
    methods: {
155
      changeMethod(e : RequestMethod) {
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
        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;
        }
      },
      sendRequest() {
        uni.showLoading({
          title: "请求中..."
        })
        this.task = uni.request({
          url: this.host + this.url,
          // dataType: "json",
          // responseType: "json",
          method: this.method,
          data: this.data,
          header: this.header,
          timeout: 6000,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
202
          success: (res) => {
H
hdx 已提交
203
            console.log('request success', JSON.stringify(res.data))
204
            console.log('request success header is :', JSON.stringify(res.header))
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
205 206 207 208 209 210 211
            uni.showToast({
              title: '请求成功',
              icon: 'success',
              mask: true,
              duration: duration
            });
            this.res = '请求结果 : ' + JSON.stringify(res);
212
          },
213
          fail: (err) => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
214 215 216 217 218
            console.log('request fail', err);
            uni.showModal({
              content: err.errMsg,
              showCancel: false
            });
219
          },
220
          complete: () => {
DCloud-WZF's avatar
DCloud-WZF 已提交
221
            uni.hideLoading()
222
            this.task = null
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
          },
        });
      },
      //自动化测试例专用
      jest_request() {
        uni.request({
          url: this.host + this.url,
          // dataType: "json",
          // responseType: "json",
          method: this.method,
          data: this.data,
          header: this.header,
          timeout: 6000,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
239
          success: () => {
240 241
            this.jest_result = true;
          },
242
          fail: () => {
243 244 245
            this.jest_result = false;
          },
        });
H
hdx 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
      },
      jest_set_cookie() {
        uni.request({
          url: this.host + "/api/http/header/setCookie",
          method: "GET",
          timeout: 6000,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
          success: () => {
            this.jest_cookie_request(true)
          },
          fail: () => {
            this.jest_result = false;
          },
        });
DCloud-WZF's avatar
DCloud-WZF 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
      },
      jest_set_cookie_expires() {
        uni.request({
          url: this.host + "/api/http/header/setCookie?expires=5",
          method: "GET",
          timeout: 6000,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
          success: () => {
            this.jest_cookie_request(true)
          },
          fail: () => {
            this.jest_result = false;
          },
        });
H
hdx 已提交
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
      },
      jest_delete_cookie() {
        uni.request({
          url: this.host + "/api/http/header/deleteCookie",
          method: "GET",
          timeout: 6000,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
          success: () => {
            this.jest_cookie_request(false)
          },
          fail: () => {
            this.jest_result = false;
          },
        });
      },
      jest_cookie_request(needCookie : boolean) {
        uni.request({
          url: this.host + "/api/http/header/requestCookie",
          method: "GET",
          timeout: 6000,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
303 304 305
          success: (res) => {
            const requestCookie = (res.data as UTSJSONObject).getJSON("data")?.getAny("requestCookie")
            this.jest_result_data = JSON.stringify(requestCookie)
H
hdx 已提交
306 307 308 309 310 311 312 313 314 315 316
            if (requestCookie instanceof Array) {
              this.jest_result = needCookie ? requestCookie.length > 0 : requestCookie.length == 0
            } else {
              this.jest_result = needCookie ? (requestCookie as UTSJSONObject).toMap().size > 0 : (requestCookie as UTSJSONObject).toMap().size == 0
            }
          },
          fail: () => {
            this.jest_result = false;
          },
        });
      },
317 318 319 320 321 322 323 324
      jest_timeout_null() {
        uni.request({
          url: this.host + (methodMap['GET'] as string),
          method: "GET",
          timeout: null,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
DCloud-WZF's avatar
DCloud-WZF 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
          success: () => {
            this.jest_result = true;
          },
          fail: () => {
            this.jest_result = false;
          },
        });
      },
      jest_get_with_data() {
        uni.request({
          url: "https://unidemo.dcloud.net.cn/api/banner/36kr",
          method: "GET",
          data: {
            column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
          },
          timeout: 6000,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
          success: () => {
            this.jest_result = true;
          },
          fail: () => {
            this.jest_result = false;
          },
        });
      },
      jest_get_with_generics() {
        uni.request<GETDataType>({
          url: this.host + (methodMap['GET'] as string),
          method: "GET",
          timeout: null,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
          success: (res : RequestSuccess<GETDataType>) => {
            this.jest_result = true;
          },
          fail: () => {
            this.jest_result = false;
          },
        });
      },
      jest_get_array() {
        uni.request<UTSJSONObject[]>({
          url: 'https://unidemo.dcloud.net.cn/api/news?column=title,author_name,cover,published_at',
          method: "GET",
          success: (res : RequestSuccess<UTSJSONObject[]>) => {
            if (res.statusCode == 200 && Array.isArray(res.data)) {
              this.jest_result = true
            } else {
              this.jest_result = false
            }
          },
          fail: () => {
            this.jest_result = false
          }
        });
      },
      jest_uts_module_invoked() {
        // #ifdef APP
        testInovkeRequest({
          success: (res : any) => {
            this.jest_result = true
          },
          fail: (err : any) => {
            this.jest_result = false
          }
        } as CommonOptions)
        // #endif
      },
      jest_respone_json_string() {
        uni.request({
          url: "https://request.dcloud.net.cn/api/http/contentType/text/json",
          success: (res : RequestSuccess<any>) => {
            this.jest_result = typeof res.data == "object"
          },
          fail: (e : RequestFail) => {
            this.jest_result = false
          }
        })
      },
      jest_respone_with_string_generics() {
        uni.request<string>({
          url: this.host + (methodMap['GET'] as string),
          method: "GET",
          timeout: null,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
          success: (res : RequestSuccess<string>) => {
416 417
            this.jest_result = true;
          },
418
          fail: () => {
419 420 421
            this.jest_result = false;
          },
        });
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
      },
      jest_respone_with_404_and_string_generics(){
        uni.request<string>({
          url: this.host + "/api/http/statusCode/404",
          method: "GET",
          timeout: null,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
          success: (res : RequestSuccess<string>) => {
            this.jest_result = true;
          },
          fail: () => {
            this.jest_result = false;
          },
        });
438
      }
439 440
    }
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
441
</script>