|
|
@@ -72,6 +72,7 @@
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
import { ref } from 'vue'
|
|
|
+import { onShow } from '@dcloudio/uni-app'
|
|
|
import CustomNav from '@/components/CustomNav.vue'
|
|
|
|
|
|
const form = ref({
|
|
|
@@ -88,6 +89,64 @@ const submitting = ref(false)
|
|
|
const isChoosing = ref(false)
|
|
|
const gettingLocation = ref(false)
|
|
|
|
|
|
+// 从后端拉取已有用户信息并填充表单
|
|
|
+const fetchUserInfo = async () => {
|
|
|
+ try {
|
|
|
+ const token = uni.getStorageSync('token')
|
|
|
+ if (!token) return
|
|
|
+ uni.showLoading({ title: '加载中...' })
|
|
|
+ const response = await uni.request({
|
|
|
+ url: 'https://wx.baiyun.work/user_info',
|
|
|
+ method: 'POST',
|
|
|
+ header: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'Authorization': `Bearer ${token}`
|
|
|
+ },
|
|
|
+ data: {}
|
|
|
+ })
|
|
|
+ uni.hideLoading()
|
|
|
+ console.log('fetchUserInfo response:', response)
|
|
|
+ if (response.statusCode === 401) {
|
|
|
+ // 未授权,跳转登录
|
|
|
+ uni.removeStorageSync('token')
|
|
|
+ uni.reLaunch({ url: '/pages/login/login' })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const resp = response.data as any
|
|
|
+ if (resp && resp.code === 200 && resp.data) {
|
|
|
+ const d = resp.data
|
|
|
+ // 填充基本字段
|
|
|
+ form.value.avatar = d.avatar || form.value.avatar
|
|
|
+ form.value.nickname = d.nickname || form.value.nickname
|
|
|
+ form.value.phone = d.phone || form.value.phone
|
|
|
+ form.value.age = d.age || form.value.age
|
|
|
+ // sex 可能为 'MALE'/'FEMALE' 或数字/1/2
|
|
|
+ if (d.sex) {
|
|
|
+ if (typeof d.sex === 'string') {
|
|
|
+ const s = d.sex.toUpperCase()
|
|
|
+ form.value.sex = s === 'MALE' ? 1 : s === 'FEMALE' ? 2 : form.value.sex
|
|
|
+ } else if (typeof d.sex === 'number') {
|
|
|
+ form.value.sex = d.sex
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // address 可能为 '重庆市 重庆市' 或包含空格,用空格或中文空格分割
|
|
|
+ if (d.address) {
|
|
|
+ const parts = String(d.address).split(/\s+/).filter(Boolean)
|
|
|
+ region.value = parts
|
|
|
+ form.value.address = parts.join(' ')
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ uni.hideLoading()
|
|
|
+ console.error('fetchUserInfo error:', err)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 在页面显示时尝试填充
|
|
|
+onShow(() => {
|
|
|
+ fetchUserInfo()
|
|
|
+})
|
|
|
+
|
|
|
|
|
|
const onChooseAvatar = (e: any) => {
|
|
|
console.log('onChooseAvatar called with event:', e)
|