user-edit.vue 26 KB

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