add.uvue 3.3 KB
Newer Older
1 2
<template>
  <view class="page">
3 4
    <unicloud-db ref="udb" v-slot:default="{error}" :collection="collection" loadtime="manual">
      <view v-if="error!=null" class="error">{{error.errMsg}}</view>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    </unicloud-db>
    <view class="form-item">
      <text class="form-item-label">姓名</text>
      <input class="form-item-input" placeholder="姓名" v-model="username" />
    </view>
    <view class="form-item">
      <text class="form-item-label">电话</text>
      <input class="form-item-input" placeholder="电话" v-model="mobile" />
    </view>
    <view class="form-item">
      <text class="form-item-label">邮箱</text>
      <input class="form-item-input" placeholder="邮箱地址" v-model="email" />
    </view>
    <view class="form-item">
      <text class="form-item-label">备注</text>
      <textarea class="form-item-input" placeholder="备注" v-model="comment" />
    </view>
    <view class="form-item">
      <text class="form-item-label">性别</text>
      <radio-group class="radio-list" @change="radioChange">
        <view class="radio-item" v-for="(item, _) in genderList" :key="item.value">
          <radio :value="item.value" :checked="item.value == gender" />
          <text>{{item.text}}</text>
        </view>
      </radio-group>
    </view>
31
    <button class="btn-add" type="primary" @click="submit">保存</button>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  </view>
</template>

<script>
  import { COLLECTION_NAME, GenderType, GenderList, UNICLOUD_DB_CONTACTS_ADD } from './types.uts'

  export default {
    data() {
      return {
        collection: COLLECTION_NAME,
        username: "",
        mobile: "",
        gender: "",
        comment: "",
        email: "",
        genderList: GenderList as GenderType[],
        $uniCloudElement: null as UniCloudDBElement | null
      }
    },
    onReady() {
      this.$uniCloudElement = this.$refs['udb'] as UniCloudDBElement
    },
    methods: {
      radioChange(e : RadioGroupChangeEvent) {
        this.gender = e.detail.value
      },
      submit() {
        const value = {
          username: this.username,
          gender: parseInt(this.gender),
          mobile: this.mobile,
          comment: this.comment,
          email: this.email,
        };

        this.$uniCloudElement!.add(value, {
          showToast: false,
          needLoading: true,
          loadingTitle: "正在保存...",
          success: (_ : UniCloudDBAddResult) => {
            // TODO 后续通过 EventChannel 实现
            uni.$emit(UNICLOUD_DB_CONTACTS_ADD, '')
            setTimeout(() => {
              uni.navigateBack()
            }, 500)
          },
          fail: (err : any | null) => {
            const error = err as UniCloudError
            uni.showModal({
              content: error.errMsg,
              showCancel: false
            })
          }
        })
      }
    }
  }
</script>

<style>
  .page {
    padding: 15px;
  }

  .form-item {
    flex-direction: row;
    margin-bottom: 15px;
    align-items: center;
  }

  .form-item-label {
    width: 45px;
    margin-right: 10px;
  }

  .form-item-input {
    flex: 1;
    font-size: 14px;
    color: #666;
    border: 1px #e5e5e5 solid;
    border-radius: 5px;
    padding: 8px;
  }

  .radio-list {
    flex-direction: row;
  }

  .radio-item {
    flex-direction: row;
    margin-right: 30px;
    align-items: center;
  }

  .btn-add {
    margin-top: 50px;
  }
129
</style>