addMain.vue 7.92 KB
Newer Older
潘琦 committed
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 26 27 28 29 30 31 32
<template>
  <div class="full-page">
    <div class="container addMainPage">
      <div class="p-title">
        <span class="label">您的基本信息</span>
        <span class="link text-orange">默认为户主</span>
      </div>
      <div class="form">
        <div class="table">
          <div class="tr">
            <div class="td label">姓名</div>
            <div class="td text-right">
              <input type="text" class="form-control" v-model="userData.name" @blur="BaseValidateName" placeholder="请输入姓名"/>
              <span v-show="!BaseValidate.nameState" class="small-hint">(只能是中文或英文,长度1~15位)</span>
            </div>
          </div>
          <div class="tr">
            <div class="td label">手机号</div>
            <div class="td text-right">
              <input type="text" class="form-control" placeholder="请输入有效的手机号码" @blur="BaseValidatePhone" v-model="userData.phone"/>
              <span v-show="!BaseValidate.phoneState" class="small-hint">请输入有效的手机号码</span>
            </div>
          </div>
          <div class="tr">
            <div class="td label">身份证号</div>
            <div class="td text-right">
              <input type="text" class="form-control" placeholder="请输入有效身份证" @blur="BaseValidateIdCard" v-model="userData.idCard"/>
              <span v-show="!BaseValidate.idCardState" class="small-hint">请输入有效身份证</span>
            </div>
          </div>
        </div>
      </div>
潘琦 committed
33
      <div class="form" v-if="userData.sex === '2'">
潘琦 committed
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
        <div class="table">
          <div class="tr">
            <div class="td label">末次月经时间</div>
            <div class="td text-right">
              <div class="td-table" @click="openPickerLastMensesTime">
                <div class="td-table-cell text">{{userData.lastMensesTime !== '' ? userData.lastMensesTime : '请选择末次月经时间'}}</div>
                <div class="td-table-cell icon">
                  <i class="glyphicon glyphicon-menu-right"></i>
                </div>
              </div>
              <span v-show="!BaseValidate.lastMensesTimeState" class="small-hint">请选择末次月经时间</span>
            </div>
          </div>
        </div>
      </div>
      <div class="footer">
        <button class="btn-default btn-block" :class="btnDisabled?'disabled':''" :disabled="btnDisabled" @click="handelSave">立即领取</button>
      </div>
    </div>
    <!-- 末次月经时间picker -->
    <mt-datetime-picker
      v-model="pickerLastMensesTimeDefault"
      ref="pickerLastMensesTime"
      type="date"
      year-format="{value} 年"
      month-format="{value} 月"
      date-format="{value} 日"
      @confirm="lastMensesTimePickerConfirm"
      :startDate="pickerLastMensesTimeStart"
      :endDate="pickerLastMensesTimeEnd">
    </mt-datetime-picker>
  </div>
</template>

<script>
69
import store from '@/store'
潘琦 committed
70 71 72 73 74 75 76 77 78
import { saveMember } from '@/api/basic/basic'
import { validatename, cardid, isvalidatemobile } from '@/util/validate'
import { SubtractDay, getBirthByIdNumber, getSexByIdCard } from '@/util/index'
import { setTimeout } from 'timers'

export default {
  name: 'Home',
  data () {
    return {
79
      userId: store.getters.user_id,
潘琦 committed
80 81 82 83 84
      orgId: this.$route.query.orgId ? parseInt(this.$route.query.orgId) : null,
      orgName: this.$route.query.orgName ? this.$route.query.orgName : '',
      userData: {
        name: '',
        idCard: '',
潘琦 committed
85
        sex: 1,
潘琦 committed
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
        phone: '',
        lastMensesTime: '' // 末次月经时间
      },
      BaseValidate: { // 表单各个列是否合法状态
        nameState: true,
        idCardState: true,
        phoneState: true,
        lastMensesTimeState: true
      },
      btnDisabled: false,
      pickerLastMensesTimeDefault: new Date(), // 末次月经时间默认选中日期为当天
      pickerLastMensesTimeStart: SubtractDay(290), // 末次月经时间起始日期为当天日期前290天
      pickerLastMensesTimeEnd: new Date() // 末次月经时间控件结束日期小于当天
    }
  },
  created () {
  },
  mounted: function () {
  },
  computed: {
  },
  components: {
  },
  methods: {
    BaseValidateHandle () { // 表单验证
      this.BaseValidateName()
      this.BaseValidateIdCard()
      this.BaseValidatePhone()
      this.BaseValidateMensesLastDate()
    },
    BaseValidateName () { // 姓名验证
      if (validatename(this.userData.name)) {
        this.BaseValidate.nameState = true
      } else {
        this.BaseValidate.nameState = false
      }
    },
    BaseValidateIdCard () { // 身份证验证
      if (cardid(this.userData.idCard)[0]) {
        this.BaseValidate.idCardState = false
      } else {
潘琦 committed
127
        this.userData.sex = getSexByIdCard(this.userData.idCard)
潘琦 committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        this.BaseValidate.idCardState = true
      }
    },
    BaseValidatePhone () { // 手机号码验证
      if (this.userData.phone === '') {
        this.BaseValidate.phoneState = false
      } else if (isvalidatemobile(this.userData.phone)[0]) {
        this.BaseValidate.phoneState = false
      } else {
        this.BaseValidate.phoneState = true
      }
    },
    BaseValidateMensesLastDate () { // 末次月经时间验证
      if (this.userData.lastMensesTime === '') {
        this.BaseValidate.lastMensesTimeState = false
      } else {
        this.BaseValidate.lastMensesTimeState = true
      }
    },
    openPickerLastMensesTime () {
      this.$refs.pickerLastMensesTime.open()
    },
    lastMensesTimePickerConfirm (date) {
      const selectedDate = new Date(date)
      let month = selectedDate.getMonth() + 1
      if (selectedDate.getMonth() + 1 <= 9) {
        month = '0' + (selectedDate.getMonth() + 1)
      }
      this.userData.lastMensesTime = selectedDate.getFullYear() + '-' + month + '-' + selectedDate.getDate()
潘琦 committed
157
      this.BaseValidateMensesLastDate()
潘琦 committed
158 159 160 161
    },
    handelSave () {
      console.log('submit')
      this.BaseValidateHandle()
潘琦 committed
162 163 164 165
      if (!this.BaseValidate.nameState || !this.BaseValidate.phoneState || !this.BaseValidate.idCardState) {
        return true
      }
      if (this.userData.sex === '2' && !this.BaseValidate.lastMensesTimeState) {
潘琦 committed
166 167
        return true
      }
168 169 170 171 172 173 174 175
      if (!this.userId || this.userId === 'null' || this.userId !== '') {
        this.$toast({
          message: '缺少用户标识(user id为空)',
          position: 'center',
          duration: 3000
        })
        return
      }
潘琦 committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      let params = {
        userId: this.userId,
        name: this.userData.name,
        phone: this.userData.phone,
        idCard: this.userData.idCard,
        sex: getSexByIdCard(this.userData.idCard),
        lastMensesTime: this.userData.lastMensesTime,
        orgId: this.orgId,
        orgName: this.orgName,
        relationType: '0',
        birth: getBirthByIdNumber(this.userData.idCard)
      }
      this.btnDisabled = true
      this.$Indicator.open()
      saveMember(params).then(res => {
        this.btnDisabled = false
        this.$Indicator.close()
        if (res.code === 0 && res.data) {
          this.$toast({
            message: '保存成功',
            position: 'center',
            duration: 3000
          })
          let that = this
          setTimeout(function () {
            that.$router.push({
202
              path: '/home'
潘琦 committed
203 204 205 206 207 208 209 210 211 212
            })
          }, 2500)
        } else {
          this.$toast({
            message: '保存失败',
            position: 'center',
            duration: 3000
          })
        }
      }).catch((error) => {
潘琦 committed
213 214
        this.btnDisabled = false
        this.$Indicator.close()
潘琦 committed
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 241
        this.$toast({
          message: error.message,
          position: 'center',
          duration: 3000
        })
      })
    }
  }
}
</script>

<style scoped>
  .full-page {
    background-color: #f5f6f7;
  }

  .addMainPage .footer{
    padding: 4em 0;
  }
  .addMainPage .p-title {
    padding: 1em 0 0 0;
  }
  .addMainPage .form > .table > .tr > .td.label {
    color: #9aa0a4;
    font-weight: normal;
  }
</style>