user-edit.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. // parent-index 是分包页面(非 TabBar 页),switchTab 会触发路由系统错误,改用 redirectTo
  502. uni.redirectTo({
  503. url: '/pages/home-pages/parent-index'
  504. })
  505. }, 1500)
  506. } catch (e) {
  507. uni.showToast({ title: '提交失败', icon: 'none' })
  508. } finally {
  509. this.loading = false
  510. }
  511. },
  512. async joinFamilyAction() {
  513. if (!this.form.inviteCode) {
  514. uni.showToast({ title: '请输入邀请码', icon: 'none' })
  515. return
  516. }
  517. if (!this.form.nickname) {
  518. uni.showToast({ title: '请输入昵称', icon: 'none' })
  519. return
  520. }
  521. this.loading = true
  522. try {
  523. let userId = this.userId
  524. // 测试环境:没有token,需要先注册用户
  525. if (!this.token && this.form.phone) {
  526. const regRes = await directRegister({
  527. phone: this.form.phone,
  528. nickname: this.form.nickname,
  529. avatar: this.form.avatar,
  530. role: this.form.role,
  531. mascot: this.form.mascot,
  532. birthday: this.form.birthday,
  533. gender: this.form.gender,
  534. inviteCode: this.form.inviteCode
  535. })
  536. userId = regRes.data.userId
  537. // 保存登录信息
  538. uni.setStorageSync('token', regRes.data.token)
  539. uni.setStorageSync('userId', userId)
  540. uni.setStorageSync('role', regRes.data.role)
  541. uni.setStorageSync('familyId', regRes.data.familyId)
  542. } else {
  543. await updateUserInfo(this.form)
  544. await joinFamily(this.form.inviteCode)
  545. }
  546. uni.showToast({ title: '加入成功' })
  547. setTimeout(() => {
  548. this.navigateToHome()
  549. }, 1500)
  550. } catch (e) {
  551. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  552. } finally {
  553. this.loading = false
  554. }
  555. },
  556. async createFamilyAction() {
  557. if (!this.form.nickname) {
  558. uni.showToast({ title: '请输入昵称', icon: 'none' })
  559. return
  560. }
  561. this.loading = true
  562. try {
  563. let userId = this.userId
  564. // 测试环境:没有token,需要先注册用户
  565. if (!this.token && this.form.phone) {
  566. const regRes = await directRegister({
  567. phone: this.form.phone,
  568. nickname: this.form.nickname,
  569. avatar: this.form.avatar,
  570. role: this.form.role,
  571. mascot: this.form.mascot,
  572. birthday: this.form.birthday,
  573. gender: this.form.gender
  574. })
  575. userId = regRes.data.userId
  576. // 保存登录信息
  577. uni.setStorageSync('token', regRes.data.token)
  578. uni.setStorageSync('userId', userId)
  579. uni.setStorageSync('role', regRes.data.role)
  580. uni.setStorageSync('familyId', regRes.data.familyId)
  581. } else {
  582. await updateUserInfo(this.form)
  583. await createFamily(this.form.nickname + '的家庭')
  584. }
  585. uni.showToast({ title: '创建成功' })
  586. setTimeout(() => {
  587. this.navigateToHome()
  588. }, 1500)
  589. } catch (e) {
  590. uni.showToast({ title: e.message || '创建失败', icon: 'none' })
  591. } finally {
  592. this.loading = false
  593. }
  594. },
  595. skip() {
  596. this.navigateToHome()
  597. },
  598. navigateToHome() {
  599. // 保存 mascot 到 userInfo 存储,供 AI 聊天页读取
  600. if (this.form.mascot) {
  601. try {
  602. const userInfo = uni.getStorageSync('userInfo') || {}
  603. userInfo.mascot = this.form.mascot
  604. uni.setStorageSync('userInfo', userInfo)
  605. } catch (e) {
  606. console.error('保存 mascot 失败', e)
  607. }
  608. }
  609. const role = this.form.role || 'parent'
  610. if (role === 'parent') {
  611. uni.navigateTo({ url: '/pages/home-pages/parent-index' })
  612. } else if (role === 'child') {
  613. uni.navigateTo({ url: '/pages/home-pages/child-index' })
  614. } else if (role === 'teacher') {
  615. uni.navigateTo({ url: '/pages/teacher/index' })
  616. }
  617. }
  618. }
  619. }
  620. </script>
  621. <style scoped>
  622. .user-edit-container {
  623. min-height: 100vh;
  624. background: #f5f5f5;
  625. padding: 30rpx;
  626. }
  627. .header {
  628. margin-bottom: 40rpx;
  629. text-align: center;
  630. }
  631. .title {
  632. font-size: 40rpx;
  633. font-weight: bold;
  634. color: #333;
  635. display: block;
  636. margin-bottom: 10rpx;
  637. }
  638. .subtitle {
  639. font-size: 28rpx;
  640. color: #999;
  641. }
  642. /* 角色选择 */
  643. .role-select-section {
  644. margin-bottom: 40rpx;
  645. }
  646. .role-card {
  647. background: #fff;
  648. border-radius: 16rpx;
  649. padding: 40rpx;
  650. margin-bottom: 20rpx;
  651. display: flex;
  652. flex-direction: column;
  653. align-items: center;
  654. border: 2rpx solid transparent;
  655. transition: all 0.3s;
  656. }
  657. .role-card.active {
  658. border-color: #4CAF50;
  659. background: #f8fdf8;
  660. }
  661. .role-icon {
  662. font-size: 80rpx;
  663. margin-bottom: 16rpx;
  664. }
  665. .role-name {
  666. font-size: 32rpx;
  667. font-weight: bold;
  668. color: #333;
  669. margin-bottom: 8rpx;
  670. }
  671. .role-desc {
  672. font-size: 24rpx;
  673. color: #999;
  674. }
  675. .btn-next {
  676. background: #4CAF50;
  677. color: #fff;
  678. border-radius: 40rpx;
  679. margin-top: 20rpx;
  680. }
  681. /* 表单 */
  682. .form-section {
  683. background: #fff;
  684. border-radius: 16rpx;
  685. padding: 30rpx;
  686. margin-bottom: 30rpx;
  687. }
  688. .form-section-title {
  689. font-size: 30rpx;
  690. font-weight: bold;
  691. color: #333;
  692. margin-bottom: 20rpx;
  693. padding-bottom: 16rpx;
  694. border-bottom: 1rpx solid #f0f0f0;
  695. }
  696. .form-item {
  697. margin-bottom: 30rpx;
  698. }
  699. .label {
  700. display: block;
  701. font-size: 28rpx;
  702. color: #333;
  703. margin-bottom: 16rpx;
  704. }
  705. .required {
  706. color: #f44336;
  707. }
  708. .input, .picker-input {
  709. padding: 20rpx;
  710. background: #f5f5f5;
  711. border-radius: 12rpx;
  712. font-size: 28rpx;
  713. }
  714. .role-select {
  715. display: flex;
  716. gap: 40rpx;
  717. }
  718. .role-option {
  719. display: flex;
  720. align-items: center;
  721. gap: 8rpx;
  722. }
  723. /* 头像上传 */
  724. .avatar-upload {
  725. width: 150rpx;
  726. height: 150rpx;
  727. }
  728. .avatar-preview {
  729. width: 150rpx;
  730. height: 150rpx;
  731. border-radius: 50%;
  732. }
  733. .avatar-placeholder {
  734. width: 150rpx;
  735. height: 150rpx;
  736. background: #f5f5f5;
  737. border-radius: 50%;
  738. display: flex;
  739. align-items: center;
  740. justify-content: center;
  741. font-size: 60rpx;
  742. color: #ccc;
  743. }
  744. /* 照片上传 */
  745. .photo-upload {
  746. width: 300rpx;
  747. height: 200rpx;
  748. }
  749. .photo-preview {
  750. width: 300rpx;
  751. height: 200rpx;
  752. border-radius: 12rpx;
  753. }
  754. .photo-placeholder {
  755. width: 300rpx;
  756. height: 200rpx;
  757. background: #f5f5f5;
  758. border-radius: 12rpx;
  759. display: flex;
  760. flex-direction: column;
  761. align-items: center;
  762. justify-content: center;
  763. }
  764. .plus-icon {
  765. font-size: 60rpx;
  766. color: #ccc;
  767. }
  768. .placeholder-text {
  769. font-size: 24rpx;
  770. color: #999;
  771. margin-top: 8rpx;
  772. }
  773. /* AI助手选择 */
  774. .mascot-cards {
  775. display: flex;
  776. gap: 20rpx;
  777. margin-top: 16rpx;
  778. }
  779. .mascot-card {
  780. flex: 1;
  781. background: #f8f8f8;
  782. border-radius: 16rpx;
  783. padding: 24rpx;
  784. display: flex;
  785. align-items: center;
  786. gap: 16rpx;
  787. border: 2rpx solid transparent;
  788. transition: all 0.3s;
  789. }
  790. .mascot-card.active {
  791. border-color: #4CAF50;
  792. background: #f0fdf4;
  793. }
  794. .mascot-card-icon {
  795. width: 72rpx;
  796. height: 72rpx;
  797. border-radius: 50%;
  798. display: flex;
  799. align-items: center;
  800. justify-content: center;
  801. font-size: 36rpx;
  802. flex-shrink: 0;
  803. }
  804. .xibao-bg {
  805. background: linear-gradient(135deg, #FFF0DB, #FFD6A5);
  806. }
  807. .fubao-bg {
  808. background: linear-gradient(135deg, #FFF8E1, #FCD34D);
  809. }
  810. .mascot-card-info {
  811. display: flex;
  812. flex-direction: column;
  813. gap: 4rpx;
  814. }
  815. .mascot-card-name {
  816. font-size: 30rpx;
  817. font-weight: bold;
  818. color: #333;
  819. }
  820. .mascot-card-desc {
  821. font-size: 22rpx;
  822. color: #999;
  823. }
  824. /* 家庭绑定 */
  825. .family-section {
  826. background: #fff;
  827. border-radius: 16rpx;
  828. padding: 30rpx;
  829. }
  830. .section-title {
  831. font-size: 30rpx;
  832. font-weight: bold;
  833. color: #333;
  834. margin-bottom: 20rpx;
  835. }
  836. .family-actions {
  837. display: flex;
  838. flex-direction: column;
  839. gap: 20rpx;
  840. margin-top: 30rpx;
  841. }
  842. .btn {
  843. border-radius: 40rpx;
  844. font-size: 30rpx;
  845. }
  846. .btn-primary {
  847. background: #4CAF50;
  848. color: #fff;
  849. }
  850. .btn-skip {
  851. background: #f5f5f5;
  852. color: #999;
  853. }
  854. </style>