unicloud-import-object.uvue 3.6 KB
Newer Older
1 2 3
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
雪洛's avatar
雪洛 已提交
4
  <!-- #endif -->
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    <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>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="success">云对象成功调用</button>
        </view>
      </view>
    </view>
雪洛's avatar
雪洛 已提交
22
  <!-- #ifdef APP -->
23 24 25 26 27 28 29 30
  </scroll-view>
  <!-- #endif -->
</template>

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

<style>

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