Blame view

pc-master/src/views/userCenter/userInfo/userInfo.vue 5.37 KB
3f535f30   杨鑫   '初始'
1
2
3
4
5
6
7
8
9
10
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
133
134
135
136
137
138
139
140
141
142
143
144
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
173
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  <template>
    <div class="userInfo">
      <div class="rowBox">
        <div class="changeAvatar">
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="userInfo.headImage" :src="userInfo.headImage" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <!-- <span class="avatarTit">更换头像</span> -->
        </div>
        <div class="mid">
          <div class="userNameBox userBlock">
            <label>昵称:</label>
            <el-input v-model="userInfo.name" placeholder="请输入内容"></el-input>
          </div>
          <div class="userNameBox userBlock">
            <label>性别:</label>
            <el-radio v-model="userInfo.sex" label="男">男</el-radio>
            <el-radio v-model="userInfo.sex" label="女">女</el-radio>
          </div>
        </div>
        <div class="right">
          <div class="birthTimeBox userBlock">
            <label>出生日期:</label>
            <el-date-picker
              v-model="userInfo.birthday"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </div>
          <div class="bindPhoneBox userBlock">
            <label>绑定手机号:</label>
            <div class="phoneInfo">
              <span>{{ userInfo.phone }}</span>
              <span @click="$router.push('/changePhone')">更换</span>
            </div>
          </div>
        </div>
      </div>
      <el-button class="userInfoSave"
        :loading="loading"
        @click="saveUserInfo"
      >保存</el-button>
    </div>
  </template>
  
  <script>
  import {
    getUserInfo,
    changeUserInfo
  } from '@/api/user/user.js'
  import {
    upload
  } from '@/api/upload.js'
  import {mapGetters, mapMutations} from 'vuex'
  
  export default {
    name: 'userInfo',
    data () {
      return {
        userName: 'Curmudgeons',
        gender: '1',
        birthTime: '',
        userInfoData: {},
        avatarImg: '',
        action: upload,
        loading: false
      }
    },
    computed: {
      ...mapGetters([
        'userInfo' // 用户信息
      ])
    },
    methods: {
      ...mapMutations({
        setUserInfo: 'SET_USERINFO', // 修改用户信息
        setAVATR: 'SET_AVATAR' // 修改用户信息
      }),
      async getUserInfoData () {
        const response = await getUserInfo()
        const res = response.data
        if (res.code === '200') {
          this.setUserInfo(res.data)
        } else {
          this.$message.error(res.message)
        }
      },
      async saveUserInfo () {
        this.loading = true
        let errMsg = ''
        if (!this.userInfo.name) {
          errMsg += ' 请输入昵称 '
        }
        if (!this.userInfo.birthday) {
          errMsg += ' 请输入生日 '
        }
        if (errMsg.length !== 0) {
          this.$message.error(errMsg)
          this.loading = false
          return
        }
        const response = await changeUserInfo({
          name: this.userInfo.name,
          sex: this.userInfo.sex,
          birthday: this.userInfo.birthday,
          headImage: this.userInfo.headImage
        })
        const res = response.data
        if (res.code === '200') {
          this.$message({
            message: '修改成功!',
            type: 'success'
          })
          this.getUserInfoData()
        }
        this.loading = false
      },
      // 上传头像
      handleAvatarSuccess (res) {
        this.setAVATR(res.data.url)
        this.$message({
          message: '上传成功,请保存修改!',
          type: 'success'
        })
    }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .userInfo {
    max-width: 1000px;
    padding: 30px;
    border: 1px solid #E5E5E5;
    .rowBox{
      min-height: 500px;
      display: flex;
      justify-content: space-between;
    }
    .changeAvatar {
      width: 134px;
      height: 134px;
      position: relative;
      .avatar {
        width: 100%;
        max-width: 120px;
        max-height: 120px;
      }
      .avatarTit {
        height: 30px;
        line-height: 30px;
        position: absolute;
        bottom: 0;
        font-size: 16px;
        background: rgba(84, 84, 84, 0.5);
        width: 100%;
        text-align: center;
        color: #ffffff;
        cursor: pointer;
      }
    }
    .mid{
      min-width: 230px;
      margin: 0 20px;
      .userNameBox {
        >>> .el-input {
          width: 180px;
        }
      }
    }
    .userBlock {
      margin-top: 30px;
      display: flex;
      align-items: center;
      label {
        font-weight: normal;
        font-size: 16px;
        color: #666666;
      }
    }
    .right{
      min-width: 230px;
      .bindPhoneBox {
        .phoneInfo {
          font-size: 16px;
          span {
            margin-right: 15px;
            text-decoration: underline;
            cursor: pointer;
          }
          span:nth-child(1) {
            color: #333333;
            text-decoration: none;
            cursor: default;
          }
          span:nth-child(2) {
            color: $mainGlod;
          }
          span:nth-child(3) {
            color: #666666;
          }
        }
      }
    }
    .userInfoSave {
      display: block;
      width: 200px;
      height: 50px;
      margin: 50px auto;
      padding: 0;
      line-height: 50px;
      background: #333333;
      border: 1px solid #333333;
      border-radius: 0px;
      text-align: center;
      color: $mainGlod;
      font-size: 16px;
      cursor: pointer;
      &:hover{
        color: #FFF;
        background: $mainGlod;
        border: 1px solid $mainGlod;
      }
    }
  }
  </style>