陶杰
2024-08-22 4aa0bd47801606b4d0e0a6a2ed8fe92a7e5f2444
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
<template>
    <view class="container-page" style="min-height: calc(100vh - 150rpx)">
        <view>
            <view class="form-item">
                <view class="label">头像</view>
                <!-- @click="uploadImg" -->
                <view class="m-l-a m-r-0 flex">
                    <button type="balanced" open-type="chooseAvatar" class="img-button" @chooseavatar="onChooseavatar">
                        <!-- <image :src="avatarUrl" class="refreshIcon"></image> -->
                        <image class="image-size image-icon flex " style="margin-right: 0;margin-bottom: 20rpx;"
                            :src="dto.picture || '../../../../../static/icons/icon_header.png'"></image>
                    </button>
 
                    <!-- <image class="image-size image-icon flex " style="margin-right: 0;margin-bottom: 20rpx;"
                        :src="dto.picture || '../../../../../static/icons/icon_header.png'"></image> -->
                </view>
            </view>
            <view class="form-item">
                <view class="label">昵称</view>
 
                <view class="m-l-a m-r-0 flex text-right">
                    <!-- {{dto.nickName || '-'}} -->
                    <input v-model="dto.nickName" type="nickname" placeholder="请输入昵称" @blur="bindblur"
                        @input="bindinput" style="margin-top: 16px;text-align: right;"></input>
                </view>
            </view>
 
            <view label="手机号" class="form-item">
                <view class="label require">手机号</view>
                <view class="m-l-a m-r-0 flex">
                    <!-- {{dto.tel || '-'}} -->
                    <!-- @click="dto.tel = '13914014930'" -->
                    <!-- -->
                    <!-- #ifdef MP -->
                    <button v-if="!dto.tel" open-type="getPhoneNumber" @getphonenumber="getPhone"
                        style="background: none; border: none; font-size: 12px; box-sizing: 0px; height: 30px; outline: none; border-inline: none; margin-top: 16px;">
                        {{ dto.tel || '点击获取手机号'}}
                    </button>
                    <input v-model="dto.tel" disabled v-if="dto.tel" placeholder="请输入手机号"
                        style="margin-top: 16px;text-align: right;"></input>
 
                    <!-- #endif -->
                    <!-- #ifndef MP -->
                    <input v-model="dto.tel" placeholder="请输入手机号" style="margin-top: 16px;text-align: right;"></input>
 
                    <!-- #endif -->
 
                    <!--     <button @click="dto.tel = '13914014930'">
                        {{ dto.tel || '点击获取手机号'}}
                    </view> -->
                </view>
            </view>
 
 
        </view>
        <view class="button-green-1 m-t-20 button-fixed-bottom" @click="update">
            保存
        </view>
    </view>
</template>
 
<script>
    const appId = 'wx1441324401626290'; //需要替换成实际的
    import WXBizDataCrypt from '@/plugins/WXBizDataCrypt.js';
    import {
        mapState
    } from 'vuex'
 
    export default {
 
        data() {
            return {
                dto: {}
            }
        },
        async mounted() {
            // await this.$store.dispatch('getCurrentInfo');
            this.dto = {
                ...this.currentInfo,
            }
        },
        methods: {
            bindblur(e) {
                this.dto.nickName = e.detail.value; // 获取微信昵称
            },
            bindinput(e) {
                this.dto.nickName = e.detail.value; // 获取微信昵称
            },
            async update() {
 
                if (!this.dto.nickName) {
                    this.$message.showToast('请填写姓名')
                    return
                }
                // "/api/current/user/update"
                this.$message.showLoading()
                if (this.dto.picture && this.dto.picture.indexOf('http://tmp/') >= 0) {
                    //上传
                    const res = await this.$http.upload(this.dto.picture)
                    var picture = res.data && res.data.length > 0 && res.data[0].url || ''
                    this.dto.picture = picture
                }
                
 
                const {
                    code
                } = await this.$http.request('post', "/api/current/user/update", {
                    data: {
                        ...this.dto,
                        nickName: this.dto.nickName,
                        // fullName: this.dto.nickName
                    }
                })
        
                // // #ifndef MP
                // if (!!this.dto.email) {
                //     if (!this.$util.isEmail(this.dto.email)) {
                //         this.$message.showToast('邮箱格式不正确')
                //         return
                //     }
                // }
                // const {
                //     code
                // } = await this.$http.request('post', '/api/personnel/employee/update', {
                //     data: {
                //         ...this.dto,
                //         nickName: this.dto.nickName,
                //         // fullName: this.dto.nickName
                //     }
                // })
                // // #endif
                this.$message.hideLoading()
 
                if (code == 0) {
                    this.$message.showToast('更新成功')
                    // this.$store.commit('updateInfo',this.dto)
                    // }
                    await this.$store.dispatch('getCurrentInfo');
                }
 
 
 
            },
            uploadImg() {
                const that = this
                uni.chooseImage({
                    count: 1, // 最多可以选择的图片张数,默认9
                    sizeType: ['original', 'compressed'], //original 原图,compressed 压缩图,默认二者都有
                    sourceType: ['album', 'camera'], //album 从相册选图,camera 使用相机,默认二者都有。如需直接开相机或直接选相册,请只使用一个选项
                    success: function({
                        errMsg,
                        tempFiles
                    }) {
                        if (errMsg === 'chooseImage:ok') {
                            // console.log(tempFiles[0])
                            that.$http.upload(tempFiles[0].path).then(res => {
                                // console.log('resupload',res)
                                that.dto.picture = res.data && res.data.length > 0 && res.data[0]
                                    .url || ''
                            })
                        }
                    }
                })
            },
            onChooseavatar(e) {
                let self = this;
                let {
                    avatarUrl
                } = e.detail;
                this.dto.picture = avatarUrl
                console.log('avatarUrl', avatarUrl)
                // that.$http.upload(tempFiles[0].path).then(res => {
                //     // console.log('resupload',res)
                //     that.dto.picture = res.data && res.data.length > 0 && res.data[0]
                //         .url || ''
                // })
            },
            async decodetel(jsCode, encry, iv) {
                // this.dto.tel = '13914014930';
                // return
                this.$message.showLoading();
                const resp = await this.$http.request('get', '/api/wx/jscode2session', {
                    params: {
                        jsCode: jsCode
                    }
                });
                this.$message.hideLoading();
                console.log('resp', resp)
                if (resp && resp.data) {
                    var cts = resp.data;
                    var openid = cts.openid; //openid 用户唯一标识
                    if (cts && cts.openid) {
                        this.$storage.setItem('openid', cts.openid);
                    }
 
                    var session_key = cts.session_key; //session_key  会话密钥
 
                    if (true) {
                        if (!!cts.errmsg) {
                            // this.dto.tel = '13914014930';
                            this.$message.showToast('获取失败' + cts.errmsg);
                        } else {
                            //成功了,再调用
                            try {
                                console.log('data1', encry, iv, appId, session_key)
                                let pc = new WXBizDataCrypt(appId, session_key); //wxXXXXXXX为你的小程序APPID
                                console.log('data2', pc)
                                let data = pc.decryptData(encry, iv);
                                console.log('var', data)
                                // console.log(data)       //data就是最终解密的用户信息
                                this.dto.tel = data.phoneNumber;
                                console.log('var', data)
                                this.$forceUpdate()
                                // this.update()
                                this.$message.showToast('获取成功');
 
                                // this.$storage.setItem('tel', this.tel);
 
                                // this.toLogin();
                            } catch (e) {
                                this.errorMsg += JSON.stringify(e);
                                console.log(e, this.errorMsg)
                                this.$message.showToast('登录过期,请重新授权');
                                // this.dto.tel = '13914014930';
                            }
                        }
                    }
                }
            },
            async getPhone(e) {
                const encry = e.detail.encryptedData;
                const iv = e.detail.iv;
                console.log('jsCode', e);
                console.log('encry iv', encry, iv)
                // this.dto.tel = '13914014930';
 
                if (e.target && e.target.errMsg === 'getPhoneNumber:fail user deny') {
                    // this.dto.tel = '13914014930';
                    this.$message.showToast('拒绝授权手机号');
                    return;
                }
 
                if (e.detail && e.detail.errMsg === 'getPhoneNumber:fail no permission') {
                    // this.dto.tel = '13914014930';
                    this.$message.showToast('拒绝授权手机号');
                    return;
                }
 
                // return 
                let tmp = this
                // // var jsCode = this.jsCode;
                wx.login({
                    success: async res => {
                        console.log(res)
                        if (res.code) {
                            this.decodetel(res.code, encry, iv)
                        } else {
                            tmp.$message.showToast('wx登录失败');
                            // this.dto.tel = '13914014930';
                        }
                    },
                    error: res => {
                        console.log('wx.login error', res)
                        // this.dto.tel = '13914014930';
                        tmp.$message.showToast('微信登录失败:' + res);
                    }
                });
 
            },
 
        },
        computed: {
            ...mapState(['currentInfo'])
        },
    }
</script>
 
<style lang="scss">
    .form-item {
        // padding: 40rpx;
        border-bottom: 1px solid #F3F3F3;
        display: flex;
        // height: 100px;
 
        .label {
            width: 200px;
            line-height: 60px;
 
        }
 
        .flex {
            line-height: 60px;
 
        }
    }
 
    .container-page {
        padding: 40rpx;
        background-color: #FFFFFF;
        // min-height: calc(100vh - 80rpx);
        min-height: 100%;
    }
 
    .img-button {
        background-color: #fff !important;
        border: 1px solid #fff;
        // border-radius: 200%;
        color: #fff;
 
        &::after {
            border: none;
        }
    }
</style>