avatarEdit.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <view class="f-straight">
  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. import { pathToBase64 } from '@/utils/image-tools.js'
  20. import { ref, watch } from 'vue'
  21. const props = defineProps({
  22. id: {
  23. type: String,
  24. default: ''
  25. },
  26. text: {
  27. type: String,
  28. default: ''
  29. },
  30. data: {
  31. type: String,
  32. default: ''
  33. }
  34. })
  35. const form = ref()
  36. const formData = ref({ avatar:'' })
  37. watch(
  38. () => props.data,
  39. (newVal) => {
  40. formData.value.avatar = newVal
  41. },
  42. { immediate: true },
  43. )
  44. // 图片预览
  45. const handlePreviewImage = () => {
  46. uni.previewImage({
  47. current: 0,
  48. urls: [formData.value.avatar]
  49. })
  50. }
  51. // 选择头像
  52. const uploadPhotos = () => {
  53. wx.chooseImage({
  54. count: 1,
  55. sizeType: ['original', 'compressed'],
  56. sourceType: ['album', 'camera'],
  57. async success(res) {
  58. console.log('res:', res)
  59. const size = res.tempFiles[0]?.size || 0
  60. if (size >= 31457280) {
  61. uni.showToast({
  62. icon: 'none',
  63. title: '头像上传大小不得超过 20MB !',
  64. duration: 2000
  65. })
  66. return
  67. }
  68. // 选取图片并转为base64
  69. formData.value.avatar = await pathToBase64(res.tempFilePaths[0])
  70. }
  71. })
  72. }
  73. const rules = {
  74. avatar:{
  75. rules: [{required: true, errorMessage: '请上传头像' }]
  76. }
  77. }
  78. const submit = async () => {
  79. return { id: props.id, data: formData.value.avatar}
  80. }
  81. defineExpose({
  82. id: props.id,
  83. submit
  84. })
  85. </script>
  86. <style lang="less" scoped>
  87. .upload-img{
  88. position: relative;
  89. width: 200rpx;
  90. height: 200rpx;
  91. border: 1px solid #f1f1f1;
  92. margin: 10rpx;
  93. }
  94. .upload-file{
  95. width: 200rpx;
  96. height: 200rpx;
  97. border: 1px solid #f1f1f1;
  98. margin: 10rpx;
  99. display: flex;
  100. justify-content: center;
  101. align-items: center;
  102. border-radius: 10rpx;
  103. }
  104. </style>