user-edit.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. <template>
  2. <view class="user-edit-container">
  3. <view class="header">
  4. <text class="title">{{ isEdit ? '完善个人信息' : '选择角色' }}</text>
  5. <text class="subtitle">{{ isEdit ? '以下信息可帮助您更好地使用服务' : '请选择您的身份类型' }}</text>
  6. </view>
  7. <!-- 角色选择(新用户首次登录时显示) -->
  8. <view class="role-select-section" v-if="!isEdit && showRoleSelect">
  9. <view
  10. class="role-card"
  11. :class="{ active: form.role === 'parent' }"
  12. @click="selectRole('parent')"
  13. >
  14. <text class="role-icon">👨‍👩‍👧</text>
  15. <text class="role-name">家长</text>
  16. <text class="role-desc">管理孩子任务、审核奖励</text>
  17. </view>
  18. <view
  19. class="role-card"
  20. :class="{ active: form.role === 'child' }"
  21. @click="selectRole('child')"
  22. >
  23. <text class="role-icon">👶</text>
  24. <text class="role-name">孩子</text>
  25. <text class="role-desc">执行任务、赚取积分</text>
  26. </view>
  27. <view
  28. class="role-card"
  29. :class="{ active: form.role === 'teacher' }"
  30. @click="selectRole('teacher')"
  31. >
  32. <text class="role-icon">👨‍🏫</text>
  33. <text class="role-name">成长规划师</text>
  34. <text class="role-desc">DAN测评、制定成长方案</text>
  35. </view>
  36. <button class="btn-next" @click="confirmRole" :disabled="!form.role">
  37. 下一步
  38. </button>
  39. </view>
  40. <!-- 详细信息填写(选择角色后显示) -->
  41. <view v-if="isEdit && (!isNewUserFlow || hasFamily)">
  42. <view class="form-section">
  43. <!-- 头像 -->
  44. <view class="form-item avatar-item">
  45. <text class="label">头像</text>
  46. <view class="avatar-upload" @click="chooseAvatar">
  47. <image v-if="form.avatar" :src="form.avatar" class="avatar-preview"></image>
  48. <view v-else class="avatar-placeholder">
  49. <text class="iconfont icon-plus">+</text>
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 昵称 -->
  54. <view class="form-item">
  55. <text class="label">昵称 <text class="required">*</text></text>
  56. <input type="text" v-model="form.nickname" placeholder="请输入昵称" class="input" maxlength="20" />
  57. </view>
  58. <!-- 新用户流程:仅展示头像+昵称,其余字段按需在功能页补充 -->
  59. <view v-if="!isNewUserFlow">
  60. <!-- 真实姓名 -->
  61. <view class="form-item">
  62. <text class="label">真实姓名</text>
  63. <input type="text" v-model="form.realName" placeholder="选填,用于家庭管理" class="input" maxlength="20" />
  64. </view>
  65. <!-- 性别(成长规划师必填) -->
  66. <view class="form-item">
  67. <text class="label">性别 <text class="required" v-if="form.role === 'teacher'">*</text></text>
  68. <radio-group class="role-select" @change="onGenderChange">
  69. <label class="role-option">
  70. <radio value="male" :checked="form.gender === 'male'" />
  71. <text>男</text>
  72. </label>
  73. <label class="role-option">
  74. <radio value="female" :checked="form.gender === 'female'" />
  75. <text>女</text>
  76. </label>
  77. </radio-group>
  78. </view>
  79. <!-- 身份证号(成长规划师必填) -->
  80. <view class="form-item">
  81. <text class="label">身份证号 <text class="required" v-if="form.role === 'teacher'">*</text></text>
  82. <input type="idcard" v-model="form.idCard" placeholder="选填,填写后自动生成生日" class="input" maxlength="18" @input="onIdCardInput" />
  83. </view>
  84. <!-- 生日 -->
  85. <view class="form-item">
  86. <text class="label">生日</text>
  87. <picker mode="date" :value="form.birthday" @change="onBirthdayChange">
  88. <view class="picker-input">{{ form.birthday || '请选择生日' }}</view>
  89. </picker>
  90. </view>
  91. <!-- 出生时辰 -->
  92. <view class="form-item">
  93. <text class="label">出生时辰</text>
  94. <picker mode="selector" :range="birthHourOptions" :value="birthHourIndex" @change="onBirthHourChange">
  95. <view class="picker-input">{{ form.birthHour || '请选择出生时辰' }}</view>
  96. </picker>
  97. </view>
  98. <!-- 民族 -->
  99. <view class="form-item">
  100. <text class="label">民族</text>
  101. <picker mode="selector" :range="ethnicityOptions" :value="ethnicityIndex" @change="onEthnicityChange">
  102. <view class="picker-input">{{ form.ethnicity || '请选择民族' }}</view>
  103. </picker>
  104. </view>
  105. <!-- 血型 -->
  106. <view class="form-item">
  107. <text class="label">血型</text>
  108. <picker mode="selector" :range="bloodTypeOptions" :value="bloodTypeIndex" @change="onBloodTypeChange">
  109. <view class="picker-input">{{ form.bloodType || '请选择血型' }}</view>
  110. </picker>
  111. </view>
  112. <!-- 最高学历 -->
  113. <view class="form-item">
  114. <text class="label">最高学历</text>
  115. <picker mode="selector" :range="educationOptions" :value="educationIndex" @change="onEducationChange">
  116. <view class="picker-input">{{ form.highestEducation || '请选择最高学历' }}</view>
  117. </picker>
  118. </view>
  119. <!-- 婚姻状态 -->
  120. <view class="form-item">
  121. <text class="label">婚姻状态</text>
  122. <picker mode="selector" :range="maritalOptions" :value="maritalIndex" @change="onMaritalChange">
  123. <view class="picker-input">{{ form.maritalStatus || '请选择婚姻状态' }}</view>
  124. </picker>
  125. </view>
  126. <!-- 地址选择 -->
  127. <view class="form-item">
  128. <text class="label">所在地区 <text class="required" v-if="isNewUserFlow">*</text></text>
  129. <AddressPicker ref="addressPicker" />
  130. </view>
  131. <!-- 兴趣爱好 -->
  132. <view class="form-item">
  133. <text class="label">兴趣爱好</text>
  134. <input type="text" v-model="form.hobbies" placeholder="如: 阅读、运动、音乐" class="input" maxlength="200" />
  135. </view>
  136. <!-- 饮食偏好 -->
  137. <view class="form-item">
  138. <text class="label">饮食偏好</text>
  139. <input type="text" v-model="form.dietPreferences" placeholder="如: 清淡、无辣、低糖" class="input" maxlength="200" />
  140. </view>
  141. </view>
  142. <!-- 成长规划师额外字段 -->
  143. <view class="form-section-title" v-if="form.role === 'teacher'">成长规划师信息</view>
  144. <!-- 成长规划师证书号(成长规划师必填) -->
  145. <view class="form-item" v-if="form.role === 'teacher'">
  146. <text class="label">成长规划师证书号 <text class="required">*</text></text>
  147. <input type="text" v-model="form.teacherNo" placeholder="请输入成长规划师证书号" class="input" maxlength="50" />
  148. </view>
  149. <!-- 成长规划师照片(成长规划师必填) -->
  150. <view class="form-item" v-if="form.role === 'teacher'">
  151. <text class="label">成长规划师照片 <text class="required">*</text></text>
  152. <view class="photo-upload" @click="chooseTeacherPhoto">
  153. <image v-if="form.teacherPhoto" :src="form.teacherPhoto" class="photo-preview" mode="aspectFill"></image>
  154. <view v-else class="photo-placeholder">
  155. <text class="plus-icon">+</text>
  156. <text class="placeholder-text">上传照片</text>
  157. </view>
  158. </view>
  159. </view>
  160. <!-- 成长规划师证书照片(成长规划师必填) -->
  161. <view class="form-item" v-if="form.role === 'teacher'">
  162. <text class="label">成长规划师证书 <text class="required">*</text></text>
  163. <view class="photo-upload" @click="chooseCertificate">
  164. <image v-if="form.certificateImage" :src="form.certificateImage" class="photo-preview" mode="aspectFill"></image>
  165. <view v-else class="photo-placeholder">
  166. <text class="plus-icon">+</text>
  167. <text class="placeholder-text">上传证书</text>
  168. </view>
  169. </view>
  170. </view>
  171. </view>
  172. <!-- AI助手选择(已屏蔽)
  173. <view class="family-section" v-if="isEdit">
  174. <view class="section-title">🤖 选择AI助手</view>
  175. <view class="mascot-cards">
  176. <view class="mascot-card" :class="{ active: form.mascot === 'xibao' }" @click="selectMascot('xibao')">
  177. <view class="mascot-card-icon xibao-bg">🌟</view>
  178. <view class="mascot-card-info">
  179. <text class="mascot-card-name">浠宝</text>
  180. <text class="mascot-card-desc">温柔活泼的阳光女孩</text>
  181. </view>
  182. </view>
  183. <view class="mascot-card" :class="{ active: form.mascot === 'fubao' }" @click="selectMascot('fubao')">
  184. <view class="mascot-card-icon fubao-bg">💪</view>
  185. <view class="mascot-card-info">
  186. <text class="mascot-card-name">福宝</text>
  187. <text class="mascot-card-desc">开朗自信的阳光男孩</text>
  188. </view>
  189. </view>
  190. </view>
  191. </view> -->
  192. <!-- 新用户流程:保存资料 / 跳过(不再创建或加入家庭) -->
  193. <view class="family-section" v-if="isEdit && form.role !== 'teacher' && isNewUserFlow">
  194. <view class="family-actions">
  195. <button class="btn btn-primary" @click="saveUserInfo" :loading="loading">
  196. 保存资料
  197. </button>
  198. <button class="btn btn-skip" @click="skip">
  199. 跳过
  200. </button>
  201. </view>
  202. </view>
  203. <!-- 修改个人资料保存按钮(非新用户流程时显示) -->
  204. <view class="family-section" v-if="isEdit && !isNewUserFlow && form.role !== 'teacher'">
  205. <view class="family-actions">
  206. <button class="btn btn-primary" @click="saveUserInfo" :loading="loading">
  207. 保存修改
  208. </button>
  209. </view>
  210. </view>
  211. <!-- 成长规划师提交按钮 -->
  212. <view class="family-section" v-if="isEdit && form.role === 'teacher' && isNewUserFlow">
  213. <view class="family-actions">
  214. <button class="btn btn-primary" @click="submitTeacherInfo" :loading="loading">
  215. 提交申请
  216. </button>
  217. </view>
  218. </view>
  219. </view>
  220. <view v-else-if="isEdit">
  221. <FamilyEmptyState type="card" />
  222. </view>
  223. </view>
  224. </template>
  225. <script>
  226. import { updateUserInfo, createFamily, directRegister, getUserInfo, requestJoinByCode } from '../../utils/api.js'
  227. import AddressPicker from '../../components/address-picker.vue'
  228. export default {
  229. components: {
  230. AddressPicker
  231. },
  232. data() {
  233. return {
  234. isEdit: false,
  235. showRoleSelect: true,
  236. isNewUserFlow: false, // 是否是新用户注册流程
  237. form: {
  238. role: '',
  239. avatar: '',
  240. nickname: '',
  241. realName: '',
  242. gender: '',
  243. idCard: '',
  244. birthday: '',
  245. birthHour: '',
  246. inviteCode: '',
  247. mascot: '',
  248. phone: '',
  249. teacherNo: '',
  250. teacherPhoto: '',
  251. certificateImage: '',
  252. ethnicity: '',
  253. bloodType: '',
  254. highestEducation: '',
  255. maritalStatus: '',
  256. hobbies: '',
  257. dietPreferences: '',
  258. address: '',
  259. addressId: null
  260. },
  261. ethnicityOptions: ['汉族','蒙古族','回族','藏族','维吾尔族','苗族','彝族','壮族','布依族','朝鲜族','满族','侗族','瑶族','白族','土家族','哈尼族','哈萨克族','傣族','黎族','其他'],
  262. bloodTypeOptions: ['A','B','AB','O','未知'],
  263. birthHourOptions: ['子时(23-01)','丑时(01-03)','寅时(03-05)','卯时(05-07)','辰时(07-09)','巳时(09-11)','午时(11-13)','未时(13-15)','申时(15-17)','酉时(17-19)','戌时(19-21)','亥时(21-23)'],
  264. birthHourValues: ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'],
  265. educationOptions: ['小学','初中','高中','中专','大专','本科','硕士','博士'],
  266. maritalOptions: ['未婚','已婚','离异','丧偶'],
  267. loading: false,
  268. userId: '',
  269. token: ''
  270. }
  271. },
  272. computed: {
  273. ethnicityIndex() {
  274. return this.ethnicityOptions.indexOf(this.form.ethnicity)
  275. },
  276. bloodTypeIndex() {
  277. return this.bloodTypeOptions.indexOf(this.form.bloodType)
  278. },
  279. birthHourIndex() {
  280. return this.birthHourValues.indexOf(this.form.birthHour)
  281. },
  282. educationIndex() {
  283. return this.educationOptions.indexOf(this.form.highestEducation)
  284. },
  285. maritalIndex() {
  286. return this.maritalOptions.indexOf(this.form.maritalStatus)
  287. },
  288. hasFamily: function() {
  289. if (this.token) return true
  290. return this.$store.getters.hasFamily
  291. },
  292. },
  293. onLoad(options) {
  294. if (options.token && options.userId) {
  295. this.token = options.token
  296. this.userId = options.userId
  297. if (options.autoParent === 'true') {
  298. // 首次登录:跳过角色选择,默认 parent,直接进入信息补充页
  299. this.showRoleSelect = false
  300. this.isEdit = true
  301. this.isNewUserFlow = true
  302. this.form.role = 'parent'
  303. } else {
  304. // 检查是否需要选择角色(新用户)- 只有明确传递 needsRoleSelect=true 才显示选择
  305. this.showRoleSelect = options.needsRoleSelect === 'true'
  306. this.isEdit = !this.showRoleSelect
  307. this.isNewUserFlow = options.needsRoleSelect === 'true'
  308. }
  309. } else if (options.phone && (options.autoParent === 'true' || options.needsRoleSelect === 'true')) {
  310. // 测试环境:没有token,只有手机号,是新用户注册流程
  311. this.showRoleSelect = options.needsRoleSelect === 'true'
  312. this.isEdit = !this.showRoleSelect
  313. this.isNewUserFlow = true
  314. if (options.autoParent === 'true') {
  315. this.form.role = 'parent'
  316. }
  317. // 将手机号传递给表单
  318. this.form.phone = options.phone
  319. } else {
  320. // 其他情况:默认进入编辑模式(如从"我的"页面进入)
  321. this.showRoleSelect = false
  322. this.isEdit = true
  323. this.isNewUserFlow = false
  324. }
  325. // 非新用户流程时,加载已有用户信息
  326. if (this.isEdit && !this.isNewUserFlow) {
  327. this.loadUserInfo()
  328. }
  329. },
  330. methods: {
  331. selectRole(role) {
  332. this.form.role = role
  333. },
  334. confirmRole() {
  335. if (!this.form.role) {
  336. uni.showToast({ title: '请选择角色', icon: 'none' })
  337. return
  338. }
  339. this.showRoleSelect = false
  340. this.isEdit = true
  341. // 测试环境:直接注册用户(不需要验证码)
  342. if (!this.token && this.form.phone) {
  343. // 自动进入家庭绑定流程
  344. }
  345. },
  346. chooseAvatar() {
  347. uni.chooseImage({
  348. count: 1,
  349. success: (res) => {
  350. this.form.avatar = res.tempFilePaths[0]
  351. }
  352. })
  353. },
  354. chooseTeacherPhoto() {
  355. uni.chooseImage({
  356. count: 1,
  357. success: (res) => {
  358. this.form.teacherPhoto = res.tempFilePaths[0]
  359. }
  360. })
  361. },
  362. chooseCertificate() {
  363. uni.chooseImage({
  364. count: 1,
  365. success: (res) => {
  366. this.form.certificateImage = res.tempFilePaths[0]
  367. }
  368. })
  369. },
  370. onGenderChange(e) {
  371. this.form.gender = e.detail.value
  372. },
  373. onIdCardInput(e) {
  374. const idCard = e.detail.value
  375. if (idCard.length === 18) {
  376. // 根据身份证号自动提取生日
  377. const year = idCard.substring(6, 10)
  378. const month = idCard.substring(10, 12)
  379. const day = idCard.substring(12, 14)
  380. this.form.birthday = `${year}-${month}-${day}`
  381. }
  382. },
  383. onBirthdayChange(e) {
  384. this.form.birthday = e.detail.value
  385. },
  386. onEthnicityChange(e) {
  387. this.form.ethnicity = this.ethnicityOptions[e.detail.value]
  388. },
  389. onBloodTypeChange(e) {
  390. this.form.bloodType = this.bloodTypeOptions[e.detail.value]
  391. },
  392. onBirthHourChange(e) {
  393. this.form.birthHour = this.birthHourValues[e.detail.value]
  394. },
  395. onEducationChange(e) {
  396. this.form.highestEducation = this.educationOptions[e.detail.value]
  397. },
  398. onMaritalChange(e) {
  399. this.form.maritalStatus = this.maritalOptions[e.detail.value]
  400. },
  401. selectMascot(code) {
  402. this.form.mascot = code
  403. },
  404. // 加载已有用户信息
  405. async loadUserInfo() {
  406. try {
  407. const res = await getUserInfo()
  408. if (res.code === 200 && res.data) {
  409. const userData = res.data
  410. this.form.nickname = userData.nickname || ''
  411. this.form.realName = userData.realName || ''
  412. this.form.gender = userData.gender || ''
  413. this.form.idCard = userData.idCard || ''
  414. // 生日只取日期部分,去掉时间和 ISO 格式的 T 分隔符
  415. this.form.birthday = userData.birthday ? String(userData.birthday).split(/[T ]/)[0] : ''
  416. this.form.avatar = userData.avatar || ''
  417. this.form.role = userData.role || ''
  418. this.form.phone = userData.phone || ''
  419. this.form.ethnicity = userData.ethnicity || ''
  420. this.form.bloodType = userData.bloodType || ''
  421. this.form.highestEducation = userData.highestEducation || ''
  422. this.form.maritalStatus = userData.maritalStatus || ''
  423. this.form.hobbies = userData.hobbies || ''
  424. this.form.dietPreferences = userData.dietPreferences || ''
  425. this.form.mascot = userData.mascot || ''
  426. this.form.address = userData.address || ''
  427. }
  428. } catch (e) {
  429. console.error('加载用户信息失败', e)
  430. }
  431. },
  432. // 保存修改
  433. async saveUserInfo() {
  434. // 新用户流程:昵称可跳过(跳过时用默认昵称);老用户流程:昵称必填
  435. if (!this.isNewUserFlow && !this.form.nickname) {
  436. uni.showToast({ title: '请输入昵称', icon: 'none' })
  437. return
  438. }
  439. if (this.isNewUserFlow && !this.form.nickname) {
  440. var phone = this.form.phone || ''
  441. this.form.nickname = phone ? '用户' + phone.substring(phone.length - 4) : '微信用户'
  442. }
  443. // 从 AddressPicker 读取地址文本
  444. if (this.$refs.addressPicker && this.$refs.addressPicker.fullAddress) {
  445. this.form.address = this.$refs.addressPicker.fullAddress
  446. }
  447. this.loading = true
  448. try {
  449. await updateUserInfo({
  450. nickname: this.form.nickname,
  451. realName: this.form.realName,
  452. gender: this.form.gender,
  453. idCard: this.form.idCard,
  454. birthday: this.form.birthday,
  455. avatar: this.form.avatar,
  456. mascot: this.form.mascot,
  457. ethnicity: this.form.ethnicity,
  458. bloodType: this.form.bloodType,
  459. highestEducation: this.form.highestEducation,
  460. maritalStatus: this.form.maritalStatus,
  461. hobbies: this.form.hobbies,
  462. dietPreferences: this.form.dietPreferences,
  463. address: this.form.address
  464. })
  465. uni.showToast({ title: '保存成功', icon: 'success' })
  466. setTimeout(() => {
  467. if (this.isNewUserFlow) {
  468. this.navigateToHome()
  469. } else {
  470. uni.navigateBack()
  471. }
  472. }, 1500)
  473. } catch (e) {
  474. uni.showToast({ title: '保存失败', icon: 'none' })
  475. } finally {
  476. this.loading = false
  477. }
  478. },
  479. async submitTeacherInfo() {
  480. // 验证必填字段
  481. if (!this.form.nickname) {
  482. uni.showToast({ title: '请输入昵称', icon: 'none' })
  483. return
  484. }
  485. if (!this.form.gender) {
  486. uni.showToast({ title: '请选择性别', icon: 'none' })
  487. return
  488. }
  489. if (!this.form.idCard) {
  490. uni.showToast({ title: '请输入身份证号', icon: 'none' })
  491. return
  492. }
  493. if (!this.form.teacherNo) {
  494. uni.showToast({ title: '请输入成长规划师证书号', icon: 'none' })
  495. return
  496. }
  497. if (!this.form.teacherPhoto) {
  498. uni.showToast({ title: '请上传成长规划师照片', icon: 'none' })
  499. return
  500. }
  501. if (!this.form.certificateImage) {
  502. uni.showToast({ title: '请上传成长规划师证书', icon: 'none' })
  503. return
  504. }
  505. this.loading = true
  506. try {
  507. await updateUserInfo({
  508. ...this.form,
  509. roles: this.form.role
  510. })
  511. uni.showToast({ title: '提交成功,请等待审核' })
  512. setTimeout(() => {
  513. uni.switchTab({ url: '/pages/index-home/index' })
  514. }, 1500)
  515. } catch (e) {
  516. uni.showToast({ title: '提交失败', icon: 'none' })
  517. } finally {
  518. this.loading = false
  519. }
  520. },
  521. async joinFamilyAction() {
  522. if (!this.form.inviteCode) {
  523. uni.showToast({ title: '请输入邀请码', icon: 'none' })
  524. return
  525. }
  526. if (!this.form.nickname) {
  527. uni.showToast({ title: '请输入昵称', icon: 'none' })
  528. return
  529. }
  530. this.loading = true
  531. try {
  532. let userId = this.userId
  533. // 测试环境:没有token,需要先注册用户
  534. if (!this.token && this.form.phone) {
  535. const regRes = await directRegister({
  536. phone: this.form.phone,
  537. nickname: this.form.nickname,
  538. avatar: this.form.avatar,
  539. role: this.form.role,
  540. mascot: this.form.mascot,
  541. birthday: this.form.birthday,
  542. gender: this.form.gender,
  543. inviteCode: this.form.inviteCode
  544. })
  545. userId = regRes.data.userId
  546. // 保存登录信息
  547. uni.setStorageSync('token', regRes.data.token)
  548. uni.setStorageSync('userId', userId)
  549. uni.setStorageSync('role', regRes.data.role)
  550. // 邀请码注册不写入 familyId(加入需管理员审批)
  551. } else {
  552. await updateUserInfo(this.form)
  553. await requestJoinByCode(this.form.inviteCode)
  554. }
  555. uni.showToast({ title: '申请已提交,等待管理员审批' })
  556. setTimeout(() => {
  557. this.navigateToHome()
  558. }, 1500)
  559. } catch (e) {
  560. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  561. } finally {
  562. this.loading = false
  563. }
  564. },
  565. async createFamilyAction() {
  566. if (!this.form.nickname) {
  567. uni.showToast({ title: '请输入昵称', icon: 'none' })
  568. return
  569. }
  570. this.loading = true
  571. try {
  572. let userId = this.userId
  573. // 测试环境:没有token,需要先注册用户
  574. if (!this.token && this.form.phone) {
  575. const regRes = await directRegister({
  576. phone: this.form.phone,
  577. nickname: this.form.nickname,
  578. avatar: this.form.avatar,
  579. role: this.form.role,
  580. mascot: this.form.mascot,
  581. birthday: this.form.birthday,
  582. gender: this.form.gender
  583. })
  584. userId = regRes.data.userId
  585. // 保存登录信息
  586. uni.setStorageSync('token', regRes.data.token)
  587. uni.setStorageSync('userId', userId)
  588. uni.setStorageSync('role', regRes.data.role)
  589. // 注册不写入 familyId(后端根据 userId 获取)
  590. } else {
  591. await updateUserInfo(this.form)
  592. const famRes = await createFamily(this.form.nickname + '的家庭')
  593. // 同步 familyId 到 store(hasFamily getter 依赖)
  594. if (famRes && famRes.data) {
  595. this.$store.commit('setFamilyId', famRes.data)
  596. }
  597. }
  598. uni.showToast({ title: '创建成功' })
  599. setTimeout(() => {
  600. this.navigateToHome()
  601. }, 1500)
  602. } catch (e) {
  603. uni.showToast({ title: e.message || '创建失败', icon: 'none' })
  604. } finally {
  605. this.loading = false
  606. }
  607. },
  608. skip() {
  609. this.navigateToHome()
  610. },
  611. navigateToHome() {
  612. // 保存 mascot 到 userInfo 存储,供 AI 聊天页读取
  613. if (this.form.mascot) {
  614. try {
  615. const userInfo = uni.getStorageSync('userInfo') || {}
  616. userInfo.mascot = this.form.mascot
  617. uni.setStorageSync('userInfo', userInfo)
  618. } catch (e) {
  619. console.error('保存 mascot 失败', e)
  620. }
  621. }
  622. // 用 switchTab 跳转带底部 TabBar 的首页(行/身/智/心/富)
  623. // parent-index/child-index 是分包页面(非 TabBar 页),用 navigateTo 会丢失底部 TabBar
  624. uni.switchTab({ url: '/pages/index-home/index' })
  625. }
  626. }
  627. }
  628. </script>
  629. <style scoped>
  630. .user-edit-container {
  631. min-height: 100vh;
  632. background: #f5f5f5;
  633. padding: 30rpx;
  634. }
  635. .header {
  636. margin-bottom: 40rpx;
  637. text-align: center;
  638. }
  639. .title {
  640. font-size: 40rpx;
  641. font-weight: bold;
  642. color: #333;
  643. display: block;
  644. margin-bottom: 10rpx;
  645. }
  646. .subtitle {
  647. font-size: 28rpx;
  648. color: #999;
  649. }
  650. /* 角色选择 */
  651. .role-select-section {
  652. margin-bottom: 40rpx;
  653. }
  654. .role-card {
  655. background: #fff;
  656. border-radius: 16rpx;
  657. padding: 40rpx;
  658. margin-bottom: 20rpx;
  659. display: flex;
  660. flex-direction: column;
  661. align-items: center;
  662. border: 2rpx solid transparent;
  663. transition: all 0.3s;
  664. }
  665. .role-card.active {
  666. border-color: #4CAF50;
  667. background: #f8fdf8;
  668. }
  669. .role-icon {
  670. font-size: 80rpx;
  671. margin-bottom: 16rpx;
  672. }
  673. .role-name {
  674. font-size: 32rpx;
  675. font-weight: bold;
  676. color: #333;
  677. margin-bottom: 8rpx;
  678. }
  679. .role-desc {
  680. font-size: 24rpx;
  681. color: #999;
  682. }
  683. .btn-next {
  684. background: #4CAF50;
  685. color: #fff;
  686. border-radius: 40rpx;
  687. margin-top: 20rpx;
  688. }
  689. /* 表单 */
  690. .form-section {
  691. background: #fff;
  692. border-radius: 16rpx;
  693. padding: 30rpx;
  694. margin-bottom: 30rpx;
  695. }
  696. .form-section-title {
  697. font-size: 30rpx;
  698. font-weight: bold;
  699. color: #333;
  700. margin-bottom: 20rpx;
  701. padding-bottom: 16rpx;
  702. border-bottom: 1rpx solid #f0f0f0;
  703. }
  704. .form-item {
  705. margin-bottom: 30rpx;
  706. }
  707. .label {
  708. display: block;
  709. font-size: 28rpx;
  710. color: #333;
  711. margin-bottom: 16rpx;
  712. }
  713. .required {
  714. color: #f44336;
  715. }
  716. .input, .picker-input {
  717. padding: 20rpx;
  718. background: #f5f5f5;
  719. border-radius: 12rpx;
  720. font-size: 28rpx;
  721. }
  722. .role-select {
  723. display: flex;
  724. gap: 40rpx;
  725. }
  726. .role-option {
  727. display: flex;
  728. align-items: center;
  729. gap: 8rpx;
  730. }
  731. /* 头像上传 */
  732. .avatar-upload {
  733. width: 150rpx;
  734. height: 150rpx;
  735. }
  736. .avatar-preview {
  737. width: 150rpx;
  738. height: 150rpx;
  739. border-radius: 50%;
  740. }
  741. .avatar-placeholder {
  742. width: 150rpx;
  743. height: 150rpx;
  744. background: #f5f5f5;
  745. border-radius: 50%;
  746. display: flex;
  747. align-items: center;
  748. justify-content: center;
  749. font-size: 60rpx;
  750. color: #ccc;
  751. }
  752. /* 照片上传 */
  753. .photo-upload {
  754. width: 300rpx;
  755. height: 200rpx;
  756. }
  757. .photo-preview {
  758. width: 300rpx;
  759. height: 200rpx;
  760. border-radius: 12rpx;
  761. }
  762. .photo-placeholder {
  763. width: 300rpx;
  764. height: 200rpx;
  765. background: #f5f5f5;
  766. border-radius: 12rpx;
  767. display: flex;
  768. flex-direction: column;
  769. align-items: center;
  770. justify-content: center;
  771. }
  772. .plus-icon {
  773. font-size: 60rpx;
  774. color: #ccc;
  775. }
  776. .placeholder-text {
  777. font-size: 24rpx;
  778. color: #999;
  779. margin-top: 8rpx;
  780. }
  781. /* AI助手选择 */
  782. .mascot-cards {
  783. display: flex;
  784. gap: 20rpx;
  785. margin-top: 16rpx;
  786. }
  787. .mascot-card {
  788. flex: 1;
  789. background: #f8f8f8;
  790. border-radius: 16rpx;
  791. padding: 24rpx;
  792. display: flex;
  793. align-items: center;
  794. gap: 16rpx;
  795. border: 2rpx solid transparent;
  796. transition: all 0.3s;
  797. }
  798. .mascot-card.active {
  799. border-color: #4CAF50;
  800. background: #f0fdf4;
  801. }
  802. .mascot-card-icon {
  803. width: 72rpx;
  804. height: 72rpx;
  805. border-radius: 50%;
  806. display: flex;
  807. align-items: center;
  808. justify-content: center;
  809. font-size: 36rpx;
  810. flex-shrink: 0;
  811. }
  812. .xibao-bg {
  813. background: linear-gradient(135deg, #FFF0DB, #FFD6A5);
  814. }
  815. .fubao-bg {
  816. background: linear-gradient(135deg, #FFF8E1, #FCD34D);
  817. }
  818. .mascot-card-info {
  819. display: flex;
  820. flex-direction: column;
  821. gap: 4rpx;
  822. }
  823. .mascot-card-name {
  824. font-size: 30rpx;
  825. font-weight: bold;
  826. color: #333;
  827. }
  828. .mascot-card-desc {
  829. font-size: 22rpx;
  830. color: #999;
  831. }
  832. /* 家庭绑定 */
  833. .family-section {
  834. background: #fff;
  835. border-radius: 16rpx;
  836. padding: 30rpx;
  837. }
  838. .section-title {
  839. font-size: 30rpx;
  840. font-weight: bold;
  841. color: #333;
  842. margin-bottom: 20rpx;
  843. }
  844. .family-actions {
  845. display: flex;
  846. flex-direction: column;
  847. gap: 20rpx;
  848. margin-top: 30rpx;
  849. }
  850. .btn {
  851. border-radius: 40rpx;
  852. font-size: 30rpx;
  853. }
  854. .btn-primary {
  855. background: #4CAF50;
  856. color: #fff;
  857. }
  858. .btn-skip {
  859. background: #f5f5f5;
  860. color: #999;
  861. }
  862. </style>