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

<script>
leiqingsong's avatar
leiqingsong committed
20
import { setAvatar } from "@/api/user";
leiqingsong's avatar
leiqingsong committed
21
import { uploadImage } from "@/api/base";
22 23 24 25
export default {
  name: "ModefyAvatar",
  data() {
    return {
leiqingsong's avatar
leiqingsong committed
26 27
      avatar: "",
      imageUrl: ""
28 29 30
    };
  },
  mounted() {
31
    console.log("123");
32 33 34 35 36
    if (!this.avatar) {
      this.avatar = require("@/assets/images/no_avatar.png");
    }
  },
  methods: {
leiqingsong's avatar
leiqingsong committed
37 38 39
    checkBtn() {
      const params = {
        headImage: this.imageUrl,
leiqingsong's avatar
leiqingsong committed
40
        userId: this.$userId
leiqingsong's avatar
leiqingsong committed
41 42 43 44 45 46 47 48
      };
      setAvatar(params).then(res => {
        if (res.code === 0) {
          this.$toast.success("头像设置成功");
          this.$router.go(-1);
        }
      });
    },
49 50 51
    onRead(file) {
      this.avatar = file.content;
      console.log("file", file);
leiqingsong's avatar
leiqingsong committed
52
      const params = {
leiqingsong's avatar
leiqingsong committed
53 54 55 56 57 58 59 60
        inviteCode: "bbbbbb"
      };
      const fd = new FormData();
      fd.append("files", file.file);
      uploadImage(params, fd).then(res => {
        console.log(res);
        this.imageUrl = res.zxUrl;
      });
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
    },
    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
87
  padding-top: 25px;
88 89 90 91 92 93 94
}
.avatar {
  width: 345px;
  height: 345px;
  margin-bottom: 52px;
  border-radius: 4px;
}
xulili's avatar
xulili committed
95 96 97
.van-uploader{
  font-size: 0;
}
98 99 100 101 102 103 104 105
.btn {
  display: flex;
  justify-content: space-around;
  .van-button {
    width: 165px;
  }
}
</style>