uni-popup-post.vue
5.57 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<template>
<view class="uni-popup-post">
<image
class="uni_post_img"
:src="postUrl"
/>
<view
@tap="saveAndClose"
class="uni_post_button_box"
>
保存图片
</view>
</view>
</template>
<script>
export default {
name: 'UniPopupPost',
props: {
postUrl: {
type: String,
default: 'https://api.glass.xiuyetang.com/adv_pic/428_0_7.png',
},
},
inject: ['popup'],
data() {
return {}
},
created() {
console.log('dada', this)
},
methods: {
// 保存图片并关闭窗口
saveAndClose() {
// 获取用户保存相册权限
const scope = 'scope.writePhotosAlbum'
const that = this
uni.getSetting({
success(res) {
console.log('获取用户保存相册权限', res)
if (!res.authSetting[scope]) {
uni.authorize({
scope,
success() {
console.log('获取用户保存相册权限---->', '授权成功')
// 保存到相册
uni.showLoading({
title: '保存中',
})
that.saveImage()
},
fail() {
console.log('获取用户保存相册权限---->', '授权失败')
that.secondGetPhoteAuthor()
},
})
} else {
console.log('拥有授权权限')
// 保存到相册
uni.showLoading({
title: '保存中',
})
that.saveImage()
}
that.popup.close()
},
fail(res) {
console.log('授权失败------>', res)
uni.showToast({
title: '保存失败',
icon: 'none',
duration: 2000,
})
},
})
},
// 保存到相册
saveImage() {
const that = this
uni.getImageInfo({
src: that.postUrl,
success(res) {
console.log('图片读取是否可行', res)
uni.saveImageToPhotosAlbum({
filePath: res.path,
success() {
uni.hideLoading()
uni.showToast({
title: '已保存到相册',
icon: 'none',
duration: 2000,
})
},
fail(err) {
console.log('用户拒绝', err)
if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
console.log('当用户拒绝,再次发起授权')
that.secondGetPhoteAuthor()
} else if (err.errMsg === 'saveImageToPhotosAlbum:fail cancel') {
uni.showToast({
title: '已取消保存',
icon: 'none',
duration: 2000,
})
uni.hideLoading()
} else {
uni.showToast({
title: '请截屏保存分享',
icon: 'none',
duration: 2000,
})
uni.hideLoading()
}
},
})
},
fail(res) {
console.log('授权失败----->', res)
uni.hideLoading()
uni.showToast({
title: '保存失败',
icon: 'none',
duration: 2000,
})
},
})
},
// 二次请求授权
secondGetPhoteAuthor() {
const that = this
uni.showModal({
title: '保存海报',
content: '需要您提供保存相册权限',
success: (res) => {
if (res.confirm) {
uni.openSetting({
success(settingdata) {
console.log('settingdata 二次弹窗获取', settingdata)
if (settingdata.authSetting['scope.writePhotosAlbum']) {
console.log('二次弹窗获取---->', '获取 相册 权限成功,给出再次点击图片保存到相册的提示。')
uni.showLoading({
title: '保存中',
})
that.saveImage()
} else {
uni.showToast({
title: '保存失败',
icon: 'none',
duration: 2000,
})
console.log('二次弹窗获取', '获取 相册 权限失败,给出不给权限就无法正常使用的提示')
}
},
})
} else {
uni.showToast({
title: '保存失败',
icon: 'none',
duration: 2000,
})
}
},
fail(err) {
uni.showToast({
title: '保存失败',
icon: 'none',
duration: 2000,
})
console.log('再次失败', err)
},
})
},
},
}
</script>
<style lang="scss" scoped>
.uni-popup-post {
border-radius: 8px 8px 0px 0px;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
}
.uni_post_img {
width: 690rpx;
height: 466.667px;
margin-top: -180rpx;
}
.uni_post_button_box {
height: 112rpx;
line-height: 112rpx;
background-color: #ff6b4a;
border-radius: 4px;
width: 350px;
color: #fff;
text-align: center;
margin-top: 44rpx;
font-family: PingFangSC-Medium;
font-size: 14px;
color: #ffffff;
letter-spacing: -0.26px;
text-align: center;
}
</style>