unicloud-import-object.uvue 4.5 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
    onUnload() {
46
      if (this.isUniTest) {
47 48 49
        uni.hideToast()
      }
    },
50
    methods: {
51 52 53 54 55 56 57 58 59 60 61
      notify(content : string, title : string) {
        if (!this.isUniTest) {
          uni.showModal({
            title,
            content,
            showCancel: false
          })
        } else {
          uni.showToast({
            title: content
          })
62
          console.log(title, content)
63 64
        }
      },
65
      async addTodo() : Promise<void> {
66 67 68
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
雪洛's avatar
雪洛 已提交
69 70
        const title = this.todoTitle
        const content = this.todoContent
71
        await todo.add(title, content).then((res : UTSJSONObject) => {
雪洛's avatar
雪洛 已提交
72 73
          this.returnTodoTitle = res['title'] as string
          this.returnTodoContent = res['content'] as string
74
          this.notify(res['showMessage'] as string, '提示')
75
        }).catch((err : any | null) => {
76 77 78 79
          const error = err as UniCloudError
          console.error(error)
        })
      },
80
      async randomFail() : Promise<void> {
81 82 83 84 85
        const todoObj = uniCloud.importObject('todo', {
          errorOptions: {
            retry: true
          }
        })
86
        await todoObj.randomFail().then((res : UTSJSONObject) => {
87
          this.notify(res['showMessage'] as string, '提示')
88
        }).catch((err : any | null) => {
89 90 91 92
          const error = err as UniCloudError
          console.error(error)
        })
      },
雪洛's avatar
雪洛 已提交
93
      async fail() : Promise<void> {
94 95 96
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
97
        await todo.fail().then((res : UTSJSONObject) => {
98
          this.notify('todo.fail应调用失败,此处错误的触发了成功回调', '错误')
99
          console.log('todo.fail: ', res);
100
        }).catch((err : any | null) => {
101
          const error = err as UniCloudError
雪洛's avatar
雪洛 已提交
102
          this.failErrCode = error.errCode as string
103
          console.error(error)
104 105
          if (this.isUniTest) {
            uni.showToast({
106
              title: error.errMsg
107 108
            })
          }
109 110
        })
      },
111 112 113 114 115 116 117 118 119
      async failWithNumberErrCode(){
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
        await todo.fail().then((res : UTSJSONObject) => {
          this.notify('todo.fail应调用失败,此处错误的触发了成功回调', '错误')
          console.log('todo.fail: ', res);
        }).catch((err : any | null) => {
          const error = err as UniCloudError
雪洛's avatar
雪洛 已提交
120
          this.failNumberErrCode = error.errCode as number
121 122 123 124 125 126 127 128
          console.error(error)
          if (this.isUniTest) {
            uni.showToast({
              title: error.errMsg
            })
          }
        })
      },
雪洛's avatar
雪洛 已提交
129
      async success() : Promise<void> {
130 131 132
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
雪洛's avatar
雪洛 已提交
133
        await todo.success().then((res : UTSJSONObject) => {
雪洛's avatar
雪洛 已提交
134
          this.successErrCode = res['errCode'] as number
135
          this.notify(res['showMessage'] as string, '提示')
136
        }).catch((err : any | null) => {
137 138 139 140 141 142 143 144 145 146
          const error = err as UniCloudError
          console.error(error)
        })
      }
    }
  }
</script>

<style>

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