cshenfu
2023-05-26 5d3b2ffa8e1dc1e1cce3e05b2131516bcde0f6bd
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<template>
    <view class="">
        <uni-popup type="bottom" ref="model">
            <view class="getUserProfile">
                <view class="head">
                    <image v-if="logo" class="logo" :src="logo"></image>
                    <view class="desc">{{ sysInfo.appName }} 申请</view>
                </view>
                <view class="title">完善你的昵称、头像</view>
                <form @submit="onSubmit(true, $event)">
                    <view class="form-item">
                        <view class="label">头像</view>
                        <button name="avatarUrl" class="auto primary" :class="{ noBg: formData.avatarUrl }"
                            style="padding: 10rpx 20rpx;" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
                            <text v-if="!formData.avatarUrl">授权头像</text>
                            <image class="avatar" v-else :src="formData.avatarUrl"></image>
                        </button>
                    </view>
                    <view class="form-item">
                        <view class="label">昵称</view>
                        <input name="nickName" type="nickname" placeholder="请填写昵称" class="ipt"
                            v-model="formData.nickName" />
                    </view>
                    <view class="foot">
                        <button @click="onSubmit(false)">取消</button>
                        <button class="primary" form-type="submit">确定</button>
                    </view>
                </form>
            </view>
        </uni-popup>
    </view>
</template>
 
<script>
    export default {
        name: 'daxiang-getUserProfile',
        data() {
            return {
                userInfoKey: 'daxiang-getUserProfile-1.0',
                sysInfo: {},
                formData: {
                    nickName: '',
                    avatarUrl: ''
                },
                options: {}
            };
        },
        created() {
            this.sysInfo = uni.getSystemInfoSync();
        },
        props: {
            logo: String,
            // 是否上传头像
            uploadAvatar: {
                type: Boolean,
                default: true
            }
        },
        methods: {
            open(options) {
                return new Promise((resolve, reject) => {
                    let canIUse = true;
                    // #ifdef MP-WEIXIN
                    let {
                        SDKVersion
                    } = this.sysInfo;
                    console.log('SDKVersion: ', SDKVersion);
                    canIUse = this.compareVersion('2.27.1', SDKVersion) == 1;
                    // #endif
                    console.log('canIUse: ', canIUse);
                    if (canIUse) {
                        uni.getUserProfile({
                            ...options
                        });
                    } else {
                        let userInfo = uni.getStorageSync(this.userInfoKey);
                        try {
                            if (userInfo) {
                                this.checkForm(userInfo);
                                this.options.success && this.options.success({
                                    userInfo
                                });
                            } else {
                                throw Error('');
                            }
                        } catch (e) {
                            this.$refs.model.open();
                            this.options = options;
                        }
                    }
                });
            },
            checkForm(data) {
                if (!data.avatarUrl) {
                    throw Error('请授权头像');
                }
                if (!data.nickName) {
                    throw Error('昵称不能为空');
                }
            },
            async onSubmit(confirm, event) {
                if (confirm) {
                    try {
                        console.log('this.formData: ', this.formData);
                        let {
                            nickName
                        } = event.detail.value;
                        this.checkForm({
                            avatarUrl: this.formData.avatarUrl,
                            nickName
                        });
                        this.formData.nickName = nickName;
                    } catch (e) {
                        //TODO handle the exception
                        uni.showToast({
                            icon: 'none',
                            title: e.message
                        });
                        return;
                    }
                    // if (this.uploadAvatar) {
                    //     this.formData.avatarUrl = await this.uploadImage(this.formData.avatarUrl);
                    // }
                    console.log('this.options: ', this.options);
                    // uni.setStorageSync(this.userInfoKey, this.formData);
                    this.options.success && this.options.success({
                        userInfo: this.formData
                    });
                } else {
                    this.options.fail && this.options.fail(Error('用户点击取消'));
                }
                this.$refs.model.close();
            },
            uploadImage(url) {
                return new Promise((resolve, reject) => {
                    if (!this.isLocalFile(url)) {
                        return resolve(url);
                    }
                    let ranNum = this.getRanNum(1000, 9999);
                    uni.showLoading({
                        mask: true
                    });
                    uniCloud.uploadFile({
                        cloudPath: `avatar/${Date.now()}_${ranNum}.jpg`,
                        filePath: url,
                        success(e) {
                            resolve(e.fileID);
                        },
                        fail: reject,
                        complete: uni.hideLoading
                    });
                });
            },
            isLocalFile(url) {
                /** 是否是base64本地地址 */
                const isBaseUrl = str => {
                    return /^\s*data:(?:[a-z]+\/[\d+.a-z-]+(?:;[a-z-]+=[\da-z-]+)?)?(?:;base64)?,([\s\w!$%&'()*+,./:;=?@~-]*?)\s*$/i
                        .test(str);
                };
                /** 是否是小程序本地地址 */
                const isTmpUrl = str => {
                    return /http:\/\/temp\/wx/.test(str);
                };
                /** 是否是网络地址 */
                const isNetworkUrl = str => {
                    return /^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w#%&+,./:=?@^~-]*[\w#%&+/=?@^~-])?$/.test(str);
                };
                return isBaseUrl(url) || isTmpUrl(url) || !isNetworkUrl(url);
            },
            getRanNum(min, max, fixedNum = 0, func = 'round') {
                return Math[func]((Math.random() * (max - min) + min) * 10 ** fixedNum) / 10 ** fixedNum;
            },
            onChooseAvatar(e) {
                this.formData.avatarUrl = e.detail.avatarUrl;
            },
            compareVersion(v1, v2) {
                v1 = v1.split('.');
                v2 = v2.split('.');
                const len = Math.max(v1.length, v2.length);
 
                while (v1.length < len) {
                    v1.push('0');
                }
                while (v2.length < len) {
                    v2.push('0');
                }
 
                for (let i = 0; i < len; i++) {
                    const num1 = parseInt(v1[i]);
                    const num2 = parseInt(v2[i]);
 
                    if (num1 > num2) {
                        return 1;
                    } else if (num1 < num2) {
                        return -1;
                    }
                }
 
                return 0;
            }
        }
    };
</script>
 
<style scoped lang="scss">
    .getUserProfile {
        background-color: #ededed;
        border-radius: 10rpx 10rpx 0 0;
        padding: 52rpx 32rpx;
        font-size: 26rpx;
        color: #333;
 
        .head {
            display: flex;
            align-items: center;
            margin-bottom: 54rpx;
 
            .logo {
                --size: 40rpx;
                width: var(--size);
                height: var(--size);
                border-radius: 50%;
                margin-right: 16rpx;
            }
        }
 
        .title {
            font-size: 28rpx;
            margin-bottom: 26rpx;
        }
 
        .form-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            height: 80rpx;
            border: 0 solid #e5e5e5;
            border-bottom-width: 1px;
            font-size: 24rpx;
 
            &:first-child {
                border-top-width: 1px;
            }
 
            .label {
                color: #666;
            }
 
            .ipt {
                font-size: 28rpx;
                background: #e5e5e5;
                height: 80%;
                text-align: right;
                padding: 0 30rpx;
            }
        }
 
        .avatar {
            --size: 60rpx;
            width: var(--size);
            height: var(--size);
            border-radius: 10rpx;
        }
 
        button {
            background: #fff;
            box-shadow: 0 0 1px 2px #eee;
            width: 180rpx;
            height: 60rpx;
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0;
            font-size: 24rpx;
            $color: #07c160;
            color: $color;
 
            &+button {
                margin-left: 30rpx;
            }
 
            &.noBg {
                background: none;
                padding: 0 !important;
            }
 
            &.auto {
                width: auto;
                height: auto;
                padding: 12rpx 20rpx;
            }
 
            &.primary {
                background-color: $color;
                color: #fff;
            }
        }
 
        .foot {
            display: flex;
            justify-content: center;
            padding-top: 48rpx;
        }
    }
</style>