kxj-previewImage.vue 9.1 KB

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