index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // miniprogram_npm/lin-ui/album/index.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. externalClasses: ['l-class', 'l-single-image-class', 'l-multi-image-class'],
  7. properties: {
  8. urls: {
  9. type: Array
  10. },
  11. // 是否可以预览
  12. preview: {
  13. type: Boolean,
  14. value: true
  15. },
  16. gapRow: {
  17. type: Number,
  18. value: 10,
  19. },
  20. gapColumn: {
  21. type: Number,
  22. value: 10,
  23. },
  24. // 单图时长边大小
  25. singleSize: {
  26. type: Number,
  27. value: 360,
  28. },
  29. // 多图时图片边长
  30. multipleSize: {
  31. type: Number,
  32. value: 158,
  33. },
  34. // 单图显示模式
  35. singleMode: {
  36. type: String,
  37. value: 'aspectFit',
  38. },
  39. // 多图显示模式
  40. multipleMode: {
  41. type: String,
  42. value: 'aspectFill',
  43. },
  44. key: {
  45. type: String,
  46. value: 'url'
  47. }
  48. },
  49. /**
  50. * 组件的初始数据
  51. */
  52. data: {
  53. // 传值方式是新方式还是旧方式
  54. newType: true,
  55. // 单图短边大小
  56. shortSideValue: 0,
  57. // 图片排列几行
  58. row: 0,
  59. // 图片排列几列
  60. colum: 0,
  61. },
  62. /**
  63. * 组件的生命周期
  64. */
  65. lifetimes: {
  66. attached() {
  67. // 在组件实例进入页面节点树时执行
  68. //判断传入urls长度
  69. if (this.data.urls.length > 9) {
  70. const urls = this.data.urls.slice(0, 9);
  71. this.setData({
  72. urls
  73. });
  74. console.warn('超过9张图片!');
  75. }
  76. this.preview();
  77. },
  78. },
  79. /**
  80. * 组件的方法列表
  81. */
  82. methods: {
  83. // 判断传入的urls是字符串列表(old模式)还是对象列表(new模式)
  84. judgeType() {
  85. const urls = this.data.urls;
  86. if (urls.length != 0) {
  87. if (typeof (urls[0]) !== 'object') {
  88. return false;
  89. }
  90. }
  91. return true;
  92. },
  93. //判断照片是横屏还是竖屏并计算短边的长度
  94. //如不指定短边的长度,短边会默认显示image组件的长度
  95. horizontalOrVertical: function (src) {
  96. wx.getImageInfo({
  97. src: src,
  98. success: (res) => {
  99. const longSide = res.width >= res.height ? res.width : res.height;
  100. const shortSide = res.width >= res.height ? res.height : res.width;
  101. this.setData({
  102. horizontalScreen: res.width >= res.height ? true : false,
  103. shortSideValue: shortSide * this.data.singleSize / longSide
  104. });
  105. }
  106. });
  107. },
  108. // 显示图片
  109. preview: function () {
  110. // 判断传入模式
  111. const newType = this.judgeType();
  112. this.setData({
  113. newType
  114. });
  115. //显示图片
  116. const urls = this.data.urls;
  117. const key = this.data.key;
  118. if (urls.length == 1) {
  119. this.horizontalOrVertical(newType ? urls[0][key] : urls[0]);
  120. }
  121. },
  122. onPreviewTap(e) {
  123. const index = e.currentTarget.id;
  124. const urls = this.data.urls;
  125. let tempFilePath = '';
  126. let previewImageList = [];
  127. const newType = this.data.newType;
  128. const key = this.data.key;
  129. if (newType) {
  130. tempFilePath = urls[index][key];
  131. for (let i = 0; i < urls.length; i++) {
  132. previewImageList.push(urls[i][key]);
  133. }
  134. } else {
  135. tempFilePath = urls[index];
  136. previewImageList = urls;
  137. }
  138. let detail = {
  139. index, // 下标
  140. current: urls[index], // 当前显示图片的http链接
  141. all: urls // 需要预览的图片http链接列表
  142. };
  143. let option = {};
  144. if (this.data.preview === true) {
  145. wx.previewImage({
  146. current: tempFilePath, // 当前显示图片的http链接
  147. urls: previewImageList // 需要预览的图片http链接列表
  148. });
  149. }
  150. this.triggerEvent('lintap', detail, option);
  151. }
  152. }
  153. });