edit-child.vue 8.2 KB

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