imageUpload.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <template>
  2. <view class="con">
  3. <template v-if="viewWidth">
  4. <movable-area class="area" :style="{ height: areaHeight }" @mouseenter="mouseenter" @mouseleave="mouseleave">
  5. <movable-view
  6. v-for="(item, index) in imageList"
  7. :key="item.id"
  8. class="view"
  9. direction="all"
  10. :y="item.y"
  11. :x="item.x"
  12. :damping="40"
  13. :disabled="item.disable"
  14. @change="onChange($event, item)"
  15. @touchstart="touchstart(item)"
  16. @mousedown="touchstart(item)"
  17. @touchend="touchend(item)"
  18. @mouseup="touchend(item)"
  19. :style="{
  20. width: viewWidth + 'px',
  21. height: viewWidth + 'px',
  22. 'z-index': item.zIndex,
  23. opacity: item.opacity,
  24. }"
  25. >
  26. <view
  27. class="area-con"
  28. :style="{
  29. width: childWidth,
  30. height: childWidth,
  31. borderRadius: borderRadius + 'rpx',
  32. transform: 'scale(' + item.scale + ')',
  33. }"
  34. >
  35. <image class="pre-image" :src="item.src" mode="aspectFill"></image>
  36. <view class="del-con" @click="delImages(item, index)" @touchstart.stop="delImageMp(item, index)" @touchend.stop="nothing()" @mousedown.stop="nothing()" @mouseup.stop="nothing()">
  37. <view class="del-wrap">
  38. <image
  39. class="del-image"
  40. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAhCAYAAABX5MJvAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAhdEVYdENyZWF0aW9uIFRpbWUAMjAyMDowNzoyNSAyMTo1NDoyOU4TkJAAAADcSURBVFhH7ZfRCoMwDEXLvkjwwVf/bH/emmAyN6glTW9WBjsgwm28OeCLpj81Sil7zvlJ90UiONS/yY5VogsO6XrBg3IEQ5a/s8vRSWUAKmLqp2w5jz5BiNQEGMo3GbloDLtFXJ1IkaEuhAiiY6gEIqB4yqACSk9piIBiKQ8VUFpLviKg3C2rESKgWERCBZSWiEfgIfffYvrrsAgoISJ3Apy3zuTxcSxLQkV6ykNEPKVQkZEyiAiiZKgDIaC4upACSlcn5fM/+WuDCAHF1E/Z/N9AhkMZnPNDPI+UDjPIXgAQIGjNAAAAAElFTkSuQmCC"
  41. >
  42. </image>
  43. </view>
  44. </view>
  45. </view>
  46. </movable-view>
  47. <view class="add" v-if="imageList.length < number" :style="{ top: add.y, left: add.x, width: viewWidth + 'px', height: viewWidth + 'px' }" @click="addImages">
  48. <view class="add-wrap" :style="{ width: childWidth, height: childWidth, borderRadius: borderRadius + 'rpx' }">
  49. <image
  50. style="width: 54rpx; height: 54rpx"
  51. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADYAAAA2CAYAAACMRWrdAAABIUlEQVRoQ+2a2w2DMAxFeQzWrsMUbadAsEw3S1CqVgppKwLX8BEOP4iHTXx8uUgWdVXoVhdaV0VhSmf7vr/H8V3XzY6V3P9iD+nYOI5P7/01LMI596AwoZV0TIBXIUWFXhKLFBWYSFGhhxQN6SFFQ5i4ogITKSr0cEVDekjRECauqMBEigq9U7piOk2yAti27SUe5ljlTfPEQ6KZecTvwl4P3ytvOv06R2HDMNzes7+6aRrvnHvtf50L13Lp50rx88zcvNlS3JpwKQ67XyK04nq2nFbk/LqVjin0TvmBNgQ2S4UUDcliHgpMpKjQwxUN6SFFQ5i4ogITKSr0cEVDekjRECauqMAsVoph+hVPtYr5+03p9tbYQ96xrYtT4ootbAJGVxxVTapVswAAAABJRU5ErkJggg=="
  52. >
  53. </image>
  54. </view>
  55. </view>
  56. </movable-area>
  57. </template>
  58. </view>
  59. </template>
  60. <script>
  61. export default {
  62. emits: ["input", "update:modelValue"],
  63. props: {
  64. // 排序图片
  65. value: {
  66. type: Array,
  67. default: function () {
  68. return [];
  69. },
  70. },
  71. // 排序图片
  72. modelValue: {
  73. type: Array,
  74. default: function () {
  75. return [];
  76. },
  77. },
  78. // 从 list 元素对象中读取的键名
  79. keyName: {
  80. type: String,
  81. default: null,
  82. },
  83. // 选择图片数量限制
  84. number: {
  85. type: Number,
  86. default: 6,
  87. },
  88. // 图片父容器宽度(实际显示的图片宽度为 imageWidth / 1.1 ),单位 rpx
  89. // imageWidth > 0 则 cols 无效
  90. imageWidth: {
  91. type: Number,
  92. default: 0,
  93. },
  94. // 图片列数
  95. cols: {
  96. type: Number,
  97. default: 3,
  98. },
  99. // 图片圆角,单位 rpx
  100. borderRadius: {
  101. type: Number,
  102. default: 0,
  103. },
  104. // 图片周围空白填充,单位 rpx
  105. padding: {
  106. type: Number,
  107. default: 10,
  108. },
  109. // 拖动图片时放大倍数 [0, ∞)
  110. scale: {
  111. type: Number,
  112. default: 1.1,
  113. },
  114. // 拖动图片时不透明度
  115. opacity: {
  116. type: Number,
  117. default: 0.7,
  118. },
  119. // 自定义添加
  120. addImage: {
  121. type: Function,
  122. default: null,
  123. },
  124. // 删除确认
  125. delImage: {
  126. type: Function,
  127. default: null,
  128. },
  129. },
  130. data() {
  131. return {
  132. imageList: [],
  133. width: 0,
  134. add: {
  135. x: 0,
  136. y: 0,
  137. },
  138. colsValue: 0,
  139. viewWidth: 0,
  140. tempItem: null,
  141. timer: null,
  142. changeStatus: true,
  143. preStatus: true,
  144. first: true,
  145. };
  146. },
  147. computed: {
  148. areaHeight() {
  149. let height = "";
  150. // return '355px'
  151. if (this.imageList.length < this.number) {
  152. height = (Math.ceil((this.imageList.length + 1) / this.colsValue) * this.viewWidth).toFixed() + "px";
  153. } else {
  154. height = (Math.ceil(this.imageList.length / this.colsValue) * this.viewWidth).toFixed() + "px";
  155. }
  156. console.log("areaHeight", height);
  157. return height;
  158. },
  159. childWidth() {
  160. return this.viewWidth - this.rpx2px(this.padding) * 2 + "px";
  161. },
  162. },
  163. watch: {
  164. value: {
  165. handler(n) {
  166. if (!this.first && this.changeStatus) {
  167. console.log("watch", n);
  168. let flag = false;
  169. for (let i = 0; i < n.length; i++) {
  170. if (flag) {
  171. this.addProperties(this.getSrc(n[i]));
  172. continue;
  173. }
  174. if (this.imageList.length === i || this.imageList[i].src !== this.getSrc(n[i])) {
  175. flag = true;
  176. this.imageList.splice(i);
  177. this.addProperties(this.getSrc(n[i]));
  178. }
  179. }
  180. }
  181. },
  182. deep: true,
  183. },
  184. modelValue: {
  185. handler(n) {
  186. if (!this.first && this.changeStatus) {
  187. console.log("watch", n);
  188. let flag = false;
  189. for (let i = 0; i < n.length; i++) {
  190. if (flag) {
  191. this.addProperties(this.getSrc(n[i]));
  192. continue;
  193. }
  194. if (this.imageList.length === i || this.imageList[i].src !== this.getSrc(n[i])) {
  195. flag = true;
  196. this.imageList.splice(i);
  197. this.addProperties(this.getSrc(n[i]));
  198. }
  199. }
  200. }
  201. },
  202. deep: true,
  203. },
  204. },
  205. created() {
  206. this.width = uni.getSystemInfoSync().windowWidth;
  207. },
  208. mounted() {
  209. const query = uni.createSelectorQuery().in(this);
  210. query.select(".con").boundingClientRect((data) => {
  211. this.colsValue = this.cols;
  212. this.viewWidth = data.width / this.cols;
  213. if (this.imageWidth > 0) {
  214. this.viewWidth = this.rpx2px(this.imageWidth);
  215. this.colsValue = Math.floor(data.width / this.viewWidth);
  216. }
  217. let list = this.value;
  218. // #ifdef VUE3
  219. list = this.modelValue;
  220. // #endif
  221. for (let item of list) {
  222. this.addProperties(this.getSrc(item));
  223. }
  224. this.first = false;
  225. });
  226. query.exec();
  227. },
  228. methods: {
  229. getSrc(item) {
  230. if (this.keyName !== null) {
  231. return item[this.keyName];
  232. }
  233. return item;
  234. },
  235. onChange(e, item) {
  236. if (!item) return;
  237. item.oldX = e.detail.x;
  238. item.oldY = e.detail.y;
  239. if (e.detail.source === "touch") {
  240. if (item.moveEnd) {
  241. item.offset = Math.sqrt(Math.pow(item.oldX - item.absX * this.viewWidth, 2) + Math.pow(item.oldY - item.absY * this.viewWidth, 2));
  242. }
  243. let x = Math.floor((e.detail.x + this.viewWidth / 2) / this.viewWidth);
  244. if (x >= this.colsValue) return;
  245. let y = Math.floor((e.detail.y + this.viewWidth / 2) / this.viewWidth);
  246. let index = this.colsValue * y + x;
  247. if (item.index != index && index < this.imageList.length) {
  248. this.changeStatus = false;
  249. for (let obj of this.imageList) {
  250. if (item.index > index && obj.index >= index && obj.index < item.index) {
  251. this.change(obj, 1);
  252. } else if (item.index < index && obj.index <= index && obj.index > item.index) {
  253. this.change(obj, -1);
  254. } else if (obj.id != item.id) {
  255. obj.offset = 0;
  256. obj.x = obj.oldX;
  257. obj.y = obj.oldY;
  258. setTimeout(() => {
  259. this.$nextTick(() => {
  260. obj.x = obj.absX * this.viewWidth;
  261. obj.y = obj.absY * this.viewWidth;
  262. });
  263. }, 0);
  264. }
  265. }
  266. item.index = index;
  267. item.absX = x;
  268. item.absY = y;
  269. if (!item.moveEnd) {
  270. setTimeout(() => {
  271. this.$nextTick(() => {
  272. item.x = item.absX * this.viewWidth;
  273. item.y = item.absY * this.viewWidth;
  274. });
  275. }, 0);
  276. }
  277. // console.log('bbb', JSON.parse(JSON.stringify(item)));
  278. this.sortList();
  279. }
  280. }
  281. },
  282. change(obj, i) {
  283. obj.index += i;
  284. obj.offset = 0;
  285. obj.x = obj.oldX;
  286. obj.y = obj.oldY;
  287. obj.absX = obj.index % this.colsValue;
  288. obj.absY = Math.floor(obj.index / this.colsValue);
  289. setTimeout(() => {
  290. this.$nextTick(() => {
  291. obj.x = obj.absX * this.viewWidth;
  292. obj.y = obj.absY * this.viewWidth;
  293. });
  294. }, 0);
  295. },
  296. touchstart(item) {
  297. this.imageList.forEach((v) => {
  298. v.zIndex = v.index + 9;
  299. });
  300. item.zIndex = 99;
  301. item.moveEnd = true;
  302. this.tempItem = item;
  303. this.timer = setTimeout(() => {
  304. item.scale = this.scale;
  305. item.opacity = this.opacity;
  306. clearTimeout(this.timer);
  307. this.timer = null;
  308. }, 200);
  309. },
  310. touchend(item) {
  311. this.previewImage(item);
  312. item.scale = 1;
  313. item.opacity = 1;
  314. item.x = item.oldX;
  315. item.y = item.oldY;
  316. item.offset = 0;
  317. item.moveEnd = false;
  318. setTimeout(() => {
  319. this.$nextTick(() => {
  320. item.x = item.absX * this.viewWidth;
  321. item.y = item.absY * this.viewWidth;
  322. this.tempItem = null;
  323. this.changeStatus = true;
  324. });
  325. // console.log('ccc', JSON.parse(JSON.stringify(item)));
  326. }, 0);
  327. // console.log('ddd', JSON.parse(JSON.stringify(item)));
  328. },
  329. previewImage(item) {
  330. if (this.timer && this.preStatus && this.changeStatus && item.offset < 28.28) {
  331. clearTimeout(this.timer);
  332. this.timer = null;
  333. const list = this.value || this.modelValue;
  334. let srcList = list.map((v) => this.getSrc(v));
  335. console.log(list, srcList);
  336. uni.previewImage({
  337. urls: srcList,
  338. current: item.src,
  339. success: () => {
  340. this.preStatus = false;
  341. setTimeout(() => {
  342. this.preStatus = true;
  343. }, 600);
  344. },
  345. fail: (e) => {
  346. console.log(e);
  347. },
  348. });
  349. } else if (this.timer) {
  350. clearTimeout(this.timer);
  351. this.timer = null;
  352. }
  353. },
  354. mouseenter() {
  355. //#ifdef H5
  356. this.imageList.forEach((v) => {
  357. v.disable = false;
  358. });
  359. //#endif
  360. },
  361. mouseleave() {
  362. //#ifdef H5
  363. if (this.tempItem) {
  364. this.imageList.forEach((v) => {
  365. v.disable = true;
  366. v.zIndex = v.index + 9;
  367. v.offset = 0;
  368. v.moveEnd = false;
  369. if (v.id == this.tempItem.id) {
  370. if (this.timer) {
  371. clearTimeout(this.timer);
  372. this.timer = null;
  373. }
  374. v.scale = 1;
  375. v.opacity = 1;
  376. v.x = v.oldX;
  377. v.y = v.oldY;
  378. this.$nextTick(() => {
  379. v.x = v.absX * this.viewWidth;
  380. v.y = v.absY * this.viewWidth;
  381. this.tempItem = null;
  382. });
  383. }
  384. });
  385. this.changeStatus = true;
  386. }
  387. //#endif
  388. },
  389. addImages() {
  390. if (typeof this.addImage === "function") {
  391. this.addImage.bind(this.$parent)();
  392. } else {
  393. let checkNumber = this.number - this.imageList.length;
  394. uni.chooseImage({
  395. count: checkNumber,
  396. sourceType: ["album", "camera"],
  397. success: (res) => {
  398. let count = checkNumber <= res.tempFilePaths.length ? checkNumber : res.tempFilePaths.length;
  399. for (let i = 0; i < count; i++) {
  400. this.addProperties(res.tempFilePaths[i]);
  401. }
  402. this.sortList();
  403. },
  404. });
  405. }
  406. },
  407. delImages(item, index) {
  408. if (typeof this.delImage === "function") {
  409. this.delImage.bind(this.$parent)(() => {
  410. this.delImageHandle(item, index);
  411. });
  412. } else {
  413. this.delImageHandle(item, index);
  414. }
  415. },
  416. delImageHandle(item, index) {
  417. this.imageList.splice(index, 1);
  418. for (let obj of this.imageList) {
  419. if (obj.index > item.index) {
  420. obj.index -= 1;
  421. obj.x = obj.oldX;
  422. obj.y = obj.oldY;
  423. obj.absX = obj.index % this.colsValue;
  424. obj.absY = Math.floor(obj.index / this.colsValue);
  425. this.$nextTick(() => {
  426. obj.x = obj.absX * this.viewWidth;
  427. obj.y = obj.absY * this.viewWidth;
  428. });
  429. }
  430. }
  431. this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + "px";
  432. this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + "px";
  433. this.sortList();
  434. },
  435. delImageMp(item, index) {
  436. //#ifdef MP
  437. this.delImages(item, index);
  438. //#endif
  439. },
  440. sortList() {
  441. console.log("sortList");
  442. const result = [];
  443. let source = this.value;
  444. // #ifdef VUE3
  445. source = this.modelValue;
  446. // #endif
  447. let list = this.imageList.slice();
  448. list.sort((a, b) => {
  449. return a.index - b.index;
  450. });
  451. for (let s of list) {
  452. let item = source.find((d) => this.getSrc(d) == s.src);
  453. if (item) {
  454. result.push(item);
  455. } else {
  456. if (this.keyName !== null) {
  457. result.push({
  458. [this.keyName]: s.src,
  459. });
  460. } else {
  461. result.push(s.src);
  462. }
  463. }
  464. }
  465. this.$emit("input", result);
  466. this.$emit("update:modelValue", result);
  467. },
  468. addProperties(item) {
  469. console.log(item);
  470. let absX = this.imageList.length % this.colsValue;
  471. let absY = Math.floor(this.imageList.length / this.colsValue);
  472. let x = absX * this.viewWidth;
  473. let y = absY * this.viewWidth;
  474. this.imageList.push({
  475. src: item,
  476. x,
  477. y,
  478. oldX: x,
  479. oldY: y,
  480. absX,
  481. absY,
  482. scale: 1,
  483. zIndex: 9,
  484. opacity: 1,
  485. index: this.imageList.length,
  486. id: this.guid(16),
  487. disable: false,
  488. offset: 0,
  489. moveEnd: false,
  490. });
  491. this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + "px";
  492. this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + "px";
  493. },
  494. nothing() {},
  495. rpx2px(v) {
  496. return (this.width * v) / 750;
  497. },
  498. guid(len = 32) {
  499. const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
  500. const uuid = [];
  501. const radix = chars.length;
  502. for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
  503. uuid.shift();
  504. return `u${uuid.join("")}`;
  505. },
  506. },
  507. };
  508. </script>
  509. <style lang="scss" scoped>
  510. .con {
  511. // padding: 30rpx;
  512. .area {
  513. width: 100%;
  514. .view {
  515. display: flex;
  516. justify-content: center;
  517. align-items: center;
  518. .area-con {
  519. position: relative;
  520. overflow: hidden;
  521. .pre-image {
  522. width: 100%;
  523. height: 100%;
  524. }
  525. .del-con {
  526. position: absolute;
  527. top: 0rpx;
  528. right: 0rpx;
  529. padding: 0 0 20rpx 20rpx;
  530. .del-wrap {
  531. width: 36rpx;
  532. height: 36rpx;
  533. background-color: rgba(0, 0, 0, 0.4);
  534. border-radius: 0 0 0 10rpx;
  535. display: flex;
  536. justify-content: center;
  537. align-items: center;
  538. .del-image {
  539. width: 20rpx;
  540. height: 20rpx;
  541. }
  542. }
  543. }
  544. }
  545. }
  546. .add {
  547. position: absolute;
  548. display: flex;
  549. justify-content: center;
  550. align-items: center;
  551. .add-wrap {
  552. display: flex;
  553. justify-content: center;
  554. align-items: center;
  555. background-color: #eeeeee;
  556. }
  557. }
  558. }
  559. }
  560. </style>