user-edit.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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-section-title" v-if="form.role === 'teacher'">指导师信息</view>
  90. <!-- 指导师证书号(指导师必填) -->
  91. <view class="form-item" v-if="form.role === 'teacher'">
  92. <text class="label">指导师证书号 <text class="required">*</text></text>
  93. <input type="text" v-model="form.teacherNo" placeholder="请输入指导师证书号" class="input" maxlength="50" />
  94. </view>
  95. <!-- 指导师照片(指导师必填) -->
  96. <view class="form-item" v-if="form.role === 'teacher'">
  97. <text class="label">指导师照片 <text class="required">*</text></text>
  98. <view class="photo-upload" @click="chooseTeacherPhoto">
  99. <image v-if="form.teacherPhoto" :src="form.teacherPhoto" class="photo-preview" mode="aspectFill"></image>
  100. <view v-else class="photo-placeholder">
  101. <text class="plus-icon">+</text>
  102. <text class="placeholder-text">上传照片</text>
  103. </view>
  104. </view>
  105. </view>
  106. <!-- 指导师证书照片(指导师必填) -->
  107. <view class="form-item" v-if="form.role === 'teacher'">
  108. <text class="label">指导师证书 <text class="required">*</text></text>
  109. <view class="photo-upload" @click="chooseCertificate">
  110. <image v-if="form.certificateImage" :src="form.certificateImage" class="photo-preview" mode="aspectFill"></image>
  111. <view v-else class="photo-placeholder">
  112. <text class="plus-icon">+</text>
  113. <text class="placeholder-text">上传证书</text>
  114. </view>
  115. </view>
  116. </view>
  117. </view>
  118. <!-- 家庭绑定(新用户注册时显示) -->
  119. <view class="family-section" v-if="isEdit && form.role !== 'teacher' && isNewUserFlow">
  120. <view class="section-title">家庭绑定</view>
  121. <!-- 邀请码输入 -->
  122. <view class="form-item">
  123. <text class="label">家庭邀请码</text>
  124. <input type="text" v-model="form.inviteCode" placeholder="选填,如有邀请码请输入" class="input" maxlength="8" />
  125. </view>
  126. <view class="family-actions">
  127. <button v-if="form.inviteCode" class="btn btn-primary" @click="joinFamilyAction" :loading="loading">
  128. 加入家庭
  129. </button>
  130. <button v-else class="btn btn-primary" @click="createFamilyAction" :loading="loading">
  131. 创建新家庭
  132. </button>
  133. <button class="btn btn-skip" @click="skip">
  134. 跳过
  135. </button>
  136. </view>
  137. </view>
  138. <!-- 修改个人资料保存按钮(非新用户流程时显示) -->
  139. <view class="family-section" v-if="isEdit && !isNewUserFlow && form.role !== 'teacher'">
  140. <view class="family-actions">
  141. <button class="btn btn-primary" @click="saveUserInfo" :loading="loading">
  142. 保存修改
  143. </button>
  144. </view>
  145. </view>
  146. <!-- 指导师提交按钮 -->
  147. <view class="family-section" v-if="isEdit && form.role === 'teacher' && isNewUserFlow">
  148. <view class="family-actions">
  149. <button class="btn btn-primary" @click="submitTeacherInfo" :loading="loading">
  150. 提交申请
  151. </button>
  152. </view>
  153. </view>
  154. </view>
  155. </template>
  156. <script>
  157. import { updateUserInfo, joinFamily, createFamily, directRegister, getUserInfo } from '../../utils/api.js'
  158. export default {
  159. data() {
  160. return {
  161. isEdit: false,
  162. showRoleSelect: true,
  163. isNewUserFlow: false, // 是否是新用户注册流程
  164. form: {
  165. role: '',
  166. avatar: '',
  167. nickname: '',
  168. realName: '',
  169. gender: '',
  170. idCard: '',
  171. birthday: '',
  172. inviteCode: '',
  173. phone: '',
  174. teacherNo: '',
  175. teacherPhoto: '',
  176. certificateImage: ''
  177. },
  178. loading: false,
  179. userId: '',
  180. token: ''
  181. }
  182. },
  183. onLoad(options) {
  184. if (options.token && options.userId) {
  185. this.token = options.token
  186. this.userId = options.userId
  187. // 检查是否需要选择角色(新用户)- 只有明确传递 needsRoleSelect=true 才显示选择
  188. this.showRoleSelect = options.needsRoleSelect === 'true'
  189. this.isEdit = !this.showRoleSelect
  190. this.isNewUserFlow = options.needsRoleSelect === 'true'
  191. } else if (options.phone && options.needsRoleSelect === 'true') {
  192. // 测试环境:没有token,只有手机号,是新用户注册流程
  193. this.showRoleSelect = true
  194. this.isEdit = false
  195. this.isNewUserFlow = true
  196. // 将手机号传递给表单
  197. this.form.phone = options.phone
  198. } else {
  199. // 其他情况:默认进入编辑模式(如从"我的"页面进入)
  200. this.showRoleSelect = false
  201. this.isEdit = true
  202. this.isNewUserFlow = false
  203. }
  204. // 非新用户流程时,加载已有用户信息
  205. if (this.isEdit && !this.isNewUserFlow) {
  206. this.loadUserInfo()
  207. }
  208. },
  209. methods: {
  210. selectRole(role) {
  211. this.form.role = role
  212. },
  213. confirmRole() {
  214. if (!this.form.role) {
  215. uni.showToast({ title: '请选择角色', icon: 'none' })
  216. return
  217. }
  218. this.showRoleSelect = false
  219. this.isEdit = true
  220. // 测试环境:直接注册用户(不需要验证码)
  221. if (!this.token && this.form.phone) {
  222. // 自动进入家庭绑定流程
  223. }
  224. },
  225. chooseAvatar() {
  226. uni.chooseImage({
  227. count: 1,
  228. success: (res) => {
  229. this.form.avatar = res.tempFilePaths[0]
  230. }
  231. })
  232. },
  233. chooseTeacherPhoto() {
  234. uni.chooseImage({
  235. count: 1,
  236. success: (res) => {
  237. this.form.teacherPhoto = res.tempFilePaths[0]
  238. }
  239. })
  240. },
  241. chooseCertificate() {
  242. uni.chooseImage({
  243. count: 1,
  244. success: (res) => {
  245. this.form.certificateImage = res.tempFilePaths[0]
  246. }
  247. })
  248. },
  249. onGenderChange(e) {
  250. this.form.gender = e.detail.value
  251. },
  252. onIdCardInput(e) {
  253. const idCard = e.detail.value
  254. if (idCard.length === 18) {
  255. // 根据身份证号自动提取生日
  256. const year = idCard.substring(6, 10)
  257. const month = idCard.substring(10, 12)
  258. const day = idCard.substring(12, 14)
  259. this.form.birthday = `${year}-${month}-${day}`
  260. }
  261. },
  262. onBirthdayChange(e) {
  263. this.form.birthday = e.detail.value
  264. },
  265. // 加载已有用户信息
  266. async loadUserInfo() {
  267. try {
  268. const res = await getUserInfo()
  269. if (res.code === 200 && res.data) {
  270. const userData = res.data
  271. this.form.nickname = userData.nickname || ''
  272. this.form.realName = userData.realName || ''
  273. this.form.gender = userData.gender || ''
  274. this.form.idCard = userData.idCard || ''
  275. // 生日只取日期部分,去掉时间
  276. this.form.birthday = userData.birthday ? userData.birthday.split(' ')[0] : ''
  277. this.form.avatar = userData.avatar || ''
  278. this.form.role = userData.role || ''
  279. this.form.phone = userData.phone || ''
  280. }
  281. } catch (e) {
  282. console.error('加载用户信息失败', e)
  283. }
  284. },
  285. // 保存修改
  286. async saveUserInfo() {
  287. if (!this.form.nickname) {
  288. uni.showToast({ title: '请输入昵称', icon: 'none' })
  289. return
  290. }
  291. this.loading = true
  292. try {
  293. await updateUserInfo({
  294. nickname: this.form.nickname,
  295. realName: this.form.realName,
  296. gender: this.form.gender,
  297. idCard: this.form.idCard,
  298. birthday: this.form.birthday,
  299. avatar: this.form.avatar
  300. })
  301. uni.showToast({ title: '保存成功', icon: 'success' })
  302. setTimeout(() => {
  303. uni.navigateBack()
  304. }, 1500)
  305. } catch (e) {
  306. uni.showToast({ title: '保存失败', icon: 'none' })
  307. } finally {
  308. this.loading = false
  309. }
  310. },
  311. async submitTeacherInfo() {
  312. // 验证必填字段
  313. if (!this.form.nickname) {
  314. uni.showToast({ title: '请输入昵称', icon: 'none' })
  315. return
  316. }
  317. if (!this.form.gender) {
  318. uni.showToast({ title: '请选择性别', icon: 'none' })
  319. return
  320. }
  321. if (!this.form.idCard) {
  322. uni.showToast({ title: '请输入身份证号', icon: 'none' })
  323. return
  324. }
  325. if (!this.form.teacherNo) {
  326. uni.showToast({ title: '请输入指导师证书号', icon: 'none' })
  327. return
  328. }
  329. if (!this.form.teacherPhoto) {
  330. uni.showToast({ title: '请上传指导师照片', icon: 'none' })
  331. return
  332. }
  333. if (!this.form.certificateImage) {
  334. uni.showToast({ title: '请上传指导师证书', icon: 'none' })
  335. return
  336. }
  337. this.loading = true
  338. try {
  339. await updateUserInfo({
  340. ...this.form,
  341. roles: this.form.role
  342. })
  343. uni.showToast({ title: '提交成功,请等待审核' })
  344. setTimeout(() => {
  345. uni.switchTab({
  346. url: '/pages/index/parent-index'
  347. })
  348. }, 1500)
  349. } catch (e) {
  350. uni.showToast({ title: '提交失败', icon: 'none' })
  351. } finally {
  352. this.loading = false
  353. }
  354. },
  355. async joinFamilyAction() {
  356. if (!this.form.inviteCode) {
  357. uni.showToast({ title: '请输入邀请码', icon: 'none' })
  358. return
  359. }
  360. if (!this.form.nickname) {
  361. uni.showToast({ title: '请输入昵称', icon: 'none' })
  362. return
  363. }
  364. this.loading = true
  365. try {
  366. let userId = this.userId
  367. // 测试环境:没有token,需要先注册用户
  368. if (!this.token && this.form.phone) {
  369. const regRes = await directRegister({
  370. phone: this.form.phone,
  371. nickname: this.form.nickname,
  372. avatar: this.form.avatar,
  373. role: this.form.role,
  374. birthday: this.form.birthday,
  375. gender: this.form.gender,
  376. inviteCode: this.form.inviteCode
  377. })
  378. userId = regRes.data.userId
  379. // 保存登录信息
  380. uni.setStorageSync('token', regRes.data.token)
  381. uni.setStorageSync('userId', userId)
  382. uni.setStorageSync('role', regRes.data.role)
  383. uni.setStorageSync('familyId', regRes.data.familyId)
  384. } else {
  385. await updateUserInfo(this.form)
  386. await joinFamily(this.form.inviteCode)
  387. }
  388. uni.showToast({ title: '加入成功' })
  389. setTimeout(() => {
  390. this.navigateToHome()
  391. }, 1500)
  392. } catch (e) {
  393. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  394. } finally {
  395. this.loading = false
  396. }
  397. },
  398. async createFamilyAction() {
  399. if (!this.form.nickname) {
  400. uni.showToast({ title: '请输入昵称', icon: 'none' })
  401. return
  402. }
  403. this.loading = true
  404. try {
  405. let userId = this.userId
  406. // 测试环境:没有token,需要先注册用户
  407. if (!this.token && this.form.phone) {
  408. const regRes = await directRegister({
  409. phone: this.form.phone,
  410. nickname: this.form.nickname,
  411. avatar: this.form.avatar,
  412. role: this.form.role,
  413. birthday: this.form.birthday,
  414. gender: this.form.gender
  415. })
  416. userId = regRes.data.userId
  417. // 保存登录信息
  418. uni.setStorageSync('token', regRes.data.token)
  419. uni.setStorageSync('userId', userId)
  420. uni.setStorageSync('role', regRes.data.role)
  421. uni.setStorageSync('familyId', regRes.data.familyId)
  422. } else {
  423. await updateUserInfo(this.form)
  424. await createFamily(this.form.nickname + '的家庭')
  425. }
  426. uni.showToast({ title: '创建成功' })
  427. setTimeout(() => {
  428. this.navigateToHome()
  429. }, 1500)
  430. } catch (e) {
  431. uni.showToast({ title: e.message || '创建失败', icon: 'none' })
  432. } finally {
  433. this.loading = false
  434. }
  435. },
  436. skip() {
  437. this.navigateToHome()
  438. },
  439. navigateToHome() {
  440. const role = this.form.role || 'parent'
  441. if (role === 'parent') {
  442. uni.switchTab({ url: '/pages/index/parent-index' })
  443. } else if (role === 'child') {
  444. uni.switchTab({ url: '/pages/index/child-index' })
  445. } else if (role === 'teacher') {
  446. uni.switchTab({ url: '/pages/teacher/index' })
  447. }
  448. }
  449. }
  450. }
  451. </script>
  452. <style scoped>
  453. .user-edit-container {
  454. min-height: 100vh;
  455. background: #f5f5f5;
  456. padding: 30rpx;
  457. }
  458. .header {
  459. margin-bottom: 40rpx;
  460. text-align: center;
  461. }
  462. .title {
  463. font-size: 40rpx;
  464. font-weight: bold;
  465. color: #333;
  466. display: block;
  467. margin-bottom: 10rpx;
  468. }
  469. .subtitle {
  470. font-size: 28rpx;
  471. color: #999;
  472. }
  473. /* 角色选择 */
  474. .role-select-section {
  475. margin-bottom: 40rpx;
  476. }
  477. .role-card {
  478. background: #fff;
  479. border-radius: 16rpx;
  480. padding: 40rpx;
  481. margin-bottom: 20rpx;
  482. display: flex;
  483. flex-direction: column;
  484. align-items: center;
  485. border: 2rpx solid transparent;
  486. transition: all 0.3s;
  487. }
  488. .role-card.active {
  489. border-color: #4CAF50;
  490. background: #f8fdf8;
  491. }
  492. .role-icon {
  493. font-size: 80rpx;
  494. margin-bottom: 16rpx;
  495. }
  496. .role-name {
  497. font-size: 32rpx;
  498. font-weight: bold;
  499. color: #333;
  500. margin-bottom: 8rpx;
  501. }
  502. .role-desc {
  503. font-size: 24rpx;
  504. color: #999;
  505. }
  506. .btn-next {
  507. background: #4CAF50;
  508. color: #fff;
  509. border-radius: 40rpx;
  510. margin-top: 20rpx;
  511. }
  512. /* 表单 */
  513. .form-section {
  514. background: #fff;
  515. border-radius: 16rpx;
  516. padding: 30rpx;
  517. margin-bottom: 30rpx;
  518. }
  519. .form-section-title {
  520. font-size: 30rpx;
  521. font-weight: bold;
  522. color: #333;
  523. margin-bottom: 20rpx;
  524. padding-bottom: 16rpx;
  525. border-bottom: 1rpx solid #f0f0f0;
  526. }
  527. .form-item {
  528. margin-bottom: 30rpx;
  529. }
  530. .label {
  531. display: block;
  532. font-size: 28rpx;
  533. color: #333;
  534. margin-bottom: 16rpx;
  535. }
  536. .required {
  537. color: #f44336;
  538. }
  539. .input, .picker-input {
  540. padding: 20rpx;
  541. background: #f5f5f5;
  542. border-radius: 12rpx;
  543. font-size: 28rpx;
  544. }
  545. .role-select {
  546. display: flex;
  547. gap: 40rpx;
  548. }
  549. .role-option {
  550. display: flex;
  551. align-items: center;
  552. gap: 8rpx;
  553. }
  554. /* 头像上传 */
  555. .avatar-upload {
  556. width: 150rpx;
  557. height: 150rpx;
  558. }
  559. .avatar-preview {
  560. width: 150rpx;
  561. height: 150rpx;
  562. border-radius: 50%;
  563. }
  564. .avatar-placeholder {
  565. width: 150rpx;
  566. height: 150rpx;
  567. background: #f5f5f5;
  568. border-radius: 50%;
  569. display: flex;
  570. align-items: center;
  571. justify-content: center;
  572. font-size: 60rpx;
  573. color: #ccc;
  574. }
  575. /* 照片上传 */
  576. .photo-upload {
  577. width: 300rpx;
  578. height: 200rpx;
  579. }
  580. .photo-preview {
  581. width: 300rpx;
  582. height: 200rpx;
  583. border-radius: 12rpx;
  584. }
  585. .photo-placeholder {
  586. width: 300rpx;
  587. height: 200rpx;
  588. background: #f5f5f5;
  589. border-radius: 12rpx;
  590. display: flex;
  591. flex-direction: column;
  592. align-items: center;
  593. justify-content: center;
  594. }
  595. .plus-icon {
  596. font-size: 60rpx;
  597. color: #ccc;
  598. }
  599. .placeholder-text {
  600. font-size: 24rpx;
  601. color: #999;
  602. margin-top: 8rpx;
  603. }
  604. /* 家庭绑定 */
  605. .family-section {
  606. background: #fff;
  607. border-radius: 16rpx;
  608. padding: 30rpx;
  609. }
  610. .section-title {
  611. font-size: 30rpx;
  612. font-weight: bold;
  613. color: #333;
  614. margin-bottom: 20rpx;
  615. }
  616. .family-actions {
  617. display: flex;
  618. flex-direction: column;
  619. gap: 20rpx;
  620. margin-top: 30rpx;
  621. }
  622. .btn {
  623. border-radius: 40rpx;
  624. font-size: 30rpx;
  625. }
  626. .btn-primary {
  627. background: #4CAF50;
  628. color: #fff;
  629. }
  630. .btn-skip {
  631. background: #f5f5f5;
  632. color: #999;
  633. }
  634. </style>