unicloud-import-object.uvue 4.2 KB
Newer Older
1 2
<template>
  <!-- #ifdef APP -->
H
hdx 已提交
3
  <scroll-view class="page-scroll-view">
雪洛's avatar
雪洛 已提交
4
  <!-- #endif -->
5 6 7 8 9 10 11 12 13 14 15 16
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="addTodo">添加Todo</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="randomFail">随机触发失败重试</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="fail">云对象失败调用</button>
        </view>
17 18 19
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="failWithNumberErrCode">云对象数字错误码</button>
        </view>
20 21 22 23 24
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="success">云对象成功调用</button>
        </view>
      </view>
    </view>
雪洛's avatar
雪洛 已提交
25
  <!-- #ifdef APP -->
26 27 28 29 30 31 32 33
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
雪洛's avatar
雪洛 已提交
34 35 36 37 38 39
        title: '请求云对象',
        todoTitle: '学习编程',
        todoContent: '熟悉uts语法',
        returnTodoTitle: '',
        returnTodoContent: '',
        failErrCode: '',
雪洛's avatar
雪洛 已提交
40
        failNumberErrCode: 0,
41 42
        successErrCode: -1,
        isUniTest: false
43 44 45
      }
    },
    methods: {
46 47 48 49 50 51 52 53
      notify(content : string, title : string) {
        if (!this.isUniTest) {
          uni.showModal({
            title,
            content,
            showCancel: false
          })
        } else {
54
          console.log(title, content)
55 56
        }
      },
57
      async addTodo() : Promise<void> {
58 59 60
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
雪洛's avatar
雪洛 已提交
61 62
        const title = this.todoTitle
        const content = this.todoContent
63
        await todo.add(title, content).then((res : UTSJSONObject) => {
雪洛's avatar
雪洛 已提交
64 65
          this.returnTodoTitle = res['title'] as string
          this.returnTodoContent = res['content'] as string
66
          this.notify(res['showMessage'] as string, '提示')
67
        }).catch((err : any | null) => {
68 69 70 71
          const error = err as UniCloudError
          console.error(error)
        })
      },
72
      async randomFail() : Promise<void> {
73 74 75 76 77
        const todoObj = uniCloud.importObject('todo', {
          errorOptions: {
            retry: true
          }
        })
78
        await todoObj.randomFail().then((res : UTSJSONObject) => {
79
          this.notify(res['showMessage'] as string, '提示')
80
        }).catch((err : any | null) => {
81 82 83 84
          const error = err as UniCloudError
          console.error(error)
        })
      },
雪洛's avatar
雪洛 已提交
85
      async fail() : Promise<void> {
86 87 88
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
89
        await todo.fail().then((res : UTSJSONObject) => {
90
          this.notify('todo.fail应调用失败,此处错误的触发了成功回调', '错误')
91
          console.log('todo.fail: ', res);
92
        }).catch((err : any | null) => {
93
          const error = err as UniCloudError
雪洛's avatar
雪洛 已提交
94
          this.failErrCode = error.errCode as string
95 96 97
          console.error(error)
        })
      },
98
      async failWithNumberErrCode() : Promise<void> {
99 100 101
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
102
        await todo.failWithNumberErrCode().then((res : UTSJSONObject) => {
103 104 105 106
          this.notify('todo.fail应调用失败,此处错误的触发了成功回调', '错误')
          console.log('todo.fail: ', res);
        }).catch((err : any | null) => {
          const error = err as UniCloudError
雪洛's avatar
雪洛 已提交
107
          this.failNumberErrCode = error.errCode as number
108 109 110
          console.error(error)
        })
      },
雪洛's avatar
雪洛 已提交
111
      async success() : Promise<void> {
112 113 114
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
雪洛's avatar
雪洛 已提交
115
        await todo.success().then((res : UTSJSONObject) => {
雪洛's avatar
雪洛 已提交
116
          this.successErrCode = res['errCode'] as number
117
          this.notify(res['showMessage'] as string, '提示')
118
        }).catch((err : any | null) => {
119 120 121 122 123 124 125 126 127 128
          const error = err as UniCloudError
          console.error(error)
        })
      }
    }
  }
</script>

<style>

雪洛's avatar
雪洛 已提交
129
</style>