edit-child.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. <!-- 扣分开关 -->
  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. <!-- 主题风格 -->
  50. <view class="form-item">
  51. <text class="label">主题风格</text>
  52. <picker :range="themes" range-key="label" @change="onThemeChange">
  53. <view class="picker">
  54. {{ currentThemeLabel || '请选择主题' }}
  55. </view>
  56. </picker>
  57. </view>
  58. <button class="btn-primary" :loading="submitting" @click="submit">保存修改</button>
  59. </view>
  60. </view>
  61. </template>
  62. <script>
  63. import { getChildren, updateChild } from '../../utils/api.js'
  64. export default {
  65. data() {
  66. return {
  67. childId: '',
  68. submitting: false,
  69. form: {
  70. nickname: '',
  71. birthday: '',
  72. gender: '',
  73. age: '',
  74. danLevel: '',
  75. phone: '',
  76. idCard: '',
  77. penaltyEnabled: 1,
  78. theme: 'default'
  79. },
  80. genderOptions: [
  81. { value: 'male', label: '男' },
  82. { value: 'female', label: '女' }
  83. ],
  84. themes: [
  85. { value: 'default', label: '默认' },
  86. { value: 'ocean', label: '海洋' },
  87. { value: 'forest', label: '森林' },
  88. { value: 'sunset', label: '日落' }
  89. ]
  90. }
  91. },
  92. computed: {
  93. currentThemeLabel() {
  94. const theme = this.themes.find(t => t.value === this.form.theme)
  95. return theme ? theme.label : '默认'
  96. },
  97. genderLabel() {
  98. const gender = this.genderOptions.find(g => g.value === this.form.gender)
  99. return gender ? gender.label : ''
  100. },
  101. todayDate() {
  102. return new Date().toISOString().split('T')[0]
  103. },
  104. calculatedAge() {
  105. if (!this.form.birthday) return ''
  106. const birth = new Date(this.form.birthday)
  107. const today = new Date()
  108. let age = today.getFullYear() - birth.getFullYear()
  109. const monthDiff = today.getMonth() - birth.getMonth()
  110. if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
  111. age--
  112. }
  113. return age > 0 ? age + '岁' : ''
  114. }
  115. },
  116. onLoad(options) {
  117. this.childId = options.childId || ''
  118. if (!this.childId) {
  119. uni.showToast({ title: '参数错误', icon: 'none' })
  120. setTimeout(() => uni.navigateBack(), 1500)
  121. return
  122. }
  123. this.loadChildData()
  124. },
  125. methods: {
  126. async loadChildData() {
  127. try {
  128. const res = await getChildren()
  129. const children = res.data || []
  130. const child = children.find(c => c.id == this.childId)
  131. if (child) {
  132. this.form = {
  133. nickname: child.nickname || '',
  134. birthday: child.birthday || '',
  135. gender: child.gender || '',
  136. age: child.age || '',
  137. danLevel: child.danLevel || '',
  138. phone: child.phone || '',
  139. idCard: child.idCard || '',
  140. penaltyEnabled: child.penaltyEnabled !== 0 ? 1 : 0,
  141. theme: child.theme || 'default'
  142. }
  143. }
  144. } catch (e) {
  145. console.error('加载孩子数据失败', e)
  146. uni.showToast({ title: '加载失败', icon: 'none' })
  147. }
  148. },
  149. onBirthdayChange(e) {
  150. this.form.birthday = e.detail.value
  151. // 根据出生日期自动计算年龄
  152. const birth = new Date(e.detail.value)
  153. const today = new Date()
  154. let age = today.getFullYear() - birth.getFullYear()
  155. const monthDiff = today.getMonth() - birth.getMonth()
  156. if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
  157. age--
  158. }
  159. this.form.age = age
  160. },
  161. onGenderChange(e) {
  162. this.form.gender = this.genderOptions[e.detail.value].value
  163. },
  164. onPenaltyChange(e) {
  165. this.form.penaltyEnabled = e.detail.value ? 1 : 0
  166. },
  167. onThemeChange(e) {
  168. this.form.theme = this.themes[e.detail.value].value
  169. },
  170. async submit() {
  171. if (!this.form.nickname) {
  172. uni.showToast({ title: '请输入孩子姓名', icon: 'none' })
  173. return
  174. }
  175. if (!this.form.birthday) {
  176. uni.showToast({ title: '请选择出生日期', icon: 'none' })
  177. return
  178. }
  179. if (!this.form.gender) {
  180. uni.showToast({ title: '请选择性别', icon: 'none' })
  181. return
  182. }
  183. this.submitting = true
  184. try {
  185. await updateChild(this.childId, {
  186. nickname: this.form.nickname,
  187. birthday: this.form.birthday,
  188. gender: this.form.gender,
  189. age: this.form.age,
  190. danLevel: this.form.danLevel,
  191. phone: this.form.phone,
  192. idCard: this.form.idCard,
  193. penaltyEnabled: this.form.penaltyEnabled,
  194. theme: this.form.theme
  195. })
  196. uni.showToast({ title: '保存成功', icon: 'success' })
  197. setTimeout(() => uni.navigateBack(), 1500)
  198. } catch (e) {
  199. console.error('保存失败', e)
  200. uni.showToast({ title: '保存失败', icon: 'none' })
  201. } finally {
  202. this.submitting = false
  203. }
  204. }
  205. }
  206. }
  207. </script>
  208. <style scoped>
  209. .container {
  210. padding: 30rpx;
  211. }
  212. .form-container {
  213. background: #fff;
  214. border-radius: 20rpx;
  215. padding: 30rpx;
  216. }
  217. .page-title {
  218. font-size: 36rpx;
  219. font-weight: bold;
  220. text-align: center;
  221. margin-bottom: 40rpx;
  222. }
  223. .form-item {
  224. margin-bottom: 30rpx;
  225. }
  226. .form-item.required .label {
  227. font-weight: bold;
  228. }
  229. .required-mark {
  230. color: #F97316;
  231. margin-right: 8rpx;
  232. }
  233. .label {
  234. display: block;
  235. font-size: 28rpx;
  236. color: #333;
  237. margin-bottom: 10rpx;
  238. }
  239. .input, .picker {
  240. border: 1rpx solid #ddd;
  241. border-radius: 10rpx;
  242. padding: 20rpx;
  243. font-size: 28rpx;
  244. }
  245. .age-display {
  246. font-size: 28rpx;
  247. color: #666;
  248. padding: 20rpx;
  249. }
  250. .hint {
  251. display: block;
  252. font-size: 24rpx;
  253. color: #999;
  254. margin-top: 10rpx;
  255. }
  256. .btn-primary {
  257. background: linear-gradient(135deg, #F97316, #FB923C);
  258. color: #fff;
  259. margin-top: 40rpx;
  260. }
  261. </style>