cloudroam
10 天以前 05316275ee6f1623cc022a3cb4967a440c105a6b
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
<template>
  <view class="edit-profile">
    <view class="avatar-box">
      <up-avatar :src="form.cover" size="80" @click="onChangeAvatar" />
    </view>
 
    <up-cell-group :border="false" :customStyle="cellGroupStyle">
      <up-cell title="名字" isLink @click="editField('名称')">
        <template #value>
          <view class="cell-value">{{ form.name }}</view>
        </template>
      </up-cell>
    </up-cell-group>
 
    <up-cell-group :border="false" :customStyle="cellGroupStyle">
      <up-cell title="简介" isLink @click="editField('简介')">
        <template #value>
          <view class="cell-value">{{ form.description }}</view>
        </template>
      </up-cell>
    </up-cell-group>
 
    <up-cell-group :border="false" :customStyle="cellGroupStyle">
      <up-cell title="性别" isLink @click="selectGender">
        <template #value>
          <view class="cell-value">{{ form.gender || '未选择' }}</view>
        </template>
      </up-cell>
      <up-cell title="地区" isLink @click="selectRegion">
        <template #value>
          <view class="cell-value">{{ form.address }}</view>
        </template>
      </up-cell>
    </up-cell-group>
 
    <view class="save-btn">
      <up-button type="primary" shape="circle" text="保存" @click="onSave" />
    </view>
  </view>
</template>
 
<script setup lang="ts">
import { ref,onMounted } from 'vue';
import { useGlobal } from '@/composables/useGlobal'
import { useUserStore } from '@/store/user'
const { $http, $message, $store } = useGlobal()
 
const cellGroupStyle = 'background-color: #ffffff; margin-bottom: 20rpx; border-radius: 16rpx;'
const userStore = useUserStore()
const form = ref({
  id: '',
  cover: '',
  name: '',
  description: '',
  gender: '',
  address: '',
});
 
 
const onChangeAvatar = () => {
  uni.chooseImage({
    count: 1,
    success: (res) => {
      form.value.cover = res.tempFilePaths[0];
    }
  });
};
 
// const editField = (field: string) => {
//   uni.showModal({
//     title: `编辑${field}`,
//     content: '',
//     placeholderText: `请输入${field}`,
//     editable: true,
//     success: (res) => {
//       if (res.confirm && res.content) {
//         form.value[field] = res.content;
//       }
//     },
//   });
// };
const editField = (field: string) => {
  // 创建字段映射关系
  const fieldMap: Record<string, keyof typeof form.value> = {
    '名称': 'name',
    '简介': 'description'
  };
 
  const fieldName = fieldMap[field];
  if (!fieldName) {
    $message.showToast('不支持的字段');
    return;
  }
 
  uni.showModal({
    title: `编辑${field}`,
    content: form.value[fieldName], // 显示当前值
    placeholderText: `请输入${field}`,
    editable: true,
    success: (res) => {
      if (res.confirm && res.content) {
        form.value[fieldName] = res.content;
      }
    },
  });
};
 
 
const selectGender = () => {
  uni.showActionSheet({
    itemList: ['男', '女'],
    success: (res) => {
      form.value.gender = res.tapIndex === 0 ? '男' : '女';
    }
  });
};
 
 
 
const selectRegion = () => {
  uni.chooseLocation({
    success: (res) => {
      form.value.address = res.address;
    }
  });
};
 
 
const onSave = async () => {
  try {
    // 构造请求体,排除 id 字段
    const updatedUserInfo = {
      id: form.value.id,
      cover: form.value.cover,
      description: form.value.description,
      name: form.value.name,
      gender: form.value.gender,
      address: form.value.address,
    };
    // 调用后端 API 更新用户信息
    const {code, data} = await $http.request('post', '/api/customer/addOrUpdate', {
      data: updatedUserInfo
    });
 
    if (code === 0) {
      $message.showToast('资料已保存');
 
      // 更新 store 中的用户信息
      await userStore.getCurrentInfo();
    } else {
      $message.showToast('保存失败:' + (data?.msg || '未知错误'));
    }
  } catch (error) {
    console.error('保存用户信息失败', error)
    $message.showToast('保存用户信息失败')
  }
};
 
 
onMounted(async () => {
  // 正确获取store实例
  if (!userStore.userInfo) {
    await userStore.getCurrentInfo();
  }
  const userInfo = userStore.userInfo?.customerDTO;
  console.log("开始处理用户数据")
  if (userInfo) {
    form.value = {
      id: userInfo.id,
      cover: userInfo.cover || 'https://cdn.uviewui.com/uview/album/1.jpg',
      name: userInfo.name || '影游四方',
      description: userInfo.description || '介绍一下自己',
      gender: userInfo.gender || '女',
      address: userInfo.address || '选择所在的地区'
    };
  }
 
})
 
 
</script>
 
<style lang="scss" scoped>
.cell-value {
  // text-align: center;
  text-align: left;
  width: 60%;
}
 
.edit-profile {
  background-color: #f7f7f7;
  min-height: 100vh;
  padding: 20rpx;
 
  .avatar-box {
    display: flex;
    justify-content: center;
    // margin: 40rpx 0;
  }
 
  .bg-img {
    width: 120rpx;
    height: 80rpx;
    border-radius: 12rpx;
  }
 
  .save-btn {
    padding: 40rpx;
  }
}
</style>