edit-child.vue 8.3 KB

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