previewImage.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <template>
  2. <view class="previewImage" :style="{ 'background-color': 'rgba(0,0,0,' + opacity + ')' }" v-if="show" @tap="close"
  3. @touchmove.stop.prevent>
  4. <swiper class="swiper" :current="index" @change="swiperChange" :disable-touch="swiper" :circular="circular">
  5. <swiper-item v-for="(img, i) in imgs" :key="'swiper-item-'+i" :id="'swiper-item-'+i">
  6. <movable-area class="marea" scale-area>
  7. <movable-view :id="'movable-view-'+i" :key="'movable-view-'+i" class="mview" direction="all"
  8. :out-of-bounds="false" :inertia="true" damping="90" friction="2" scale="true" scale-min="1"
  9. scale-max="4" :scale-value="scale" @scale="onScale" @change="movableChange">
  10. <image :id="'image-'+i" :key="'movable-view'+i" class="image" :src="img.url"
  11. :style="{ transform: 'rotateZ(' + deg + 'deg)' }" :data-index="i" :data-src="img.url"
  12. mode="widthFix" @touchmove="handletouchmove" @touchstart="handletouchstart"
  13. @touchend="handletouchend" />
  14. </movable-view>
  15. </movable-area>
  16. </swiper-item>
  17. </swiper>
  18. <view class="page" v-if="imgs.length > 0">
  19. <text class="text">{{ index + 1 }} / {{ imgs.length }}</text>
  20. </view>
  21. <view class="save" v-if="saveBtn" @click.stop.prevent="save"><text class="text">保存</text></view>
  22. <view class="rotate" v-if="rotateBtn" @click.stop.prevent="rotate"><text class="text">旋转</text></view>
  23. <view class="desc" v-if="descs.length > 0 && descs.length == imgs.length && descs[index].length > 0">
  24. {{ descs[index] }}
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. export default {
  30. name: 'ksj-previewImage', //插件名称
  31. props: {
  32. imgs: {
  33. //描述列表
  34. type: Array,
  35. required: true,
  36. default: () => {
  37. return [];
  38. }
  39. },
  40. descs: {
  41. //描述列表
  42. type: Array,
  43. required: false,
  44. default: () => {
  45. return [];
  46. }
  47. },
  48. //透明度,0到1之间。
  49. opacity: {
  50. type: Number,
  51. default: 0.8
  52. },
  53. //保存按键
  54. saveBtn: {
  55. type: Boolean,
  56. default: false
  57. },
  58. //旋转按键
  59. rotateBtn: {
  60. type: Boolean,
  61. default: true
  62. },
  63. //循环预览
  64. circular: {
  65. type: Boolean,
  66. default: false
  67. },
  68. },
  69. data() {
  70. return {
  71. swiper: false, //是否禁用
  72. show: false, //显示状态
  73. index: 0, //当前页
  74. deg: 0, //旋转角度
  75. time: 0, //定时器
  76. interval: 1000, //长按事件
  77. scale: 1, //缩放比例
  78. dataimgs: [...this.imgs]
  79. };
  80. },
  81. methods: {
  82. //比例变化
  83. onScale(e) {
  84. },
  85. //长按事件相关内容---------开始-------------------
  86. //接触开始
  87. handletouchstart(e) {
  88. var tchs = e.touches.length;
  89. if (tchs != 1) {
  90. return false;
  91. }
  92. this.time = setTimeout(() => {
  93. this.onLongPress(e);
  94. }, this.interval);
  95. return false;
  96. },
  97. //清除定时器
  98. handletouchend() {
  99. clearTimeout(this.time);
  100. if (this.time != 0) {
  101. //处理点击时间
  102. }
  103. return false;
  104. },
  105. //清除定时器
  106. handletouchmove() {
  107. clearTimeout(this.time);
  108. this.time = 0;
  109. },
  110. // 处理长按事件
  111. onLongPress(e) {
  112. var src = e.currentTarget.dataset.src;
  113. var index = e.currentTarget.dataset.index;
  114. var data = {
  115. src: src,
  116. index: index
  117. };
  118. this.$emit('longPress', data);
  119. },
  120. //长按事件相关内容---------结束-------------------
  121. //图片改变
  122. swiperChange(e) {
  123. this.index = e.target.current; //更新当前图片index
  124. this.$nextTick(function() {
  125. this.scale = 1;
  126. })
  127. //this.deg = 0; //旋转角度
  128. //this.swiper=true;
  129. },
  130. //移动变化
  131. movableChange(e) {
  132. /* if(this.old.scale <= 1){
  133. this.swiper=false;
  134. }else if(e.detail.x===0){
  135. this.swiper=false;
  136. } */
  137. },
  138. //下载并保存文件
  139. downloadImg(src) {
  140. //下载图片文件
  141. uni.showLoading({
  142. title: '保存中'
  143. });
  144. uni.saveImageToPhotosAlbum({
  145. filePath: src,
  146. success: (res) => {
  147. uni.hideLoading();
  148. uni.showToast({
  149. title: '已保存至相册',
  150. duration: 1000
  151. });
  152. },
  153. fail: function(err) {
  154. uni.hideLoading();
  155. uni.showToast({
  156. title: '图片下载失败',
  157. icon: 'none',
  158. duration: 1000
  159. });
  160. }
  161. });
  162. },
  163. //旋转
  164. rotate(e) {
  165. this.deg = this.deg == 270 ? 0 : this.deg + 90;
  166. },
  167. //打开
  168. open(e) {
  169. const newArray = [...this.imgs];
  170. if (e === null || e === '') {
  171. // console.log('kxj-previewImage:打开参数无效');
  172. return;
  173. }
  174. if (!isNaN(e)) {
  175. if (e >= newArray.length) {
  176. // console.log('kxj-previewImage:打开参数无效');
  177. } else {
  178. this.index = e;
  179. }
  180. } else {
  181. newArray.map((ele, index) => {
  182. if (e == ele.url) {
  183. this.index = index;
  184. }
  185. })
  186. }
  187. this.show = true;
  188. },
  189. //关闭
  190. close(e) {
  191. this.show = false;
  192. this.index = 0; //当前页
  193. this.deg = 0; //旋转角度
  194. this.time = 0; //定时器
  195. this.interval = 1000; //长按事件
  196. this.scale = 1; //缩放比例
  197. }
  198. }
  199. };
  200. </script>
  201. <!--使用scss,只在本组件生效-->
  202. <style lang="scss" scoped>
  203. .previewImage {
  204. z-index: 99999;
  205. position: fixed;
  206. top: 0;
  207. left: 0;
  208. width: 100%;
  209. height: 100%;
  210. background-color: #000000;
  211. user-select: none;
  212. .swiper {
  213. width: 100%;
  214. height: 100%;
  215. .marea {
  216. height: 100%;
  217. width: 100%;
  218. position: fixed;
  219. overflow: hidden;
  220. .mview {
  221. display: flex;
  222. align-items: center;
  223. justify-content: center;
  224. width: 100%;
  225. height: auto;
  226. min-height: 100%;
  227. .image {
  228. width: 100%;
  229. }
  230. }
  231. }
  232. }
  233. .page {
  234. position: absolute;
  235. width: 100%;
  236. bottom: 20rpx;
  237. text-align: center;
  238. .text {
  239. color: #fff;
  240. font-size: 26rpx;
  241. background-color: rgba(0, 0, 0, 0.5);
  242. padding: 3rpx 16rpx;
  243. border-radius: 20rpx;
  244. user-select: none;
  245. }
  246. }
  247. .save {
  248. position: absolute;
  249. left: 10rpx;
  250. width: 120rpx;
  251. height: 56rpx;
  252. bottom: 10rpx;
  253. text-align: center;
  254. padding: 10rpx;
  255. .text {
  256. background-color: rgba(0, 0, 0, 0.5);
  257. color: #fff;
  258. font-size: 30rpx;
  259. border-radius: 20rpx;
  260. border: 1rpx solid #f1f1f1;
  261. padding: 6rpx 22rpx;
  262. user-select: none;
  263. }
  264. .text:active {
  265. background-color: rgba(100, 100, 100, 0.5);
  266. }
  267. }
  268. .rotate {
  269. position: absolute;
  270. right: 0;
  271. left: 0;
  272. height: 56rpx;
  273. bottom: 200rpx;
  274. text-align: center;
  275. padding: 10rpx;
  276. margin: auto;
  277. .text {
  278. background-color: rgba(0, 0, 0, 0.5);
  279. color: #fff;
  280. font-size: 30rpx;
  281. border-radius: 20rpx;
  282. border: 1rpx solid #f1f1f1;
  283. padding: 6rpx 22rpx;
  284. user-select: none;
  285. }
  286. .text:active {
  287. background-color: rgba(100, 100, 100, 0.5);
  288. }
  289. }
  290. .desc {
  291. position: absolute;
  292. top: 0;
  293. width: 100%;
  294. padding: 5rpx 10rpx;
  295. text-align: center;
  296. overflow: hidden;
  297. text-overflow: ellipsis;
  298. white-space: nowrap;
  299. background-color: rgba(0, 0, 0, 0.5);
  300. color: #fff;
  301. font-size: 28rpx;
  302. letter-spacing: 3rpx;
  303. user-select: none;
  304. }
  305. }
  306. </style>