profile.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <div class="full-width flex">
  3. <div class="flex flex-col flex-justify-around flex-center full-height">
  4. <div class="bold font-16" style="margin-top: -10px">我的信息</div>
  5. <div class="mt-10 ml-20" @click="show = true">
  6. <el-avatar :src="user.info.avatarUrl" :size="100"></el-avatar>
  7. <el-icon :size="25" style="margin-left: -30px">
  8. <EditPen />
  9. </el-icon>
  10. </div>
  11. <div class="mt-10" style="margin-left: 33px">
  12. {{ user.info.nickName }}
  13. </div>
  14. </div>
  15. <div
  16. class="flex flex-col flex-child-average mt-20"
  17. style="margin-left: 60px"
  18. >
  19. <el-input
  20. :placeholder="placeholder"
  21. size="large"
  22. maxlength="6"
  23. v-model="status"
  24. class="input"
  25. ></el-input>
  26. <div
  27. class="flex flex-center flex-justify-between mt-10 padding-left padding-right"
  28. >
  29. <el-tooltip
  30. v-for="icon in icons"
  31. :key="icon.value"
  32. :content="icon.tips"
  33. >
  34. <img
  35. :src="icon.icon"
  36. class="icon-task pointer"
  37. @click="changeWorkStatus(icon)"
  38. />
  39. </el-tooltip>
  40. </div>
  41. </div>
  42. <el-dialog title="修改头像" v-model="show" width="780px" @close="close">
  43. <div>
  44. <div>
  45. <span
  46. class="full-width text-left flex flex-justify-start padding-top padding-bottom"
  47. >
  48. 新头像不允许涉及政治敏感与色情;<br />
  49. 图片格式必须为:png,jpeg,jpg;不可大于2M;<br />
  50. 建议使用png格式图片,以保持最佳效果;建议图片尺寸为144px*144px
  51. </span>
  52. <div class="flex flex-justify-start padding-bottom">
  53. <upload-office
  54. accept=".jpg,.png,.jpeg"
  55. :max="1"
  56. :show-progress="false"
  57. @upload="upload"
  58. />
  59. </div>
  60. </div>
  61. <el-divider />
  62. <div class="flex flex-justify-start">
  63. <div class="ml-20 flex flex-justify-start flex-col">
  64. <div class="bold text-left">头像裁剪</div>
  65. <div class="mt-20 upload flex flex-center">
  66. <cropper
  67. ref="cropper"
  68. class="cropper"
  69. :src="img"
  70. :stencil-props="{
  71. aspectRatio: 1 / 1,
  72. resizable: false
  73. }"
  74. @change="changeAvatar"
  75. ></cropper>
  76. </div>
  77. </div>
  78. <div class="ml-20 flex flex-justify-start flex-col">
  79. <div class="bold text-left">头像预览</div>
  80. <div class="mt-20 preview flex flex-center">
  81. <el-avatar
  82. v-if="previewImg"
  83. :size="114"
  84. fit="fill"
  85. :src="previewImg"
  86. ></el-avatar>
  87. <el-avatar
  88. v-if="previewImg"
  89. :size="114"
  90. shape="square"
  91. fit="fill"
  92. class="ml-20"
  93. :src="previewImg"
  94. ></el-avatar>
  95. </div>
  96. </div>
  97. </div>
  98. <div class="mt-20 full-width flex flex-justify-end">
  99. <el-button type="primary" plain @click="show = false"
  100. >取 消
  101. </el-button>
  102. <el-button type="primary" @click="uploadImage">确 定</el-button>
  103. </div>
  104. </div>
  105. </el-dialog>
  106. </div>
  107. </template>
  108. <script>
  109. import { useStore } from '@/store/user.js'
  110. import { Cropper } from 'vue-advanced-cropper'
  111. import 'vue-advanced-cropper/dist/style.css'
  112. import api from '@/api/index.js'
  113. import website from '@/config/website.js'
  114. import { getToken } from '@/utils/auth.js'
  115. import { Base64 } from 'js-base64'
  116. import uploadOffice from '@/components/upload-office/index.vue'
  117. export default {
  118. components: {
  119. Cropper,
  120. uploadOffice
  121. },
  122. setup() {
  123. const user = useStore()
  124. return { user }
  125. },
  126. watch: {
  127. status: {
  128. handler(val) {
  129. if (val.length > 0) {
  130. setTimeout(() => {
  131. this.update()
  132. }, 2000)
  133. }
  134. },
  135. immediate: true
  136. }
  137. },
  138. data() {
  139. return {
  140. show: false,
  141. status: '',
  142. img: '',
  143. previewImg: '',
  144. placeholder: '请填写状态(最长6个字符)',
  145. id: '',
  146. headers: {
  147. Authorization: `Basic ${Base64.encode(
  148. `${website.clientId}:${website.clientSecret}`
  149. )}`,
  150. 'Blade-Auth': 'bearer ' + getToken()
  151. },
  152. icons: [
  153. {
  154. icon: new URL('../../../assets/svg/task/work.svg', import.meta.url)
  155. .href,
  156. tips: '沉迷工作',
  157. value: 0
  158. },
  159. {
  160. icon: new URL('../../../assets/svg/task/trip.svg', import.meta.url)
  161. .href,
  162. tips: '出差',
  163. value: 1
  164. },
  165. {
  166. icon: new URL('../../../assets/svg/task/leave.svg', import.meta.url)
  167. .href,
  168. tips: '休假',
  169. value: 2
  170. },
  171. {
  172. icon: new URL('../../../assets/svg/task/stay.svg', import.meta.url)
  173. .href,
  174. tips: '休息',
  175. value: 3
  176. }
  177. ]
  178. }
  179. },
  180. created() {
  181. this.info()
  182. },
  183. methods: {
  184. info() {
  185. this.$api.dash.workInfo().then(res => {
  186. if (res.code === 200) {
  187. this.placeholder = res.data.workStatusDescribe
  188. ? res.data.workStatusDescribe
  189. : '请填写状态(最长6个字符)'
  190. this.id = res.data.id === -1 ? '' : res.data.id
  191. } else {
  192. console.log(res)
  193. }
  194. })
  195. },
  196. update() {
  197. if (this.status.length === 0) {
  198. return
  199. }
  200. this.$api.dash
  201. .submit({ id: this.id, workStatusDescribe: this.status })
  202. .then(res => {
  203. if (res.code === 200) {
  204. console.log(res)
  205. } else {
  206. console.log(res)
  207. }
  208. })
  209. },
  210. changeWorkStatus(res) {
  211. this.status = res.tips
  212. },
  213. upload(list) {
  214. this.previewImg = list[0].filePath
  215. this.img = list[0].filePath
  216. },
  217. changeAvatar({ coordinates, image, canvas }) {
  218. this.previewImg = canvas.toDataURL()
  219. },
  220. uploadImage() {
  221. const { canvas } = this.$refs.cropper.getResult()
  222. if (canvas) {
  223. const form = new FormData()
  224. canvas.toBlob(blob => {
  225. form.append('file', blob)
  226. // You can use axios, superagent and other libraries instead here
  227. fetch(api.uploadPath, {
  228. method: 'POST',
  229. headers: this.headers,
  230. body: form
  231. })
  232. .then(response => response.json())
  233. .then(body => {
  234. if (body.code === 200) {
  235. this.updateAvatar(body.data.filePath)
  236. }
  237. })
  238. }, 'image/png')
  239. }
  240. },
  241. updateAvatar(avatar) {
  242. const data = {
  243. id: this.user.info.userId,
  244. deptId: this.user.info.deptId,
  245. avatar
  246. }
  247. this.$api.dash.updateAvatar(data).then(res => {
  248. if (res.code === 200) {
  249. this.$message.success(res.msg)
  250. this.user.info.avatarUrl = avatar
  251. this.user.setUserInfo(this.user.info)
  252. this.show = false
  253. } else {
  254. this.$message.error(res.msg)
  255. }
  256. })
  257. },
  258. close() {
  259. this.img = ''
  260. this.previewImg = ''
  261. }
  262. }
  263. }
  264. </script>
  265. <style lang="scss" scoped>
  266. .preview {
  267. background-color: #eeeeee;
  268. height: 240px;
  269. width: 360px;
  270. border-radius: 8px;
  271. }
  272. .upload {
  273. background-color: #eeeeee;
  274. height: 240px;
  275. width: 320px;
  276. border-radius: 8px;
  277. }
  278. .icon-task {
  279. width: 38px;
  280. height: 45px;
  281. object-fit: fill;
  282. }
  283. .cropper {
  284. width: 280px;
  285. height: 280px;
  286. }
  287. .input {
  288. height: 100px;
  289. :deep(.el-input__wrapper) {
  290. background-color: #eeeeee;
  291. }
  292. }
  293. </style>