user-edit.vue 15 KB

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