user-edit.vue 26 KB

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