index.uvue 21.5 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
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
<template>
  <scroll-view  style="flex:1">
      <view >
        <button @click="testUtsSync">点击测试uts同步方法</button>
        <view>测试return:
            {{ format(testUtsSyncResult) }}
        </view>
        <button @click="testUtsSyncWithCallback">
            点击测试uts带callback的同步方法
        </button>
        <view>测试return:{{ format(testUtsSyncWithCallbackResult['return']) }}</view>
        <view>测试success:{{ format(testUtsSyncWithCallbackResult['success']) }}</view>
        <view>测试complete:{{ format(testUtsSyncWithCallbackResult['complete']) }}</view>
        <button @click="testUtsAsync">点击测试uts异步方法</button>
        <view>测试return:{{ format(testUtsAsyncResult['return']) }}</view>
        <view>测试success:{{ format(testUtsAsyncResult['success']) }}</view>
        <view>测试complete:{{ format(testUtsAsyncResult['complete']) }}</view>
        <button @click="testUtsAsyncMulitParam">点击测试uts异步方法-多参数</button>
        <view>测试return:{{ format(testUtsAsyncMulitParamResult['return']) }}</view>
        <view>测试success:{{ format(testUtsAsyncMulitParamResult['success']) }}</view>
        <view>测试complete:{{ format(testUtsAsyncMulitParamResult['complete']) }}</view>
        <button @click="testUtsClassConstructor">点击测试uts class构造函数</button>
        <view>测试callback:{{ format(testUtsClassConstructorResult['callback']) }}</view>
        <button @click="testUtsClassStaticProp">点击测试uts class静态属性</button>
        <view>测试value:{{ format(testUtsClassStaticPropResult) }}</view>
        <button @click="testUtsClassStaticSyncWithCallback">
            点击测试uts class静态方法
        </button>
        <view>测试return:{{ format(testUtsClassStaticSyncWithCallbackResult['return']) }}</view>
        <view>测试success:{{ format(testUtsClassStaticSyncWithCallbackResult['success']) }}</view>
        <view>测试complete:{{ format(testUtsClassStaticSyncWithCallbackResult['complete']) }}</view>
        <button @click="testUtsClassStaticAsync">点击测试uts class静态异步方法</button>
        <view>测试return:{{ format(testUtsClassStaticAsyncResult['return']) }}</view>
        <view>测试success:{{ format(testUtsClassStaticAsyncResult['success']) }}</view>
        <view>测试complete:{{ format(testUtsClassStaticAsyncResult['complete']) }}</view>
        <button @click="testUtsClassProp">点击测试uts class实例属性</button>
        <view>测试value:{{ format(testUtsClassPropResult) }}</view>
        <button @click="testUtsClassSyncWithCallback">
            点击测试uts class实例方法
        </button>
        <view>测试return:{{ format(testUtsClassSyncWithCallbackResult['return']) }}</view>
        <view>测试success:{{ format(testUtsClassSyncWithCallbackResult['success']) }}</view>
        <view>测试complete:{{ format(testUtsClassSyncWithCallbackResult['complete']) }}</view>
        <button @click="testUtsClassAsync">点击测试uts class实例异步方法</button>
        <view>测试return:{{ format(testUtsClassAsyncResult['return']) }}</view>
        <view>测试success:{{ format(testUtsClassAsyncResult['success']) }}</view>
        <view>测试complete:{{ format(testUtsClassAsyncResult['complete']) }}</view>
        <button @click="testUtsClassInstance">点击测试uts方法返回类实例</button>
        <view>测试prop:{{ format(testUtsClassInstanceResult['prop']) }}</view>
        <view>测试return:{{ format(testUtsClassInstanceResult['return']) }}</view>
        <view>测试success:{{ format(testUtsClassInstanceResult['success']) }}</view>
        <view>测试complete:{{ format(testUtsClassInstanceResult['complete']) }}</view>
lizhongyi_'s avatar
lizhongyi_ 已提交
53 54
        <view>测试callback:{{ format(testUtsClassInstanceResult['callback']) }}</view>
        <button @click="testUtsClassSetter">点击测试class 示例setter方法</button>
55 56
        <view>测试setter:{{ format(testUtsClassSetterResult) }}</view>
        <view>测试promise返回值:{{ format(testUtsReturnPromiseResult) }}</view>
lizhongyi_'s avatar
lizhongyi_ 已提交
57 58 59 60
        <button @click="testAll">点击测试所有</button>
        <!-- #ifdef APP-IOS -->
        <button @click="testPromiseStaticMethod">点击测试promise静态方法</button>
        <!-- #endif -->
杜庆泉's avatar
杜庆泉 已提交
61 62 63
      </view>
      <view style="height: 20px;"></view>
  </scroll-view>
Y
yurj26 已提交
64
</template>
杜庆泉's avatar
杜庆泉 已提交
65 66 67 68 69 70 71 72 73 74 75 76
<script>
    // #ifndef H5
    import {
        MAX,
        testSync,
        testSyncWithCallback,
        testAsync,
        AsyncOptions,
        TestOptions,
        SyncOptions,
        testAsyncParam3,
        Test,
lizhongyi_'s avatar
lizhongyi_ 已提交
77
        request,
78 79
        SetterTest,
        PromiseReturnDemo,
杜庆泉's avatar
杜庆泉 已提交
80
    } from "../../uni_modules/uts-syntaxcase";
lizhongyi_'s avatar
lizhongyi_ 已提交
81 82 83 84 85
    // #endif
    // #ifdef APP-IOS
    import {
      testPromiseStaticMethod
    } from "@/uni_modules/uts-syntaxcase"
杜庆泉's avatar
杜庆泉 已提交
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
    // #endif
    let test:Test|null = null
    let id = 0
    export default {
        data() {
            return {
                testUtsSyncResult: null,
                testUtsSyncWithCallbackResult: {
                    return: null,
                    success: null,
                    fail: null,
                    complete: null,
                },
                testUtsAsyncResult: {
                    return: null,
                    success: null,
                    fail: null,
                    complete: null,
                },
                testUtsAsyncMulitParamResult: {
                    return: null,
                    success: null,
                    fail: null,
                    complete: null,
                },
                testUtsClassConstructorResult: {
                    callback: null
                },
                testUtsClassStaticPropResult: null,
                testUtsClassStaticSyncWithCallbackResult: {
                    return: null,
                    success: null,
                    fail: null,
                    complete: null,
                },
                testUtsClassStaticAsyncResult: {
                    return: null,
                    success: null,
                    fail: null,
                    complete: null,
                },
                testUtsClassPropResult: null,
                testUtsClassSyncWithCallbackResult: {
                    return: null,
                    success: null,
                    fail: null,
                    complete: null,
                },
                testUtsClassAsyncResult: {
                    return: null,
                    success: null,
                    fail: null,
                    complete: null,
                },
                testUtsClassInstanceResult: {
                    prop: null,
                    return: null,
                    success: null,
                    fail: null,
                    complete: null,
                    callback: null
lizhongyi_'s avatar
lizhongyi_ 已提交
147
                },
148 149
                testUtsClassSetterResult: null,
                testUtsReturnPromiseResult: null,
杜庆泉's avatar
杜庆泉 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
            }
        },
        methods: {
            format(v:any|null):string {
                if(v == null){
                  return '--'
                }
                if(v == true){
                  return "通过"
                }
                return "未通过"
            },
            testAll() {
                this.testUtsSync();
                this.testUtsSyncWithCallback();
                this.testUtsAsync();
                this.testUtsAsyncMulitParam()
                this.testUtsClassConstructor();
                this.testUtsClassStaticProp();
                this.testUtsClassStaticSyncWithCallback();
                this.testUtsClassStaticAsync();
                this.testUtsClassProp();
                this.testUtsClassSyncWithCallback();
                this.testUtsClassAsync();
lizhongyi_'s avatar
lizhongyi_ 已提交
174
                this.testUtsClassInstance();
175 176
                this.testUtsClassSetter();
                this.testUtsReturnPromise();
杜庆泉's avatar
杜庆泉 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
            },
            testUtsSync() {
                this.testUtsSyncResult = false;
                try {
                    if (testSync("dcloud").msg == "hello dcloud") {
                        this.testUtsSyncResult = true;
                    }
                } catch (e) {
                    console.error("testUtsSync", e);
                }
            },
            testUtsSyncWithCallback() {
                try {
                    this.testUtsSyncWithCallbackResult['return'] = false;
                    this.testUtsSyncWithCallbackResult['success'] = false;
                    this.testUtsSyncWithCallbackResult['complete'] = false;
193
                  
杜庆泉's avatar
杜庆泉 已提交
194
                    if (
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
                        testSyncWithCallback({
                            type: "success",
                            success: (res:any) => {
                                console.log("testSyncWithCallback.success.callback", res);
                                this.testUtsSyncWithCallbackResult['success'] = true;
                            },
                            fail: (res:any) => {
                                console.log("testSyncWithCallback.fail.callback", res);
                                // testUtsSyncWithCallbackResult.fail = true;
                            },
                            complete: (res:any) => {
                                console.log("testSyncWithCallback.complete.callback", res);
                                this.testUtsSyncWithCallbackResult['complete'] = true;
                            },
                        } as AsyncOptions)['name'] === "testSyncWithCallback"
                        
杜庆泉's avatar
杜庆泉 已提交
211 212 213 214 215 216 217 218 219 220
                    ) {
                        this.testUtsSyncWithCallbackResult['return'] = true;
                    }
                } catch (e) {}
            },
            async testUtsAsync():Promise<Void> {
                this.testUtsAsyncResult['return'] = false;
                this.testUtsAsyncResult['success'] = false;
                // testUtsAsyncResult.fail = false;
                this.testUtsAsyncResult['complete'] = false;
221
                
杜庆泉's avatar
杜庆泉 已提交
222
                try {
223 224 225 226 227 228 229 230 231 232 233 234 235 236
                    const res = await testAsync({
                      type:"success",
                      success:(res:any) => {
                          console.log("testAsync.success.callback", res);
                          this.testUtsAsyncResult['success'] = true;
                      },
                      fail:(res:any) => {
                          console.log("testAsync.fail.callback", res);
                      },
                      complete:(res:any) => {
                          console.log("testAsync.complete.callback", res);
                          this.testUtsAsyncResult['complete'] = true;
                      },
                    } as AsyncOptions);
杜庆泉's avatar
杜庆泉 已提交
237 238 239 240 241 242 243 244 245 246 247
                    if (res.name === "testAsync") {
                        this.testUtsAsyncResult['return'] = true;
                    }
                } catch (e) {}
                
                return null
            },
            async testUtsAsyncMulitParam():Promise<Void> {
                this.testUtsAsyncMulitParamResult['return'] = false;
                this.testUtsAsyncMulitParamResult['success'] = false;
                this.testUtsAsyncMulitParamResult['complete'] = false;
248
                
杜庆泉's avatar
杜庆泉 已提交
249
                try {
250 251 252 253 254 255 256 257 258 259 260 261 262 263
                    const res = await testAsyncParam3(100,"hello",{
                      type:"success",
                      success:(res) => {
                          console.log("testUtsAsyncMulitParam.success.callback", res);
                          this.testUtsAsyncMulitParamResult['success'] = true;
                      },
                      fail:(res) => {
                          console.log("testUtsAsyncMulitParam.fail.callback", res);
                      },
                      complete:(res) => {
                          console.log("testUtsAsyncMulitParam.complete.callback", res);
                          this.testUtsAsyncMulitParamResult['complete'] = true;
                      },
                    } as AsyncOptions);
杜庆泉's avatar
杜庆泉 已提交
264 265 266 267 268 269 270 271 272 273
                    if (res.name === "testUtsAsyncMulitParam") {
                        this.testUtsAsyncMulitParamResult['return'] = true;
                    }
                } catch (e) {}
                
                return null
            },
            testUtsClassConstructor() {
                this.testUtsClassConstructorResult['callback'] = false
                id++
杜庆泉's avatar
杜庆泉 已提交
274 275 276
                test = new Test(id, {
                  name:'name' + id,
                  callback:(res) => {
杜庆泉's avatar
杜庆泉 已提交
277 278 279
                      console.log(res)
                      this.testUtsClassConstructorResult['callback'] = true
                  }
杜庆泉's avatar
杜庆泉 已提交
280
                } as TestOptions)
杜庆泉's avatar
杜庆泉 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293
            },
            testUtsClassStaticProp() {
                this.testUtsClassStaticPropResult = false
                if (Test.type === 'Test') {
                    this.testUtsClassStaticPropResult = true
                }
            },
            testUtsClassStaticSyncWithCallback() {
                try {
                    this.testUtsClassStaticSyncWithCallbackResult['return'] = false;
                    this.testUtsClassStaticSyncWithCallbackResult['success'] = false;
                    this.testUtsClassStaticSyncWithCallbackResult['complete'] = false;
                    if (
294 295 296 297 298 299 300 301 302 303 304 305 306 307
                        Test.testClassStaticSyncWithCallback({
                          type:"success",
                          success:(res) => {
                              console.log("testStaticSyncWithCallback.success.callback", res);
                              this.testUtsClassStaticSyncWithCallbackResult['success'] = true;
                          },
                          fail:(res) => {
                              console.log("testStaticSyncWithCallback.fail.callback", res);
                          },
                          complete:(res) => {
                              console.log("testStaticSyncWithCallback.complete.callback", res);
                              this.testUtsClassStaticSyncWithCallbackResult['complete'] = true;
                          },
                        } as AsyncOptions)['name'] === "testSyncWithCallback"
杜庆泉's avatar
杜庆泉 已提交
308 309 310 311 312 313 314 315 316
                    ) {
                        this.testUtsClassStaticSyncWithCallbackResult['return'] = true;
                    }
                } catch (e) {}
            },
            async testUtsClassStaticAsync():Promise<Void> {
                this.testUtsClassStaticAsyncResult['return'] = false;
                this.testUtsClassStaticAsyncResult['success'] = false;
                this.testUtsClassStaticAsyncResult['complete'] = false;
317
                
杜庆泉's avatar
杜庆泉 已提交
318
                try {
319 320 321 322 323 324 325 326 327 328 329 330 331 332
                    const res = await Test.testClassStaticAsync({
                      type:"success",
                      success:(res:any) => {
                          console.log("testAsync.success.callback", res);
                          this.testUtsClassStaticAsyncResult['success'] = true;
                      },
                      fail:(res:any) => {
                          console.log("testAsync.fail.callback", res);
                      },
                      complete:(res:any) => {
                          console.log("testAsync.complete.callback", res);
                          this.testUtsClassStaticAsyncResult['complete'] = true;
                      },
                    } as AsyncOptions);
杜庆泉's avatar
杜庆泉 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
                    if (res.name === "testAsync") {
                        this.testUtsClassStaticAsyncResult['return'] = true;
                    }
                } catch (e) {}
                
                return null
            },
            testUtsClassProp() {
                if (test == null) {
                    this.testUtsClassConstructor()
                }
                this.testUtsClassPropResult = false
                if (test!.id > 0) {
                    this.testUtsClassPropResult = true
                }
            },
            testUtsClassSyncWithCallback() {
                if (test == null) {
                    this.testUtsClassConstructor()
                }
                try {
                    this.testUtsClassSyncWithCallbackResult['return'] = false;
                    this.testUtsClassSyncWithCallbackResult['success'] = false;
                    this.testUtsClassSyncWithCallbackResult['complete'] = false;
杜庆泉's avatar
杜庆泉 已提交
357
                    
杜庆泉's avatar
杜庆泉 已提交
358
                    if (
杜庆泉's avatar
杜庆泉 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
                        test!.testClassSyncWithCallback({
                          type:"success",
                          success:(res) => {
                              console.log("testSyncWithCallback.success.callback", res);
                              this.testUtsClassSyncWithCallbackResult['success'] = true;
                          },
                          fail:(res) => {
                              console.log("testSyncWithCallback.fail.callback", res);
                              // testUtsClassSyncWithCallbackResult.fail = true;
                          },
                          complete:(res) => {
                              console.log("testSyncWithCallback.complete.callback", res);
                              this.testUtsClassSyncWithCallbackResult['complete'] = true;
                          }
                        } as AsyncOptions).name === "testSyncWithCallback"
杜庆泉's avatar
杜庆泉 已提交
374 375 376 377 378 379 380 381 382 383 384 385
                    ) {
                        this.testUtsClassSyncWithCallbackResult['return'] = true;
                    }
                } catch (e) {}
            },
            async testUtsClassAsync():Promise<Void> {
                if (test == null) {
                    this.testUtsClassConstructor()
                }
                this.testUtsClassAsyncResult['return'] = false;
                this.testUtsClassAsyncResult['success'] = false;
                this.testUtsClassAsyncResult['complete'] = false;
386
                
杜庆泉's avatar
杜庆泉 已提交
387
                try {
388 389 390 391 392 393 394 395 396 397 398 399 400 401
                    const res = await test!.testClassAsync({
                      type:"success",
                      success:(res) => {
                          console.log("testAsync.success.callback", res);
                          this.testUtsClassAsyncResult['success'] = true;
                      },
                      fail:(res) => {
                          console.log("testAsync.fail.callback", res);
                      },
                      complete:(res) => {
                          console.log("testAsync.complete.callback", res);
                          this.testUtsClassAsyncResult['complete'] = true;
                      },
                    } as AsyncOptions);
杜庆泉's avatar
杜庆泉 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
                    if (res.name === "testAsync") {
                        this.testUtsClassAsyncResult['return'] = true;
                    }
                } catch (e) {
                    console.error(e)
                }
                
                return null
            },
            testUtsClassInstance() {
                this.testUtsClassInstanceResult['prop'] = false;
                this.testUtsClassInstanceResult['return'] = false;
                this.testUtsClassInstanceResult['success'] = false;
                // testUtsClassAsyncResult.fail = false;
                this.testUtsClassInstanceResult['complete'] = false;
                this.testUtsClassInstanceResult['callback'] = false;
                const url = 'https://dcloud.io/'
                const task = request(url)
                if (task!.url === url && task!.abort().url === url) {
                    this.testUtsClassInstanceResult['prop'] = true;
                }
                task!.onCallback((res) => {
                    if (res === 'onCallback') {
                        this.testUtsClassInstanceResult['callback'] = true;
                    }
                })
428 429 430
                
                const res = task.sync({ 
                  success:(res) => {
杜庆泉's avatar
杜庆泉 已提交
431 432 433
                      console.log("task.sync.success.callback", res);
                      this.testUtsClassInstanceResult['success'] = true;
                  },
434
                  fail:(res) => {
杜庆泉's avatar
杜庆泉 已提交
435 436
                      console.log("task.sync.fail.callback", res);
                  },
437
                  complete:(res) => {
杜庆泉's avatar
杜庆泉 已提交
438 439
                      console.log("task.sync.complete.callback", res);
                      this.testUtsClassInstanceResult['complete'] = true;
440
                  }} as SyncOptions)
杜庆泉's avatar
杜庆泉 已提交
441 442 443
                if (res === 'sync') {
                    this.testUtsClassInstanceResult['return'] = true;
                }
lizhongyi_'s avatar
lizhongyi_ 已提交
444 445 446 447 448 449 450 451 452 453 454 455
            },
            testUtsClassSetter() {
              this.testUtsClassSetterResult = false;
              try {
                let obj = new SetterTest()
                obj.nickName = "Tom";
                if (obj.nickName == "Tom") {  
                    this.testUtsClassSetterResult = true;
                }
              } catch (e) {
                    console.error("testUtsClassSetter", e);
              }
lizhongyi_'s avatar
lizhongyi_ 已提交
456
            },
457 458 459 460 461 462 463 464 465 466 467 468 469
            testUtsReturnPromise() {
              this.testUtsReturnPromiseResult = false;
              try {
                let demo = new PromiseReturnDemo();
                demo.test1();
                demo.test2();
                if (demo.recordRet == 2) {
                    this.testUtsReturnPromiseResult = true;
                }
              } catch (e) {
                    console.error("testUtsClassSetter", e);
              }
            },
lizhongyi_'s avatar
lizhongyi_ 已提交
470 471 472 473 474
            // #ifdef APP-IOS
            testPromiseStaticMethod() {
              testPromiseStaticMethod()
            }
            // #endif
杜庆泉's avatar
杜庆泉 已提交
475 476
        }
    }
477
</script>