MyProfile.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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" readonly />
  16. <el-button size="mini" type="info" @click="showUpdateDialog(1)">更新</el-button>
  17. </el-form-item>
  18. <el-form-item label="邮箱">
  19. <el-input v-model="loginUser.email" style="width: 70%; padding-right: 10px" readonly />
  20. <el-button size="mini" type="info" @click="showUpdateDialog(2)">更新</el-button>
  21. </el-form-item>
  22. <el-form-item label="手机">
  23. <el-input v-model="loginUser.mobile" style="width: 70%; padding-right: 10px" readonly />
  24. <el-button size="mini" type="info" @click="showUpdateDialog(3)">更新</el-button>
  25. </el-form-item>
  26. <el-form-item label="显示名">
  27. <el-input v-model="loginUser.screenName" style="width: 70%; padding-right: 10px" readonly />
  28. <el-button size="mini" type="info" @click="showUpdateDialog(4)">更新</el-button>
  29. </el-form-item>
  30. <el-form-item label="签名">
  31. <el-input v-model="loginUser.signature" type="textarea" style="width: 70%; padding-right: 10px" readonly />
  32. <el-button size="mini" type="info" @click="showUpdateDialog(5)">更新</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-dialog
  66. :title="updateTitle"
  67. append-to-body
  68. :visible.sync="updateDialog"
  69. width="30%"
  70. center
  71. >
  72. <el-row>
  73. <el-col :md="12" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  74. <el-row>
  75. <el-form :inline="true" :model="updateForm">
  76. <el-form-item>
  77. <el-input
  78. v-model="updateForm.value"
  79. type="textarea"
  80. :rows="3"
  81. placeholder="添加留言..."
  82. />
  83. </el-form-item>
  84. <el-form-item>
  85. <el-button size="mini" type="warning" @click="onUpdate">更新</el-button>
  86. </el-form-item>
  87. </el-form>
  88. </el-row>
  89. </el-col>
  90. </el-row>
  91. </el-dialog>
  92. </el-row>
  93. </template>
  94. <script>
  95. import { updateAvatar } from '@/api/account'
  96. import { getAuthedUser, updateAuthedUser } from '@/utils/auth'
  97. import { getAvatarChannelInfo } from '@/api/file'
  98. export default {
  99. name: 'MyProfile',
  100. data() {
  101. return {
  102. imgOssUrl: '',
  103. imgHeaders: {
  104. Authorization: ''
  105. },
  106. imgData: {
  107. channelId: 0
  108. },
  109. coverUrl: null,
  110. // ****************************************************************************************************************
  111. loginUser: null,
  112. updateDialog: false,
  113. updateType: 1,
  114. updateTitle: '',
  115. updateForm: {
  116. type: 1,
  117. value: null
  118. }
  119. }
  120. },
  121. created() {
  122. this.loginUser = getAuthedUser()
  123. getAvatarChannelInfo().then(res => {
  124. if (res.code === 0) {
  125. const resData = res.data
  126. this.imgData.channelId = resData.channelId
  127. this.imgOssUrl = resData.ossUrl
  128. this.imgHeaders.Authorization = 'Bearer ' + resData.token
  129. } else {
  130. this.$notify({
  131. title: '提示',
  132. message: '获取 OSS 服务器地址失败, 暂时无法上传文件',
  133. type: 'error',
  134. duration: 3000
  135. })
  136. }
  137. }).catch(error => {
  138. this.$notify({
  139. title: '提示',
  140. message: error.message,
  141. type: 'warning',
  142. duration: 3000
  143. })
  144. })
  145. },
  146. mounted() {
  147. },
  148. methods: {
  149. // ****************************************************************************************************************
  150. beforeAvatarUpload(file) {
  151. const isJPG = file.type === 'image/jpeg'
  152. const isLt2M = file.size / 1024 / 1024 < 2
  153. if (!isJPG) {
  154. this.$message.error('头像文件只能是 JPG 格式!')
  155. }
  156. if (!isLt2M) {
  157. this.$message.error('头像文件大小不能超过 2MB!')
  158. }
  159. return isJPG && isLt2M
  160. },
  161. handleAvatarSuccess(res, file) {
  162. if (res.code === 0) {
  163. const resData = res.data
  164. this.coverUrl = URL.createObjectURL(file.raw)
  165. const avatar = {}
  166. avatar.channelId = this.imgData.channelId
  167. avatar.uploadId = resData.uploadId
  168. updateAvatar(avatar).then(resp => {
  169. if (resp.code === 0) {
  170. this.loginUser.avatarUrl = resp.data.url
  171. updateAuthedUser(this.loginUser)
  172. } else {
  173. this.$notify({
  174. title: '头像更新失败',
  175. message: resp.msg,
  176. type: 'warning',
  177. duration: 3000
  178. })
  179. }
  180. })
  181. } else {
  182. this.$notify({
  183. title: '提示',
  184. message: '头像上传失败,请重试!' + res.msg,
  185. type: 'warning',
  186. duration: 3000
  187. })
  188. }
  189. },
  190. // ****************************************************************************************************************
  191. showUpdateDialog(type) {
  192. if (type === 1) {
  193. this.updateType = 1
  194. this.updateTitle = '更新用户名'
  195. } else if (type === 2) {
  196. this.updateType = 2
  197. this.updateTitle = '更新用户邮箱'
  198. } else if (type === 3) {
  199. this.updateType = 3
  200. this.updateTitle = '更新用户手机号'
  201. } else if (type === 4) {
  202. this.updateType = 4
  203. this.updateTitle = '更新用户显示名'
  204. } else if (type === 5) {
  205. this.updateType = 5
  206. this.updateTitle = '更新用户签名'
  207. }
  208. this.updateDialog = true
  209. },
  210. onUpdate() {
  211. this.$message.info('更新用户资料')
  212. this.updateDialog = false
  213. }
  214. }
  215. }
  216. </script>
  217. <style>
  218. .uploader-example .uploader-btn {
  219. margin-right: 4px;
  220. }
  221. .uploader-example .uploader-list {
  222. max-height: 440px;
  223. overflow: auto;
  224. overflow-x: hidden;
  225. overflow-y: auto;
  226. }
  227. .avatar-uploader .el-upload {
  228. border: 1px dashed #d9d9d9;
  229. border-radius: 6px;
  230. cursor: pointer;
  231. position: relative;
  232. overflow: hidden;
  233. }
  234. .avatar-uploader .el-upload:hover {
  235. border-color: #409EFF;
  236. }
  237. .avatar-uploader-icon {
  238. font-size: 28px;
  239. color: #8c939d;
  240. width: 256px;
  241. height: 256px;
  242. line-height: 178px;
  243. text-align: center;
  244. }
  245. .avatar {
  246. width: 256px;
  247. height: 256px;
  248. display: block;
  249. }
  250. </style>