edit-name.uvue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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-[40px]'
  12. }"
  13. >
  14. <template #append>
  15. <cl-text color="info" :size="12" :pt="{ className: 'ml-2' }"
  16. >{{ content.length }} / 20</cl-text
  17. >
  18. </template>
  19. </cl-input>
  20. <view class="p-3">
  21. <cl-text color="info" :size="12">{{
  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, t } from "@/.cool";
  35. import { useUi } from "@/uni_modules/cool-ui";
  36. import { ref } from "vue";
  37. const ui = useUi();
  38. const { user } = useStore();
  39. // 输入框内容
  40. const content = ref("");
  41. // 确认按钮点击事件
  42. async function confirm() {
  43. // 检查昵称长度和特殊字符
  44. if (content.value.length < 2 || content.value.length > 20) {
  45. ui.showToast({
  46. message: t("昵称长度需在2-20个字符之间")
  47. });
  48. return;
  49. }
  50. // 正则匹配 - 不允许特殊字符@<>/等
  51. const reg = /^[^@<>\/]*$/;
  52. if (!reg.test(content.value)) {
  53. ui.showToast({
  54. message: t("昵称不能包含@<>/等特殊字符")
  55. });
  56. return;
  57. }
  58. // 更新用户昵称
  59. await user.update({
  60. nickName: content.value
  61. });
  62. router.back();
  63. }
  64. // 页面加载时,设置输入框内容
  65. onReady(() => {
  66. content.value = userInfo.value?.nickName ?? "";
  67. });
  68. </script>