绿满眶商城微信小程序-uniapp
zxl
2025-07-23 1ae987d882c1d67c0ee11c40af05d311df9e214b
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<template>
    <view class="person-msg">
        <view class="head c-content">
            <image v-if="form.face" :src="endpoint + '/' + form.face" mode=""></image>
            <image v-else :src="'/pages/subComponents/static/missing-face.png'" mode=""></image>
            <button class="avatar-button" type="default" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">点击修改头像</button>
        </view>
        <u-form :model="form" ref="uForm" class="form">
            <u-form-item label="昵称" label-width="150">
                <u-input v-model="form.nickName" placeholder="请输入昵称" type="nickname"/>
                
            </u-form-item>
            
            <u-form-item label="性别" label-width="150">
                <u-radio-group v-model="form.sex" :active-color="lightColor">
                    <u-radio name="1">男</u-radio>
                    <u-radio name="0">女</u-radio>
                </u-radio-group>
            </u-form-item>
 
            <u-form-item label="生日" label-width="150" right-icon="arrow-right">
                <div style="width: 100%;" @click="showBirthday = true">{{ birthday || '请选择出生日期' }}</div>
                <u-picker v-model="showBirthday" mode="time" :confirm-color="lightColor"
                    @confirm="selectTime"></u-picker>
            </u-form-item>
            <u-form-item label="城市" label-width="150" placeholder="请选择城市" right-icon="arrow-right">
                <div style="width: 100%;" @click="clickRegion">{{ form.___path || '请选择城市' }}</div>
            </u-form-item>
 
            <u-form-item label="手机号" label-width="150">
                <view v-if="form.mobile">
                    {{form.mobile}}
                </view>
                <view v-else>
                    <view class="submit" @click="navigateTo(form.username)">绑定手机号码</view>
                </view>
            </u-form-item>
            <u-form-item label="我的标签" label-width="150">
 
                <view class="tags-container">
                    <view v-for="(tag, index) in memberTags" :key="index"
                        :class="tag.tagName.length >=5 ? 'longTag' : 'tag'" @click="toggleTag(tag)">
                        {{tag.tagName}}
                        <u-icon name="close" size="16" class="delete-icon" @click.stop="removeTag(tag)"></u-icon>
                    </view>
                    <view class="add-tag" @click="showAddTagDialog">
                        <u-icon name="plus" size="24"></u-icon> 自定义
                    </view>
                </view>
            </u-form-item>
 
        </u-form>
        <div class="bottom">
            <view class="submit" @click="submit">保存</view>
            <view class="submit light" @click="quiteLoginOut">退出登录</view>
        </div>
        <m-city :provinceData="region" headTitle="区域选择" ref="cityPicker" @funcValue="getPickerParentValue"
            pickerSize="4"></m-city>
    </view>
</template>
<script>
    import '@/components/uview-components/uview-ui';
    import {
        getSTSToken
    } from "@/api/common.js";
    import {
        saveUserInfo,
        getUserInfo
    } from "@/api/members.js";
    import {
        upload
    } from "@/api/common.js";
    import {
        getFileKey
    } from "@/utils/file.js";
    import storage from "@/utils/storage.js";
    import city from "@/pages/mine/set/m-city/m-city.vue";
    import {
        getMemberTag,
        addTag,
        removeTag
    } from "@/api/memberCustomerTag.js"
    export default {
        components: {
            "m-city": city
        },
        data() {
            return {
                memberTags: null,
                cosClient: null,
                bucket: '',
                region: '',
                endpoint: '',
                lightColor: this.$lightColor, //高亮颜色
                form: {
                    nickName: storage.getUserInfo().nickName || "",
                    birthday: storage.getUserInfo().birthday || "",
                    face: storage.getUserInfo().face, //默认头像
                    regionId: [], //地址Id
                    region: storage.getUserInfo().region || [], //地址
                    sex: storage.getUserInfo().sex, //性别
                    ___path: storage.getUserInfo().region,
                    mobile: storage.getUserInfo().mobile,
                    username: storage.getUserInfo().username,
                },
                birthday: storage.getUserInfo().birthday || "", //生日
                photo: [{
                        text: "立即拍照",
                        color: this.$mainColor
                    },
                    {
                        text: "从相册选择",
                        color: this.$mainColor
                    },
                ],
                region: [
                    //请求城市默认地址
                    {
                        id: "",
                        localName: "请选择",
                        children: [],
                    },
                ],
                showBirthday: false, //显示生日日期
            };
        },
        onLoad() {
            this.initCOS()
            this.getMemberTag()
        },
        methods: {
            
            toggleTag(tag) {
                console.log(tag)
            },
            addTag(content) {
                const form = {
                    tagName: content,
                }
                addTag(form).then(res => {
                    if (res.statusCode === 200) {
                        if(res.data.code !==200){
                            uni.showToast({
                                title: res.data.msg,
                                icon: 'none'
                            });
                            return
                        }
                        this.getMemberTag()
                    }
                })
            },
            removeTag(tag) {
                removeTag(tag.id).then(res => {
                    if (res.statusCode === 200) {
                        if(res.data.code !==200){
                            uni.showToast({
                                title: res.data.msg,
                                icon: 'none'
                            });
                            return
                        }
                        this.getMemberTag()
                    }
                })
            },
            getMemberTag() {
                uni.showLoading({
                    title: '加载中',
                    mask: true
                });
                getMemberTag().then(res => {
                    uni.hideLoading();
                    if (res.statusCode === 200) {
                        if(res.data.code !== 200){
                            uni.showToast({
                                title: res.data.msg,
                                icon: 'none'
                            });
                            return
                        }
                        this.memberTags = res.data.data
 
                    }
                })
            },
            showAddTagDialog() {
                uni.showModal({
                    title: '添加自定义标签',
                    editable: true,
                    placeholderText: '请输入标签内容(最多8个字)',
                    success: (res) => {
                        if (res.confirm) {
                            if (!res.content) {
                                uni.showToast({
                                    title: '标签内容不能为空',
                                    icon: 'none'
                                });
                                return;
                            }
 
                            if (res.content.length > 8) {
                                uni.showToast({
                                    title: '标签长度不能超过8个字',
                                    icon: 'none'
                                });
                                return;
                            }
                            this.addTag(res.content)
                        }
                    }
                });
            },
            // 初始化腾讯云cos客户端
            initCOS() {
                // 调用后端获取sts临时访问凭证
                getSTSToken().then(res => {
                    const COS = require('@/lib/cos-wx-sdk-v5.js'); // 开发时使用
                    // const COS = require('./lib/cos-wx-sdk-v5.min.js'); // 上线时使用压缩包
 
                    // console.log(COS.version);  sdk 版本需要不低于 1.7.2
                    this.cosClient = new COS({
                        SecretId: res.data.data.tmpSecretId, // sts 服务下发的临时 secretId
                        SecretKey: res.data.data.tmpSecretKey, // sts 服务下发的临时 secretKey
                        SecurityToken: res.data.data.sessionToken, // sts 服务下发的临时 SessionToken
                        StartTime: res.data.data.stsStartTime, // 建议传入服务端时间,可避免客户端时间不准导致的签名错误
                        ExpiredTime: res.data.data.stsEndTime, // 临时密钥过期时间
                        SimpleUploadMethod: 'putObject', // 强烈建议,高级上传、批量上传内部对小文件做简单上传时使用 putObject,sdk 版本至少需要v1.3.0
                    });
                    this.bucket = res.data.data.bucket
                    this.region = res.data.data.region
                    this.endpoint = res.data.data.endpoint
                })
            },
            /**
             * 退出登录
             */
            quiteLoginOut() {
                this.$options.filters.quiteLoginOut();
            },
 
            /**
             * 选择地址回调
             */
            getPickerParentValue(e) {
                this.form.region = [];
                this.form.regionId = [];
                let name = "";
 
                e.forEach((item, index) => {
                    if (item.id) {
                        this.form.region.push(item.localName);
                        this.form.regionId.push(item.id);
                        if (index == e.length - 1) {
                            name += item.localName;
                        } else {
                            name += item.localName + ",";
                        }
                        this.form.___path = name;
                    }
                });
            },
 
            /**
             * 点击选择地址
             */
            clickRegion() {
                this.$refs.cityPicker.show();
            },
 
            /**
             * 提交保存
             */
            submit() {
                delete this.form.___path;
                let params = JSON.parse(JSON.stringify(this.form));
                saveUserInfo(params).then((res) => {
                    if (res.statusCode == 200) {
                        storage.setUserInfo(res.data.result);
                        uni.navigateBack();
                    }
                });
            },
            /**
             * 修改头像
             * @param {Object} e
             */
            onChooseAvatar(e){
                console.log(e.detail.avatarUrl)
                console.log("")
                console.log(e.detail)
                const tempFilePath = e.detail.avatarUrl;
                let fileName = tempFilePath.substring(tempFilePath.lastIndexOf('/') + 1);
                if (fileName.indexOf('%') > -1) {
                    fileName = decodeURIComponent(fileName);
                }
                const fileKey = getFileKey(fileName);
                this.cosClient.uploadFile({
                    Bucket: this.bucket,
                    Region: this.region,
                    Key: fileKey,
                    FilePath: tempFilePath,
                    SliceSize: 1024 * 1024 * 5 /* 触发分块上传的阈值,5M */
                }, (err, data) => {
                    if (err) {
                        console.log('上传失败', err);
                    } else {
                        this.form.face = fileKey;
                    }
                });
            },
            /**
             * 修改头像
             */
            // changeFace(index) {
            //     uni.chooseImage({
            //         count: 1,
            //         sizeType: ['compressed'],
            //         sourceType: ['album'],
            //         success: (chooseImageRes) => {
                        
            //             const tempFilePath = chooseImageRes.tempFilePaths[0];
            //             let fileName = tempFilePath.substring(tempFilePath.lastIndexOf('/') + 1);
            //             // 处理安卓可能的URI编码
            //             if (fileName.indexOf('%') > -1) {
            //                 fileName = decodeURIComponent(fileName);
            //             }
            //             const fileKey = getFileKey(fileName);
            //             this.cosClient.uploadFile({
            //                 Bucket: this.bucket,
            //                 Region: this.region,
            //                 Key: fileKey,
            //                 FilePath: tempFilePath,
            //                 SliceSize: 1024 * 1024 * 5 /* 触发分块上传的阈值,5M */
            //             }, (err, data) => {
            //                 if (err) {
            //                     console.log('上传失败', err);
            //                 } else {
            //                     this.form.face = fileKey;
            //                 }
            //             });
            //         },
            //     });
            // },
 
            /**
             * 选择地址
             */
            selectRegion(region) {
                this.$set(
                    this.form,
                    "address",
                    `${region.province.label} ${region.city.label} ${region.area.label}`
                );
            },
 
            /**
             * 选择时间
             */
            selectTime(time) {
                this.form.birthday = `${time.year}-${time.month}-${time.day}`;
                this.birthday = `${time.year} - ${time.month} - ${time.day}`;
            },
 
            navigateTo(username) {
                uni.navigateTo({
                    url: '/pages/mine/set/securityCenter/bindMobile' + '?username=' + username,
                });
            },
        }
 
    };
</script>
<style>
    page {
        background: #fff;
    }
</style>
<style lang="scss" scoped>
    .submit {
        height: 90rpx;
        line-height: 90rpx;
        text-align: center;
        margin-top: 90rpx;
 
        width: 100%;
        margin: 0 auto;
        color: $main-color;
        border-radius: 100px;
    }
 
    .head {
        height: 260rpx;
        color: $font-color-light;
        font-size: $font-sm;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        line-height: 2em;
 
        image {
            width: 144rpx;
            height: 144rpx;
            border-radius: 50%;
        }
    }
 
    /deep/ .u-form {
        background-color: #ffffff;
        padding: 0;
        margin-top: 30rpx;
 
        .u-form-item {
            padding: 0 20rpx;
            height: 110rpx;
            line-height: 110rpx;
        }
    }
 
    .form {
        background-color: #ffffff;
    }
 
    .bottom {
        position: fixed;
        bottom: 40px;
        display: flex;
        align-items: center;
        width: 100%;
 
        >.submit {
            background: $light-color;
            color: #fff;
            width: 40%;
        }
 
    }
 
    .light {
        background: rgba($color: $light-color, $alpha: 0.2) !important;
        color: $light-color !important;
    }
 
    /**
    * 标签位置样式 
    */
    .tags-container {
        display: flex;
        flex-flow: row wrap;
        /* 保持宽度且换行 */
        gap: 12rpx;
        /* 标签间距 */
        padding: 10rpx 0;
        width: 100%;
        /* 确保容器宽度填满 */
 
    }
 
    /* 标签基础样式 */
    .tag,
    .add-tag {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        height: 60rpx;
        line-height: 60rpx;
        border-radius: 30rpx;
        font-size: 26rpx;
        padding: 0 24rpx;
        white-space: nowrap;
        margin-right: 12rpx;
        margin-bottom: 12rpx;
        flex-shrink: 0;
        /* 禁止压缩 */
    }
 
    /* 普通标签样式 */
    .tag {
        background-color: #f5f5f5;
        color: #666;
        position: relative;
        padding-right: 48rpx;
        /* 为删除图标留空间 */
    }
 
    .longTag {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        height: 60rpx;
        line-height: 60rpx;
        border-radius: 30rpx;
        font-size: 26rpx;
        padding: 0 24rpx;
        white-space: nowrap;
        margin-right: 12rpx;
        margin-bottom: 12rpx;
        flex-shrink: 0;
        /* 禁止压缩 */
        width: 300rpx;
        background-color: #f5f5f5;
        color: #666;
        position: relative;
        padding-right: 48rpx;
        /* 为删除图标留空间 */
    }
 
    /* 添加标签按钮样式 */
    .add-tag {
        border: 1px dashed #ccc;
        color: #999;
    }
 
    /* 删除按钮 */
    .delete-icon {
        position: absolute;
        right: 12rpx;
        top: 50%;
        transform: translateY(-50%);
        width: 24rpx;
        height: 24rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0.1);
        color: #999;
        font-size: 24rpx;
    }
    .avatar-button {
        background: transparent;
        color: $font-color-light;
        font-size: $font-sm;
        line-height: 2em;
        margin: 0;
        padding: 0;
        border: none;
        &::after {
          border: none;
        }
      }
</style>