曹睿
2025-04-25 ec1bef3a37e8dcdf22f1bf52e7c272a18306f4b9
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
<template>
  <view class="profile">
    <view class="profile-card">
      <wd-cell-group border>
        <wd-cell
          title="账户密码"
          label="定期修改密码有助于保护账户安全"
          value="修改"
          is-link
          @click="handleOpenDialog(DialogType.PASSWORD)"
        />
        <wd-cell
          title="绑定手机"
          :value="userProfile?.mobile"
          is-link
          @click="handleOpenDialog(DialogType.MOBILE)"
        />
        <wd-cell
          title="绑定邮箱"
          :value="userProfile?.email ? userProfile.email : '未绑定邮箱'"
          is-link
          @click="handleOpenDialog(DialogType.EMAIL)"
        />
      </wd-cell-group>
    </view>
 
    <!--用户信息编辑弹出框-->
    <wd-popup v-model="dialog.visible" position="bottom">
      <wd-form
        v-if="dialog.type === DialogType.PASSWORD"
        ref="passwordChangeFormRef"
        :model="passwordChangeForm"
        custom-class="edit-form"
      >
        <wd-cell-group border>
          <wd-input
            v-model="passwordChangeForm.oldPassword"
            label="原密码"
            label-width="160rpx"
            show-password
            clearable
            placeholder="请输入原密码"
            prop="oldPassword"
            :rules="rules.oldPassword"
          />
          <wd-input
            v-model="passwordChangeForm.newPassword"
            label="新密码"
            label-width="160rpx"
            show-password
            clearable
            placeholder="请输入新密码"
            prop="newPassword"
            :rules="rules.newPassword"
          />
          <wd-input
            v-model="passwordChangeForm.confirmPassword"
            label="确认密码"
            label-width="160rpx"
            show-password
            clearable
            placeholder="请确认新密码"
            prop="confirmPassword"
            :rules="rules.confirmPassword"
          />
        </wd-cell-group>
        <view class="footer">
          <wd-button type="primary" size="large" block @click="handleSubmit">提交</wd-button>
        </view>
      </wd-form>
      <wd-form
        v-if="dialog.type === DialogType.MOBILE"
        ref="mobileBindingFormRef"
        :model="mobileBindingForm"
        custom-class="edit-form"
      >
        <wd-cell-group border>
          <wd-input
            v-model="mobileBindingForm.mobile"
            label="手机号码"
            label-width="160rpx"
            clearable
            placeholder="请输入手机号码"
            prop="mobile"
            :rules="rules.mobile"
          />
          <wd-input
            v-model="mobileBindingForm.code"
            label="验证码"
            label-width="160rpx"
            clearable
            placeholder="请输入验证码"
            prop="code"
            :rules="rules.code"
          >
            <template #suffix>
              <wd-button
                plain
                :disabled="mobileCountdown > 0"
                @click="handleSendVerificationCode('MOBILE')"
              >
                {{ mobileCountdown > 0 ? `${mobileCountdown}s后重新发送` : "发送验证码" }}
              </wd-button>
            </template>
          </wd-input>
        </wd-cell-group>
        <view class="footer">
          <wd-button type="primary" size="large" block @click="handleSubmit">提交</wd-button>
        </view>
      </wd-form>
      <wd-form
        v-if="dialog.type === DialogType.EMAIL"
        ref="emailBindingFormRef"
        :model="emailBindingForm"
        custom-class="edit-form"
      >
        <wd-cell-group border>
          <wd-input
            v-model="emailBindingForm.email"
            label="邮箱"
            label-width="160rpx"
            clearable
            placeholder="请输入邮箱"
            prop="email"
            :rules="rules.email"
          />
          <wd-input
            v-model="emailBindingForm.code"
            label="验证码"
            label-width="160rpx"
            clearable
            placeholder="请输入验证码"
            prop="code"
            :rules="rules.code"
          >
            <template #suffix>
              <wd-button
                plain
                :disabled="emailCountdown > 0"
                @click="handleSendVerificationCode('EMAIL')"
              >
                {{ emailCountdown > 0 ? `${emailCountdown}s后重新发送` : "发送验证码" }}
              </wd-button>
            </template>
          </wd-input>
        </wd-cell-group>
        <view class="footer">
          <wd-button type="primary" size="large" block @click="handleSubmit">提交</wd-button>
        </view>
      </wd-form>
    </wd-popup>
  </view>
</template>
<script setup lang="ts">
import UserAPI, {
  PasswordChangeForm,
  MobileBindingForm,
  EmailBindingForm,
  UserProfileVO,
} from "@/api/system/user";
 
const validatorConfirmPassword = (value: string) => {
  if (!value) {
    return Promise.reject("请确认密码");
  } else {
    if (value !== passwordChangeForm.newPassword) {
      return Promise.reject("两次输入的密码不一致");
    } else {
      return Promise.resolve();
    }
  }
};
// 本页面中所有的校验规则
const rules = reactive({
  oldPassword: [{ required: true, message: "请填写原密码" }],
  newPassword: [{ required: true, message: "请填写新密码" }],
  confirmPassword: [{ required: true, message: "请确认密码", validator: validatorConfirmPassword }],
  mobile: [{ required: true, pattern: /^1[3-9]\d{9}$/, message: "请填写正确的手机号码" }],
  code: [{ required: true, message: "请填写验证码" }],
  email: [
    {
      required: true,
      pattern: /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/,
      message: "请填写正确的邮箱地址",
    },
  ],
});
 
enum DialogType {
  PASSWORD = "password",
  MOBILE = "mobile",
  EMAIL = "email",
}
 
const dialog = reactive({
  visible: false,
  type: "" as DialogType, // 修改账号资料,修改密码、绑定手机、绑定邮箱
});
 
const userProfile = ref<UserProfileVO>(); //用户信息
const passwordChangeForm = reactive<PasswordChangeForm>({});
const mobileBindingForm = reactive<MobileBindingForm>({});
const emailBindingForm = reactive<EmailBindingForm>({});
const passwordChangeFormRef = ref();
const mobileBindingFormRef = ref();
const emailBindingFormRef = ref();
 
const mobileCountdown = ref(0);
const mobileTimer = ref<ReturnType<typeof setInterval> | null>(null);
 
const emailCountdown = ref(0);
const emailTimer = ref<ReturnType<typeof setTimeout> | null>(null);
 
/** 加载用户信息 */
const loadUserProfile = async () => {
  userProfile.value = await UserAPI.getProfile();
};
 
/**
 * 打开弹窗
 * @param type 弹窗类型 ACCOUNT: 账号资料 PASSWORD: 修改密码 MOBILE: 绑定手机 EMAIL: 绑定邮箱
 */
const handleOpenDialog = (type: DialogType) => {
  dialog.type = type;
  dialog.visible = true;
  switch (type) {
    case DialogType.PASSWORD:
      passwordChangeForm.oldPassword = "";
      passwordChangeForm.newPassword = "";
      passwordChangeForm.confirmPassword = "";
      break;
    case DialogType.MOBILE:
      mobileBindingForm.mobile = "";
      mobileBindingForm.code = "";
      break;
    case DialogType.EMAIL:
      emailBindingForm.email = "";
      emailBindingForm.code = "";
      break;
  }
};
 
/**
 *  发送验证码
 *
 * @param contactType 联系方式类型 MOBILE: 手机号码  EMAIL: 邮箱
 */
const handleSendVerificationCode = async (contactType: string) => {
  if (contactType === "MOBILE") {
    mobileBindingFormRef.value.validate("mobile").then(({ valid }: { valid: boolean }) => {
      if (valid) {
        UserAPI.sendVerificationCode(mobileBindingForm.mobile!, "MOBILE").then(() => {
          uni.showToast({ title: "验证码已发送", icon: "none" });
          mobileCountdown.value = 60;
          mobileTimer.value = setInterval(() => {
            if (mobileCountdown.value > 0) {
              mobileCountdown.value -= 1;
            } else {
              clearInterval(mobileTimer.value!);
            }
          }, 1000);
        });
      }
    });
  } else if (contactType === "EMAIL") {
    emailBindingFormRef.value.validate("email").then(({ valid }: { valid: boolean }) => {
      if (valid) {
        UserAPI.sendVerificationCode(emailBindingForm.email!, "EMAIL").then(() => {
          uni.showToast({ title: "验证码已发送", icon: "none" });
          emailCountdown.value = 60;
          emailTimer.value = setInterval(() => {
            if (emailCountdown.value > 0) {
              emailCountdown.value -= 1;
            } else {
              clearInterval(emailTimer.value!);
            }
          }, 1000);
        });
      }
    });
  }
};
 
// 提交表单
function handleSubmit() {
  if (dialog.type === DialogType.PASSWORD) {
    passwordChangeFormRef.value.validate().then(({ valid }: { valid: boolean }) => {
      if (valid) {
        UserAPI.changePassword(passwordChangeForm).then(() => {
          uni.showToast({ title: "密码修改成功", icon: "none" });
          dialog.visible = false;
        });
      }
    });
  } else if (dialog.type === DialogType.MOBILE) {
    mobileBindingFormRef.value.validate().then(({ valid }: { valid: boolean }) => {
      if (valid) {
        UserAPI.bindMobile(mobileBindingForm).then(() => {
          uni.showToast({ title: "手机号绑定成功", icon: "none" });
          dialog.visible = false;
          loadUserProfile();
        });
      }
    });
  } else if (dialog.type === DialogType.EMAIL) {
    emailBindingFormRef.value.validate().then(({ valid }: { valid: boolean }) => {
      if (valid) {
        UserAPI.bindEmail(emailBindingForm).then(() => {
          uni.showToast({ title: "邮箱绑定成功", icon: "none" });
          dialog.visible = false;
          loadUserProfile();
        });
      }
    });
  }
}
 
onMounted(() => {
  loadUserProfile();
});
</script>
<style lang="scss" scoped>
.profile {
  .profile-card {
    padding: 10rpx;
    margin-bottom: 24rpx;
    line-height: 1.1;
    background-color: rgb(255, 255, 255);
    border-radius: 16px;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.05);
  }
  .edit-form {
    padding-top: 40rpx;
    .ef-radio-group {
      line-height: 1;
      text-align: left;
    }
    .footer {
      padding: 24rpx;
    }
  }
}
</style>