uploadImg.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <view class="uploadImg">
  3. <view
  4. class="uploadImg__item"
  5. v-for="(item, index) in imgArr || []"
  6. :key="index"
  7. @click="previewImage(imgArr, index)"
  8. >
  9. <image class="uploadImg__img" :src="item" mode="aspectFill"> </image>
  10. <view
  11. class="uploadImg__close"
  12. @click.stop="delImg(index)"
  13. v-if="type === 'add'"
  14. >
  15. <uv-icon
  16. name="close-circle-fill"
  17. size="40rpx"
  18. color="#000000"
  19. ></uv-icon>
  20. </view>
  21. </view>
  22. <view
  23. class="uploadImg__item add_img"
  24. v-if="imgArr.length < imgCount && type === 'add'"
  25. @click.stop="uploadImg"
  26. >
  27. <uv-icon name="plus" size="40rpx" color="#999999"></uv-icon>
  28. </view>
  29. </view>
  30. </template>
  31. <script setup name="uploadImg">
  32. import { ref, watch } from "vue";
  33. import { commonUpload_api } from "@/api/index";
  34. // 支持 v-model 的定义
  35. const props = defineProps({
  36. modelValue: {
  37. type: [Array, String],
  38. },
  39. imgArr: {
  40. type: Array,
  41. default: () => [],
  42. }, // 图片数组
  43. imgCount: {
  44. type: Number,
  45. default: 5,
  46. }, // 可上传图片总数
  47. uploadImgCount: {
  48. type: Number,
  49. default: 1,
  50. }, // 一次上传图片数
  51. type: {
  52. type: String,
  53. default: "add",
  54. }, // add 添加图片,view 查看
  55. imgSize: {
  56. //图片大小 单位M
  57. type: Number,
  58. default: 4,
  59. },
  60. imgType: {
  61. //如果是小程序,这个值则没用作用
  62. type: [Array],
  63. default: function () {
  64. return ["jpeg", "png", "jpg"];
  65. },
  66. },
  67. closeTip: {
  68. type: Boolean,
  69. default: false,
  70. },
  71. });
  72. const emit = defineEmits(["update:modelValue", "result"]);
  73. let list = props.modelValue ? typeof props.modelValue == "object" ? props.modelValue : [props.modelValue] : null;
  74. // console.log("list===>", list);
  75. // 使用 modelValue(v-model)或 imgArr 或 value 作为初始值
  76. const imgArr = ref(list || props.imgArr || []);
  77. const imgCount = ref(props.imgCount);
  78. const uploadImgCount = ref(props.uploadImgCount);
  79. const type = ref(props.type);
  80. const imgSize = ref(props.imgSize);
  81. const imgType = ref(props.imgType);
  82. const closeTip = ref(props.closeTip);
  83. // 监听 imgArr 变化
  84. watch(
  85. () => props.imgArr,
  86. (newValue) => {
  87. if (!props.modelValue) {
  88. imgArr.value = newValue || [];
  89. }
  90. }
  91. );
  92. // 监听 value 变化
  93. watch(
  94. () => props.modelValue,
  95. (newValue) => {
  96. // console.log("newValue===>", newValue);
  97. let newValueList = JSON.parse(JSON.stringify(newValue));
  98. let list = [];
  99. if (newValue) {
  100. if (typeof newValue == "object") {
  101. list = [...newValueList];
  102. } else {
  103. list = newValueList.split(",");
  104. }
  105. }
  106. imgArr.value = list;
  107. }
  108. );
  109. watch(
  110. () => props.imgCount,
  111. (newValue) => {
  112. // console.log("newValue===>", newValue);
  113. imgCount.value = (newValue || 0) * 1;
  114. }
  115. );
  116. watch(
  117. () => props.uploadImgCount,
  118. (newValue) => {
  119. uploadImgCount.value = (newValue || 0) * 1;
  120. }
  121. );
  122. watch(
  123. () => props.type,
  124. (newValue) => {
  125. type.value = newValue || "add";
  126. }
  127. );
  128. watch(
  129. () => props.imgSize,
  130. (newValue) => {
  131. imgSize.value = newValue;
  132. }
  133. );
  134. watch(
  135. () => props.imgType,
  136. (newValue) => {
  137. imgType.value = newValue;
  138. }
  139. );
  140. watch(
  141. () => props.closeTip,
  142. (newValue) => {
  143. closeTip.value = newValue;
  144. }
  145. );
  146. // 图片预览
  147. const previewImage = (urls, index) => {
  148. uni.previewImage({
  149. urls: urls,
  150. current: index,
  151. });
  152. };
  153. // 删除图片
  154. const delImg = (index) => {
  155. let list = [...imgArr.value];
  156. list.splice(index, 1);
  157. imgArr.value = list;
  158. emit("result", list);
  159. // 触发所有可能的事件
  160. if (props.imgCount > 1) {
  161. emit("update:modelValue", list);
  162. } else {
  163. emit("update:modelValue", "");
  164. }
  165. };
  166. // 上传图片
  167. const uploadImg = () => {
  168. // console.log("上传图片");
  169. uni.chooseImage({
  170. count: uploadImgCount.value,
  171. success(res) {
  172. console.log("上传图片成功", res);
  173. if (res.tempFiles[0].size > imgSize.value * 1024 * 1024) {
  174. uni.showToast({
  175. title: `图片不能大于${imgSize.value}M`,
  176. icon: "none",
  177. });
  178. return false;
  179. }
  180. uni.showLoading({ title: "上传中...", mask: true });
  181. commonUpload_api(res.tempFilePaths[0])
  182. .then((res) => {
  183. // console.log(res);
  184. uni.hideLoading();
  185. if (res.code == 200) {
  186. let list = [...imgArr.value];
  187. list.push(res.data.url);
  188. emit("result", list);
  189. // console.log("上传图片成功", props.imgCount > 1);
  190. // 触发所有可能的事件
  191. if (props.imgCount > 1) {
  192. emit("update:modelValue", list);
  193. } else {
  194. emit("update:modelValue", res.data.url);
  195. }
  196. }
  197. })
  198. .catch((err) => {
  199. console.log("上传图片失败", err);
  200. // uni.hideLoading();
  201. });
  202. },
  203. fail(err) {
  204. // console.log("上传图片失败", err);
  205. // uni.showToast({
  206. // title: "请检查格式是否正确" + imgType.value.join(","),
  207. // icon: "none",
  208. // duration: 3000,
  209. // });
  210. },
  211. });
  212. };
  213. </script>
  214. <style lang="scss" scoped>
  215. .uploadImg {
  216. display: flex;
  217. flex-wrap: wrap;
  218. .add_img {
  219. display: flex;
  220. align-items: center;
  221. justify-content: center;
  222. border: 1rpx dashed #999999;
  223. }
  224. .uploadImg__item {
  225. flex-shrink: 0;
  226. position: relative;
  227. width: 200rpx;
  228. height: 200rpx;
  229. margin-right: 20rpx;
  230. margin-bottom: 20rpx;
  231. border-radius: 10rpx;
  232. .uploadImg__img {
  233. width: 100%;
  234. height: 100%;
  235. border-radius: 10rpx;
  236. }
  237. .uploadImg__close {
  238. position: absolute;
  239. top: 0;
  240. right: 0;
  241. z-index: 1;
  242. // background-color: #ffffff;
  243. // border-radius: 50%;
  244. }
  245. }
  246. }
  247. </style>