移动系统liao
2025-02-17 557c2711a3e103ebc3d0492344eca9730d5e92b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<template>
    <view>
        <u-toast ref="uToast" /><u-no-network></u-no-network>
        <u-navbar title="分享" safeAreaInsetTop fixed placeholder>
            <view class="coreshop-navbar-left-slot" slot="left">
                <u-icon name="arrow-left" size="19" @click="goNavigateBack"></u-icon>
                <u-line direction="column" :hairline="false" length="16" margin="0 8px"></u-line>
                <u-icon name="home" size="22" @click="goHome"></u-icon>
            </view>
            <view slot="right">
            </view>
        </u-navbar>
        <view class="coreshop-margin-bottom-25 coreshop-padding-top-25 coreshop-text-center">
            <img class="share-img" :src="poster" mode="widthFix" />
        </view>
        <view class="share-bot flex flex-direction">
            <view class="coreshop-margin-bottom-20">
                <u-button shape="square" type="primary" @click="savePoster()">
                    <u-icon name="download" :margin-right="20" label="保存到本地" color="#fff" labelColor="#fff"></u-icon>
                </u-button>
            </view>
            <u-button shape="square" @click="goBack()">
                <u-icon name="arrow-leftward" :margin-right="20" label="返回" labelColor="#333"></u-icon>
            </u-button>
        </view>
        <!-- 登录提示 -->
        <coreshop-login-modal></coreshop-login-modal>
    </view>
</template>
<script>
    export default {
        data() {
            return {
                poster: ''
            };
        },
        onLoad(options) {
            this.poster = decodeURIComponent(options.poster);
        },
        computed: {
        },
        methods: {
            goBack() {
                uni.navigateBack({
                    delta: 1
                });
            },
            // 保存海报到本地
            savePoster() {
                let _this = this;
                // #ifdef MP || MP-ALIPAY || APP-PLUS || APP-PLUS-NVUE
                _this.downloadImageOfMp(_this.poster)
                // #endif
            },
            //下载图片地址和图片名
            downloadIamge(imgsrc, name) {
                var image = new Image();
                // 解决跨域 Canvas 污染问题
                image.setAttribute('crossorigin', 'anonymous');
                image.onload = () => {
                    var canvas = document.createElement('canvas');
                    canvas.width = image.width;
                    canvas.height = image.height;
                    var context = canvas.getContext('2d');
                    context.drawImage(image, 0, 0, image.width, image.height);
                    var url = canvas.toDataURL('image/png'); //得到图片的base64编码数据
                    var a = document.createElement('a'); // 生成一个a元素
                    var event = new MouseEvent('click'); // 创建一个单击事件
                    a.download = name || 'photo'; // 设置图片名称
                    a.href = url; // 将生成的URL设置为a.href属性
                    a.dispatchEvent(event); // 触发a的单击事件
                };
                image.src = imgsrc;
            },
            downloadImageOfMp(image) {
                let _this = this
 
                // #ifdef APP-PLUS || APP-PLUS-NVUE
                uni.downloadFile({
                    url: image,
                    success(res) {
                        uni.saveImageToPhotosAlbum({
                            filePath: res.tempFilePath,
                            success() {
                                _this.$refs.uToast.show({ message: '操作成功', type: 'success' })
                            },
                            fail() {
                                _this.$u.toast('图片保存失败')
                            }
                        });
                    },
                    fail() {
                        _this.$u.toast('下载失败')
                    }
                })
                // #endif
 
                // #ifdef MP
                uni.authorize({
                    scope: 'scope.writePhotosAlbum',
                    success() {
                        // 先下载到本地
                        uni.downloadFile({
                            url: image,
                            success(res) {
                                uni.saveImageToPhotosAlbum({
                                    filePath: res.tempFilePath,
                                    success() {
                                        _this.$refs.uToast.show({ message: '保存成功', type: 'success' })
                                    },
                                    fail() {
                                        _this.$u.toast('图片保存失败')
                                    }
                                });
                            },
                            fail() {
                                _this.$u.toast('下载失败')
                            }
                        })
                    },
                    fail() {
                        //console.log('授权失败')
                    }
                })
                // #endif
            }
        }
    };
</script>
<style scoped lang="scss">
    @import "sharePoster.scss";
</style>