modefyAvatar.vue 3.23 KB
Newer Older
1 2 3 4 5
<template>
  <div class="modefy-avatar">
    <img class="avatar" :src="avatar" alt />
    <div class="btn">
      <van-uploader
leiqingsong's avatar
leiqingsong committed
6
        accept="image/*"
7 8 9 10
        :max-count="1"
        :before-delete="onDeleteAvatar"
        :after-read="onRead"
      >
leiqingsong's avatar
leiqingsong committed
11
        <van-button type="primary">上传新头像</van-button>
12
      </van-uploader>
leiqingsong's avatar
leiqingsong committed
13 14 15 16 17 18 19
      <van-button
        type="primary"
        :class="!canSubmit && 'deactive-btn'"
        plain
        @click="checkBtn"
        >确定</van-button
      >
20 21 22 23 24
    </div>
  </div>
</template>

<script>
leiqingsong's avatar
leiqingsong committed
25
import { setAvatar } from "@/api/user";
leiqingsong's avatar
leiqingsong committed
26
import { uploadImage } from "@/api/base";
27 28 29 30
export default {
  name: "ModefyAvatar",
  data() {
    return {
leiqingsong's avatar
leiqingsong committed
31
      canSubmit: false,
leiqingsong's avatar
leiqingsong committed
32 33
      avatar: "",
      imageUrl: ""
34 35 36
    };
  },
  mounted() {
leiqingsong's avatar
leiqingsong committed
37 38 39
    console.log(this.$route.query);
    const headImage = this.$route.query.headImage;
    if (headImage) {
leiqingsong's avatar
leiqingsong committed
40
      this.avatar = process.env.VUE_APP_BASE_URL + headImage;
leiqingsong's avatar
leiqingsong committed
41
    } else {
42 43 44 45
      this.avatar = require("@/assets/images/no_avatar.png");
    }
  },
  methods: {
leiqingsong's avatar
leiqingsong committed
46
    checkBtn() {
leiqingsong's avatar
leiqingsong committed
47 48 49 50
      if (!this.canSubmit) {
        return;
      }
      this.canSubmit = false;
leiqingsong's avatar
leiqingsong committed
51 52
      const params = {
        headImage: this.imageUrl,
leiqingsong's avatar
leiqingsong committed
53
        userId: this.$userId
leiqingsong's avatar
leiqingsong committed
54 55 56 57 58 59 60 61
      };
      setAvatar(params).then(res => {
        if (res.code === 0) {
          this.$toast.success("头像设置成功");
          this.$router.go(-1);
        }
      });
    },
62
    onRead(file) {
leiqingsong's avatar
leiqingsong committed
63 64 65
      if (!localStorage.getItem("user").inviteCode) {
        this.$toast.fail("当前用户没有邀请码");
        return;
leiqingsong's avatar
leiqingsong committed
66
      }
leiqingsong's avatar
leiqingsong committed
67
      const inviteCode = JSON.parse(localStorage.getItem("user").inviteCode);
leiqingsong's avatar
leiqingsong committed
68
      const params = {
leiqingsong's avatar
leiqingsong committed
69
        inviteCode: inviteCode
leiqingsong's avatar
leiqingsong committed
70 71 72
      };
      const fd = new FormData();
      fd.append("files", file.file);
leiqingsong's avatar
leiqingsong committed
73 74 75 76 77 78 79 80 81 82 83
      uploadImage(params, fd)
        .then(res => {
          console.log(res);
          this.avatar = file.content;
          this.imageUrl = res.zxUrl;
          this.canSubmit = true;
        })
        .catch(err => {
          console.log("上传图像", err);
          this.$toast.fail("图片上传失败,请重新选择");
        });
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
    },
    onDeleteAvatar(file, detail) {
      return new Promise((resolve, reject) => {
        this.$dialog
          .confirm({
            message: "确认删除图片?"
          })
          .then(() => {
            this.fileList.splice(detail.index, 1);
            this.$toast.success("删除成功");
            resolve();
          })
          .catch(error => {
            this.$toast.fail("已取消");
            reject(error);
          });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.modefy-avatar {
  box-sizing: border-box;
  text-align: center;
xulili's avatar
xulili committed
110
  padding-top: 25px;
111 112 113 114 115 116 117
}
.avatar {
  width: 345px;
  height: 345px;
  margin-bottom: 52px;
  border-radius: 4px;
}
leiqingsong's avatar
leiqingsong committed
118

119
.btn {
leiqingsong's avatar
leiqingsong committed
120
  box-sizing: border-box;
121
  display: flex;
leiqingsong's avatar
leiqingsong committed
122 123 124 125 126 127 128 129 130 131 132
  justify-content: space-between;
  align-content: center;
  width: 345px;
  height: 44px;
  margin: 0 auto;
  .van-uploader {
    .van-button {
      position: absolute;
      top: 0;
      left: 0;
    }
leiqingsong's avatar
leiqingsong committed
133 134 135 136
    ::v-deep input {
      width: 165px;
      height: 44px;
    }
leiqingsong's avatar
leiqingsong committed
137
  }
138 139 140
  .van-button {
    width: 165px;
  }
leiqingsong's avatar
leiqingsong committed
141 142 143 144 145
  .deactive-btn {
    color: #969595e9;
    background-color: #dcd6d6;
    border-color: #dcd6d6;
  }
146 147
}
</style>