u-album.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const _sfc_main = {
  4. name: "u-album",
  5. mixins: [common_vendor.mpMixin, common_vendor.mixin, common_vendor.props$14],
  6. data() {
  7. return {
  8. // 单图的宽度
  9. singleWidth: 0,
  10. // 单图的高度
  11. singleHeight: 0,
  12. // 单图时,如果无法获取图片的尺寸信息,让图片宽度默认为容器的一定百分比
  13. singlePercent: 0.6
  14. };
  15. },
  16. watch: {
  17. urls: {
  18. immediate: true,
  19. handler(newVal) {
  20. if (newVal.length === 1) {
  21. this.getImageRect();
  22. }
  23. }
  24. }
  25. },
  26. emits: ["albumWidth"],
  27. computed: {
  28. imageStyle() {
  29. return (index1, index2) => {
  30. const { space, rowCount, multipleSize, urls } = this, { addUnit, addStyle } = common_vendor.index.$u, rowLen = this.showUrls.length;
  31. this.urls.length;
  32. const style = {
  33. marginRight: addUnit(space),
  34. marginBottom: addUnit(space)
  35. };
  36. if (index1 === rowLen)
  37. style.marginBottom = 0;
  38. if (index2 === rowCount || index1 === rowLen && index2 === this.showUrls[index1 - 1].length)
  39. style.marginRight = 0;
  40. return style;
  41. };
  42. },
  43. // 将数组划分为二维数组
  44. showUrls() {
  45. const arr = [];
  46. this.urls.map((item, index) => {
  47. if (index + 1 <= this.maxCount) {
  48. const itemIndex = Math.floor(index / this.rowCount);
  49. if (!arr[itemIndex]) {
  50. arr[itemIndex] = [];
  51. }
  52. arr[itemIndex].push(item);
  53. }
  54. });
  55. return arr;
  56. },
  57. imageWidth() {
  58. return common_vendor.index.$u.addUnit(
  59. this.urls.length === 1 ? this.singleWidth : this.multipleSize
  60. );
  61. },
  62. imageHeight() {
  63. return common_vendor.index.$u.addUnit(
  64. this.urls.length === 1 ? this.singleHeight : this.multipleSize
  65. );
  66. },
  67. // 此变量无实际用途,仅仅是为了利用computed特性,让其在urls长度等变化时,重新计算图片的宽度
  68. // 因为用户在某些特殊的情况下,需要让文字与相册的宽度相等,所以这里事件的形式对外发送
  69. albumWidth() {
  70. let width = 0;
  71. if (this.urls.length === 1) {
  72. width = this.singleWidth;
  73. } else {
  74. width = this.showUrls[0].length * this.multipleSize + this.space * (this.showUrls[0].length - 1);
  75. }
  76. this.$emit("albumWidth", width);
  77. return width;
  78. }
  79. },
  80. methods: {
  81. // 预览图片
  82. onPreviewTap(url) {
  83. const urls = this.urls.map((item) => {
  84. return this.getSrc(item);
  85. });
  86. common_vendor.index.previewImage({
  87. current: url,
  88. urls
  89. });
  90. },
  91. // 获取图片的路径
  92. getSrc(item) {
  93. return common_vendor.index.$u.test.object(item) ? this.keyName && item[this.keyName] || item.src : item;
  94. },
  95. // 单图时,获取图片的尺寸
  96. // 在小程序中,需要将网络图片的的域名添加到小程序的download域名才可能获取尺寸
  97. // 在没有添加的情况下,让单图宽度默认为盒子的一定宽度(singlePercent)
  98. getImageRect() {
  99. const src = this.getSrc(this.urls[0]);
  100. common_vendor.index.getImageInfo({
  101. src,
  102. success: (res) => {
  103. const isHorizotal = res.width >= res.height;
  104. this.singleWidth = isHorizotal ? this.singleSize : res.width / res.height * this.singleSize;
  105. this.singleHeight = !isHorizotal ? this.singleSize : res.height / res.width * this.singleWidth;
  106. },
  107. fail: () => {
  108. this.getComponentWidth();
  109. }
  110. });
  111. },
  112. // 获取组件的宽度
  113. async getComponentWidth() {
  114. await common_vendor.index.$u.sleep(30);
  115. this.$uGetRect(".u-album__row").then((size) => {
  116. this.singleWidth = size.width * this.singlePercent;
  117. });
  118. }
  119. }
  120. };
  121. if (!Array) {
  122. const _easycom_u__text2 = common_vendor.resolveComponent("u--text");
  123. _easycom_u__text2();
  124. }
  125. const _easycom_u__text = () => "../u-text/u-text.js";
  126. if (!Math) {
  127. _easycom_u__text();
  128. }
  129. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  130. return {
  131. a: common_vendor.f($options.showUrls, (arr, index, i0) => {
  132. return {
  133. a: common_vendor.f(arr, (item, index1, i1) => {
  134. return common_vendor.e({
  135. a: $options.getSrc(item),
  136. b: _ctx.showMore && _ctx.urls.length > _ctx.rowCount * $options.showUrls.length && index === $options.showUrls.length - 1 && index1 === $options.showUrls[$options.showUrls.length - 1].length - 1
  137. }, _ctx.showMore && _ctx.urls.length > _ctx.rowCount * $options.showUrls.length && index === $options.showUrls.length - 1 && index1 === $options.showUrls[$options.showUrls.length - 1].length - 1 ? {
  138. c: "6fcabaad-0-" + i0 + "-" + i1,
  139. d: common_vendor.p({
  140. text: `+${_ctx.urls.length - _ctx.maxCount}`,
  141. color: "#fff",
  142. size: _ctx.multipleSize * 0.3,
  143. align: "center",
  144. customStyle: "justify-content: center"
  145. })
  146. } : {}, {
  147. e: index1,
  148. f: common_vendor.s($options.imageStyle(index + 1, index1 + 1)),
  149. g: common_vendor.o(($event) => _ctx.previewFullImage ? $options.onPreviewTap($options.getSrc(item)) : "", index1)
  150. });
  151. }),
  152. b: index
  153. };
  154. }),
  155. b: _ctx.urls.length === 1 ? $options.imageHeight > 0 ? _ctx.singleMode : "widthFix" : _ctx.multipleMode,
  156. c: common_vendor.s({
  157. width: $options.imageWidth,
  158. height: $options.imageHeight
  159. }),
  160. d: $options.albumWidth
  161. };
  162. }
  163. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-6fcabaad"], ["__file", "D:/project/赋强公证/notarization_applet/node_modules/uview-plus/components/u-album/u-album.vue"]]);
  164. wx.createComponent(Component);