unicloud-import-object.uvue 5.4 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
    <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>
11 12 13
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="addTodoWithGeneric">添加Todo传入泛型</button>
        </view>
14 15 16 17 18 19
        <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>
20 21 22
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="failWithNumberErrCode">云对象数字错误码</button>
        </view>
23 24 25 26 27
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="success">云对象成功调用</button>
        </view>
      </view>
    </view>
雪洛's avatar
雪洛 已提交
28
  <!-- #ifdef APP -->
29 30 31 32 33 34 35 36
  </scroll-view>
  <!-- #endif -->
</template>

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

<style>

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