previewImage.vue 7.1 KB

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