祖安之光
2025-08-07 41193d186d157937ba052e73dd04c12018e9ecab
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
const app = getApp();
const avatorPic = '/static/avator.png';
const api = require('../../../utils/api');
const cloudApi = require('../../../utils/cloudApi')
Page({
  data: {
    page: 'pages/tabBar/count/count',
    statusBarHeight: '',
    username: '',
    user: {},
    avator: avatorPic,
    showModal: false,
    pwdForm: {
      id: null,
      password: '',
      rePassword: ''
    },
    isPwd1: true,
    isPwd2: true,
  },
 
  onLoad() {
    this.setData({
      statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
      username: wx.getStorageSync('username'),
      user: wx.getStorageSync('userSet') || {}
    });
    wx.hideTabBar();
  },
 
  onFormInput(e) {
    const field = e.currentTarget.dataset.field; // 获取字段名
    this.setData({
      [field]: e.detail.value  // 动态更新对应字段
    });
  },
 
  toHazmat() {
    wx.setStorageSync("prevPage", '/pages/tabBar/count/count');
    wx.navigateTo({
      url: '/pages/tabBar/count/countDetail'
    });
  },
 
  toProduct() {
    wx.setStorageSync("prevPage", '/pages/tabBar/count/count');
    wx.navigateTo({
      url: '/pages/tabBar/count/productDetail'
    });
  },
 
  loginOut() {
    wx.showModal({
      title: '提示',
      content: '是否确认退出该账号?',
      success: (res) => {
        if (res.confirm) {
          // api接口
          api.loginOut().then(res => {
            if(res.code == 200) {
              wx.showToast({
                title: '账户已退出',
                duration: 800
              });
              setTimeout(() => {
                if(!wx.getStorageSync('isSave') || wx.getStorageSync('isSave') == false){
                  wx.clearStorageSync();
                }
                wx.navigateTo({
                  url: '/pages/index/index'
                });
              }, 800);
            }
          });
        }
      }
    });
  },
 
  openResetPwd() {
    console.log(this.data.showModal,1)
    this.setData({
      showModal: true
    });
    console.log(this.data.showModal,2)
  },
 
  togglePwd1() {
    this.setData({
      isPwd1: !this.data.isPwd1
    });
  },
 
  togglePwd2() {
    this.setData({
      isPwd2: !this.data.isPwd2
    });
  },
 
  confirmPwd() {
    // 密码验证
    if (!this.data.pwdForm.password) {
      wx.showToast({
        title: '新密码不可为空',
        icon: 'error'
      });
      return;
    }
    if (!/^(?![a-zA-Z]+$)(?!\d+$)(?![!@#$%^&\.*]+$)(?![a-zA-Z\d]+$)(?![a-zA-Z!@#$%^&\.*]+$)(?![\d!@#$%^&\.*]+$)[a-zA-Z\d!@#$%^&\.*\-_]{6,16}$/.test(this.data.pwdForm.password)) {
      wx.showToast({
        title: '密码须同时包含字母、数字和特殊字符,长度在6-16之间',
        icon: 'none'
      });
      return;
    }
    if (!this.data.pwdForm.rePassword) {
      wx.showToast({
        title: '请再次输入新密码',
        icon: 'error'
      });
      return;
    }
    
    if (this.data.pwdForm.password !== this.data.pwdForm.rePassword) {
      wx.showToast({
        title: '两次输入密码不一致',
        icon: 'none'
      });
      return;
    }
    // const Id = wx.getStorageSync("uid")
    // cloudApi.updateKey('userList',Id,{password: this.base64Encode(this.data.pwdForm.password)}).then(res=>{
    //   if(res.result == 'success'){
    //     wx.showToast({
    //       title: '密码已修改,请重新登录',
    //       icon: 'none',
    //       duration: 1500
    //     });
    //     wx.clearStorageSync();
    //     wx.navigateTo({
    //       url: '/pages/index/index'
    //     });
    //   }else{
    //    wx.showToast({
    //       title: 密码修改失败,
    //       duration: 2000
    //     });
    //   }
    // })
    // const db = wx.cloud.database()
    // db.collection('userList').doc(Id).update({
    //   data: {
    //     password: this.base64Encode(this.data.pwdForm.password)
    //   },
    //   success: function(res) {
    //     wx.showToast({
    //       title: '密码已修改,请重新登录',
    //       icon: 'none',
    //       duration: 1000
    //     });
    //     wx.clearStorageSync();
    //     wx.navigateTo({
    //       url: '/pages/index/index'
    //     });
    //   },
    //   error: function(res){
    //     wx.showToast({
    //       title: 密码修改失败,
    //       duration: 2000
    //     });
    //   }
    // })
 
    // api请求
    let data = {
      id: wx.getStorageSync("uid"),
      password: this.base64Encode(this.data.pwdForm.password)
    };
    api.resetPwd(data).then((r) => {
      if(r.code == 200) {
        wx.showToast({
          title: '密码已修改,请重新登录',
          icon: 'none',
          duration: 1000
        });
        loginOut().then(res => {
          if(res.code == 200) {
            setTimeout(() => {
              wx.clearStorageSync();
              wx.navigateTo({
                url: '/pages/index/index'
              });
            }, 1000);
          }
        });
      } else {
        wx.showToast({
          title: r.message,
          icon: 'none'
        });
      }
    }).catch((err) => {
      wx.showToast({
        title: err,
        duration: 2000
      });
    });
    
    this.closeModal();
  },
 
  base64Encode(e) {
    const utf8Bytes = unescape(encodeURIComponent(e));
    const array = new Uint8Array(utf8Bytes.length);
    for (let i = 0; i < utf8Bytes.length; i++) {
      array[i] = utf8Bytes.charCodeAt(i);
    }
    return wx.arrayBufferToBase64(array);
  },
 
  closeModal() {
    this.setData({
      showModal: false,
      pwdForm: {
        id: null,
        password: '',
        rePassword: ''
      },
      isPwd1: true,
      isPwd2: true
    });
  }
});