user-edit.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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 && 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. return this.$store.getters.hasFamily
  290. },
  291. },
  292. onLoad(options) {
  293. if (options.token && options.userId) {
  294. this.token = options.token
  295. this.userId = options.userId
  296. if (options.autoParent === 'true') {
  297. // 首次登录:跳过角色选择,默认 parent,直接进入信息补充页
  298. this.showRoleSelect = false
  299. this.isEdit = true
  300. this.isNewUserFlow = true
  301. this.form.role = 'parent'
  302. } else {
  303. // 检查是否需要选择角色(新用户)- 只有明确传递 needsRoleSelect=true 才显示选择
  304. this.showRoleSelect = options.needsRoleSelect === 'true'
  305. this.isEdit = !this.showRoleSelect
  306. this.isNewUserFlow = options.needsRoleSelect === 'true'
  307. }
  308. } else if (options.phone && (options.autoParent === 'true' || options.needsRoleSelect === 'true')) {
  309. // 测试环境:没有token,只有手机号,是新用户注册流程
  310. this.showRoleSelect = options.needsRoleSelect === 'true'
  311. this.isEdit = !this.showRoleSelect
  312. this.isNewUserFlow = true
  313. if (options.autoParent === 'true') {
  314. this.form.role = 'parent'
  315. }
  316. // 将手机号传递给表单
  317. this.form.phone = options.phone
  318. } else {
  319. // 其他情况:默认进入编辑模式(如从"我的"页面进入)
  320. this.showRoleSelect = false
  321. this.isEdit = true
  322. this.isNewUserFlow = false
  323. }
  324. // 非新用户流程时,加载已有用户信息
  325. if (this.isEdit && !this.isNewUserFlow) {
  326. this.loadUserInfo()
  327. }
  328. },
  329. methods: {
  330. selectRole(role) {
  331. this.form.role = role
  332. },
  333. confirmRole() {
  334. if (!this.form.role) {
  335. uni.showToast({ title: '请选择角色', icon: 'none' })
  336. return
  337. }
  338. this.showRoleSelect = false
  339. this.isEdit = true
  340. // 测试环境:直接注册用户(不需要验证码)
  341. if (!this.token && this.form.phone) {
  342. // 自动进入家庭绑定流程
  343. }
  344. },
  345. chooseAvatar() {
  346. uni.chooseImage({
  347. count: 1,
  348. success: (res) => {
  349. this.form.avatar = res.tempFilePaths[0]
  350. }
  351. })
  352. },
  353. chooseTeacherPhoto() {
  354. uni.chooseImage({
  355. count: 1,
  356. success: (res) => {
  357. this.form.teacherPhoto = res.tempFilePaths[0]
  358. }
  359. })
  360. },
  361. chooseCertificate() {
  362. uni.chooseImage({
  363. count: 1,
  364. success: (res) => {
  365. this.form.certificateImage = res.tempFilePaths[0]
  366. }
  367. })
  368. },
  369. onGenderChange(e) {
  370. this.form.gender = e.detail.value
  371. },
  372. onIdCardInput(e) {
  373. const idCard = e.detail.value
  374. if (idCard.length === 18) {
  375. // 根据身份证号自动提取生日
  376. const year = idCard.substring(6, 10)
  377. const month = idCard.substring(10, 12)
  378. const day = idCard.substring(12, 14)
  379. this.form.birthday = `${year}-${month}-${day}`
  380. }
  381. },
  382. onBirthdayChange(e) {
  383. this.form.birthday = e.detail.value
  384. },
  385. onEthnicityChange(e) {
  386. this.form.ethnicity = this.ethnicityOptions[e.detail.value]
  387. },
  388. onBloodTypeChange(e) {
  389. this.form.bloodType = this.bloodTypeOptions[e.detail.value]
  390. },
  391. onBirthHourChange(e) {
  392. this.form.birthHour = this.birthHourValues[e.detail.value]
  393. },
  394. onEducationChange(e) {
  395. this.form.highestEducation = this.educationOptions[e.detail.value]
  396. },
  397. onMaritalChange(e) {
  398. this.form.maritalStatus = this.maritalOptions[e.detail.value]
  399. },
  400. selectMascot(code) {
  401. this.form.mascot = code
  402. },
  403. // 加载已有用户信息
  404. async loadUserInfo() {
  405. try {
  406. const res = await getUserInfo()
  407. if (res.code === 200 && res.data) {
  408. const userData = res.data
  409. this.form.nickname = userData.nickname || ''
  410. this.form.realName = userData.realName || ''
  411. this.form.gender = userData.gender || ''
  412. this.form.idCard = userData.idCard || ''
  413. // 生日只取日期部分,去掉时间和 ISO 格式的 T 分隔符
  414. this.form.birthday = userData.birthday ? String(userData.birthday).split(/[T ]/)[0] : ''
  415. this.form.avatar = userData.avatar || ''
  416. this.form.role = userData.role || ''
  417. this.form.phone = userData.phone || ''
  418. this.form.ethnicity = userData.ethnicity || ''
  419. this.form.bloodType = userData.bloodType || ''
  420. this.form.highestEducation = userData.highestEducation || ''
  421. this.form.maritalStatus = userData.maritalStatus || ''
  422. this.form.hobbies = userData.hobbies || ''
  423. this.form.dietPreferences = userData.dietPreferences || ''
  424. this.form.mascot = userData.mascot || ''
  425. this.form.address = userData.address || ''
  426. }
  427. } catch (e) {
  428. console.error('加载用户信息失败', e)
  429. }
  430. },
  431. // 保存修改
  432. async saveUserInfo() {
  433. // 新用户流程:昵称可跳过(跳过时用默认昵称);老用户流程:昵称必填
  434. if (!this.isNewUserFlow && !this.form.nickname) {
  435. uni.showToast({ title: '请输入昵称', icon: 'none' })
  436. return
  437. }
  438. if (this.isNewUserFlow && !this.form.nickname) {
  439. var phone = this.form.phone || ''
  440. this.form.nickname = phone ? '用户' + phone.substring(phone.length - 4) : '微信用户'
  441. }
  442. // 从 AddressPicker 读取地址文本
  443. if (this.$refs.addressPicker && this.$refs.addressPicker.fullAddress) {
  444. this.form.address = this.$refs.addressPicker.fullAddress
  445. }
  446. this.loading = true
  447. try {
  448. await updateUserInfo({
  449. nickname: this.form.nickname,
  450. realName: this.form.realName,
  451. gender: this.form.gender,
  452. idCard: this.form.idCard,
  453. birthday: this.form.birthday,
  454. avatar: this.form.avatar,
  455. mascot: this.form.mascot,
  456. ethnicity: this.form.ethnicity,
  457. bloodType: this.form.bloodType,
  458. highestEducation: this.form.highestEducation,
  459. maritalStatus: this.form.maritalStatus,
  460. hobbies: this.form.hobbies,
  461. dietPreferences: this.form.dietPreferences,
  462. address: this.form.address
  463. })
  464. uni.showToast({ title: '保存成功', icon: 'success' })
  465. setTimeout(() => {
  466. if (this.isNewUserFlow) {
  467. this.navigateToHome()
  468. } else {
  469. uni.navigateBack()
  470. }
  471. }, 1500)
  472. } catch (e) {
  473. uni.showToast({ title: '保存失败', icon: 'none' })
  474. } finally {
  475. this.loading = false
  476. }
  477. },
  478. async submitTeacherInfo() {
  479. // 验证必填字段
  480. if (!this.form.nickname) {
  481. uni.showToast({ title: '请输入昵称', icon: 'none' })
  482. return
  483. }
  484. if (!this.form.gender) {
  485. uni.showToast({ title: '请选择性别', icon: 'none' })
  486. return
  487. }
  488. if (!this.form.idCard) {
  489. uni.showToast({ title: '请输入身份证号', icon: 'none' })
  490. return
  491. }
  492. if (!this.form.teacherNo) {
  493. uni.showToast({ title: '请输入成长规划师证书号', icon: 'none' })
  494. return
  495. }
  496. if (!this.form.teacherPhoto) {
  497. uni.showToast({ title: '请上传成长规划师照片', icon: 'none' })
  498. return
  499. }
  500. if (!this.form.certificateImage) {
  501. uni.showToast({ title: '请上传成长规划师证书', icon: 'none' })
  502. return
  503. }
  504. this.loading = true
  505. try {
  506. await updateUserInfo({
  507. ...this.form,
  508. roles: this.form.role
  509. })
  510. uni.showToast({ title: '提交成功,请等待审核' })
  511. setTimeout(() => {
  512. uni.switchTab({ url: '/pages/index-home/index' })
  513. }, 1500)
  514. } catch (e) {
  515. uni.showToast({ title: '提交失败', icon: 'none' })
  516. } finally {
  517. this.loading = false
  518. }
  519. },
  520. async joinFamilyAction() {
  521. if (!this.form.inviteCode) {
  522. uni.showToast({ title: '请输入邀请码', icon: 'none' })
  523. return
  524. }
  525. if (!this.form.nickname) {
  526. uni.showToast({ title: '请输入昵称', icon: 'none' })
  527. return
  528. }
  529. this.loading = true
  530. try {
  531. let userId = this.userId
  532. // 测试环境:没有token,需要先注册用户
  533. if (!this.token && this.form.phone) {
  534. const regRes = await directRegister({
  535. phone: this.form.phone,
  536. nickname: this.form.nickname,
  537. avatar: this.form.avatar,
  538. role: this.form.role,
  539. mascot: this.form.mascot,
  540. birthday: this.form.birthday,
  541. gender: this.form.gender,
  542. inviteCode: this.form.inviteCode
  543. })
  544. userId = regRes.data.userId
  545. // 保存登录信息
  546. uni.setStorageSync('token', regRes.data.token)
  547. uni.setStorageSync('userId', userId)
  548. uni.setStorageSync('role', regRes.data.role)
  549. // 邀请码注册不写入 familyId(加入需管理员审批)
  550. } else {
  551. await updateUserInfo(this.form)
  552. await requestJoinByCode(this.form.inviteCode)
  553. }
  554. uni.showToast({ title: '申请已提交,等待管理员审批' })
  555. setTimeout(() => {
  556. this.navigateToHome()
  557. }, 1500)
  558. } catch (e) {
  559. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  560. } finally {
  561. this.loading = false
  562. }
  563. },
  564. async createFamilyAction() {
  565. if (!this.form.nickname) {
  566. uni.showToast({ title: '请输入昵称', icon: 'none' })
  567. return
  568. }
  569. this.loading = true
  570. try {
  571. let userId = this.userId
  572. // 测试环境:没有token,需要先注册用户
  573. if (!this.token && this.form.phone) {
  574. const regRes = await directRegister({
  575. phone: this.form.phone,
  576. nickname: this.form.nickname,
  577. avatar: this.form.avatar,
  578. role: this.form.role,
  579. mascot: this.form.mascot,
  580. birthday: this.form.birthday,
  581. gender: this.form.gender
  582. })
  583. userId = regRes.data.userId
  584. // 保存登录信息
  585. uni.setStorageSync('token', regRes.data.token)
  586. uni.setStorageSync('userId', userId)
  587. uni.setStorageSync('role', regRes.data.role)
  588. // 注册不写入 familyId(后端根据 userId 获取)
  589. } else {
  590. await updateUserInfo(this.form)
  591. await createFamily(this.form.nickname + '的家庭')
  592. }
  593. uni.showToast({ title: '创建成功' })
  594. setTimeout(() => {
  595. this.navigateToHome()
  596. }, 1500)
  597. } catch (e) {
  598. uni.showToast({ title: e.message || '创建失败', icon: 'none' })
  599. } finally {
  600. this.loading = false
  601. }
  602. },
  603. skip() {
  604. this.navigateToHome()
  605. },
  606. navigateToHome() {
  607. // 保存 mascot 到 userInfo 存储,供 AI 聊天页读取
  608. if (this.form.mascot) {
  609. try {
  610. const userInfo = uni.getStorageSync('userInfo') || {}
  611. userInfo.mascot = this.form.mascot
  612. uni.setStorageSync('userInfo', userInfo)
  613. } catch (e) {
  614. console.error('保存 mascot 失败', e)
  615. }
  616. }
  617. // 用 switchTab 跳转带底部 TabBar 的首页(行/身/智/心/富)
  618. // parent-index/child-index 是分包页面(非 TabBar 页),用 navigateTo 会丢失底部 TabBar
  619. uni.switchTab({ url: '/pages/index-home/index' })
  620. }
  621. }
  622. }
  623. </script>
  624. <style scoped>
  625. .user-edit-container {
  626. min-height: 100vh;
  627. background: #f5f5f5;
  628. padding: 30rpx;
  629. }
  630. .header {
  631. margin-bottom: 40rpx;
  632. text-align: center;
  633. }
  634. .title {
  635. font-size: 40rpx;
  636. font-weight: bold;
  637. color: #333;
  638. display: block;
  639. margin-bottom: 10rpx;
  640. }
  641. .subtitle {
  642. font-size: 28rpx;
  643. color: #999;
  644. }
  645. /* 角色选择 */
  646. .role-select-section {
  647. margin-bottom: 40rpx;
  648. }
  649. .role-card {
  650. background: #fff;
  651. border-radius: 16rpx;
  652. padding: 40rpx;
  653. margin-bottom: 20rpx;
  654. display: flex;
  655. flex-direction: column;
  656. align-items: center;
  657. border: 2rpx solid transparent;
  658. transition: all 0.3s;
  659. }
  660. .role-card.active {
  661. border-color: #4CAF50;
  662. background: #f8fdf8;
  663. }
  664. .role-icon {
  665. font-size: 80rpx;
  666. margin-bottom: 16rpx;
  667. }
  668. .role-name {
  669. font-size: 32rpx;
  670. font-weight: bold;
  671. color: #333;
  672. margin-bottom: 8rpx;
  673. }
  674. .role-desc {
  675. font-size: 24rpx;
  676. color: #999;
  677. }
  678. .btn-next {
  679. background: #4CAF50;
  680. color: #fff;
  681. border-radius: 40rpx;
  682. margin-top: 20rpx;
  683. }
  684. /* 表单 */
  685. .form-section {
  686. background: #fff;
  687. border-radius: 16rpx;
  688. padding: 30rpx;
  689. margin-bottom: 30rpx;
  690. }
  691. .form-section-title {
  692. font-size: 30rpx;
  693. font-weight: bold;
  694. color: #333;
  695. margin-bottom: 20rpx;
  696. padding-bottom: 16rpx;
  697. border-bottom: 1rpx solid #f0f0f0;
  698. }
  699. .form-item {
  700. margin-bottom: 30rpx;
  701. }
  702. .label {
  703. display: block;
  704. font-size: 28rpx;
  705. color: #333;
  706. margin-bottom: 16rpx;
  707. }
  708. .required {
  709. color: #f44336;
  710. }
  711. .input, .picker-input {
  712. padding: 20rpx;
  713. background: #f5f5f5;
  714. border-radius: 12rpx;
  715. font-size: 28rpx;
  716. }
  717. .role-select {
  718. display: flex;
  719. gap: 40rpx;
  720. }
  721. .role-option {
  722. display: flex;
  723. align-items: center;
  724. gap: 8rpx;
  725. }
  726. /* 头像上传 */
  727. .avatar-upload {
  728. width: 150rpx;
  729. height: 150rpx;
  730. }
  731. .avatar-preview {
  732. width: 150rpx;
  733. height: 150rpx;
  734. border-radius: 50%;
  735. }
  736. .avatar-placeholder {
  737. width: 150rpx;
  738. height: 150rpx;
  739. background: #f5f5f5;
  740. border-radius: 50%;
  741. display: flex;
  742. align-items: center;
  743. justify-content: center;
  744. font-size: 60rpx;
  745. color: #ccc;
  746. }
  747. /* 照片上传 */
  748. .photo-upload {
  749. width: 300rpx;
  750. height: 200rpx;
  751. }
  752. .photo-preview {
  753. width: 300rpx;
  754. height: 200rpx;
  755. border-radius: 12rpx;
  756. }
  757. .photo-placeholder {
  758. width: 300rpx;
  759. height: 200rpx;
  760. background: #f5f5f5;
  761. border-radius: 12rpx;
  762. display: flex;
  763. flex-direction: column;
  764. align-items: center;
  765. justify-content: center;
  766. }
  767. .plus-icon {
  768. font-size: 60rpx;
  769. color: #ccc;
  770. }
  771. .placeholder-text {
  772. font-size: 24rpx;
  773. color: #999;
  774. margin-top: 8rpx;
  775. }
  776. /* AI助手选择 */
  777. .mascot-cards {
  778. display: flex;
  779. gap: 20rpx;
  780. margin-top: 16rpx;
  781. }
  782. .mascot-card {
  783. flex: 1;
  784. background: #f8f8f8;
  785. border-radius: 16rpx;
  786. padding: 24rpx;
  787. display: flex;
  788. align-items: center;
  789. gap: 16rpx;
  790. border: 2rpx solid transparent;
  791. transition: all 0.3s;
  792. }
  793. .mascot-card.active {
  794. border-color: #4CAF50;
  795. background: #f0fdf4;
  796. }
  797. .mascot-card-icon {
  798. width: 72rpx;
  799. height: 72rpx;
  800. border-radius: 50%;
  801. display: flex;
  802. align-items: center;
  803. justify-content: center;
  804. font-size: 36rpx;
  805. flex-shrink: 0;
  806. }
  807. .xibao-bg {
  808. background: linear-gradient(135deg, #FFF0DB, #FFD6A5);
  809. }
  810. .fubao-bg {
  811. background: linear-gradient(135deg, #FFF8E1, #FCD34D);
  812. }
  813. .mascot-card-info {
  814. display: flex;
  815. flex-direction: column;
  816. gap: 4rpx;
  817. }
  818. .mascot-card-name {
  819. font-size: 30rpx;
  820. font-weight: bold;
  821. color: #333;
  822. }
  823. .mascot-card-desc {
  824. font-size: 22rpx;
  825. color: #999;
  826. }
  827. /* 家庭绑定 */
  828. .family-section {
  829. background: #fff;
  830. border-radius: 16rpx;
  831. padding: 30rpx;
  832. }
  833. .section-title {
  834. font-size: 30rpx;
  835. font-weight: bold;
  836. color: #333;
  837. margin-bottom: 20rpx;
  838. }
  839. .family-actions {
  840. display: flex;
  841. flex-direction: column;
  842. gap: 20rpx;
  843. margin-top: 30rpx;
  844. }
  845. .btn {
  846. border-radius: 40rpx;
  847. font-size: 30rpx;
  848. }
  849. .btn-primary {
  850. background: #4CAF50;
  851. color: #fff;
  852. }
  853. .btn-skip {
  854. background: #f5f5f5;
  855. color: #999;
  856. }
  857. </style>