edit.vue 6.9 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<template>
  <view class="uni-container">
    <uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast">
      <uni-forms-item name="username" label="真实姓名">
        <uni-easyinput placeholder="限制只能输入中文" v-model="formData.username"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="gender" label="性别">
        <uni-data-checkbox v-model="formData.gender" :localdata="formOptions.gender_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="birth_date" label="生日">
        <uni-datetime-picker return-type="timestamp" v-model="formData.birth_date"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="weight" label="体重">
        <uni-easyinput placeholder="限输入 >50 && <=500" type="number" v-model="formData.weight"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="mobile" label="手机号码">
        <uni-easyinput placeholder="手机号码" v-model="formData.mobile"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="email" label="邮箱账号">
        <uni-easyinput placeholder="请输入你的邮箱账号" v-model="formData.email"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="url" label="个人博客">
        <uni-easyinput placeholder="请输入网址的地址" v-model="formData.url"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="favorite_book_id" label="喜欢的书">
Anne_LXM's avatar
Anne_LXM 已提交
26 27
        <uni-data-checkbox v-model="formData.favorite_book_id" collection="book" field="title as text, _id as value"
          orderby="desc"></uni-data-checkbox>
study夏羽's avatar
study夏羽 已提交
28 29
      </uni-forms-item>
      <uni-forms-item name="address_code" label="地址">
Anne_LXM's avatar
Anne_LXM 已提交
30 31
        <uni-data-picker self-field="code" parent-field="parent_code" v-model="formData.address_code"
          collection="opendb-city-china" orderby="value asc" field="code as value, name as text"></uni-data-picker>
study夏羽's avatar
study夏羽 已提交
32 33 34 35 36
      </uni-forms-item>
      <uni-forms-item name="party_member" label="是否为党员">
        <switch @change="binddata('party_member', $event.detail.value)" :checked="formData.party_member"></switch>
      </uni-forms-item>
      <uni-forms-item name="hobby" label="业余爱好">
Anne_LXM's avatar
Anne_LXM 已提交
37 38
        <uni-data-checkbox multiple="true" :multiple="true" v-model="formData.hobby"
          :localdata="formOptions.hobby_localdata"></uni-data-checkbox>
study夏羽's avatar
study夏羽 已提交
39 40
      </uni-forms-item>
      <uni-forms-item name="comment" label="备注">
Anne_LXM's avatar
Anne_LXM 已提交
41 42
        <textarea placeholder="拒绝违禁词,如:test" @input="binddata('comment', $event.detail.value)"
          class="uni-textarea-border" v-model="formData.comment"></textarea>
study夏羽's avatar
study夏羽 已提交
43 44 45 46 47 48 49 50 51
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" @click="submit">提交</button>
      </view>
    </uni-forms>
  </view>
</template>

<script>
Anne_LXM's avatar
Anne_LXM 已提交
52 53 54
  import {
    validator
  } from '../../js_sdk/validator/user-info.js';
study夏羽's avatar
study夏羽 已提交
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

  const db = uniCloud.database();
  const dbCollectionName = 'user-info';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.indexOf(key) > -1) {
        result[key] = validator[key]
      }
    }
    return result
  }

  export default {
    data() {
      let formData = {
        "username": "",
        "gender": 0,
        "birth_date": null,
        "weight": null,
        "mobile": "",
        "email": "",
        "url": "",
        "favorite_book_id": "",
        "address_code": "",
        "party_member": null,
        "hobby": [],
        "comment": ""
      }
      return {
Anne_LXM's avatar
Anne_LXM 已提交
86 87
        //自动化测试
        isTest: false,
study夏羽's avatar
study夏羽 已提交
88 89
        formData,
        formOptions: {
Anne_LXM's avatar
Anne_LXM 已提交
90
          "gender_localdata": [{
study夏羽's avatar
study夏羽 已提交
91 92 93 94 95 96 97 98 99 100 101 102
              "text": "未知",
              "value": 0
            },
            {
              "text": "",
              "value": 1
            },
            {
              "text": "",
              "value": 2
            }
          ],
Anne_LXM's avatar
Anne_LXM 已提交
103
          "hobby_localdata": [{
study夏羽's avatar
study夏羽 已提交
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 129 130 131 132 133 134 135 136 137 138 139
              "text": "唱歌",
              "value": "Sing"
            },
            {
              "text": "跳舞",
              "value": "dance"
            },
            {
              "text": "画画",
              "value": "draw"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      /**
       * 触发表单提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
Anne_LXM's avatar
Anne_LXM 已提交
140
        return this.$refs.form.validate().then(async (res) => {
A
Anne_LXM 已提交
141
          return await this.submitForm(res)
study夏羽's avatar
study夏羽 已提交
142 143 144 145 146
        }).catch(() => {
          uni.hideLoading()
        })
      },

A
Anne_LXM 已提交
147
      async submitForm(value) {
study夏羽's avatar
study夏羽 已提交
148
        // 使用 clientDB 提交数据
A
Anne_LXM 已提交
149
        return await db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
study夏羽's avatar
study夏羽 已提交
150 151 152 153
          uni.showToast({
            icon: 'none',
            title: '修改成功'
          })
Anne_LXM's avatar
Anne_LXM 已提交
154 155 156 157 158 159
          if (!this.isTest) {
            this.getOpenerEventChannel().emit('refreshData')
            setTimeout(() => uni.navigateBack(), 500)
          }
          console.log('res: ', res);
          return res.result
study夏羽's avatar
study夏羽 已提交
160 161 162 163 164
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
Anne_LXM's avatar
Anne_LXM 已提交
165
          return err
study夏羽's avatar
study夏羽 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
Anne_LXM's avatar
Anne_LXM 已提交
179 180 181
        db.collection(dbCollectionName).doc(id).field(
          "username,gender,birth_date,weight,mobile,email,url,favorite_book_id,address_code,party_member,hobby,comment"
        ).get().then((res) => {
study夏羽's avatar
study夏羽 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
          const data = res.result.data[0]
          if (data) {
            this.formData = data
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>

<style>
  .uni-container {
    padding: 15px;
  }

  .uni-input-border,
  .uni-textarea-border {
    width: 100%;
    font-size: 14px;
    color: #666;
    border: 1px #e5e5e5 solid;
    border-radius: 5px;
    box-sizing: border-box;
  }

  .uni-input-border {
    padding: 0 10px;
    height: 35px;

  }

  .uni-textarea-border {
    padding: 10px;
    height: 80px;
  }

  .uni-button-group {
    margin-top: 50px;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    justify-content: center;
  }

  .uni-button {
    width: 184px;
    padding: 12px 20px;
    font-size: 14px;
    border-radius: 4px;
    line-height: 1;
    margin: 0;
  }
Anne_LXM's avatar
Anne_LXM 已提交
241
</style>