unicloud-call-function.uvue 1.6 KB
Newer Older
雪洛's avatar
雪洛 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
    <!-- #endif -->
    <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" @click="callFunction">请求云函数</button>
        </view>
      </view>
    </view>
    <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>
<script>
  export default {
    data() {
      return {
雪洛's avatar
雪洛 已提交
21 22 23
        title: '请求云函数',
        callFunctionResult: {},
        callFunctionError: {}
雪洛's avatar
雪洛 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
      }
    },
    onLoad() {
    },
    onUnload() {
    },
    methods: {
      callFunction: function () {
        uni.showLoading({
          title: '加载中...'
        })
        uniCloud.callFunction({
          name: 'echo-cf',
          data: {
            num: 1,
            str: 'ABC'
雪洛's avatar
雪洛 已提交
40
          }
雪洛's avatar
雪洛 已提交
41 42
        }).then<void>(res => {
          const result = res.result
雪洛's avatar
雪洛 已提交
43
          this.callFunctionResult = result
雪洛's avatar
雪洛 已提交
44 45 46 47 48 49 50
          console.log(JSON.stringify(result))
          uni.hideLoading()
          uni.showModal({
            title: '提示',
            content: result['showMessage'] as string,
            showCancel: false
          })
雪洛's avatar
雪洛 已提交
51
        }).catch<void>((err: any | null) => {
雪洛's avatar
雪洛 已提交
52
          const error = err as UniCloudError
雪洛's avatar
雪洛 已提交
53 54 55 56
          this.callFunctionError = {
            errCode: error.errCode,
            errMsg: error.errMsg
          }
雪洛's avatar
雪洛 已提交
57 58 59
          uni.hideLoading()
          uni.showModal({
            title: '错误',
雪洛's avatar
雪洛 已提交
60
            content: error.errMsg,
雪洛's avatar
雪洛 已提交
61 62 63 64 65 66 67 68 69 70
            showCancel: false
          })
        })
      }
    }
  }
</script>

<style>
</style>