create-child.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <view class="container">
  3. <view class="form-container">
  4. <view class="page-title">{{ isEditMode ? '编辑孩子信息' : '添加孩子' }}</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. <!-- 扣分开关 -->
  44. <view class="form-item">
  45. <text class="label">扣分开关</text>
  46. <switch :checked="form.penaltyEnabled === 1" @change="onPenaltyChange" color="#F97316" />
  47. <text class="hint">开启后,超时未完成任务将扣减积分</text>
  48. </view>
  49. <button class="btn-primary" :loading="submitting" @click="submit">{{ isEditMode ? '保存修改' : '保存' }}</button>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import { createChild, getChildren, updateChild } from '../../utils/api.js'
  55. export default {
  56. data() {
  57. return {
  58. childId: '',
  59. isEditMode: false,
  60. submitting: false,
  61. form: {
  62. nickname: '',
  63. birthday: '',
  64. gender: '',
  65. age: '',
  66. phone: '',
  67. idCard: '',
  68. penaltyEnabled: 0
  69. },
  70. genderOptions: [
  71. { value: 'male', label: '男' },
  72. { value: 'female', label: '女' }
  73. ],
  74. todayDate: ''
  75. }
  76. },
  77. computed: {
  78. genderLabel() {
  79. const gender = this.genderOptions.find(g => g.value === this.form.gender)
  80. return gender ? gender.label : ''
  81. },
  82. calculatedAge() {
  83. if (!this.form.birthday) return ''
  84. const birth = new Date(this.form.birthday)
  85. const today = new Date()
  86. let age = today.getFullYear() - birth.getFullYear()
  87. const monthDiff = today.getMonth() - birth.getMonth()
  88. if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
  89. age--
  90. }
  91. return age >= 0 ? age + '岁' : ''
  92. }
  93. },
  94. onLoad(options) {
  95. // 设置今天的日期作为选择器结束日期
  96. const today = new Date()
  97. this.todayDate = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`
  98. this.isEditMode = options.mode === 'edit'
  99. this.childId = options.childId || ''
  100. if (this.isEditMode && this.childId) {
  101. uni.setNavigationBarTitle({ title: '编辑孩子' })
  102. this.loadChildDetail()
  103. } else {
  104. uni.setNavigationBarTitle({ title: '添加孩子' })
  105. }
  106. },
  107. methods: {
  108. async loadChildDetail() {
  109. try {
  110. const res = await getChildren()
  111. const children = res.data || []
  112. const targetChild = children.find(child => String(child.id) === String(this.childId))
  113. if (!targetChild) {
  114. uni.showToast({ title: '未找到孩子信息', icon: 'none' })
  115. setTimeout(() => uni.navigateBack(), 1200)
  116. return
  117. }
  118. this.form.nickname = targetChild.nickname || ''
  119. this.form.birthday = targetChild.birthday || ''
  120. this.form.gender = targetChild.gender || ''
  121. this.form.age = targetChild.age != null ? String(targetChild.age) : ''
  122. this.form.phone = targetChild.phone || ''
  123. this.form.idCard = targetChild.idCard || ''
  124. this.form.penaltyEnabled = targetChild.penaltyEnabled != null ? targetChild.penaltyEnabled : 0
  125. } catch (e) {
  126. uni.showToast({ title: '加载孩子信息失败', icon: 'none' })
  127. }
  128. },
  129. onBirthdayChange(e) {
  130. this.form.birthday = e.detail.value
  131. },
  132. onGenderChange(e) {
  133. this.form.gender = this.genderOptions[e.detail.value].value
  134. },
  135. onPenaltyChange(e) {
  136. this.form.penaltyEnabled = e.detail.value ? 1 : 0
  137. },
  138. async submit() {
  139. // 必填校验
  140. if (!this.form.nickname || !this.form.nickname.trim()) {
  141. uni.showToast({ title: '请输入孩子姓名', icon: 'none' })
  142. return
  143. }
  144. if (!this.form.birthday) {
  145. uni.showToast({ title: '请选择出生日期', icon: 'none' })
  146. return
  147. }
  148. if (!this.form.gender) {
  149. uni.showToast({ title: '请选择性别', icon: 'none' })
  150. return
  151. }
  152. // 手机号格式校验(如果填写了)
  153. if (this.form.phone && !/^1[3-9]\d{9}$/.test(this.form.phone)) {
  154. uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
  155. return
  156. }
  157. // 身份证格式校验(如果填写了)
  158. if (this.form.idCard && !/^\d{15}$|^\d{17}[\dXx]$/.test(this.form.idCard)) {
  159. uni.showToast({ title: '请输入正确的身份证号', icon: 'none' })
  160. return
  161. }
  162. // 计算年龄
  163. const birth = new Date(this.form.birthday)
  164. const today = new Date()
  165. let age = today.getFullYear() - birth.getFullYear()
  166. const monthDiff = today.getMonth() - birth.getMonth()
  167. if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
  168. age--
  169. }
  170. try {
  171. this.submitting = true
  172. const payload = {
  173. nickname: this.form.nickname.trim(),
  174. birthday: this.form.birthday,
  175. gender: this.form.gender,
  176. age: age >= 0 ? age : 0,
  177. phone: this.form.phone || null,
  178. idCard: this.form.idCard || null,
  179. penaltyEnabled: this.form.penaltyEnabled
  180. }
  181. if (this.isEditMode) {
  182. await updateChild(this.childId, payload)
  183. } else {
  184. await createChild(payload)
  185. }
  186. uni.showToast({ title: this.isEditMode ? '修改成功' : '添加成功', icon: 'success' })
  187. setTimeout(() => uni.navigateBack(), 1500)
  188. } catch (e) {
  189. uni.showToast({ title: this.isEditMode ? '修改失败' : '添加失败', icon: 'none' })
  190. } finally {
  191. this.submitting = false
  192. }
  193. }
  194. }
  195. }
  196. </script>
  197. <style scoped>
  198. .container { padding: 30rpx; }
  199. .form-container { background: #fff; border-radius: 20rpx; padding: 30rpx; }
  200. .page-title { font-size: 34rpx; font-weight: bold; color: #333; margin-bottom: 30rpx; }
  201. .form-item { margin-bottom: 30rpx; }
  202. .form-item.required .label { font-weight: bold; }
  203. .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 10rpx; }
  204. .required-mark { color: #F97316; margin-right: 4rpx; }
  205. .input { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; }
  206. .picker { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #666; }
  207. .age-display { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #999; background: #f9f9f9; }
  208. .hint { font-size: 24rpx; color: #999; margin-left: 20rpx; }
  209. .btn-primary { background: #F97316; color: #fff; border-radius: 20rpx; margin-top: 40rpx; }
  210. </style>