kxj-previewImage.vue 7.7 KB

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