previewImage.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. };
  79. },
  80. methods: {
  81. //比例变化
  82. onScale(e) {
  83. },
  84. //长按事件相关内容---------开始-------------------
  85. //接触开始
  86. handletouchstart(e) {
  87. var tchs = e.touches.length;
  88. if (tchs != 1) {
  89. return false;
  90. }
  91. this.time = setTimeout(() => {
  92. this.onLongPress(e);
  93. }, this.interval);
  94. return false;
  95. },
  96. //清除定时器
  97. handletouchend() {
  98. clearTimeout(this.time);
  99. if (this.time != 0) {
  100. //处理点击时间
  101. }
  102. return false;
  103. },
  104. //清除定时器
  105. handletouchmove() {
  106. clearTimeout(this.time);
  107. this.time = 0;
  108. },
  109. // 处理长按事件
  110. onLongPress(e) {
  111. var src = e.currentTarget.dataset.src;
  112. var index = e.currentTarget.dataset.index;
  113. var data = {
  114. src: src,
  115. index: index
  116. };
  117. this.$emit('longPress', data);
  118. },
  119. //长按事件相关内容---------结束-------------------
  120. //图片改变
  121. swiperChange(e) {
  122. this.index = e.target.current; //更新当前图片index
  123. this.$nextTick(function() {
  124. this.scale = 1;
  125. })
  126. //this.deg = 0; //旋转角度
  127. //this.swiper=true;
  128. },
  129. //移动变化
  130. movableChange(e) {
  131. /* if(this.old.scale <= 1){
  132. this.swiper=false;
  133. }else if(e.detail.x===0){
  134. this.swiper=false;
  135. } */
  136. },
  137. //保存
  138. save(e) {
  139. var src = this.imgs[this.index];
  140. this.downloadImg(src);
  141. },
  142. //下载并保存文件
  143. downloadImg(src) {
  144. //下载图片文件
  145. uni.showLoading({
  146. title: '保存中'
  147. });
  148. uni.saveImageToPhotosAlbum({
  149. filePath: src,
  150. success: (res) => {
  151. uni.hideLoading();
  152. uni.showToast({
  153. title: '已保存至相册',
  154. duration: 1000
  155. });
  156. },
  157. fail: function(err) {
  158. uni.hideLoading();
  159. uni.showToast({
  160. title: '图片下载失败',
  161. icon: 'none',
  162. duration: 1000
  163. });
  164. }
  165. });
  166. },
  167. //旋转
  168. rotate(e) {
  169. this.deg = this.deg == 270 ? 0 : this.deg + 90;
  170. },
  171. //打开
  172. open(e) {
  173. if (e === null || e === '') {
  174. // console.log('kxj-previewImage:打开参数无效');
  175. return;
  176. }
  177. if (!isNaN(e)) {
  178. if (e >= this.imgs.length) {
  179. // console.log('kxj-previewImage:打开参数无效');
  180. } else {
  181. this.index = e;
  182. }
  183. } else {
  184. var index = this.imgs.indexOf(e);
  185. if (index === -1) {
  186. this.imgs = [e];
  187. this.index = 0;
  188. // console.log('kxj-previewImage:未在图片地址数组中找到传入的图片,已为你自动打开单张预览模式')
  189. } else {
  190. this.index = this.imgs.indexOf(e);
  191. }
  192. }
  193. // console.log('kxj-previewImage:当前预览图片序号' + this.index);
  194. this.show = true;
  195. },
  196. //关闭
  197. close(e) {
  198. this.show = false;
  199. this.index = 0; //当前页
  200. this.deg = 0; //旋转角度
  201. this.time = 0; //定时器
  202. this.interval = 1000; //长按事件
  203. this.scale = 1; //缩放比例
  204. }
  205. }
  206. };
  207. </script>
  208. <!--使用scss,只在本组件生效-->
  209. <style lang="scss" scoped>
  210. .previewImage {
  211. z-index: 999;
  212. position: fixed;
  213. top: 0;
  214. left: 0;
  215. width: 100%;
  216. height: 100%;
  217. background-color: #000000;
  218. user-select: none;
  219. .swiper {
  220. width: 100%;
  221. height: 100%;
  222. .marea {
  223. height: 100%;
  224. width: 100%;
  225. position: fixed;
  226. overflow: hidden;
  227. .mview {
  228. display: flex;
  229. align-items: center;
  230. justify-content: center;
  231. width: 100%;
  232. height: auto;
  233. min-height: 100%;
  234. .image {
  235. width: 100%;
  236. }
  237. }
  238. }
  239. }
  240. .page {
  241. position: absolute;
  242. width: 100%;
  243. bottom: 20rpx;
  244. text-align: center;
  245. .text {
  246. color: #fff;
  247. font-size: 26rpx;
  248. background-color: rgba(0, 0, 0, 0.5);
  249. padding: 3rpx 16rpx;
  250. border-radius: 20rpx;
  251. user-select: none;
  252. }
  253. }
  254. .save {
  255. position: absolute;
  256. left: 10rpx;
  257. width: 120rpx;
  258. height: 56rpx;
  259. bottom: 10rpx;
  260. text-align: center;
  261. padding: 10rpx;
  262. .text {
  263. background-color: rgba(0, 0, 0, 0.5);
  264. color: #fff;
  265. font-size: 30rpx;
  266. border-radius: 20rpx;
  267. border: 1rpx solid #f1f1f1;
  268. padding: 6rpx 22rpx;
  269. user-select: none;
  270. }
  271. .text:active {
  272. background-color: rgba(100, 100, 100, 0.5);
  273. }
  274. }
  275. .rotate {
  276. position: absolute;
  277. right: 0;
  278. left: 0;
  279. height: 56rpx;
  280. bottom: 200rpx;
  281. text-align: center;
  282. padding: 10rpx;
  283. margin: auto;
  284. .text {
  285. background-color: rgba(0, 0, 0, 0.5);
  286. color: #fff;
  287. font-size: 30rpx;
  288. border-radius: 20rpx;
  289. border: 1rpx solid #f1f1f1;
  290. padding: 6rpx 22rpx;
  291. user-select: none;
  292. }
  293. .text:active {
  294. background-color: rgba(100, 100, 100, 0.5);
  295. }
  296. }
  297. .desc {
  298. position: absolute;
  299. top: 0;
  300. width: 100%;
  301. padding: 5rpx 10rpx;
  302. text-align: center;
  303. overflow: hidden;
  304. text-overflow: ellipsis;
  305. white-space: nowrap;
  306. background-color: rgba(0, 0, 0, 0.5);
  307. color: #fff;
  308. font-size: 28rpx;
  309. letter-spacing: 3rpx;
  310. user-select: none;
  311. }
  312. }
  313. </style>