index.uvue 15.8 KB
Newer Older
Y
yurj26 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3 4
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
    <!-- #endif -->
Y
yurj26 已提交
5
    <view>
DCloud-WZF's avatar
DCloud-WZF 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
      <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>
Y
yurj26 已提交
26 27 28
        <view>测试return:{{ format(testUtsAsyncResult.return) }}</view>
        <view>测试success:{{ format(testUtsAsyncResult.success) }}</view>
        <view>测试complete:{{ format(testUtsAsyncResult.complete) }}</view> -->
DCloud-WZF's avatar
DCloud-WZF 已提交
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
      <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>
Y
yurj26 已提交
58 59 60
        <view>测试return:{{ format(testUtsClassStaticAsyncResult.return) }}</view>
        <view>测试success:{{ format(testUtsClassStaticAsyncResult.success) }}</view>
        <view>测试complete:{{ format(testUtsClassStaticAsyncResult.complete) }}</view> -->
DCloud-WZF's avatar
DCloud-WZF 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      <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>
Y
yurj26 已提交
82 83 84
        <view>测试return:{{ format(testUtsClassAsyncResult.return) }}</view>
        <view>测试success:{{ format(testUtsClassAsyncResult.success) }}</view>
        <view>测试complete:{{ format(testUtsClassAsyncResult.complete) }}</view> -->
DCloud-WZF's avatar
DCloud-WZF 已提交
85 86 87 88 89 90 91 92 93 94 95
      <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
      >
      <view
        >测试callback:{{ format(testUtsClassInstanceResult.callback) }}</view
      >
      <button @click="testAll">点击测试所有</button>
Y
yurj26 已提交
96
    </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
97 98 99
    <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
Y
yurj26 已提交
100 101
</template>
<script lang="ts">
DCloud-WZF's avatar
DCloud-WZF 已提交
102 103 104 105 106 107 108 109 110 111 112
import {
  MAX,
  testSync,
  testSyncWithCallback,
  testAsync,
  Test,
  request,
  AsyncOptions,
  TestOptions,
  SyncOptions,
} from '../../uni_modules/uts-syntaxcase'
Y
yurj26 已提交
113

DCloud-WZF's avatar
DCloud-WZF 已提交
114 115 116
const TRUE = 'true'
const FALSE = 'false'
const NULL = 'null'
Y
yurj26 已提交
117

DCloud-WZF's avatar
DCloud-WZF 已提交
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
let test: Test | null = null
let id = 0

type TestResult = {
  return?: string
  success?: string
  fail?: string
  complete?: string
  callback?: string
  prop?: string
}

export default {
  data() {
    return {
      // 'null' | 'true' | 'false'
      testUtsSyncResult: 'null',
      testUtsSyncWithCallbackResult: {
        return: 'null',
        success: 'null',
        fail: 'null',
        complete: 'null',
      } as TestResult,
      testUtsAsyncResult: {
        return: 'null',
        success: 'null',
        fail: 'null',
        complete: 'null',
      } as TestResult,
      testUtsClassConstructorResult: {
        callback: 'null',
      } as TestResult,
      testUtsClassStaticPropResult: 'null',
      testUtsClassStaticSyncWithCallbackResult: {
        return: 'null',
        success: 'null',
        fail: 'null',
        complete: 'null',
      } as TestResult,
      testUtsClassStaticAsyncResult: {
        return: 'null',
        success: 'null',
        fail: 'null',
        complete: 'null',
      } as TestResult,
      testUtsClassPropResult: 'null',
      testUtsClassSyncWithCallbackResult: {
        return: 'null',
        success: 'null',
        fail: 'null',
        complete: 'null',
      } as TestResult,
      testUtsClassAsyncResult: {
        return: 'null',
        success: 'null',
        fail: 'null',
        complete: 'null',
      } as TestResult,
      testUtsClassInstanceResult: {
        prop: 'null',
        return: 'null',
        success: 'null',
        fail: 'null',
        complete: 'null',
        callback: 'null',
      } as TestResult,
Y
yurj26 已提交
184
    }
DCloud-WZF's avatar
DCloud-WZF 已提交
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
  },
  methods: {
    format(v: string | null): string {
      return v === 'null' || v == null ? '--' : v === 'true' ? '通过' : '未通过'
    },
    testAll() {
      this.testUtsSync()
      this.testUtsSyncWithCallback()
      // this.testUtsAsync();
      this.testUtsClassConstructor()
      this.testUtsClassStaticProp()
      this.testUtsClassStaticSyncWithCallback()
      // this.testUtsClassStaticAsync();
      this.testUtsClassProp()
      this.testUtsClassSyncWithCallback()
      // this.testUtsClassAsync();
      this.testUtsClassInstance()
    },
    testUtsSync() {
      this.testUtsSyncResult = FALSE
      try {
        console.log('testSync("dcloud").msg', testSync('dcloud').msg)
        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
        // testUtsSyncWithCallbackResult.fail = FALSE;
        this.testUtsSyncWithCallbackResult.complete = FALSE
        if (
          testSyncWithCallback({
            type: 'success',
            success: (res) => {
              console.log('testSyncWithCallback.success.callback', res)
              this.testUtsSyncWithCallbackResult.success = TRUE
Y
yurj26 已提交
226
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
227 228 229
            fail: (res) => {
              console.log('testSyncWithCallback.fail.callback', res)
              // testUtsSyncWithCallbackResult.fail = TRUE;
Y
yurj26 已提交
230
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
231 232 233
            complete: (res) => {
              console.log('testSyncWithCallback.complete.callback', res)
              this.testUtsSyncWithCallbackResult.complete = TRUE
Y
yurj26 已提交
234
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
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
          } as AsyncOptions).name === 'testSyncWithCallback'
        ) {
          this.testUtsSyncWithCallbackResult.return = TRUE
        }
      } catch (e) {}
    },
    // TODO uvue 暂不支持 async await
    // async testUtsAsync() {
    //     this.testUtsAsyncResult.return = FALSE;
    //     this.testUtsAsyncResult.success = FALSE;
    //     // testUtsAsyncResult.fail = FALSE;
    //     this.testUtsAsyncResult.complete = FALSE;
    //     try {
    //         const res = await testAsync({
    //             type: "success",
    //             success: (res) => {
    //                 console.log("testAsync.success.callback", res);
    //                 this.testUtsAsyncResult.success = TRUE;
    //             },
    //             fail: (res) => {
    //                 console.log("testAsync.fail.callback", res);
    //             },
    //             complete: (res) => {
    //                 console.log("testAsync.complete.callback", res);
    //                 this.testUtsAsyncResult.complete = TRUE;
    //             },
    //         } as AsyncOptions);
    //         if (res.name === "testAsync") {
    //             this.testUtsAsyncResult.return = TRUE;
    //         }
    //     } catch (e) {}
    // },
    testUtsClassConstructor() {
      this.testUtsClassConstructorResult.callback = FALSE
      id++
      test = new Test(id, {
        name: 'name' + id,
        callback: (res) => {
          console.log(res)
          this.testUtsClassConstructorResult.callback = TRUE
        },
      } as TestOptions)
    },
    testUtsClassStaticProp() {
      this.testUtsClassStaticPropResult = FALSE
      if (Test.type === 'Test') {
        this.testUtsClassStaticPropResult = TRUE
      }
    },
    testUtsClassStaticSyncWithCallback() {
      try {
        this.testUtsClassStaticSyncWithCallbackResult.return = FALSE
        this.testUtsClassStaticSyncWithCallbackResult.success = FALSE
        // testUtsClassStaticSyncWithCallbackResult.fail = FALSE;
        this.testUtsClassStaticSyncWithCallbackResult.complete = FALSE
        if (
          Test.testClassStaticSyncWithCallback({
            type: 'success',
            success: (res) => {
              console.log('testStaticSyncWithCallback.success.callback', res)
              this.testUtsClassStaticSyncWithCallbackResult.success = TRUE
Y
yurj26 已提交
296
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
297 298 299
            fail: (res) => {
              console.log('testStaticSyncWithCallback.fail.callback', res)
              // testUtsClassStaticSyncWithCallbackResult.fail = TRUE;
Y
yurj26 已提交
300
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
301 302 303
            complete: (res) => {
              console.log('testStaticSyncWithCallback.complete.callback', res)
              this.testUtsClassStaticSyncWithCallbackResult.complete = TRUE
Y
yurj26 已提交
304
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
          } as AsyncOptions).name === 'testSyncWithCallback'
        ) {
          this.testUtsClassStaticSyncWithCallbackResult.return = TRUE
        }
      } catch (e) {}
    },

    // async testUtsClassStaticAsync() {
    //     this.testUtsClassStaticAsyncResult.return = FALSE;
    //     this.testUtsClassStaticAsyncResult.success = FALSE;
    //     // testUtsClassStaticAsyncResult.fail = FALSE;
    //     this.testUtsClassStaticAsyncResult.complete = FALSE;
    //     try {
    //         const res = await Test.testClassStaticAsync({
    //             type: "success",
    //             success: (res) => {
    //                 console.log("testAsync.success.callback", res);
    //                 this.testUtsClassStaticAsyncResult.success = TRUE;
    //             },
    //             fail: (res) => {
    //                 console.log("testAsync.fail.callback", res);
    //             },
    //             complete: (res) => {
    //                 console.log("testAsync.complete.callback", res);
    //                 this.testUtsClassStaticAsyncResult.complete = TRUE;
    //             },
    //         });
    //         if (res.name === "testAsync") {
    //             this.testUtsClassStaticAsyncResult.return = TRUE;
    //         }
    //     } catch (e) {}
    // },
    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
        // testUtsClassSyncWithCallbackResult.fail = FALSE;
        this.testUtsClassSyncWithCallbackResult.complete = FALSE
        if (
          test!.testClassSyncWithCallback({
            type: 'success',
            success: (res) => {
              console.log('testSyncWithCallback.success.callback', res)
              this.testUtsClassSyncWithCallbackResult.success = TRUE
Y
yurj26 已提交
361
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
362 363 364
            fail: (res) => {
              console.log('testSyncWithCallback.fail.callback', res)
              // testUtsClassSyncWithCallbackResult.fail = TRUE;
Y
yurj26 已提交
365
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
366 367 368
            complete: (res) => {
              console.log('testSyncWithCallback.complete.callback', res)
              this.testUtsClassSyncWithCallbackResult.complete = TRUE
Y
yurj26 已提交
369
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
370 371 372
          } as AsyncOptions).name === 'testSyncWithCallback'
        ) {
          this.testUtsClassSyncWithCallbackResult.return = TRUE
Y
yurj26 已提交
373
        }
DCloud-WZF's avatar
DCloud-WZF 已提交
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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
      } catch (e) {}
    },
    // async testUtsClassAsync() {
    //     if (test != null) {
    //         this.testUtsClassConstructor()
    //     }
    //     this.testUtsClassAsyncResult.return = FALSE;
    //     this.testUtsClassAsyncResult.success = FALSE;
    //     // testUtsClassAsyncResult.fail = FALSE;
    //     this.testUtsClassAsyncResult.complete = FALSE;
    //     try {
    //         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;
    //             },
    //         });
    //         console.log('res', res)
    //         if (res.name === "testAsync") {
    //             this.testUtsClassAsyncResult.return = TRUE;
    //         }
    //     } catch (e) {
    //         console.error(e)
    //     }
    // },
    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 != null) {
        if (task.url === url && task.abort().url === url) {
          this.testUtsClassInstanceResult.prop = TRUE
        }
        task.onCallback((res) => {
          if (res === 'onCallback') {
            this.testUtsClassInstanceResult.callback = TRUE
          }
        })
        const res = task.sync({
          success: (res) => {
            console.log('task.sync.success.callback', res)
            this.testUtsClassInstanceResult.success = TRUE
          },
          fail: (res) => {
            console.log('task.sync.fail.callback', res)
          },
          complete: (res) => {
            console.log('task.sync.complete.callback', res)
            this.testUtsClassInstanceResult.complete = TRUE
          },
        } as SyncOptions)
        if (res === 'sync') {
          this.testUtsClassInstanceResult.return = TRUE
        }
      }
    },
  },
}
</script>