avatarEdit.vue 2.5 KB

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