user-edit.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <template>
  2. <view class="user-edit-container">
  3. <view class="header">
  4. <text class="title">完善个人信息</text>
  5. <text class="subtitle">以下信息可帮助您更好地使用服务</text>
  6. </view>
  7. <view class="form-section">
  8. <!-- 头像 -->
  9. <view class="form-item avatar-item">
  10. <text class="label">头像</text>
  11. <view class="avatar-upload" @click="chooseAvatar">
  12. <image v-if="form.avatar" :src="form.avatar" class="avatar-preview"></image>
  13. <view v-else class="avatar-placeholder">
  14. <text class="iconfont icon-plus">+</text>
  15. </view>
  16. </view>
  17. </view>
  18. <!-- 昵称 -->
  19. <view class="form-item">
  20. <text class="label">昵称 <text class="required">*</text></text>
  21. <input type="text" v-model="form.nickname" placeholder="请输入昵称" class="input" maxlength="20" />
  22. </view>
  23. <!-- 真实姓名 -->
  24. <view class="form-item">
  25. <text class="label">真实姓名</text>
  26. <input type="text" v-model="form.realName" placeholder="选填,用于家庭管理" class="input" maxlength="20" />
  27. </view>
  28. <!-- 角色选择 -->
  29. <view class="form-item">
  30. <text class="label">我是</text>
  31. <radio-group class="role-select" @change="onRoleChange">
  32. <label class="role-option">
  33. <radio value="parent" :checked="form.role === 'parent'" />
  34. <text>家长</text>
  35. </label>
  36. <label class="role-option">
  37. <radio value="child" :checked="form.role === 'child'" />
  38. <text>孩子</text>
  39. </label>
  40. </radio-group>
  41. </view>
  42. <!-- 性别选择 -->
  43. <view class="form-item">
  44. <text class="label">性别</text>
  45. <radio-group class="role-select" @change="onGenderChange">
  46. <label class="role-option">
  47. <radio value="male" :checked="form.gender === 'male'" />
  48. <text>男</text>
  49. </label>
  50. <label class="role-option">
  51. <radio value="female" :checked="form.gender === 'female'" />
  52. <text>女</text>
  53. </label>
  54. </radio-group>
  55. </view>
  56. <!-- 身份证号 -->
  57. <view class="form-item">
  58. <text class="label">身份证号</text>
  59. <input type="idcard" v-model="form.idCard" placeholder="选填,填写后自动生成生日" class="input" maxlength="18" @input="onIdCardInput" />
  60. </view>
  61. <!-- 生日(可选,如果填了身份证则自动填充) -->
  62. <view class="form-item">
  63. <text class="label">生日</text>
  64. <picker mode="date" :value="form.birthday" @change="onBirthdayChange">
  65. <view class="picker-input">{{ form.birthday || '请选择生日' }}</view>
  66. </picker>
  67. </view>
  68. </view>
  69. <view class="family-section">
  70. <view class="section-title">家庭绑定</view>
  71. <!-- 邀请码输入 -->
  72. <view class="form-item">
  73. <text class="label">家庭邀请码</text>
  74. <input type="text" v-model="form.inviteCode" placeholder="选填,如有邀请码请输入" class="input" maxlength="8" />
  75. </view>
  76. <view class="family-actions">
  77. <button v-if="form.inviteCode" class="btn btn-primary" @click="joinFamilyAction" :loading="loading">
  78. 加入家庭
  79. </button>
  80. <button v-else class="btn btn-primary" @click="createFamilyAction" :loading="loading">
  81. 创建新家庭
  82. </button>
  83. <button class="btn btn-skip" @click="skip">
  84. 跳过
  85. </button>
  86. </view>
  87. </view>
  88. </view>
  89. </template>
  90. <script>
  91. import { updateUserInfo, joinFamily, createFamily } from '../../utils/api.js'
  92. export default {
  93. data() {
  94. return {
  95. form: {
  96. avatar: '',
  97. nickname: '',
  98. realName: '',
  99. role: 'parent',
  100. gender: '',
  101. idCard: '',
  102. birthday: '',
  103. inviteCode: ''
  104. },
  105. loading: false,
  106. token: '',
  107. userId: null
  108. }
  109. },
  110. onLoad(options) {
  111. // 从登录页面传递过来的参数
  112. this.token = options.token || ''
  113. this.userId = options.userId || null
  114. // 从本地存储获取用户信息
  115. const userInfo = uni.getStorageSync('userInfo')
  116. if (userInfo) {
  117. this.form.nickname = userInfo.nickname || ''
  118. this.form.avatar = userInfo.avatar || ''
  119. }
  120. },
  121. methods: {
  122. // 选择头像
  123. chooseAvatar() {
  124. uni.chooseImage({
  125. count: 1,
  126. sizeType: ['compressed'],
  127. sourceType: ['album', 'camera'],
  128. success: (res) => {
  129. this.form.avatar = res.tempFilePaths[0]
  130. }
  131. })
  132. },
  133. // 角色选择
  134. onRoleChange(e) {
  135. this.form.role = e.detail.value
  136. },
  137. // 性别选择
  138. onGenderChange(e) {
  139. this.form.gender = e.detail.value
  140. },
  141. // 生日选择
  142. onBirthdayChange(e) {
  143. this.form.birthday = e.detail.value
  144. },
  145. // 身份证输入 - 自动提取生日
  146. onIdCardInput(e) {
  147. const idCard = e.detail.value
  148. if (idCard.length === 15) {
  149. // 15位身份证:1985年07月08日 -> 19850708
  150. const birthYear = '19' + idCard.substring(6, 8)
  151. const birthMonth = idCard.substring(8, 10)
  152. const birthDay = idCard.substring(10, 12)
  153. this.form.birthday = `${birthYear}-${birthMonth}-${birthDay}`
  154. } else if (idCard.length === 18) {
  155. // 18位身份证:1985年07月08日 -> 19850708
  156. const birthYear = idCard.substring(6, 10)
  157. const birthMonth = idCard.substring(10, 12)
  158. const birthDay = idCard.substring(12, 14)
  159. this.form.birthday = `${birthYear}-${birthMonth}-${birthDay}`
  160. }
  161. },
  162. // 加入现有家庭
  163. async joinFamilyAction() {
  164. if (!this.form.inviteCode) {
  165. uni.showToast({ title: '请输入邀请码', icon: 'none' })
  166. return
  167. }
  168. await this.saveUserInfo('join')
  169. },
  170. // 创建新家庭
  171. async createFamilyAction() {
  172. await this.saveUserInfo('create')
  173. },
  174. // 跳过
  175. async skip() {
  176. await this.saveUserInfo('skip')
  177. },
  178. // 保存用户信息
  179. async saveUserInfo(action) {
  180. if (!this.form.nickname) {
  181. uni.showToast({ title: '请输入昵称', icon: 'none' })
  182. return
  183. }
  184. this.loading = true
  185. try {
  186. // 1. 更新用户信息(包括新字段:role, gender, birthday, idCard)
  187. await updateUserInfo({
  188. nickname: this.form.nickname,
  189. avatar: this.form.avatar,
  190. realName: this.form.realName,
  191. role: this.form.role,
  192. gender: this.form.gender,
  193. birthday: this.form.birthday,
  194. idCard: this.form.idCard
  195. })
  196. // 2. 根据 action 处理家庭绑定
  197. if (action === 'join' && this.form.inviteCode) {
  198. await joinFamily(this.form.inviteCode)
  199. } else if (action === 'create') {
  200. await createFamily('我的家庭')
  201. }
  202. // action === 'skip' 时不做处理,使用自动创建的默认家庭
  203. uni.showToast({ title: '保存成功', icon: 'success' })
  204. // 更新本地存储的用户信息
  205. uni.setStorageSync('userInfo', {
  206. nickname: this.form.nickname,
  207. avatar: this.form.avatar
  208. })
  209. uni.setStorageSync('role', this.form.role)
  210. // 跳转到首页
  211. setTimeout(() => {
  212. // 根据角色跳转到对应的首页
  213. const role = uni.getStorageSync('role') || 'parent'
  214. if (role === 'parent') {
  215. uni.reLaunch({
  216. url: '/pages/index/parent-index'
  217. })
  218. } else {
  219. uni.reLaunch({
  220. url: '/pages/index/child-index'
  221. })
  222. }
  223. }, 1000)
  224. } catch (error) {
  225. uni.showToast({ title: error.message || '保存失败', icon: 'none' })
  226. } finally {
  227. this.loading = false
  228. }
  229. }
  230. }
  231. }
  232. </script>
  233. <style lang="scss" scoped>
  234. .user-edit-container {
  235. min-height: 100vh;
  236. background: #f8f8f8;
  237. padding: 40rpx;
  238. }
  239. .header {
  240. text-align: center;
  241. margin-bottom: 60rpx;
  242. .title {
  243. display: block;
  244. font-size: 48rpx;
  245. font-weight: bold;
  246. color: #333;
  247. margin-bottom: 20rpx;
  248. }
  249. .subtitle {
  250. display: block;
  251. font-size: 28rpx;
  252. color: #999;
  253. }
  254. }
  255. .form-section {
  256. background: #fff;
  257. border-radius: 20rpx;
  258. padding: 40rpx;
  259. margin-bottom: 40rpx;
  260. }
  261. .form-item {
  262. margin-bottom: 40rpx;
  263. &:last-child {
  264. margin-bottom: 0;
  265. }
  266. .label {
  267. display: block;
  268. font-size: 28rpx;
  269. color: #333;
  270. margin-bottom: 20rpx;
  271. .required {
  272. color: #ff6b6b;
  273. }
  274. }
  275. .input {
  276. width: 100%;
  277. height: 80rpx;
  278. background: #f8f8f8;
  279. border-radius: 12rpx;
  280. padding: 0 30rpx;
  281. font-size: 28rpx;
  282. }
  283. .picker-input {
  284. width: 100%;
  285. height: 80rpx;
  286. background: #f8f8f8;
  287. border-radius: 12rpx;
  288. padding: 0 30rpx;
  289. font-size: 28rpx;
  290. line-height: 80rpx;
  291. color: #333;
  292. }
  293. }
  294. .role-select {
  295. display: flex;
  296. gap: 40rpx;
  297. padding: 10rpx 0;
  298. }
  299. .role-option {
  300. display: flex;
  301. align-items: center;
  302. gap: 10rpx;
  303. }
  304. .avatar-item {
  305. display: flex;
  306. align-items: center;
  307. justify-content: space-between;
  308. .avatar-upload {
  309. width: 120rpx;
  310. height: 120rpx;
  311. border-radius: 50%;
  312. overflow: hidden;
  313. background: #f8f8f8;
  314. display: flex;
  315. align-items: center;
  316. justify-content: center;
  317. .avatar-preview {
  318. width: 100%;
  319. height: 100%;
  320. }
  321. .avatar-placeholder {
  322. font-size: 48rpx;
  323. color: #ccc;
  324. }
  325. }
  326. }
  327. .family-section {
  328. background: #fff;
  329. border-radius: 20rpx;
  330. padding: 40rpx;
  331. .section-title {
  332. font-size: 32rpx;
  333. font-weight: bold;
  334. color: #333;
  335. margin-bottom: 30rpx;
  336. }
  337. }
  338. .family-actions {
  339. margin-top: 40rpx;
  340. .btn {
  341. width: 100%;
  342. height: 88rpx;
  343. border-radius: 44rpx;
  344. font-size: 32rpx;
  345. margin-bottom: 20rpx;
  346. }
  347. .btn-primary {
  348. background: #ff6b6b;
  349. color: #fff;
  350. }
  351. .btn-skip {
  352. background: #f8f8f8;
  353. color: #999;
  354. }
  355. }
  356. </style>