kxj-previewImage.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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"
  11. :style="{ transform: 'rotateZ(' + deg + 'deg)' }" :data-index="i" :data-src="img"
  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: true
  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. //console.log(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. save(e) {
  140. var _this = this;
  141. var src = this.imgs[this.index];
  142. //#ifdef MP-WEIXIN
  143. //提前向用户发起授权请求
  144. uni.authorize({
  145. scope: 'scope.writePhotosAlbum',
  146. success() {
  147. console.log('kxj-previewImage:允许储存');
  148. _this.downloadImg(src);
  149. }
  150. });
  151. //#endif
  152. //#ifdef APP-PLUS
  153. this.downloadImg(src);
  154. //#endif
  155. //#ifdef H5
  156. //非同源图片将直接打开
  157. var abtn = document.createElement('a');
  158. abtn.href = src;
  159. abtn.download = '';
  160. abtn.target = '_blank';
  161. abtn.click();
  162. //#endif
  163. },
  164. //下载并保存文件
  165. downloadImg(src) {
  166. //下载图片文件
  167. uni.showLoading({
  168. title: '大图提取中'
  169. });
  170. uni.downloadFile({
  171. url: src,
  172. success: function(res) {
  173. console.log('kxj-previewImage:下载成功');
  174. uni.hideLoading();
  175. uni.saveImageToPhotosAlbum({
  176. filePath: res.tempFilePath,
  177. success: () => {
  178. uni.showToast({
  179. title: '已保存至相册',
  180. duration: 1000
  181. });
  182. }
  183. });
  184. },
  185. fail: function() {
  186. uni.hideLoading();
  187. uni.showToast({
  188. title: '图片下载失败',
  189. icon: 'none',
  190. duration: 1000
  191. });
  192. }
  193. });
  194. },
  195. //旋转
  196. rotate(e) {
  197. this.deg = this.deg == 270 ? 0 : this.deg + 90;
  198. },
  199. //打开
  200. open(e) {
  201. console.log('open');
  202. if (e === null || e === '') {
  203. console.log('kxj-previewImage:打开参数无效');
  204. return;
  205. }
  206. if (!isNaN(e)) {
  207. if (e >= this.imgs.length) {
  208. console.log('kxj-previewImage:打开参数无效');
  209. } else {
  210. this.index = e;
  211. }
  212. } else {
  213. var index = this.imgs.indexOf(e);
  214. if (index === -1) {
  215. this.imgs = [e];
  216. this.index = 0;
  217. console.log('kxj-previewImage:未在图片地址数组中找到传入的图片,已为你自动打开单张预览模式')
  218. } else {
  219. this.index = this.imgs.indexOf(e);
  220. }
  221. }
  222. console.log('kxj-previewImage:当前预览图片序号' + this.index);
  223. this.show = true;
  224. },
  225. //关闭
  226. close(e) {
  227. this.show = false;
  228. this.index = 0; //当前页
  229. this.deg = 0; //旋转角度
  230. this.time = 0; //定时器
  231. this.interval = 1000; //长按事件
  232. this.scale = 1; //缩放比例
  233. }
  234. }
  235. };
  236. </script>
  237. <!--使用scss,只在本组件生效-->
  238. <style lang="scss" scoped>
  239. .previewImage {
  240. z-index: 999999;
  241. position: fixed;
  242. top: 0;
  243. left: 0;
  244. width: 100%;
  245. height: 100%;
  246. background-color: #000000;
  247. user-select: none;
  248. .swiper {
  249. width: 100%;
  250. height: 100%;
  251. .marea {
  252. height: 100%;
  253. width: 100%;
  254. position: fixed;
  255. overflow: hidden;
  256. .mview {
  257. display: flex;
  258. align-items: center;
  259. justify-content: center;
  260. width: 100%;
  261. height: auto;
  262. min-height: 100%;
  263. .image {
  264. width: 100%;
  265. }
  266. }
  267. }
  268. }
  269. .page {
  270. position: absolute;
  271. width: 100%;
  272. bottom: 20rpx;
  273. text-align: center;
  274. .text {
  275. color: #fff;
  276. font-size: 26rpx;
  277. background-color: rgba(0, 0, 0, 0.5);
  278. padding: 3rpx 16rpx;
  279. border-radius: 20rpx;
  280. user-select: none;
  281. }
  282. }
  283. .save {
  284. position: absolute;
  285. left: 10rpx;
  286. width: 160rpx;
  287. height: 56rpx;
  288. bottom: 10rpx;
  289. text-align: center;
  290. padding: 10rpx;
  291. .text {
  292. background-color: rgba(0, 0, 0, 0.5);
  293. color: #fff;
  294. font-size: 26rpx;
  295. border-radius: 20rpx;
  296. border: 1rpx solid #f1f1f1;
  297. padding: 6rpx 22rpx;
  298. user-select: none;
  299. }
  300. .text:active {
  301. background-color: rgba(100, 100, 100, 0.5);
  302. }
  303. }
  304. .rotate {
  305. position: absolute;
  306. right: 10rpx;
  307. width: 160rpx;
  308. height: 56rpx;
  309. bottom: 10rpx;
  310. text-align: center;
  311. padding: 10rpx;
  312. .text {
  313. background-color: rgba(0, 0, 0, 0.5);
  314. color: #fff;
  315. font-size: 26rpx;
  316. border-radius: 20rpx;
  317. border: 1rpx solid #f1f1f1;
  318. padding: 6rpx 22rpx;
  319. user-select: none;
  320. }
  321. .text:active {
  322. background-color: rgba(100, 100, 100, 0.5);
  323. }
  324. }
  325. .desc {
  326. position: absolute;
  327. top: 0;
  328. width: 100%;
  329. padding: 5rpx 10rpx;
  330. text-align: center;
  331. overflow: hidden;
  332. text-overflow: ellipsis;
  333. white-space: nowrap;
  334. background-color: rgba(0, 0, 0, 0.5);
  335. color: #fff;
  336. font-size: 28rpx;
  337. letter-spacing: 3rpx;
  338. user-select: none;
  339. }
  340. }
  341. </style>