edit-name.uvue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <cl-page>
  3. <cl-topbar safe-area-top :title="t('编辑昵称')" background-color="transparent"> </cl-topbar>
  4. <view class="p-3">
  5. <cl-input
  6. v-model="content"
  7. autofocus
  8. :placeholder="t('请输入昵称')"
  9. :border="false"
  10. :pt="{
  11. className: '!h-[80rpx]'
  12. }"
  13. >
  14. <template #append>
  15. <cl-text color="info" :pt="{ className: '!text-sm ml-2' }"
  16. >{{ content.length }}/20</cl-text
  17. >
  18. </template>
  19. </cl-input>
  20. <view class="p-3">
  21. <cl-text color="info" :pt="{ className: '!text-sm' }">{{
  22. t("请设置2-20个字符,不包括@<>/等无效字符")
  23. }}</cl-text>
  24. </view>
  25. </view>
  26. <cl-footer>
  27. <cl-button size="large" :disabled="content == ''" @tap="confirm">{{
  28. t("确认")
  29. }}</cl-button>
  30. </cl-footer>
  31. </cl-page>
  32. </template>
  33. <script setup lang="ts">
  34. import { router, userInfo, useStore } from "@/cool";
  35. import { t } from "@/locale";
  36. import { useUi } from "@/uni_modules/cool-ui";
  37. import { ref } from "vue";
  38. const ui = useUi();
  39. const { user } = useStore();
  40. // 输入框内容
  41. const content = ref("");
  42. // 确认按钮点击事件
  43. async function confirm() {
  44. // 检查昵称长度和特殊字符
  45. if (content.value.length < 2 || content.value.length > 20) {
  46. ui.showToast({
  47. message: t("昵称长度需在2-20个字符之间")
  48. });
  49. return;
  50. }
  51. // 正则匹配 - 不允许特殊字符@<>/等
  52. const reg = /^[^@<>\/]*$/;
  53. if (!reg.test(content.value)) {
  54. ui.showToast({
  55. message: t("昵称不能包含@<>/等特殊字符")
  56. });
  57. return;
  58. }
  59. // 更新用户昵称
  60. await user.update({
  61. nickName: content.value
  62. });
  63. router.back();
  64. }
  65. // 页面加载时,设置输入框内容
  66. onReady(() => {
  67. content.value = userInfo.value?.nickName ?? "";
  68. });
  69. </script>