shmily-drag-image.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <template>
  2. <view class="con">
  3. <movable-area class="area" :style="{ height: areaHeight ? areaHeight : imageHeight + 'rpx' }" @mouseenter="mouseenter" @mouseleave="mouseleave">
  4. <block v-for="(item, index) in imageList" :key="item.id">
  5. <movable-view
  6. class="view"
  7. :x="item.x"
  8. :y="item.y"
  9. direction="all"
  10. :damping="40"
  11. :disabled="item.disable"
  12. @change="onChange($event, item)"
  13. @touchstart="touchstart(item)"
  14. @mousedown="touchstart(item)"
  15. @touchend="touchend(item)"
  16. @mouseup="touchend(item)"
  17. :style="{ width: viewWidth + 'px', height: viewWidth + 'px', 'z-index': item.zIndex, opacity: item.opacity }"
  18. >
  19. <view class="area-con" :style="{ width: childWidth, height: childWidth, transform: 'scale(' + item.scale + ')' }">
  20. <image class="pre-image" :src="$util.img(item.src)" mode="aspectFit"></image>
  21. <view
  22. class="del-con"
  23. @click="delImage(item, index)"
  24. @touchstart.stop="delImageMp(item, index)"
  25. @touchend.stop="nothing()"
  26. @mousedown.stop="nothing()"
  27. @mouseup.stop="nothing()"
  28. >
  29. <view class="del-wrap iconfont iconclose"></view>
  30. </view>
  31. </view>
  32. </movable-view>
  33. </block>
  34. <view class="add" v-if="imageList.length < number" :style="{ top: add.y, left: add.x, width: viewWidth + 'px', height: viewWidth + 'px' }" @click="addImages">
  35. <view class="add-wrap iconfont iconadd1" :style="{ width: childWidth, height: childWidth }"></view>
  36. </view>
  37. </movable-area>
  38. <!-- 选择图片 -->
  39. <uni-popup ref="choosePicturePopup" type="bottom">
  40. <view class="popup choose-picture" @touchmove.prevent.stop>
  41. <view class="popup-body" :class="{ 'safe-area': isIphoneX }">
  42. <view class="select-wrap">
  43. <view class="item color-base-text" @click="goAlbum()">从相册图库选择</view>
  44. <view class="item" @click="photograph()">手机拍照</view>
  45. <view class="item" @click="selectPhonePhoto()">从手机相册选择</view>
  46. </view>
  47. <view class="item cancle" @click="closeChoosePicturePop()">取消</view>
  48. </view>
  49. </view>
  50. </uni-popup>
  51. </view>
  52. </template>
  53. <script>
  54. export default {
  55. data() {
  56. return {
  57. imageList: [],
  58. width: 0,
  59. add: {
  60. x: 0,
  61. y: 0
  62. },
  63. colsValue: 0,
  64. viewWidth: 0,
  65. tempItem: null,
  66. timer: null,
  67. changeStatus: true,
  68. preStatus: true,
  69. isIphoneX: false
  70. };
  71. },
  72. props: {
  73. // 返回排序后图片
  74. list: {
  75. type: Array,
  76. default: function() {
  77. return [];
  78. }
  79. },
  80. // 选择图片数量限制
  81. number: {
  82. type: Number,
  83. default: 6
  84. },
  85. // 选择图片的数组下标
  86. index: {
  87. type: Number,
  88. default: 0
  89. },
  90. // 图片父容器宽度(实际显示的图片宽度为 imageWidth / 1.1 ),单位 rpx
  91. imageWidth: {
  92. type: Number,
  93. default: 230
  94. },
  95. imageHeight: {
  96. type: Number,
  97. default: 230
  98. },
  99. // 图片列数(cols > 0 则 imageWidth 无效)
  100. cols: {
  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. // 自定义添加(需配合 @aaddImage 事件使用)
  120. custom: {
  121. type: Boolean,
  122. default: false
  123. },
  124. uploadMethod: {
  125. type: String,
  126. default: 'image'
  127. },
  128. // 开启选择图片方式
  129. openSelectMode: {
  130. type: Boolean,
  131. default: false
  132. },
  133. // 是否需要等待数据
  134. isAWait: {
  135. type: Boolean,
  136. default: false
  137. }
  138. },
  139. computed: {
  140. areaHeight() {
  141. if (this.isAWait && this.colsValue == 0) return '';
  142. var height = '';
  143. if (this.imageList.length < this.number) {
  144. height = Math.ceil((this.imageList.length + 1) / this.colsValue) * this.viewWidth;
  145. } else {
  146. height = Math.ceil(this.imageList.length / this.colsValue) * this.viewWidth;
  147. }
  148. if (height != 'Infinity') return height + 'px';
  149. else return '';
  150. },
  151. childWidth() {
  152. return this.viewWidth - this.rpx2px(this.padding) * 2 + 'px';
  153. }
  154. },
  155. created() {
  156. this.width = uni.getSystemInfoSync().windowWidth;
  157. this.viewWidth = this.rpx2px(this.imageWidth);
  158. this.isIphoneX = this.$util.uniappIsIPhoneX();
  159. },
  160. mounted() {
  161. this.refresh();
  162. },
  163. methods: {
  164. refresh() {
  165. var flag = false;
  166. var time = setInterval(() => {
  167. if (this.isAWait && this.list.length == 0) return;
  168. this.imageList = [];
  169. const query = uni.createSelectorQuery().in(this);
  170. query.select('.area').boundingClientRect(data => {
  171. this.colsValue = Math.floor(data.width / this.viewWidth);
  172. if (this.cols > 0) {
  173. this.colsValue = this.cols;
  174. this.viewWidth = data.width / this.cols;
  175. }
  176. });
  177. query.exec();
  178. if ((this.isAWait && this.colsValue > 0) || !this.isAWait) {
  179. for (let item of this.list) {
  180. this.addProperties(item);
  181. }
  182. }
  183. if (this.areaHeight) {
  184. flag = true;
  185. if (flag) {
  186. this.$emit('callback', { height: this.areaHeight, index: this.index, isLoad: true });
  187. clearInterval(time);
  188. }
  189. }
  190. }, 10);
  191. },
  192. onChange(e, item) {
  193. if (!item) return;
  194. item.oldX = e.detail.x;
  195. item.oldY = e.detail.y;
  196. if (e.detail.source === 'touch') {
  197. if (item.moveEnd) {
  198. item.offset = Math.sqrt(Math.pow(item.oldX - item.absX * this.viewWidth, 2) + Math.pow(item.oldY - item.absY * this.viewWidth, 2));
  199. }
  200. let x = Math.floor((e.detail.x + this.viewWidth / 2) / this.viewWidth);
  201. if (x >= this.colsValue) return;
  202. let y = Math.floor((e.detail.y + this.viewWidth / 2) / this.viewWidth);
  203. let index = this.colsValue * y + x;
  204. if (item.index != index && index < this.imageList.length) {
  205. this.changeStatus = false;
  206. for (let obj of this.imageList) {
  207. if (item.index > index && obj.index >= index && obj.index < item.index) {
  208. this.change(obj, 1);
  209. } else if (item.index < index && obj.index <= index && obj.index > item.index) {
  210. this.change(obj, -1);
  211. } else if (obj.id != item.id) {
  212. obj.offset = 0;
  213. obj.x = obj.oldX;
  214. obj.y = obj.oldY;
  215. setTimeout(() => {
  216. this.$nextTick(() => {
  217. obj.x = obj.absX * this.viewWidth;
  218. obj.y = obj.absY * this.viewWidth;
  219. });
  220. }, 0);
  221. }
  222. }
  223. item.index = index;
  224. item.absX = x;
  225. item.absY = y;
  226. this.sortList();
  227. }
  228. }
  229. },
  230. change(obj, i) {
  231. obj.index += i;
  232. obj.offset = 0;
  233. obj.x = obj.oldX;
  234. obj.y = obj.oldY;
  235. obj.absX = obj.index % this.colsValue;
  236. obj.absY = Math.floor(obj.index / this.colsValue);
  237. setTimeout(() => {
  238. this.$nextTick(() => {
  239. obj.x = obj.absX * this.viewWidth;
  240. obj.y = obj.absY * this.viewWidth;
  241. });
  242. }, 0);
  243. },
  244. touchstart(item) {
  245. this.imageList.forEach(v => {
  246. v.zIndex = v.index + 9;
  247. });
  248. item.zIndex = 99;
  249. item.moveEnd = true;
  250. this.tempItem = item;
  251. this.timer = setTimeout(() => {
  252. item.scale = this.scale;
  253. item.opacity = this.opacity;
  254. clearTimeout(this.timer);
  255. this.timer = null;
  256. }, 200);
  257. },
  258. touchend(item) {
  259. this.previewImage(item);
  260. item.scale = 1;
  261. item.opacity = 1;
  262. item.x = item.oldX;
  263. item.y = item.oldY;
  264. item.offset = 0;
  265. item.moveEnd = false;
  266. setTimeout(() => {
  267. this.$nextTick(() => {
  268. item.x = item.absX * this.viewWidth;
  269. item.y = item.absY * this.viewWidth;
  270. this.tempItem = null;
  271. this.changeStatus = true;
  272. });
  273. }, 0);
  274. },
  275. previewImage(item) {
  276. if (this.timer && this.preStatus && this.changeStatus && item.offset < 28.28) {
  277. clearTimeout(this.timer);
  278. this.timer = null;
  279. this.list.forEach((item, i) => {
  280. this.list[i] = this.$util.img(item);
  281. });
  282. let src = this.list.findIndex(v => v === item.src);
  283. uni.previewImage({
  284. urls: this.list,
  285. current: src,
  286. success: () => {
  287. this.preStatus = false;
  288. setTimeout(() => {
  289. this.preStatus = true;
  290. }, 600);
  291. }
  292. });
  293. } else if (this.timer) {
  294. clearTimeout(this.timer);
  295. this.timer = null;
  296. }
  297. },
  298. mouseenter() {
  299. //#ifdef H5
  300. this.imageList.forEach(v => {
  301. v.disable = false;
  302. });
  303. //#endif
  304. },
  305. mouseleave() {
  306. //#ifdef H5
  307. if (this.tempItem) {
  308. this.imageList.forEach(v => {
  309. v.disable = true;
  310. v.zIndex = v.index + 9;
  311. v.offset = 0;
  312. v.moveEnd = false;
  313. if (v.id == this.tempItem.id) {
  314. if (this.timer) {
  315. clearTimeout(this.timer);
  316. this.timer = null;
  317. }
  318. v.scale = 1;
  319. v.opacity = 1;
  320. v.x = v.oldX;
  321. v.y = v.oldY;
  322. this.$nextTick(() => {
  323. v.x = v.absX * this.viewWidth;
  324. v.y = v.absY * this.viewWidth;
  325. this.tempItem = null;
  326. });
  327. }
  328. });
  329. this.changeStatus = true;
  330. }
  331. //#endif
  332. },
  333. addImages() {
  334. if (this.custom) {
  335. this.$emit('addImage');
  336. } else if (this.openSelectMode) {
  337. this.openChoosePicturePop();
  338. } else {
  339. this.selectPhonePhoto(['album', 'camera']);
  340. }
  341. },
  342. // 选择图片
  343. openChoosePicturePop() {
  344. this.$refs.choosePicturePopup.open();
  345. },
  346. closeChoosePicturePop() {
  347. this.$refs.choosePicturePopup.close();
  348. },
  349. // 从相册图库选择
  350. goAlbum() {
  351. this.closeChoosePicturePop();
  352. // this.number - this.imageList.length
  353. var temp = {
  354. list: this.list.toString(),
  355. index: this.index
  356. };
  357. uni.setStorageSync('selectedAlbumImgTemp', JSON.stringify(temp));
  358. this.$util.redirectTo('/pages/goods/album', { number: this.number });
  359. },
  360. // 手机拍照
  361. photograph() {
  362. this.closeChoosePicturePop();
  363. let checkNumber = this.number - this.imageList.length;
  364. this.$util.upload(
  365. {
  366. number: checkNumber,
  367. path: this.uploadMethod,
  368. sourceType: ['camera']
  369. },
  370. res => {
  371. for (var i = 0; i < res.length; i++) {
  372. this.addProperties(res[i]);
  373. }
  374. this.$emit('callback', { height: this.areaHeight, index: this.index });
  375. }
  376. );
  377. },
  378. // 从手机相册选择
  379. selectPhonePhoto(sourceType) {
  380. sourceType = sourceType || ['album'];
  381. this.closeChoosePicturePop();
  382. let checkNumber = this.number - this.imageList.length;
  383. this.$util.upload(
  384. {
  385. number: checkNumber,
  386. path: this.uploadMethod,
  387. sourceType: sourceType
  388. },
  389. res => {
  390. for (var i = 0; i < res.length; i++) {
  391. this.addProperties(res[i]);
  392. }
  393. var time = setInterval(() => {
  394. if (this.areaHeight) {
  395. this.$emit('callback', { height: this.areaHeight, index: this.index });
  396. clearInterval(time);
  397. }
  398. }, 10);
  399. }
  400. );
  401. },
  402. addImage(image) {
  403. this.addProperties(image);
  404. },
  405. delImage(item, index) {
  406. this.imageList.splice(index, 1);
  407. for (let obj of this.imageList) {
  408. if (obj.index > item.index) {
  409. obj.index -= 1;
  410. obj.x = obj.oldX;
  411. obj.y = obj.oldY;
  412. obj.absX = obj.index % this.colsValue;
  413. obj.absY = Math.floor(obj.index / this.colsValue);
  414. this.$nextTick(() => {
  415. obj.x = obj.absX * this.viewWidth;
  416. obj.y = obj.absY * this.viewWidth;
  417. });
  418. }
  419. }
  420. this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + 'px';
  421. this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + 'px';
  422. this.$emit('callback', { height: this.areaHeight, index: this.index });
  423. this.sortList();
  424. },
  425. delImageMp(item, index) {
  426. //#ifdef MP
  427. this.delImage(item, index);
  428. //#endif
  429. },
  430. sortList() {
  431. let list = this.imageList.slice();
  432. list.sort((a, b) => {
  433. return a.index - b.index;
  434. });
  435. for (let i = 0; i < list.length; i++) {
  436. list[i] = list[i].src;
  437. }
  438. this.$emit('update:list', list);
  439. },
  440. addProperties(item) {
  441. let absX = this.imageList.length % this.colsValue;
  442. let absY = Math.floor(this.imageList.length / this.colsValue);
  443. let x = absX * this.viewWidth;
  444. let y = absY * this.viewWidth;
  445. this.imageList.push({
  446. src: item,
  447. x,
  448. y,
  449. oldX: x,
  450. oldY: y,
  451. absX,
  452. absY,
  453. scale: 1,
  454. zIndex: 9,
  455. opacity: 1,
  456. index: this.imageList.length,
  457. id: this.guid(),
  458. disable: false,
  459. offset: 0,
  460. moveEnd: false
  461. });
  462. this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + 'px';
  463. this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + 'px';
  464. this.sortList();
  465. },
  466. nothing() {},
  467. rpx2px(v) {
  468. return (this.width * v) / 750;
  469. },
  470. guid() {
  471. function S4() {
  472. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  473. }
  474. return S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4();
  475. }
  476. }
  477. };
  478. </script>
  479. <style lang="scss" scoped>
  480. .con {
  481. // padding: 30rpx;
  482. .area {
  483. width: 100%;
  484. .view {
  485. display: flex;
  486. justify-content: center;
  487. align-items: center;
  488. .area-con {
  489. position: relative;
  490. .pre-image {
  491. width: 100%;
  492. height: 100%;
  493. }
  494. .del-con {
  495. position: absolute;
  496. top: -14rpx;
  497. right: -14rpx;
  498. .del-wrap {
  499. width: 32rpx;
  500. height: 32rpx;
  501. background-color: rgba(0, 0, 0, 0.5);
  502. border-radius: 50%;
  503. display: flex;
  504. justify-content: center;
  505. align-items: center;
  506. font-size: $font-size-tag;
  507. color: #fff;
  508. font-weight: bold;
  509. }
  510. }
  511. }
  512. }
  513. .add {
  514. position: absolute;
  515. display: flex;
  516. justify-content: center;
  517. align-items: center;
  518. .add-wrap {
  519. display: flex;
  520. justify-content: center;
  521. align-items: center;
  522. border: 1px dashed $color-disabled;
  523. width: 140rpx;
  524. height: 140rpx;
  525. line-height: 140rpx;
  526. text-align: center;
  527. color: $color-tip;
  528. font-weight: bold;
  529. font-size: 40rpx;
  530. }
  531. }
  532. }
  533. }
  534. .popup {
  535. width: 100vw;
  536. background: #fff;
  537. border-top-left-radius: 24rpx;
  538. border-top-right-radius: 24rpx;
  539. .popup-body {
  540. height: calc(100% - 250rpx);
  541. &.safe-area {
  542. height: calc(100% - 270rpx);
  543. }
  544. }
  545. &.choose-picture {
  546. background-color: $color-bg;
  547. .popup-header {
  548. border-bottom: none;
  549. background-color: #fff;
  550. }
  551. .popup-body {
  552. background-color: $color-bg;
  553. height: initial;
  554. .select-wrap {
  555. background-color: #fff;
  556. padding: 0 30rpx;
  557. }
  558. .item {
  559. text-align: center;
  560. padding: 20rpx;
  561. background-color: #fff;
  562. border-bottom: 1px solid $color-bg;
  563. &:last-child {
  564. border-bottom: none;
  565. }
  566. &.cancle {
  567. margin-top: 20rpx;
  568. }
  569. }
  570. }
  571. }
  572. }
  573. </style>