user-top-image.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <v-row justify="center" align="center">
  3. <v-col>
  4. <v-card
  5. class="mx-auto"
  6. outlined
  7. >
  8. <v-row justify="center">
  9. <v-col cols="10">
  10. <h2>首页顶部大图修改</h2>
  11. </v-col>
  12. </v-row>
  13. <v-row justify="center">
  14. <v-col cols="10">
  15. <v-img :src="userInfo.backgroundUrl" :aspect-ratio="5.98" />
  16. </v-col>
  17. </v-row>
  18. <v-row justify="center">
  19. <v-col cols="10">
  20. <v-file-input
  21. :rules="rules"
  22. accept="image/png, image/jpeg, image/bmp"
  23. placeholder="选择图片"
  24. prepend-icon="mdi-camera"
  25. label="首页顶部大图"
  26. @change="setFile"
  27. />
  28. <v-btn color="primary" @click="uploadFile">
  29. 上传
  30. </v-btn>
  31. </v-col>
  32. </v-row>
  33. <v-row justify="center">
  34. <v-col cols="10">
  35. <v-btn color="primary" @click="save">
  36. 保存
  37. </v-btn>
  38. </v-col>
  39. </v-row>
  40. </v-card>
  41. </v-col>
  42. <v-snackbar
  43. v-model="showMessage"
  44. :top="true"
  45. :timeout="3000"
  46. >
  47. {{ message }}
  48. <template v-slot:action="{ attrs }">
  49. <v-btn
  50. color="pink"
  51. text
  52. v-bind="attrs"
  53. @click="showMessage = false"
  54. >
  55. 关闭
  56. </v-btn>
  57. </template>
  58. </v-snackbar>
  59. </v-row>
  60. </template>
  61. <script>
  62. export default {
  63. name: 'UserTopSetting',
  64. data() {
  65. return {
  66. userInfo: {
  67. username: ''
  68. },
  69. files: [],
  70. rules: [
  71. value => !value || value.size < 2000000 || '图片大小必须在2MB以内!'
  72. ],
  73. showMessage: false,
  74. message: ''
  75. }
  76. },
  77. created() {
  78. this.userInfo = this.$store.state.user.userInfo
  79. },
  80. methods: {
  81. setFile(value) {
  82. this.files = []
  83. this.files.push(value)
  84. },
  85. uploadFile() {
  86. if (this.files.length === 0) {
  87. this.message = '请先选择图片,然后上传!'
  88. this.showMessage = true
  89. return
  90. }
  91. const formData = new FormData()
  92. for (let i = 0; i < this.files.length; i++) {
  93. formData.append('file[]', this.files[i])
  94. }
  95. fetch(`/api/upload/top`, {
  96. headers: {
  97. 'X-XSRF-TOKEN': this.$cookies.get('XSRF-TOKEN')
  98. },
  99. method: 'POST',
  100. credentials: 'include',
  101. body: formData
  102. }).then(response => response.json())
  103. .then(json => {
  104. if (json.status === 200) {
  105. //
  106. this.userInfo.backgroundUrl = json.data[0].fileUrl
  107. this.message = '上传成功,请点击保存,保存设置!'
  108. this.showMessage = true
  109. } else {
  110. this.message = '上传失败,请重试!' + json.message
  111. this.showMessage = true
  112. }
  113. })
  114. .catch(e => {
  115. return null
  116. })
  117. },
  118. save() {
  119. fetch(`/api/user/update/top`, {
  120. headers: {
  121. 'Content-Type': 'application/json; charset=UTF-8',
  122. 'X-XSRF-TOKEN': this.$cookies.get('XSRF-TOKEN')
  123. },
  124. method: 'POST',
  125. credentials: 'include',
  126. body: JSON.stringify(this.userInfo)
  127. }).then(response => response.json())
  128. .then(json => {
  129. if (json.status === 200) {
  130. this.$store.commit('user/SET_USER_INFO', this.userInfo)
  131. this.message = '保存成功!'
  132. this.showMessage = true
  133. } else {
  134. //
  135. this.message = '保存失败!' + json.message
  136. this.showMessage = true
  137. }
  138. })
  139. .catch(e => {
  140. return null
  141. })
  142. }
  143. }
  144. }
  145. </script>
  146. <style>
  147. </style>