avatarEdit.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <view class="f-straight wrapper">
  3. <uni-forms ref="form" :modelValue="formData" :rules="rules" validateTrigger="bind" label-width="105px" label-align="right">
  4. <uni-forms-item label="头像" name="avatar" class="f-straight" required>
  5. <view style="display: flex;flex-wrap: wrap;">
  6. <view class="upload-img" v-if="formData?.avatar">
  7. <uni-icons size="35" type="clear" color="#fe574a" style="position: absolute;right: -15px; top: -15px; z-index: 9" @click="formData.avatar = ''"></uni-icons>
  8. <image :src="formData?.avatar" mode="contain" style="width: 200rpx;height: 200rpx;" @click="handlePreviewImage"></image>
  9. </view>
  10. <view v-else class="upload-file" @click="uploadPhotos">
  11. <uni-icons type="plusempty" size="50" color="#f1f1f1"></uni-icons>
  12. </view>
  13. </view>
  14. </uni-forms-item>
  15. </uni-forms>
  16. </view>
  17. </template>
  18. <script setup>
  19. defineOptions({name: 'resumeAnalysis-avatarEdit'})
  20. import { pathToBase64 } from '@/utils/image-tools.js'
  21. import { ref, watch } from 'vue'
  22. const props = defineProps({
  23. data: {
  24. type: String,
  25. default: ''
  26. }
  27. })
  28. const form = ref()
  29. const formData = ref({ avatar:'' })
  30. watch(
  31. () => props.data,
  32. (newVal) => {
  33. formData.value.avatar = newVal
  34. },
  35. { immediate: true },
  36. )
  37. // 图片预览
  38. const handlePreviewImage = () => {
  39. uni.previewImage({
  40. current: 0,
  41. urls: [formData.value.avatar]
  42. })
  43. }
  44. // 选择头像
  45. const uploadPhotos = () => {
  46. wx.chooseImage({
  47. count: 1,
  48. sizeType: ['original', 'compressed'],
  49. sourceType: ['album', 'camera'],
  50. async success(res) {
  51. console.log('res:', res)
  52. const size = res.tempFiles[0]?.size || 0
  53. if (size >= 31457280) {
  54. uni.showToast({
  55. icon: 'none',
  56. title: '头像上传大小不得超过 20MB !',
  57. duration: 2000
  58. })
  59. return
  60. }
  61. // 选取图片并转为base64
  62. formData.value.avatar = await pathToBase64(res.tempFilePaths[0])
  63. }
  64. })
  65. }
  66. const rules = {
  67. avatar:{
  68. rules: [{required: true, errorMessage: '请上传头像' }]
  69. }
  70. }
  71. // const submit = async () => {
  72. // const valid = await unref(form).validate()
  73. // if (!valid) return
  74. // }
  75. </script>
  76. <style lang="less" scoped>
  77. .wrapper{
  78. padding: 15px;
  79. padding-top: 30px;
  80. }
  81. .upload-img{
  82. position: relative;
  83. width: 200rpx;
  84. height: 200rpx;
  85. border: 1px solid #f1f1f1;
  86. margin: 10rpx;
  87. }
  88. .upload-file{
  89. width: 200rpx;
  90. height: 200rpx;
  91. border: 1px solid #f1f1f1;
  92. margin: 10rpx;
  93. display: flex;
  94. justify-content: center;
  95. align-items: center;
  96. border-radius: 10rpx;
  97. }
  98. </style>