edit-child.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <view class="container">
  3. <view class="form-container">
  4. <view class="page-title">编辑孩子信息</view>
  5. <!-- 姓名(必填) -->
  6. <view class="form-item required">
  7. <text class="label"><text class="required-mark">*</text>孩子姓名</text>
  8. <input class="input" v-model="form.nickname" placeholder="请输入孩子姓名" />
  9. </view>
  10. <!-- 出生日期(必填) -->
  11. <view class="form-item required">
  12. <text class="label"><text class="required-mark">*</text>出生日期</text>
  13. <picker mode="date" :value="form.birthday" @change="onBirthdayChange" :end="todayDate">
  14. <view class="picker">
  15. {{ form.birthday || '请选择出生日期' }}
  16. </view>
  17. </picker>
  18. </view>
  19. <!-- 性别(必填) -->
  20. <view class="form-item required">
  21. <text class="label"><text class="required-mark">*</text>性别</text>
  22. <picker :range="genderOptions" range-key="label" @change="onGenderChange">
  23. <view class="picker">
  24. {{ genderLabel || '请选择性别' }}
  25. </view>
  26. </picker>
  27. </view>
  28. <!-- 年龄(根据出生日期自动计算) -->
  29. <view class="form-item">
  30. <text class="label">年龄</text>
  31. <view class="age-display">{{ calculatedAge || '根据出生日期自动计算' }}</view>
  32. </view>
  33. <!-- 手机号(非必填) -->
  34. <view class="form-item">
  35. <text class="label">手机号(选填)</text>
  36. <input class="input" type="number" v-model="form.phone" placeholder="请输入手机号" maxlength="11" />
  37. </view>
  38. <!-- 身份证号(非必填) -->
  39. <view class="form-item">
  40. <text class="label">身份证号(选填)</text>
  41. <input class="input" v-model="form.idCard" placeholder="请输入身份证号" maxlength="18" />
  42. </view>
  43. <!-- 扣分开关(仅 children 系统支持) -->
  44. <view class="form-item" v-if="!isFamilyMember">
  45. <text class="label">扣分开关</text>
  46. <switch :checked="form.penaltyEnabled === 1" @change="onPenaltyChange" color="#F97316" />
  47. <text class="hint">开启后,超时未完成任务将扣减积分</text>
  48. </view>
  49. <!-- 对家庭成员可见(仅 children 系统支持) -->
  50. <view class="form-item" v-if="!isFamilyMember">
  51. <text class="label">对家庭成员可见</text>
  52. <switch :checked="form.showToFamily !== 0" @change="onShowToFamilyChange" color="#5B9BD5" />
  53. <text class="hint">关闭后,其他家庭成员将看不到此孩子</text>
  54. </view>
  55. <!-- 主题风格(仅 children 系统支持) -->
  56. <view class="form-item" v-if="!isFamilyMember">
  57. <text class="label">主题风格</text>
  58. <picker :range="themes" range-key="label" @change="onThemeChange">
  59. <view class="picker">
  60. {{ currentThemeLabel || '请选择主题' }}
  61. </view>
  62. </picker>
  63. </view>
  64. <button class="btn-primary" :loading="submitting" @click="submit">保存修改</button>
  65. </view>
  66. </view>
  67. </template>
  68. <script>
  69. import { getChildren, updateChild, updateFamilyMember } from '../../utils/api.js'
  70. export default {
  71. data() {
  72. return {
  73. childId: '',
  74. memberId: '',
  75. isFamilyMember: false,
  76. submitting: false,
  77. form: {
  78. nickname: '',
  79. birthday: '',
  80. gender: '',
  81. age: '',
  82. phone: '',
  83. idCard: '',
  84. penaltyEnabled: 1,
  85. showToFamily: 1,
  86. theme: 'default'
  87. },
  88. genderOptions: [
  89. { value: 'male', label: '男' },
  90. { value: 'female', label: '女' }
  91. ],
  92. themes: [
  93. { value: 'default', label: '默认' },
  94. { value: 'ocean', label: '海洋' },
  95. { value: 'forest', label: '森林' },
  96. { value: 'sunset', label: '日落' }
  97. ]
  98. }
  99. },
  100. computed: {
  101. currentThemeLabel() {
  102. const theme = this.themes.find(t => t.value === this.form.theme)
  103. return theme ? theme.label : '默认'
  104. },
  105. genderLabel() {
  106. const gender = this.genderOptions.find(g => g.value === this.form.gender)
  107. return gender ? gender.label : ''
  108. },
  109. todayDate() {
  110. return new Date().toISOString().split('T')[0]
  111. },
  112. calculatedAge() {
  113. if (!this.form.birthday) return ''
  114. const birth = new Date(this.form.birthday)
  115. const today = new Date()
  116. let age = today.getFullYear() - birth.getFullYear()
  117. const monthDiff = today.getMonth() - birth.getMonth()
  118. if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
  119. age--
  120. }
  121. return age > 0 ? age + '岁' : ''
  122. }
  123. },
  124. onLoad(options) {
  125. this.childId = options.childId || ''
  126. this.memberId = options.memberId || ''
  127. if (!this.childId && !this.memberId) {
  128. uni.showToast({ title: '参数错误', icon: 'none' })
  129. setTimeout(() => uni.navigateBack(), 1500)
  130. return
  131. }
  132. // 优先按 childId 从 children 系统加载,找不到则按 memberId 从 family-member 系统加载
  133. if (this.childId) {
  134. this.loadChildData()
  135. } else {
  136. this.isFamilyMember = true
  137. this.loadFamilyMemberData(options)
  138. }
  139. },
  140. methods: {
  141. async loadChildData() {
  142. try {
  143. const res = await getChildren()
  144. const children = res.data || []
  145. const child = children.find(c => c.id == this.childId)
  146. if (child) {
  147. this.form = {
  148. nickname: child.nickname || '',
  149. birthday: child.birthday || '',
  150. gender: child.gender || '',
  151. age: child.age || '',
  152. phone: child.phone || '',
  153. idCard: child.idCard || '',
  154. penaltyEnabled: child.penaltyEnabled !== 0 ? 1 : 0,
  155. showToFamily: child.showToFamily !== 0 ? 1 : 0,
  156. theme: child.theme || 'default'
  157. }
  158. } else {
  159. // children 系统找不到,尝试作为 family member 加载
  160. this.isFamilyMember = true
  161. this.memberId = this.childId
  162. this.childId = ''
  163. }
  164. } catch (e) {
  165. console.error('加载孩子数据失败', e)
  166. uni.showToast({ title: '加载失败', icon: 'none' })
  167. }
  168. },
  169. loadFamilyMemberData(options) {
  170. // 从 URL 参数中填充基本信息(family member 系统字段有限)
  171. this.form.nickname = options.nickname ? decodeURIComponent(options.nickname) : ''
  172. this.form.gender = options.gender || ''
  173. this.form.phone = options.phone || ''
  174. this.form.birthday = options.birthday || ''
  175. },
  176. onBirthdayChange(e) {
  177. this.form.birthday = e.detail.value
  178. // 根据出生日期自动计算年龄
  179. const birth = new Date(e.detail.value)
  180. const today = new Date()
  181. let age = today.getFullYear() - birth.getFullYear()
  182. const monthDiff = today.getMonth() - birth.getMonth()
  183. if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
  184. age--
  185. }
  186. this.form.age = age
  187. },
  188. onGenderChange(e) {
  189. this.form.gender = this.genderOptions[e.detail.value].value
  190. },
  191. onPenaltyChange(e) {
  192. this.form.penaltyEnabled = e.detail.value ? 1 : 0
  193. },
  194. onShowToFamilyChange(e) {
  195. this.form.showToFamily = e.detail.value ? 1 : 0
  196. },
  197. onThemeChange(e) {
  198. this.form.theme = this.themes[e.detail.value].value
  199. },
  200. async submit() {
  201. if (!this.form.nickname) {
  202. uni.showToast({ title: '请输入孩子姓名', icon: 'none' })
  203. return
  204. }
  205. if (!this.form.birthday) {
  206. uni.showToast({ title: '请选择出生日期', icon: 'none' })
  207. return
  208. }
  209. if (!this.form.gender) {
  210. uni.showToast({ title: '请选择性别', icon: 'none' })
  211. return
  212. }
  213. this.submitting = true
  214. try {
  215. if (this.isFamilyMember) {
  216. await updateFamilyMember({
  217. memberId: this.memberId,
  218. nickname: this.form.nickname,
  219. birthday: this.form.birthday,
  220. gender: this.form.gender,
  221. phone: this.form.phone
  222. })
  223. } else {
  224. await updateChild(this.childId, {
  225. nickname: this.form.nickname,
  226. birthday: this.form.birthday,
  227. gender: this.form.gender,
  228. age: this.form.age,
  229. phone: this.form.phone,
  230. idCard: this.form.idCard,
  231. penaltyEnabled: this.form.penaltyEnabled,
  232. showToFamily: this.form.showToFamily,
  233. theme: this.form.theme
  234. })
  235. }
  236. uni.showToast({ title: '保存成功', icon: 'success' })
  237. setTimeout(() => uni.navigateBack(), 1500)
  238. } catch (e) {
  239. console.error('保存失败', e)
  240. uni.showToast({ title: '保存失败', icon: 'none' })
  241. } finally {
  242. this.submitting = false
  243. }
  244. }
  245. }
  246. }
  247. </script>
  248. <style scoped>
  249. .container {
  250. padding: 30rpx;
  251. }
  252. .form-container {
  253. background: #fff;
  254. border-radius: 20rpx;
  255. padding: 30rpx;
  256. }
  257. .page-title {
  258. font-size: 36rpx;
  259. font-weight: bold;
  260. text-align: center;
  261. margin-bottom: 40rpx;
  262. }
  263. .form-item {
  264. margin-bottom: 30rpx;
  265. }
  266. .form-item.required .label {
  267. font-weight: bold;
  268. }
  269. .required-mark {
  270. color: #F97316;
  271. margin-right: 8rpx;
  272. }
  273. .label {
  274. display: block;
  275. font-size: 28rpx;
  276. color: #333;
  277. margin-bottom: 10rpx;
  278. }
  279. .input, .picker {
  280. border: 1rpx solid #ddd;
  281. border-radius: 10rpx;
  282. padding: 20rpx;
  283. font-size: 28rpx;
  284. }
  285. .age-display {
  286. font-size: 28rpx;
  287. color: #666;
  288. padding: 20rpx;
  289. }
  290. .hint {
  291. display: block;
  292. font-size: 24rpx;
  293. color: #999;
  294. margin-top: 10rpx;
  295. }
  296. .btn-primary {
  297. background: linear-gradient(135deg, #F97316, #FB923C);
  298. color: #fff;
  299. margin-top: 40rpx;
  300. }
  301. </style>