user-edit.vue 13 KB

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