userInfo.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div class="bg-[#f8f8f8] pt-20">
  3. <van-form>
  4. <van-cell-group inset>
  5. <van-field name="uploader" label="头像">
  6. <template #input>
  7. <van-uploader
  8. style="--van-uploader-border-radius: 40px"
  9. :after-read="afterRead"
  10. v-model="fileList"
  11. :max-count="1"
  12. :deletable="false"
  13. :multiple="false"
  14. accept="image/*"
  15. reupload
  16. />
  17. </template>
  18. </van-field>
  19. <van-field
  20. v-model="userInfo.showName"
  21. name="昵称"
  22. label="昵称"
  23. placeholder="昵称"
  24. maxlength="20"
  25. :rules="[{ required: true, message: '请填写昵称' }]"
  26. />
  27. <van-field name="radio" label="性别">
  28. <template #input>
  29. <van-radio-group v-model="userInfo.sex" direction="horizontal">
  30. <van-radio name="1">男</van-radio>
  31. <van-radio name="2">女</van-radio>
  32. </van-radio-group>
  33. </template>
  34. </van-field>
  35. <van-field
  36. v-model="userInfo.email"
  37. name="邮箱"
  38. label="邮箱"
  39. placeholder="邮箱"
  40. maxlength="100"
  41. :rules="[
  42. {
  43. required: true,
  44. validator: validatorEmail,
  45. message: '请填写正确的邮箱',
  46. },
  47. ]"
  48. />
  49. <van-field
  50. v-model="userInfo.job"
  51. name="职业"
  52. label="职业"
  53. placeholder="职业"
  54. maxlength="100"
  55. />
  56. <van-field
  57. v-model="userInfo.address"
  58. name="居住地"
  59. label="居住地"
  60. maxlength="100"
  61. placeholder="居住地"
  62. />
  63. <van-field
  64. v-model="userInfo.personalSign"
  65. rows="3"
  66. autosize
  67. label="个性签名"
  68. maxlength="200"
  69. type="textarea"
  70. placeholder="请输入留言"
  71. show-word-limit
  72. />
  73. </van-cell-group>
  74. <div style="margin: 16px">
  75. <van-button
  76. @click="handleSubmit"
  77. round
  78. block
  79. style="background-color: #fa8446; color: #fff; font-size: 18px"
  80. >
  81. 提交
  82. </van-button>
  83. </div>
  84. </van-form>
  85. </div>
  86. </template>
  87. <script setup>
  88. const userInfoStore = useUserInfoStore();
  89. const { userInfo } = storeToRefs(userInfoStore);
  90. const fileList = ref([]);
  91. const form = reactive({
  92. showName: null,
  93. email: null,
  94. sex: null,
  95. email: null,
  96. headImageUrl: null,
  97. address: null,
  98. job: null,
  99. personalSign: null,
  100. });
  101. const validatorEmail = (val) =>
  102. /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
  103. val
  104. );
  105. async function afterRead(file) {
  106. console.log(file);
  107. const formData = new FormData();
  108. formData.append("uploadFile", file.file);
  109. formData.append("asImage", true);
  110. formData.append("fieldName", "headImageUrl");
  111. try {
  112. const { data } = await request("/website/tourism/user/upload", {
  113. method: "post",
  114. body: formData,
  115. });
  116. form.headImageUrl = data.fileUrl;
  117. userInfoStore.getUserInfo();
  118. } catch (error) {}
  119. }
  120. watch(
  121. userInfo,
  122. () => {
  123. form.showName = userInfo.value.showName;
  124. form.email = userInfo.value.email;
  125. form.sex = userInfo.value.sex;
  126. form.headImageUrl = userInfo.value.headImageUrl;
  127. fileList.value[0] = {
  128. url: userInfo.value.headImageUrl,
  129. };
  130. form.address = userInfo.value.address;
  131. form.job = userInfo.value.job;
  132. form.personalSign = userInfo.value.personalSign;
  133. },
  134. {
  135. immediate: true,
  136. deep: true,
  137. }
  138. );
  139. onMounted(() => {
  140. userInfoStore.getUserInfo();
  141. });
  142. async function handleSubmit() {
  143. try {
  144. await request("/website/tourism/user/update", {
  145. method: "post",
  146. body: form,
  147. });
  148. showToast("保存成功");
  149. userInfoStore.getUserInfo();
  150. } catch (error) {}
  151. }
  152. </script>
  153. <style lang="scss" scoped></style>