profile.vue 11 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4 5 6 7 8 9
<template>
  <div class="app-container">
    <el-row :gutter="20" v-if="user">
      <el-col :span="6" :xs="24">
        <el-card style="margin-bottom: 20px">
          <div slot="header" class="clearfix"><span>关于我</span></div>
          <div class="user-profile">
            <div class="box-center">
              <pan-thumb
wanli's avatar
wanli committed
10
                image="http://statics.evmiot.com/evue-logo.png"
wanli's avatar
wanli committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 129 130 131 132
                :height="'100px'"
                :width="'100px'"
                :hoverable="false"
                ><div>Hello</div>
                {{ user.role }}</pan-thumb
              >
            </div>
            <div class="box-center">
              <div class="user-name text-center">{{ user.name }}</div>
              <div class="user-role text-center text-muted">
                未知
              </div>
            </div>
          </div>
          <div class="user-bio">
            <div class="user-education user-bio-section">
              <div class="user-bio-section-header">
                <svg-icon icon-class="education" /><span>个性签名</span>
              </div>
              <div class="user-bio-section-body">
                <div class="text-muted">{{ user.sign | wrapperSign }}</div>
              </div>
            </div>
            <div class="user-skills user-bio-section">
              <div class="user-bio-section-header">
                <svg-icon icon-class="skill" /><span>所属公司</span>
              </div>
              <div class="user-bio-section-body">{{ user.company }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="账号信息" name="account">
              <el-form
                size="mini"
                :model="user"
                status-icon
                :rules="rules"
                ref="post"
                label-width="80px"
              >
                <el-form-item label="账号">
                  <el-col :md="8" :xs="24">
                    <el-input v-model.trim="currentValue.account" disabled />
                  </el-col>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                  <el-col :md="8" :xs="24">
                    <el-radio-group v-model="user.gender">
                      <el-radio :label="1"></el-radio>
                      <el-radio :label="2"></el-radio>
                    </el-radio-group>
                  </el-col>
                </el-form-item>
                <el-form-item label="生日" prop="birthday">
                  <el-col :md="8" :xs="24">
                    <el-date-picker
                      v-model="user.birthday"
                      type="date"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      placeholder="选择日期"
                    >
                    </el-date-picker>
                  </el-col>
                </el-form-item>
                <el-form-item label="手机" prop="contact">
                  <el-col :md="8" :xs="24">
                    <el-input v-model.trim="user.contact" />
                  </el-col>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                  <el-col :md="8" :xs="24">
                    <el-input v-model.trim="user.email" />
                  </el-col>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm('post')"
                    >更新资料</el-button
                  >
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="activity">
              <el-form size="mini" label-width="100px">
                <el-form-item label="原密码">
                  <el-col :md="8" :xs="24">
                    <el-input
                      v-model="form.password"
                      type="password"
                      autocomplete="off"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item label="新密码">
                  <el-col :md="8" :xs="24">
                    <el-input
                      v-model="form.newPassword"
                      type="password"
                      autocomplete="off"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item label="确认新密码">
                  <el-col :md="8" :xs="24">
                    <el-input
                      v-model="form.confirmPassword"
                      type="password"
                      autocomplete="off"
                    />
                  </el-col>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="updatePassword"
                    >更新密码</el-button
                  >
                </el-form-item>
              </el-form>
            </el-tab-pane>
wanli's avatar
wanli committed
133 134 135 136 137 138 139 140 141 142 143 144
            <el-tab-pane label="接口密钥" name="apiKey">
              <el-form size="mini" label-width="100px">
                <el-form-item label="AccessKey">
                  <el-col :md="10" :xs="24">
                    <el-input v-model="user.uuid" type="text" autocomplete="off" disabled />
                  </el-col>
                  <el-col :md="4" :xs="24">
                      <el-button @click="copyAccessKey">复制</el-button>
                  </el-col>
                </el-form-item>
              </el-form>
            </el-tab-pane>
wanli's avatar
wanli committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { updateUser, updateUserPassword } from "@/api/index";
import PanThumb from "@/components/PanThumb";

export default {
  name: "Profile",
  components: { PanThumb },
  filters: {
    wrapperSign(sign) {
      return sign === "" || !sign ? "此人非常懒,一个字都不写" : sign;
    },
  },
  data() {
    return {
      user: {
        birthday: "",
        email: "",
        contact: "",
        hometown: "",
        gender: 1,
wanli's avatar
wanli committed
173
        uuid: "",
wanli's avatar
wanli committed
174 175 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 202 203 204 205
      },
      form: {
        password: "",
        newPassword: "",
        confirmPassword: "",
      },
      rules: {
        email: [
          {
            type: "string",
            required: false,
            message: "邮箱不能为空",
            trigger: "blur",
          },
        ],
        contact: [
          {
            type: "string",
            required: false,
            message: "手机不能为空",
            trigger: "blur",
          },
        ],
        hometown: [
          {
            type: "string",
            required: false,
            message: "邮箱不能为空",
            trigger: "blur",
          },
        ],
      },
wanli's avatar
wanli committed
206 207 208 209
      currentValue: {
        uuid: null,
        account: null
      },
wanli's avatar
wanli committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      activeTab: "account",
    };
  },
  computed: {
    ...mapGetters(["name", "avatar", "role"]),
  },
  created() {
    this.getUser();
  },
  methods: {
    getUser() {
      const user = JSON.parse(window.sessionStorage.getItem("user"));
      if (user) {
        this.currentValue = user;
        this.user.birthday = user.birthday;
        this.user.email = user.email;
        this.user.contact = user.contact;
        this.user.hometown = user.hometown;
        this.user.gender = user.gender;
wanli's avatar
wanli committed
229
        this.user.uuid = user.uuid;
wanli's avatar
wanli committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        let result = true;
        if (valid) {
          updateUser(this.currentValue.uuid, this.user)
            .then((res) => {
              this.user = res.data;
              this.$message({ type: "success", message: "更新成功" });
              const profile = Object.assign({}, this.currentValue, res.data);
              window.sessionStorage.setItem("user", JSON.stringify(profile));
            })
            .catch((err) => {
              this.$message.error(err.message);
            });
        } else {
          result = false;
        }
        this.dialogVisible = false;
        return result;
      });
    },
wanli's avatar
wanli committed
253 254 255 256 257 258 259 260 261
    copyAccessKey() {
        let oInput = document.createElement('input')
        oInput.value = this.user.uuid
        document.body.appendChild(oInput)
        oInput.select()
        document.execCommand("Copy")
        this.$message({ message: '复制成功', type: 'success' })
        oInput.remove()
    },
wanli's avatar
wanli committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    updatePassword() {
      if (
        this.form.password.trim().length > 0 &&
        this.form.newPassword.trim().length > 0 &&
        this.form.confirmPassword.trim().length > 0
      ) {
        if (this.form.newPassword === this.form.confirmPassword) {
          if (this.form.newPassword.trim().length < 6) {
            return this.$message.error("新密码长度不能少于6位");
          }

          // if (!/\d/.test(this.form.newPassword.trim())) {
          //   return this.$message.error("新密码必须包含数字");
          // } // 如果用户输入的密码 包含了数字

          // if (!/[a-z]/.test(this.form.newPassword.trim())) {
          //   return this.$message.error("新密码必须包含小写字母");
          // } // 如果用户输入的密码 包含了小写的a到z

          // if (!/[A-Z]/.test(this.form.newPassword.trim())) {
          //   return this.$message.error("新密码必须包含大写字母");
          // } // 如果用户输入的密码 包含了大写的A到Z

          // if (!/_|\W/.test(this.form.newPassword.trim())) {
          //   return this.$message.error("新密码必须包含字符");
          // } // 如果是非数字 字母

          updateUserPassword({
            uuid: this.currentValue.uuid,
            password: this.form.password,
            newPassword: this.form.newPassword,
          })
            .then((res) => {
              console.log(res);
              this.$message({ type: "success", message: "密码更新成功" });
            })
            .catch((err) => {
              this.$message.error(err.message);
            });
        } else this.$message.error("新密码和确认新密码不一致");
      } else this.$message.error("原密码、新密码、确认新密码都不能为空");
    },
  },
};
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>