MyProfile.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <el-row class="movie-list">
  3. <el-col :md="8" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  4. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  5. <el-card class="box-card">
  6. <div slot="header" class="clearfix">
  7. <span>我的资料</span>
  8. </div>
  9. <div class="text item">
  10. <el-form ref="form" :model="loginUser" label-width="80px">
  11. <el-form-item label="用户 ID">
  12. <el-input v-model="loginUser.userId" style="padding-right: 1px" readonly />
  13. </el-form-item>
  14. <el-form-item label="用户名">
  15. <el-input v-model="loginUser.username" style="width: 70%; padding-right: 10px" />
  16. <el-button size="mini" type="info" @click="updateScreenName">更新</el-button>
  17. </el-form-item>
  18. <el-form-item label="邮箱">
  19. <el-input v-model="loginUser.email" style="width: 70%; padding-right: 10px" />
  20. <el-button size="mini" type="info" @click="updateScreenName">更新</el-button>
  21. </el-form-item>
  22. <el-form-item label="手机">
  23. <el-input v-model="loginUser.mobile" style="width: 70%; padding-right: 10px" />
  24. <el-button size="mini" type="info" @click="updateScreenName">更新</el-button>
  25. </el-form-item>
  26. <el-form-item label="显示名">
  27. <el-input v-model="loginUser.screenName" style="width: 70%; padding-right: 10px" />
  28. <el-button size="mini" type="info" @click="updateScreenName">更新</el-button>
  29. </el-form-item>
  30. <el-form-item label="签名">
  31. <el-input v-model="loginUser.signature" style="width: 70%; padding-right: 10px" />
  32. <el-button size="mini" type="info" @click="updateSignature">更新</el-button>
  33. </el-form-item>
  34. </el-form>
  35. </div>
  36. </el-card>
  37. </el-row>
  38. </el-col>
  39. <el-col :md="8" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  40. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  41. <el-card class="box-card">
  42. <div slot="header" class="clearfix">
  43. <span>更新我的头像</span>
  44. </div>
  45. <div class="text item">
  46. <el-tooltip class="item" effect="dark" content="点击更新我的头像" placement="top-end">
  47. <el-upload
  48. class="avatar-uploader"
  49. :action="imgOssUrl"
  50. :headers="imgHeaders"
  51. :data="imgData"
  52. :with-credentials="true"
  53. :show-file-list="false"
  54. :before-upload="beforeAvatarUpload"
  55. :on-success="handleAvatarSuccess"
  56. >
  57. <img v-if="loginUser" :src="loginUser.avatarUrl" class="avatar">
  58. <i v-else class="el-icon-plus avatar-uploader-icon" />
  59. </el-upload>
  60. </el-tooltip>
  61. </div>
  62. </el-card>
  63. </el-row>
  64. </el-col>
  65. </el-row>
  66. </template>
  67. <script>
  68. import { updateAvatar } from '@/api/account'
  69. import { getAuthedUser, updateAuthedUser } from '@/utils/auth'
  70. import { getAvatarChannelInfo } from '@/api/file'
  71. export default {
  72. name: 'MyProfile',
  73. data() {
  74. return {
  75. imgOssUrl: '',
  76. imgHeaders: {
  77. Authorization: ''
  78. },
  79. imgData: {
  80. channelId: 0
  81. },
  82. coverUrl: null,
  83. // ****************************************************************************************************************
  84. loginUser: null
  85. }
  86. },
  87. created() {
  88. this.loginUser = getAuthedUser()
  89. getAvatarChannelInfo().then(res => {
  90. if (res.code === 0) {
  91. const resData = res.data
  92. this.imgData.channelId = resData.channelId
  93. this.imgOssUrl = resData.ossUrl
  94. this.imgHeaders.Authorization = 'Bearer ' + resData.token
  95. } else {
  96. this.$notify({
  97. title: '提示',
  98. message: '获取 OSS 服务器地址失败, 暂时无法上传文件',
  99. type: 'error',
  100. duration: 3000
  101. })
  102. }
  103. }).catch(error => {
  104. this.$notify({
  105. title: '提示',
  106. message: error.message,
  107. type: 'warning',
  108. duration: 3000
  109. })
  110. })
  111. },
  112. mounted() {
  113. },
  114. methods: {
  115. // ****************************************************************************************************************
  116. beforeAvatarUpload(file) {
  117. const isJPG = file.type === 'image/jpeg'
  118. const isLt2M = file.size / 1024 / 1024 < 2
  119. if (!isJPG) {
  120. this.$message.error('头像文件只能是 JPG 格式!')
  121. }
  122. if (!isLt2M) {
  123. this.$message.error('头像文件大小不能超过 2MB!')
  124. }
  125. return isJPG && isLt2M
  126. },
  127. handleAvatarSuccess(res, file) {
  128. if (res.code === 0) {
  129. const resData = res.data
  130. this.coverUrl = URL.createObjectURL(file.raw)
  131. const avatar = {}
  132. avatar.channelId = this.imgData.channelId
  133. avatar.uploadId = resData.uploadId
  134. updateAvatar(avatar).then(resp => {
  135. if (resp.code === 0) {
  136. this.loginUser.avatarUrl = resp.data.url
  137. updateAuthedUser(this.loginUser)
  138. } else {
  139. this.$notify({
  140. title: '头像更新失败',
  141. message: resp.msg,
  142. type: 'warning',
  143. duration: 3000
  144. })
  145. }
  146. })
  147. } else {
  148. this.$notify({
  149. title: '提示',
  150. message: '头像上传失败,请重试!' + res.msg,
  151. type: 'warning',
  152. duration: 3000
  153. })
  154. }
  155. },
  156. // ****************************************************************************************************************
  157. updateScreenName() {
  158. console.log('更新显示名')
  159. },
  160. updateSignature() {
  161. console.log('更新签名')
  162. }
  163. }
  164. }
  165. </script>
  166. <style>
  167. .uploader-example .uploader-btn {
  168. margin-right: 4px;
  169. }
  170. .uploader-example .uploader-list {
  171. max-height: 440px;
  172. overflow: auto;
  173. overflow-x: hidden;
  174. overflow-y: auto;
  175. }
  176. .avatar-uploader .el-upload {
  177. border: 1px dashed #d9d9d9;
  178. border-radius: 6px;
  179. cursor: pointer;
  180. position: relative;
  181. overflow: hidden;
  182. }
  183. .avatar-uploader .el-upload:hover {
  184. border-color: #409EFF;
  185. }
  186. .avatar-uploader-icon {
  187. font-size: 28px;
  188. color: #8c939d;
  189. width: 256px;
  190. height: 256px;
  191. line-height: 178px;
  192. text-align: center;
  193. }
  194. .avatar {
  195. width: 256px;
  196. height: 256px;
  197. display: block;
  198. }
  199. </style>