quanyawei
2023-11-20 5f8cd55f32939d15c6224d491f89743421cab0f9
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<template>
    <uni-popup id="privacy" type="center" ref="privacyPopup" :maskClick="false">
        <view class="ws-privacy-popup" :style="rootStyle">
            <view class="ws-privacy-popup__header">
                <!--标题-->
                <view class="ws-picker__title">{{ title }}</view>
            </view>
            <view class="ws-privacy-popup__container">
                <text>{{ desc }}</text>
                <text class="ws-privacy-popup__container-protocol" :style="protocolStyle"
                    @click="openPrivacyContract">{{ privacyContractName||protocol }}</text>
                <text>{{ subDesc }}</text>
            </view>
            <view class="ws-privacy-popup__footer">
                <button class="is-agree" :style="agreeStyle" id="agree-btn" open-type="agreePrivacyAuthorization"
                    @agreeprivacyauthorization="handleAgree">
                    {{agreeText}}
                </button>
                <button class="is-disagree" id="disagree-btn" @click="handleDisagree">
                    {{disagreeText}}
                </button>
            </view>
        </view>
    </uni-popup>
</template>
<script>
    import {
        getContext,
        getComponent
    } from './util'
    const privacyResolves = new Set() // onNeedPrivacyAuthorization的reslove
 
    let privacyHandler = null
    // 注册监听
    if (uni.onNeedPrivacyAuthorization) {
        uni.onNeedPrivacyAuthorization((resolve) => {
            if (typeof privacyHandler === 'function') {
                privacyHandler(resolve)
            }
        })
    }
 
    export default {
        name: 'wsWxPrivacy',
        emits: ['disagree', 'agree'],
        props: {
            // 标题
            title: {
                type: String,
                default: '用户隐私保护提示'
            },
            // 描述
            desc: {
                type: String,
                default: '感谢您使用本应用,您使用本应用的服务之前请仔细阅读并同意'
            },
            // 自定义隐私保护指引名称
            protocol: {
                type: String,
                default: '《用户隐私保护指引》'
            },
            // 是否自动获取隐私保护指引名称(开启后调用getPrivacySetting获取名称)
            enableAutoProtocol: {
                type: Boolean,
                default: false, // 默认为使用自定义隐私指引名称
            },
            // 子描述
            subDesc: {
                type: String,
                default: '。当您点击同意并开始使用产品服务时,即表示你已理解并同意该条款内容,该条款将对您产生法律约束力。如您拒绝,将无法使用相应服务。'
            },
            /**
             * 控制是否可以点击不同意按钮并显示提示。
             * 如果设置为 true,用户可以点击不同意按钮执行后续逻辑。
             * 如果设置为 false,点击不同意按钮会显示提示信息,但不会执行后续逻辑。
             * 默认为 true
             */
            disagreeEnabled: {
                type: Boolean,
                default: true, // 默认为可以点击
            },
            /**
             * 配置不同意按钮的提示消息内容。
             */
            disagreePromptText: {
                type: String,
                default: '请先仔细阅读并同意隐私协议', // 默认提示消息
            },
            // 拒绝按钮文字
            disagreeText: {
                type: String,
                default: '不同意'
            },
            // 同意按钮文字
            agreeText: {
                type: String,
                default: '同意并继续'
            },
            // 自定义背景颜色
            bgColor: {
                type: String,
                default: ''
            },
            // 自定义主题颜色(控制同意按钮和隐私协议名称的颜色)
            themeColor: {
                type: String,
                default: ''
            }
        },
        data() {
            return {
                privacyContractName: '',
            }
        },
        computed: {
            rootStyle() {
                if (this.bgColor) {
                    return `background:${this.bgColor}`
                } else {
                    return ''
                }
            },
            protocolStyle() {
                if (this.themeColor) {
                    return `color:${this.themeColor}`
                } else {
                    return ''
                }
            },
            agreeStyle() {
                if (this.themeColor) {
                    return `background:${this.themeColor}`
                } else {
                    return ''
                }
            }
        },
        created() {
            privacyHandler = (resolve) => {
                const context = getContext()
                const privacyPopup = getComponent(context, '#privacy-popup')
                if (privacyPopup) {
                    const privacy = getComponent(privacyPopup, '#privacy')
                    if (privacy && privacy.open) {
                        privacy.open()
                    }
                }
                privacyResolves.add(resolve)
            }
            if (this.enableAutoProtocol && uni.getPrivacySetting) {
                uni.getPrivacySetting({
                    success: res => {
                        if (res.privacyContractName) {
                            this.privacyContractName = res.privacyContractName
                        }
                    },
                    fail: () => {},
                    complete: () => {}
                })
            }
        },
        methods: {
            /**
             * 打开隐私协议
             */
            openPrivacyContract() {
                wx.openPrivacyContract({
                    success: (res) => {
                        console.log('openPrivacyContract success')
                    },
                    fail: (res) => {
                        console.error('openPrivacyContract fail', res)
                    }
                })
            },
 
            /**
             * 拒绝隐私协议
             */
            handleDisagree() {
                if (this.disagreeEnabled) {
                    this.$refs.privacyPopup.close()
                    privacyResolves.forEach((resolve) => {
                        resolve({
                            event: 'disagree'
                        })
                    })
                    privacyResolves.clear()
                    this.$emit('disagree')
                } else {
                    uni.showToast({
                        icon: 'none',
                        title: this.disagreePromptText
                    })
                }
 
            },
 
            /**
             * 同意隐私协议
             */
            handleAgree() {
                this.$refs.privacyPopup.close()
                privacyResolves.forEach((resolve) => {
                    resolve({
                        event: 'agree',
                        buttonId: 'agree-btn'
                    })
                })
                privacyResolves.clear()
                this.$emit('agree')
            }
        }
    }
</script>
<style lang="scss" scoped>
    .ws-privacy-popup {
        padding: 48rpx;
        box-sizing: border-box;
        overflow: hidden;
        width: 560rpx;
        background: linear-gradient(180deg, #e5edff 0%, #ffffff 100%);
        border-radius: 24rpx;
 
        &__header {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            height: 52rpx;
            font-size: 36rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 550;
            color: #1a1a1a;
            line-height: 52rpx;
            margin-bottom: 48rpx;
        }
 
        &__container {
            width: 100%;
            box-sizing: border-box;
            font-size: 28rpx;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            line-height: 48rpx;
            margin-bottom: 48rpx;
 
            &-protocol {
                font-weight: 550;
                color: #4D80F0;
            }
        }
 
        &__footer {
            display: flex;
            flex-direction: column;
 
            .is-disagree,
            .is-agree {
                width: 100%;
                height: 88rpx;
                background: #ffffff;
                border-radius: 44rpx;
                font-size: 32rpx;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #666666;
            }
 
            .is-agree {
                background: #4D80F0;
                color: #ffffff;
                margin-bottom: 18rpx;
            }
 
            button {
                border: none;
                outline: none;
 
                &::after {
                    border: none;
                }
            }
        }
    }
</style>