kxj-previewImage.vue 9.0 KB

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