123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <template>
- <button @click="showShareMenu" type="default" class="phone-white-btn bz-tel-btn fx-btn-box">分享</button>
- <!-- 答题卡 -->
- <uni-popup ref="downPopupRef" background-color="#fff" :animation="false" :is-mask-click="true" :mask-click="false">
- <view class="share-haibao-box">
- <view class="phone-line-title">分享</view>
- <view class="share-list-box">
- <view class="share-item-box" @click="getImage">
- <view class="share-icon-box">
- <icon></icon>
- </view>
- <text>下载图片</text>
- </view>
- </view>
- </view>
- </uni-popup>
- </template>
- <script>
- import {
- jiazhengSharePic
- } from "@/api/jiazheng.js"
- export default {
- data() {
- return {
- imageUrl: ''
- };
- },
- props: {
- id: {
- type: Number,
- }
- },
- methods: {
- showShareMenu() {
- this.$refs.downPopupRef.open('bottom')
- },
- getImage() {
- console.log('id', this.id);
- jiazhengSharePic({
- id: this.id
- }).then(res => {
- console.log('res', res);
- if (res.code == 0 && res.data) {
- this.imageUrl = res.data
- this.downloadImage()
- } else {
- uni.showToast({
- title: '获取图片失败',
- icon: 'none'
- });
- return false
- }
- })
- },
- downloadImage() {
- // #ifdef H5
- this.downloadImageForH5();
- // #endif
- // #ifdef APP-PLUS
- this.downloadImageForApp();
- // #endif
- },
- downloadImageForH5() {
- const link = document.createElement('a');
- link.href = this.imageUrl;
- link.download = 'image.jpg';
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- uni.showToast({
- title: '图片已下载,请手动保存到相册。',
- icon: 'none'
- });
- },
- downloadImageForApp() {
- uni.showLoading({
- title: '下载中...',
- mask: true
- });
- uni.downloadFile({
- url: this.imageUrl,
- success: (res) => {
- if (res.statusCode === 200) {
- const tempFilePath = res.tempFilePath; // 下载后的临时文件路径
- this.saveImageToAlbum(tempFilePath); // 保存到相册
- } else {
- uni.showToast({
- title: '下载失败',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- console.error('下载失败:', err);
- uni.showToast({
- title: '下载失败',
- icon: 'none'
- });
- },
- complete: () => {
- uni.hideLoading();
- }
- });
- },
- saveImageToAlbum(tempFilePath) {
- // #ifdef APP-PLUS
- this.saveImage(tempFilePath);
- // #endif
- },
- saveImage(tempFilePath) {
- uni.saveImageToPhotosAlbum({
- filePath: tempFilePath,
- success: () => {
- uni.showToast({
- title: '保存成功',
- icon: 'success'
- });
- this.$refs.downPopupRef.close()
- },
- fail: (err) => {
- console.error('保存失败:', err);
- uni.showToast({
- title: '保存失败',
- icon: 'none'
- });
- this.$refs.downPopupRef.close()
- }
- });
- }
- }
- };
- </script>
- <style>
- button {
- margin-top: 20px;
- padding: 10px 20px;
- background-color: #007AFF;
- color: white;
- border-radius: 5px;
- }
- </style>
|