preview-image.uvue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1">
  4. <!-- #endif -->
  5. <view style="padding-left: 8px; padding-right: 8px">
  6. <view>
  7. <text class="text-desc">图片指示器样式</text>
  8. <radio-group class="cell-ct" style="background-color: white" @change="onIndicatorChanged">
  9. <view class="indicator-it" v-for="(item, index) in indicator" :key="item.value">
  10. <radio :disabled="isWeb" :checked="index == 0" :value="item.value">{{
  11. item.name
  12. }}</radio>
  13. </view>
  14. </radio-group>
  15. </view>
  16. <view>
  17. <checkbox-group @change="onCheckboxChange" style="margin-top: 16px; margin-bottom: 16px; margin-left: 8px">
  18. <checkbox :disabled="isWeb" :checked="isLoop" style="margin-right: 15px">循环播放</checkbox>
  19. </checkbox-group>
  20. </view>
  21. <view>
  22. <checkbox-group @change="onLongPressCheckboxChange"
  23. style="margin-top: 16px; margin-bottom: 16px; margin-left: 8px">
  24. <checkbox :disabled="isWeb" :checked="isLongPress" style="margin-right: 15px">支持长按事件</checkbox>
  25. </checkbox-group>
  26. </view>
  27. <view style="background-color: white">
  28. <text class="text-desc">点击图片开始预览</text>
  29. <view class="cell-ct" style="margin: 8px;">
  30. <view class="cell cell-choose-image" v-for="(image, index) in imageList" :key="index">
  31. <text style="width: 100px; height: 100px;background-color: lightgray; color: red; text-align: center; line-height: 100px;font-size: 14px;" v-if="image.error"
  32. @click="previewImage(index)">图片路径非法</text>
  33. <image style="width: 100px; height: 100px;background-color: white;" mode="aspectFit" :src="image.src"
  34. v-if="!image.error" @click="previewImage(index)"
  35. @error="onImageLoadError(index,$event as ImageErrorEvent)">
  36. </image>
  37. </view>
  38. <image class="cell cell-choose-image" src="/static/plus.png" @click="chooseImage">
  39. <view></view>
  40. </image>
  41. </view>
  42. </view>
  43. <view style="margin:8px;">
  44. <text style="color: black;font-size: 18px;margin-bottom: 4px;">注意事项:</text>
  45. <text style="font-size: 17px;margin-left: 4px;color: darkgray;">1、indicator属性仅App平台支持。</text>
  46. <text style="font-size: 17px;margin-left: 4px;color: darkgray;">2、Web平台不支持loop属性。</text>
  47. </view>
  48. </view>
  49. <!-- #ifdef APP -->
  50. </scroll-view>
  51. <!-- #endif -->
  52. </template>
  53. <script>
  54. type Indicator = "number" | "default" | "none"
  55. type ItemType = {
  56. value : Indicator,
  57. name : string
  58. }
  59. type ImageType = {
  60. src : string,
  61. error : boolean
  62. }
  63. export default {
  64. data() {
  65. return {
  66. imageList: [{ src: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png", error: false }, { src: "/static/uni.png", error: false },
  67. // #ifdef APP
  68. { src: "/static/uni2.png", error: false },
  69. // #endif
  70. ] as ImageType[],
  71. indicator: [{
  72. value: "default",
  73. name: "圆点"
  74. }, {
  75. value: "number",
  76. name: "数字"
  77. }, {
  78. value: "none",
  79. name: "不显示"
  80. }] as ItemType[],
  81. currentIndicator: "default" as Indicator,
  82. // #ifdef WEB
  83. isWeb: true,
  84. // #endif
  85. // #ifndef WEB
  86. isWeb: false,
  87. // #endif
  88. // #ifdef APP-IOS
  89. isIOS: true,
  90. // #endif
  91. // #ifndef APP-IOS
  92. isIOS: false,
  93. // #endif
  94. isLongPress: true,
  95. isLoop: true,
  96. }
  97. },
  98. methods: {
  99. previewImage(index : number) {
  100. let list = [] as Array<string>
  101. this.imageList.forEach((item : ImageType) => {
  102. list.push(item.src)
  103. })
  104. uni.previewImage({
  105. urls: list,
  106. current: index,
  107. indicator: this.currentIndicator,
  108. loop: this.isLoop,
  109. longPressActions: (this.isLongPress ? ({
  110. itemList: ["按钮1", "按钮2", "按钮3"],
  111. itemColor: "#ccc",
  112. success: (e : LongPressActionsSuccessResult) => {
  113. uni.showToast({
  114. title: "用户选中了第" + (e.index + 1) + "张图片,并选中了第" + (e.tapIndex + 1) + "个选项",
  115. position: "bottom"
  116. })
  117. },
  118. fail: (e : LongPressActionsFailResult) => {
  119. uni.showToast({
  120. title: "用户关闭了action sheet",
  121. position: "bottom"
  122. })
  123. }
  124. } as LongPressActionsOptions) : null)
  125. })
  126. },
  127. chooseImage() {
  128. uni.chooseImage({
  129. sourceType: ['album'],
  130. count: 1,
  131. success: (e) => {
  132. this.imageList = this.imageList.concat({ src: e.tempFilePaths[0], error: false } as ImageType)
  133. // this.imageList = this.imageList.concat(e.tempFilePaths)
  134. },
  135. fail(_) {
  136. }
  137. })
  138. },
  139. onIndicatorChanged(e : UniRadioGroupChangeEvent) {
  140. this.currentIndicator = e.detail.value as Indicator
  141. },
  142. onCheckboxChange(_ : UniCheckboxGroupChangeEvent) {
  143. this.isLoop = !this.isLoop
  144. },
  145. onLongPressCheckboxChange() {
  146. this.isLongPress = !this.isLongPress
  147. },
  148. onImageLoadError(index : number, error : UniImageErrorEvent) {
  149. this.imageList[index].error = true
  150. }
  151. }
  152. }
  153. </script>
  154. <style>
  155. .text-desc {
  156. margin-top: 16px;
  157. margin-left: 8px;
  158. margin-bottom: 16px;
  159. font-weight: bold;
  160. }
  161. .cell-ct {
  162. display: flex;
  163. flex-wrap: wrap;
  164. flex-direction: row;
  165. }
  166. .cell {
  167. margin-left: 3px;
  168. margin-right: 3px;
  169. width: 100px;
  170. height: 100px;
  171. }
  172. .cell-choose-image {
  173. border-width: 1px;
  174. border-style: solid;
  175. border-color: lightgray;
  176. }
  177. .indicator-it {
  178. margin: 8px;
  179. }
  180. .cell-pd {
  181. padding: 11px 0px;
  182. }
  183. </style>