AccountDetail.vue 7.02 KB
Newer Older
Davve's avatar
Davve committed
1 2 3 4 5
<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">

      <sticky :class-name="'sub-navbar '+postForm.status">
Davve's avatar
Davve committed
6
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">保存</el-button>
Davve's avatar
Davve committed
7 8 9 10 11 12
      </sticky>

      <div class="createPost-main-container">
        <el-row>

          <el-col :span="24">
Davve's avatar
Davve committed
13
            <el-form-item style="margin-bottom: 40px;" prop="username" v-if="isShow">
Davve's avatar
Davve committed
14 15 16 17 18 19
              <MDinput v-model="postForm.username" :maxlength="100" name="username" required disabled="disabled">
                账号
              </MDinput>
            </el-form-item>

            <el-form-item style="margin-bottom: 40px;" prop="username" v-else>
Davve's avatar
Davve committed
20
              <MDinput v-model="postForm.username" :maxlength="100" name="username" required>
Davve's avatar
Davve committed
21
                账号
Davve's avatar
Davve committed
22 23 24
              </MDinput>
            </el-form-item>

Davve's avatar
Davve committed
25 26 27 28 29
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="24">
Davve's avatar
Davve committed
30
            <el-form-item style="margin-bottom: 40px;" prop="password" v-show="is_show">
Davve's avatar
Davve committed
31
              <MDinput v-model="postForm.password" :maxlength="100" name="password" required>
Davve's avatar
Davve committed
32 33 34 35 36 37 38
                密码
              </MDinput>
            </el-form-item>

          </el-col>
        </el-row>
        <el-row>
Davve's avatar
Davve committed
39

Davve's avatar
Davve committed
40 41 42 43 44 45 46 47 48 49 50 51
          <el-col :span="24">
            <el-form-item style="margin-bottom: 40px;" prop="email">
              <MDinput v-model="postForm.email" :maxlength="100" name="email" required>
                邮箱
              </MDinput>
            </el-form-item>

          </el-col>
        </el-row>
        <el-row>

          <el-col :span="24">
Davve's avatar
Davve committed
52 53
            <el-form-item style="margin-bottom: 40px;" prop="nick_name">
              <MDinput v-model="postForm.nick_name" :maxlength="100" name="nick_name" required>
Davve's avatar
Davve committed
54 55 56
                姓名
              </MDinput>
            </el-form-item>
Davve's avatar
Davve committed
57 58 59

          </el-col>
        </el-row>
Davve's avatar
Davve committed
60
        <el-row>
Davve's avatar
Davve committed
61

Davve's avatar
Davve committed
62 63 64 65 66 67
          <el-col :span="24">
            <el-form-item style="margin-bottom: 40px;" prop="phone">
              <MDinput v-model="postForm.phone" :maxlength="100" name="phone" required>
                电话
              </MDinput>
            </el-form-item>
Davve's avatar
Davve committed
68

Davve's avatar
Davve committed
69 70
          </el-col>
        </el-row>
Davve's avatar
Davve committed
71 72 73 74 75 76 77 78

      </div>
    </el-form>

  </div>
</template>

<script>
Davve's avatar
Davve committed
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
  import MDinput from '@/components/MDinput'
  import Sticky from '@/components/Sticky' // 粘性header组件
  import {validateURL} from '@/utils/validate'
  import {CreateAccount, fetchAccountDetail,} from '@/api/account'
  import {userSearch} from '@/api/remoteSearch'

  const defaultForm = {
    status: 'draft',
    username: '',
    password: '',
    email: '',
    phone: '',
    nick_name: ''
  }

  export default {
    name: 'GroupDetail',
    components: {MDinput, Sticky},
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      const validateRequire = (rule, value, callback) => {
Davve's avatar
Davve committed
105
        value = value.trim()
Davve's avatar
Davve committed
106 107 108 109 110 111 112 113 114 115
        if (value === '') {
          this.$message({
            message: rule.field + '为必传项',
            type: 'error'
          })
          callback(new Error(rule.field + '为必传项'))
        } else {
          callback()
        }
      }
Davve's avatar
Davve committed
116 117

      const validatePasswordLength = (rule, value, callback) => {
Davve's avatar
Davve committed
118
        value = value.trim()
Davve's avatar
Davve committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        if (value === '') {
          this.$message({
            message: rule.field + '为必传项',
            type: 'error'
          })
          callback(new Error(rule.field + '为必传项'))
        } else if ( value.length < 6) {
          this.$message({
            message: '密码不得少于6位',
            type: 'error'
          })
          callback(new Error('密码不得少于6位'))
        } else {
          callback()
        }
      }
Davve's avatar
Davve committed
135 136 137 138 139
      return {
        postForm: Object.assign({}, defaultForm),
        loading: false,
        userListOptions: [],
        rules: {
Davve's avatar
Davve committed
140 141 142
          username: [{validator: validateRequire, trigger: 'blur'}],
          nick_name: [{validator: validateRequire, trigger: 'blur'}],
          email: [{validator: validateRequire, trigger: 'blur'}],
Davve's avatar
Davve committed
143
          password: [{validator: validatePasswordLength, trigger: 'blur'}],
Davve's avatar
Davve committed
144
          phone: [{validator: validateRequire, trigger: 'blur'}],
Davve's avatar
Davve committed
145
        },
Davve's avatar
Davve committed
146
        is_show: true
Davve's avatar
Davve committed
147 148 149 150 151 152
      }
    },
    created() {
      if (this.isEdit) {
        const id = this.$route.params && this.$route.params.id
        this.fetchData(id)
Davve's avatar
Davve committed
153
      } else {
Davve's avatar
Davve committed
154
        this.postForm = Object.assign({}, defaultForm)
Davve's avatar
Davve committed
155 156
      }

Davve's avatar
Davve committed
157
      this.tempRoute = Object.assign({}, this.$route)
Davve's avatar
Davve committed
158
    },
Davve's avatar
Davve committed
159 160 161 162
    methods: {
      fetchData(id) {
        fetchAccountDetail(id).then(response => {
          this.postForm = response.data.data.data
Davve's avatar
Davve committed
163
          this.is_show = false
Davve's avatar
Davve committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        }).catch(err => {
          console.log(err)
        })
      },
      submitForm() {
        this.$refs.postForm.validate(valid => {
          if (valid) {
            this.loading = true
            CreateAccount(this.postForm).then(response => {
              if (response.data.data.code == 500) {
                this.$notify({
                  title: '失败',
                  message: response.data.data.message,
                  type: 'error',
                  duration: 2000
                })
                this.loading = false;
                return false;
              }
Davve's avatar
Davve committed
183
              this.$notify({
Davve's avatar
Davve committed
184 185 186 187 188 189 190 191 192 193
                title: '成功',
                message: response.data.data.message,
                type: 'success',
                duration: 2000
              })
              setTimeout(() => {
                this.$router.push('/account/list')
              }, 1000)

            }).catch(err => {
Davve's avatar
Davve committed
194
              this.$notify({
Davve's avatar
Davve committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                title: '失败',
                message: '操作失败',
                type: 'danger',
                duration: 2000
              })
            });

            this.postForm.status = 'published'
            this.loading = false
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      getRemoteUserList(query) {
        userSearch(query).then(response => {
          if (!response.data.items) return
          this.userListOptions = response.data.items.map(v => v.name)
        })
      }
Davve's avatar
Davve committed
216 217 218 219 220
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
Davve's avatar
Davve committed
221 222 223 224 225 226 227 228 229 230 231 232 233
  @import "src/styles/mixin.scss";

  .createPost-container {
    position: relative;
    .createPost-main-container {
      padding: 40px 45px 20px 50px;
      .postInfo-container {
        position: relative;
        @include clearfix;
        margin-bottom: 10px;
        .postInfo-container-item {
          float: left;
        }
Davve's avatar
Davve committed
234
      }
Davve's avatar
Davve committed
235 236 237 238 239 240 241 242 243
      .editor-container {
        min-height: 500px;
        margin: 0 0 30px;
        .editor-upload-btn-container {
          text-align: right;
          margin-right: 10px;
          .editor-upload-btn {
            display: inline-block;
          }
Davve's avatar
Davve committed
244 245 246
        }
      }
    }
Davve's avatar
Davve committed
247 248 249 250 251 252
    .word-counter {
      width: 40px;
      position: absolute;
      right: -10px;
      top: 0px;
    }
Davve's avatar
Davve committed
253 254
  }
</style>