profile.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <step-layout
  3. title="填写个人信息"
  4. :showSkip="!isEditMode"
  5. @next="goNext"
  6. @skip="goSkip">
  7. <view class="form-list">
  8. <!-- Avatar -->
  9. <view class="form-item avatar-item" @tap="chooseAvatar">
  10. <text class="label">头像</text>
  11. <view class="right-content">
  12. <text class="hint-text">上传真实头像,你的简历更加分</text>
  13. <image class="avatar-img" :src="formData.avatarUrl" mode="aspectFill"></image>
  14. <text class="arrow" />
  15. </view>
  16. </view>
  17. <!-- Name -->
  18. <view class="form-item">
  19. <text class="label">姓名</text>
  20. <view class="input-box">
  21. <input type="text" v-model="formData.name" placeholder="请输入您的真实姓名" placeholder-class="ph-style" />
  22. </view>
  23. </view>
  24. <!-- Gender -->
  25. <view class="form-item">
  26. <text class="label">性别</text>
  27. <view class="gender-switch">
  28. <view :class="['g-slider', genderSliderClass]"></view>
  29. <view
  30. v-for="item in genderOptions"
  31. :key="item.dictValue"
  32. :class="['g-item', formData.gender === item.dictValue ? 'active' : '']"
  33. @tap="setGender(item.dictValue)">
  34. {{ item.dictLabel }}
  35. </view>
  36. </view>
  37. </view>
  38. <!-- ID Card -->
  39. <view class="form-item">
  40. <text class="label">身份证号</text>
  41. <view class="input-box">
  42. <input type="idcard" v-model="formData.idCard" placeholder="请输入您的身份证号" placeholder-class="ph-style" />
  43. </view>
  44. </view>
  45. <!-- Email -->
  46. <view class="form-item">
  47. <text class="label">邮箱</text>
  48. <view class="input-box">
  49. <input type="text" v-model="formData.email" placeholder="请输入您的邮箱" placeholder-class="ph-style" />
  50. </view>
  51. </view>
  52. </view>
  53. </step-layout>
  54. </template>
  55. <script setup lang="js">
  56. import { computed, reactive, ref } from 'vue';
  57. import { onLoad } from '@dcloudio/uni-app';
  58. import { updateStudent, getStudent } from '../../api/student';
  59. import { getDicts } from '../../api/dict';
  60. import { UPLOAD_URL } from '../../utils/request';
  61. const isEditMode = ref(false);
  62. const genderOptions = ref([
  63. { dictLabel: '男', dictValue: '0' },
  64. { dictLabel: '女', dictValue: '1' }
  65. ]);
  66. const normalizeGenderValue = (value) => {
  67. if (value === undefined || value === null || value === '') {
  68. return '0';
  69. }
  70. const gender = String(value).trim().toUpperCase();
  71. if (gender === 'M') return '0';
  72. if (gender === 'F') return '1';
  73. if (gender === '0' || gender === '1' || gender === '2') return gender;
  74. return '0';
  75. };
  76. const loadGenderDict = async () => {
  77. try {
  78. const res = await getDicts('sys_user_sex');
  79. const data = res?.data || [];
  80. if (Array.isArray(data) && data.length > 0) {
  81. genderOptions.value = data
  82. .filter(item => item && item.dictValue !== '2')
  83. .map(item => ({
  84. dictLabel: item.dictLabel,
  85. dictValue: String(item.dictValue)
  86. }));
  87. }
  88. } catch (error) {
  89. console.error('加载性别字典失败', error);
  90. }
  91. };
  92. const genderSliderClass = computed(() => {
  93. const currentIndex = genderOptions.value.findIndex(item => item.dictValue === formData.gender);
  94. if (currentIndex <= 0) {
  95. return currentIndex === 0 ? 'left' : 'none';
  96. }
  97. return 'right';
  98. });
  99. onLoad(async (options) => {
  100. await loadGenderDict();
  101. if (options.editMode === '1') {
  102. isEditMode.value = true;
  103. // 从后端获取真实数据进行回显
  104. const userInfo = uni.getStorageSync('userInfo');
  105. if (userInfo && userInfo.studentId) {
  106. try {
  107. uni.showLoading({ title: '加载中...' });
  108. const res = await getStudent(userInfo.studentId);
  109. uni.hideLoading();
  110. if (res && res.data) {
  111. const data = res.data;
  112. Object.assign(formData, {
  113. avatarUrl: data.avatarUrl || '/static/images/default-avatar.svg',
  114. avatarId: data.avatar || null,
  115. name: data.name || '',
  116. gender: normalizeGenderValue(data.gender),
  117. idCard: data.idCardNumber || '',
  118. email: data.email || ''
  119. });
  120. }
  121. } catch (err) {
  122. uni.hideLoading();
  123. console.error('获取用户信息失败', err);
  124. uni.showToast({ title: '加载数据失败', icon: 'none' });
  125. }
  126. }
  127. }
  128. });
  129. const formData = reactive({
  130. avatarUrl: '/static/images/default-avatar.svg', // 用于展示
  131. avatarId: null, // 用于传给后端
  132. name: '',
  133. gender: '0',
  134. idCard: '',
  135. email: ''
  136. });
  137. const chooseAvatar = () => {
  138. uni.chooseImage({
  139. count: 1,
  140. success: (res) => {
  141. const tempFilePath = res.tempFilePaths[0];
  142. formData.avatarUrl = tempFilePath; // 先本地展示
  143. // 执行上传
  144. uni.showLoading({ title: '上传头像中...' });
  145. uni.uploadFile({
  146. url: UPLOAD_URL + '/portal/oss/upload',
  147. filePath: tempFilePath,
  148. name: 'file',
  149. header: {
  150. 'clientid': 'e5cd7e4891bf95d1d19206ce24a7b32e',
  151. 'PLATFORM_CODE': 'PINGTAIDUAN',
  152. 'Authorization': uni.getStorageSync('token') ? `Bearer ${uni.getStorageSync('token')}` : ''
  153. },
  154. success: (uploadFileRes) => {
  155. uni.hideLoading();
  156. const data = JSON.parse(uploadFileRes.data);
  157. if (data.code === 200) {
  158. formData.avatarId = data.data.ossId; // 保存返回的ID
  159. uni.showToast({ title: '上传成功', icon: 'success' });
  160. } else {
  161. uni.showToast({ title: data.msg || '上传失败', icon: 'none' });
  162. }
  163. },
  164. fail: (err) => {
  165. uni.hideLoading();
  166. console.error('上传失败', err);
  167. uni.showToast({ title: '上传失败', icon: 'none' });
  168. }
  169. });
  170. }
  171. });
  172. };
  173. const setGender = (val) => {
  174. formData.gender = val;
  175. };
  176. const goNext = async () => {
  177. // 校验姓名必填
  178. if (!formData.name) {
  179. uni.showToast({ title: '请输入您的真实姓名', icon: 'none' });
  180. return;
  181. }
  182. // 校验身份证号格式(如果填写了的话)
  183. if (formData.idCard) {
  184. const idCardReg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/;
  185. if (!idCardReg.test(formData.idCard)) {
  186. uni.showToast({ title: '请输入正确的身份证号', icon: 'none' });
  187. return;
  188. }
  189. }
  190. // 校验邮箱格式(如果填写了的话)
  191. if (formData.email) {
  192. const emailReg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  193. if (!emailReg.test(formData.email)) {
  194. uni.showToast({ title: '请输入正确的邮箱地址', icon: 'none' });
  195. return;
  196. }
  197. }
  198. uni.showLoading({ title: '保存中...' });
  199. try {
  200. const userInfo = uni.getStorageSync('userInfo');
  201. const studentId = userInfo ? userInfo.studentId : null;
  202. if (!studentId) {
  203. uni.hideLoading();
  204. uni.showToast({ title: '用户信息失效,请重新登录', icon: 'none' });
  205. return;
  206. }
  207. const res = await updateStudent({
  208. id: studentId,
  209. name: formData.name,
  210. gender: formData.gender,
  211. email: formData.email,
  212. idCardNumber: formData.idCard,
  213. avatar: formData.avatarId // 传入上传后的 ossId, 是 Long 类型的
  214. });
  215. uni.hideLoading();
  216. if (res.code === 200) {
  217. const url = isEditMode.value ? '/pages/experience/experience?editMode=1' : '/pages/experience/experience';
  218. uni.navigateTo({ url });
  219. } else {
  220. uni.showToast({
  221. title: res.msg || '保存失败',
  222. icon: 'none'
  223. });
  224. }
  225. } catch (err) {
  226. uni.hideLoading();
  227. console.error('保存个人信息异常', err);
  228. uni.showToast({ title: '网络异常,请重试', icon: 'none' });
  229. }
  230. };
  231. const goSkip = () => {
  232. uni.navigateTo({
  233. url: '/pages/experience/experience'
  234. });
  235. };
  236. </script>
  237. <style lang="scss" scoped>
  238. @import './profile.scss';
  239. </style>