create-child.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // 设置今天的日期作为选择器结�日� const today = new Date()
  96. this.todayDate = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`
  97. this.isEditMode = options.mode === 'edit'
  98. this.childId = options.childId || ''
  99. if (this.isEditMode && this.childId) {
  100. uni.setNavigationBarTitle({ title: '编辑孩�' })
  101. this.loadChildDetail()
  102. } else {
  103. uni.setNavigationBarTitle({ title: '添加孩�' })
  104. }
  105. },
  106. methods: {
  107. async loadChildDetail() {
  108. try {
  109. const res = await getChildren()
  110. const children = res.data || []
  111. const targetChild = children.find(child => String(child.id) === String(this.childId))
  112. if (!targetChild) {
  113. uni.showToast({ title: '未找到孩�信�, icon: 'none' })
  114. setTimeout(() => uni.navigateBack(), 1200)
  115. return
  116. }
  117. this.form.nickname = targetChild.nickname || ''
  118. this.form.birthday = targetChild.birthday || ''
  119. this.form.gender = targetChild.gender || ''
  120. this.form.age = targetChild.age != null ? String(targetChild.age) : ''
  121. this.form.phone = targetChild.phone || ''
  122. this.form.idCard = targetChild.idCard || ''
  123. this.form.penaltyEnabled = targetChild.penaltyEnabled != null ? targetChild.penaltyEnabled : 0
  124. } catch (e) {
  125. uni.showToast({ title: '加载孩�信�失败', icon: 'none' })
  126. }
  127. },
  128. onBirthdayChange(e) {
  129. this.form.birthday = e.detail.value
  130. },
  131. onGenderChange(e) {
  132. this.form.gender = this.genderOptions[e.detail.value].value
  133. },
  134. onPenaltyChange(e) {
  135. this.form.penaltyEnabled = e.detail.value ? 1 : 0
  136. },
  137. async submit() {
  138. // 必填校验
  139. if (!this.form.nickname || !this.form.nickname.trim()) {
  140. uni.showToast({ title: '请输入孩�姓�, icon: 'none' })
  141. return
  142. }
  143. if (!this.form.birthday) {
  144. uni.showToast({ title: '请选择出生日期', icon: 'none' })
  145. return
  146. }
  147. if (!this.form.gender) {
  148. uni.showToast({ title: '请选择性别', icon: 'none' })
  149. return
  150. }
  151. // 手机�格�校验(如果填写了)
  152. if (this.form.phone && !/^1[3-9]\d{9}$/.test(this.form.phone)) {
  153. uni.showToast({ title: '请输入正确的手机�, icon: 'none' })
  154. return
  155. }
  156. // 身份�格�校验(如果填写了)
  157. if (this.form.idCard && !/^\d{15}$|^\d{17}[\dXx]$/.test(this.form.idCard)) {
  158. uni.showToast({ title: '请输入正确的身份��', icon: 'none' })
  159. return
  160. }
  161. // 计算年龄
  162. const birth = new Date(this.form.birthday)
  163. const today = new Date()
  164. let age = today.getFullYear() - birth.getFullYear()
  165. const monthDiff = today.getMonth() - birth.getMonth()
  166. if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
  167. age--
  168. }
  169. try {
  170. this.submitting = true
  171. const payload = {
  172. nickname: this.form.nickname.trim(),
  173. birthday: this.form.birthday,
  174. gender: this.form.gender,
  175. age: age >= 0 ? age : 0,
  176. phone: this.form.phone || null,
  177. idCard: this.form.idCard || null,
  178. penaltyEnabled: this.form.penaltyEnabled
  179. }
  180. if (this.isEditMode) {
  181. await updateChild(this.childId, payload)
  182. } else {
  183. await createChild(payload)
  184. }
  185. uni.showToast({ title: this.isEditMode ? '修改�功' : '添加�功', icon: 'success' })
  186. setTimeout(() => uni.navigateBack(), 1500)
  187. } catch (e) {
  188. uni.showToast({ title: this.isEditMode ? '修改失败' : '添加失败', icon: 'none' })
  189. } finally {
  190. this.submitting = false
  191. }
  192. }
  193. }
  194. }
  195. </script>
  196. <style scoped>
  197. .container { padding: 30rpx; }
  198. .form-container { background: #fff; border-radius: 20rpx; padding: 30rpx; }
  199. .page-title { font-size: 34rpx; font-weight: bold; color: #333; margin-bottom: 30rpx; }
  200. .form-item { margin-bottom: 30rpx; }
  201. .form-item.required .label { font-weight: bold; }
  202. .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 10rpx; }
  203. .required-mark { color: #F97316; margin-right: 4rpx; }
  204. .input { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; }
  205. .picker { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #666; }
  206. .age-display { border: 1rpx solid #ddd; border-radius: 10rpx; padding: 20rpx; font-size: 28rpx; color: #999; background: #f9f9f9; }
  207. .hint { font-size: 24rpx; color: #999; margin-left: 20rpx; }
  208. .btn-primary { background: #F97316; color: #fff; border-radius: 20rpx; margin-top: 40rpx; }
  209. </style>