|
|
@@ -0,0 +1,88 @@
|
|
|
+<template>
|
|
|
+ <div>
|
|
|
+ <b-notification type="is-danger" :active.sync="isServerError">
|
|
|
+ {{ errorMassage }}
|
|
|
+ </b-notification>
|
|
|
+ <b-field
|
|
|
+ label="昵称"
|
|
|
+ :type="$v.nickname.$error ? 'is-danger' : ''"
|
|
|
+ :message="$v.nickname.$error ? '昵称须由长度为4至16的字母或数字组成' : ''"
|
|
|
+ >
|
|
|
+ <b-input
|
|
|
+ v-model="$v.nickname.$model"
|
|
|
+ placeholder="在此输入昵称"
|
|
|
+ ></b-input>
|
|
|
+ </b-field>
|
|
|
+ <b-field label="介绍一下你自己">
|
|
|
+ <b-input maxlength="100" v-model="bio" type="textarea"></b-input>
|
|
|
+ </b-field>
|
|
|
+ <b-field>
|
|
|
+ <button
|
|
|
+ :class="{ 'is-loading': submitting }"
|
|
|
+ class="button is-primary"
|
|
|
+ @click="updateProfileAction"
|
|
|
+ >
|
|
|
+ 更 新
|
|
|
+ </button>
|
|
|
+ </b-field>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+import { alphaNum, minLength, maxLength } from "vuelidate/lib/validators";
|
|
|
+import { postUserProfile } from "@/api/user";
|
|
|
+import { FETCH_PROFILE } from "@/store/type/actions.type";
|
|
|
+
|
|
|
+export default {
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ nickname: "",
|
|
|
+ bio: "",
|
|
|
+ submitting: false,
|
|
|
+ errorMassage: null,
|
|
|
+ isServerError: false
|
|
|
+ };
|
|
|
+ },
|
|
|
+ validations: {
|
|
|
+ nickname: {
|
|
|
+ alphaNum,
|
|
|
+ minLength: minLength(4),
|
|
|
+ maxLength: maxLength(16)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ this.getProfileFromStore();
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ async updateProfileAction() {
|
|
|
+ this.$v.$touch();
|
|
|
+ if (!this.$v.$invalid) {
|
|
|
+ this.submitting = true;
|
|
|
+ const { id, username } = this.$store.state.user.profile;
|
|
|
+ const { bio = "", nickname = "" } = this;
|
|
|
+ try {
|
|
|
+ await postUserProfile(id, { bio, nickname });
|
|
|
+ await this.$store.dispatch(FETCH_PROFILE, id);
|
|
|
+ this.$toast.open({
|
|
|
+ message: `用户「${
|
|
|
+ nickname.length === 0 ? username : nickname
|
|
|
+ }」修改成功`,
|
|
|
+ type: "is-success"
|
|
|
+ });
|
|
|
+ this.getProfileFromStore();
|
|
|
+ } catch (e) {
|
|
|
+ this.errorMassage = e.message;
|
|
|
+ this.isServerError = true;
|
|
|
+ } finally {
|
|
|
+ this.submitting = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ getProfileFromStore() {
|
|
|
+ const { nickname = "", bio = "" } = this.$store.state.user.profile;
|
|
|
+ this.nickname = nickname;
|
|
|
+ this.bio = bio;
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+</script>
|