| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <template>
- <view class="container">
- <view class="form-container">
- <view class="page-title">编辑孩子信息</view>
- <!-- 姓名(必填) -->
- <view class="form-item required">
- <text class="label"><text class="required-mark">*</text>孩子姓名</text>
- <input class="input" v-model="form.nickname" placeholder="请输入孩子姓名" />
- </view>
- <!-- 出生日期(必填) -->
- <view class="form-item required">
- <text class="label"><text class="required-mark">*</text>出生日期</text>
- <picker mode="date" :value="form.birthday" @change="onBirthdayChange" :end="todayDate">
- <view class="picker">
- {{ form.birthday || '请选择出生日期' }}
- </view>
- </picker>
- </view>
- <!-- 性别(必填) -->
- <view class="form-item required">
- <text class="label"><text class="required-mark">*</text>性别</text>
- <picker :range="genderOptions" range-key="label" @change="onGenderChange">
- <view class="picker">
- {{ genderLabel || '请选择性别' }}
- </view>
- </picker>
- </view>
- <!-- 年龄(根据出生日期自动计算) -->
- <view class="form-item">
- <text class="label">年龄</text>
- <view class="age-display">{{ calculatedAge || '根据出生日期自动计算' }}</view>
- </view>
- <!-- 手机号(非必填) -->
- <view class="form-item">
- <text class="label">手机号(选填)</text>
- <input class="input" type="number" v-model="form.phone" placeholder="请输入手机号" maxlength="11" />
- </view>
- <!-- 身份证号(非必填) -->
- <view class="form-item">
- <text class="label">身份证号(选填)</text>
- <input class="input" v-model="form.idCard" placeholder="请输入身份证号" maxlength="18" />
- </view>
- <!-- 扣分开关 -->
- <view class="form-item">
- <text class="label">扣分开关</text>
- <switch :checked="form.penaltyEnabled === 1" @change="onPenaltyChange" color="#F97316" />
- <text class="hint">开启后,超时未完成任务将扣减积分</text>
- </view>
- <!-- 主题风格 -->
- <view class="form-item">
- <text class="label">主题风格</text>
- <picker :range="themes" range-key="label" @change="onThemeChange">
- <view class="picker">
- {{ currentThemeLabel || '请选择主题' }}
- </view>
- </picker>
- </view>
- <button class="btn-primary" :loading="submitting" @click="submit">保存修改</button>
- </view>
- </view>
- </template>
- <script>
- import { getChildren, updateChild } from '../../utils/api.js'
- export default {
- data() {
- return {
- childId: '',
- submitting: false,
- form: {
- nickname: '',
- birthday: '',
- gender: '',
- age: '',
- danLevel: '',
- phone: '',
- idCard: '',
- penaltyEnabled: 1,
- theme: 'default'
- },
- genderOptions: [
- { value: 'male', label: '男' },
- { value: 'female', label: '女' }
- ],
- themes: [
- { value: 'default', label: '默认' },
- { value: 'ocean', label: '海洋' },
- { value: 'forest', label: '森林' },
- { value: 'sunset', label: '日落' }
- ]
- }
- },
- computed: {
- currentThemeLabel() {
- const theme = this.themes.find(t => t.value === this.form.theme)
- return theme ? theme.label : '默认'
- },
- genderLabel() {
- const gender = this.genderOptions.find(g => g.value === this.form.gender)
- return gender ? gender.label : ''
- },
- todayDate() {
- return new Date().toISOString().split('T')[0]
- },
- calculatedAge() {
- if (!this.form.birthday) return ''
- const birth = new Date(this.form.birthday)
- const today = new Date()
- let age = today.getFullYear() - birth.getFullYear()
- const monthDiff = today.getMonth() - birth.getMonth()
- if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
- age--
- }
- return age > 0 ? age + '岁' : ''
- }
- },
- onLoad(options) {
- this.childId = options.childId || ''
- if (!this.childId) {
- uni.showToast({ title: '参数错误', icon: 'none' })
- setTimeout(() => uni.navigateBack(), 1500)
- return
- }
- this.loadChildData()
- },
- methods: {
- async loadChildData() {
- try {
- const res = await getChildren()
- const children = res.data || []
- const child = children.find(c => c.id == this.childId)
- if (child) {
- this.form = {
- nickname: child.nickname || '',
- birthday: child.birthday || '',
- gender: child.gender || '',
- age: child.age || '',
- danLevel: child.danLevel || '',
- phone: child.phone || '',
- idCard: child.idCard || '',
- penaltyEnabled: child.penaltyEnabled !== 0 ? 1 : 0,
- theme: child.theme || 'default'
- }
- }
- } catch (e) {
- console.error('加载孩子数据失败', e)
- uni.showToast({ title: '加载失败', icon: 'none' })
- }
- },
- onBirthdayChange(e) {
- this.form.birthday = e.detail.value
- // 根据出生日期自动计算年龄
- const birth = new Date(e.detail.value)
- const today = new Date()
- let age = today.getFullYear() - birth.getFullYear()
- const monthDiff = today.getMonth() - birth.getMonth()
- if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
- age--
- }
- this.form.age = age
- },
- onGenderChange(e) {
- this.form.gender = this.genderOptions[e.detail.value].value
- },
- onPenaltyChange(e) {
- this.form.penaltyEnabled = e.detail.value ? 1 : 0
- },
- onThemeChange(e) {
- this.form.theme = this.themes[e.detail.value].value
- },
- async submit() {
- if (!this.form.nickname) {
- uni.showToast({ title: '请输入孩子姓名', icon: 'none' })
- return
- }
- if (!this.form.birthday) {
- uni.showToast({ title: '请选择出生日期', icon: 'none' })
- return
- }
- if (!this.form.gender) {
- uni.showToast({ title: '请选择性别', icon: 'none' })
- return
- }
- this.submitting = true
- try {
- await updateChild(this.childId, {
- nickname: this.form.nickname,
- birthday: this.form.birthday,
- gender: this.form.gender,
- age: this.form.age,
- danLevel: this.form.danLevel,
- phone: this.form.phone,
- idCard: this.form.idCard,
- penaltyEnabled: this.form.penaltyEnabled,
- theme: this.form.theme
- })
- uni.showToast({ title: '保存成功', icon: 'success' })
- setTimeout(() => uni.navigateBack(), 1500)
- } catch (e) {
- console.error('保存失败', e)
- uni.showToast({ title: '保存失败', icon: 'none' })
- } finally {
- this.submitting = false
- }
- }
- }
- }
- </script>
- <style scoped>
- .container {
- padding: 30rpx;
- }
- .form-container {
- background: #fff;
- border-radius: 20rpx;
- padding: 30rpx;
- }
- .page-title {
- font-size: 36rpx;
- font-weight: bold;
- text-align: center;
- margin-bottom: 40rpx;
- }
- .form-item {
- margin-bottom: 30rpx;
- }
- .form-item.required .label {
- font-weight: bold;
- }
- .required-mark {
- color: #F97316;
- margin-right: 8rpx;
- }
- .label {
- display: block;
- font-size: 28rpx;
- color: #333;
- margin-bottom: 10rpx;
- }
- .input, .picker {
- border: 1rpx solid #ddd;
- border-radius: 10rpx;
- padding: 20rpx;
- font-size: 28rpx;
- }
- .age-display {
- font-size: 28rpx;
- color: #666;
- padding: 20rpx;
- }
- .hint {
- display: block;
- font-size: 24rpx;
- color: #999;
- margin-top: 10rpx;
- }
- .btn-primary {
- background: linear-gradient(135deg, #F97316, #FB923C);
- color: #fff;
- margin-top: 40rpx;
- }
- </style>
|