diff --git a/admin/src/app/lang/zh-cn/venue.venue.json b/admin/src/app/lang/zh-cn/venue.venue.json index 7a139265..c84e03d5 100644 --- a/admin/src/app/lang/zh-cn/venue.venue.json +++ b/admin/src/app/lang/zh-cn/venue.venue.json @@ -9,6 +9,8 @@ "availabilityStatusPlaceholder": "请输入场地可用状态", "timeRangeType": "时间范围类型", "timeRangeTypePlaceholder": "请输入场地可用时间范围类型", + "timeRange": "时间范围", + "allTimeAvailable": "全天可用提示", "fixedTimeRanges": "时间范围", "fixedTimeRangesPlaceholder": "请输入时间范围", "createdAt": "创建时间", diff --git a/admin/src/app/views/venue/components/venue-edit.vue b/admin/src/app/views/venue/components/venue-edit.vue index 9ee0105f..8a57913c 100644 --- a/admin/src/app/views/venue/components/venue-edit.vue +++ b/admin/src/app/views/venue/components/venue-edit.vue @@ -217,7 +217,7 @@ const initialFormData = { campus_id: '', venue_name: '', capacity: '', - availability_status: '1', + availability_status: '', // 改为空字符串,避免与字典加载冲突 time_range_type: '', time_range_start: '', time_range_end: '', @@ -483,31 +483,41 @@ const confirm = async (formEl: FormInstance | undefined) => { // 获取字典数据 let availability_statusList = ref([]) -const availability_statusDictList = async () => { - availability_statusList.value = await ( - await useDictionary('SiteStatus') - ).data.dictionary -} -availability_statusDictList() -watch( - () => availability_statusList.value, - () => { - formData.availability_status = availability_statusList.value[0].value - } -) let time_range_typeList = ref([]) -const time_range_typeDictList = async () => { - time_range_typeList.value = await ( - await useDictionary('ALLOTTED_TIME') - ).data.dictionary -} -time_range_typeDictList() -watch( - () => time_range_typeList.value, - () => { - formData.time_range_type = time_range_typeList.value[0].value +let dictLoaded = ref(false) // 添加加载状态标识 + +const loadDictionaries = async () => { + if (dictLoaded.value) return // 防止重复加载 + + try { + // 并行加载字典数据 + const [siteStatusRes, timeRangeRes] = await Promise.all([ + useDictionary('SiteStatus'), + useDictionary('ALLOTTED_TIME') + ]) + + availability_statusList.value = siteStatusRes.data.dictionary || [] + time_range_typeList.value = timeRangeRes.data.dictionary || [] + + // 只在数据加载完成、有数据且formData对应字段为空时设置默认值 + if (availability_statusList.value.length > 0 && !formData.availability_status) { + formData.availability_status = availability_statusList.value[0].value + } + + if (time_range_typeList.value.length > 0 && !formData.time_range_type) { + formData.time_range_type = time_range_typeList.value[0].value + } + + dictLoaded.value = true + } catch (error) { + console.error('加载字典数据失败:', error) + availability_statusList.value = [] + time_range_typeList.value = [] } -) +} + +// 初始化时加载字典数据 +loadDictionaries() const campusIdList = ref([] as any[]) const setCampusIdList = async () => { diff --git a/niucloud/app/adminapi/controller/venue/Venue.php b/niucloud/app/adminapi/controller/venue/Venue.php index c7d5ee22..7c637600 100644 --- a/niucloud/app/adminapi/controller/venue/Venue.php +++ b/niucloud/app/adminapi/controller/venue/Venue.php @@ -62,6 +62,8 @@ class Venue extends BaseAdminController ["capacity", 0], ["availability_status", 0], ["time_range_type", ""], + ["time_range_start", ""], + ["time_range_end", ""], ["fixed_time_ranges", []], ]); diff --git a/niucloud/app/service/admin/dict/DictService.php b/niucloud/app/service/admin/dict/DictService.php index 9660c3f0..ba75ead3 100644 --- a/niucloud/app/service/admin/dict/DictService.php +++ b/niucloud/app/service/admin/dict/DictService.php @@ -117,6 +117,26 @@ class DictService extends BaseAdminService if($info['dictionary'] == null) { $info['dictionary'] = []; + } else if(is_string($info['dictionary'])) { + // 解析JSON字符串,处理可能的嵌套编码 + $decoded = json_decode($info['dictionary'], true); + if (json_last_error() === JSON_ERROR_NONE) { + if (is_array($decoded)) { + $info['dictionary'] = $decoded; + } else if (is_string($decoded)) { + // 双重JSON编码的情况,再次解析 + $double_decoded = json_decode($decoded, true); + if (json_last_error() === JSON_ERROR_NONE && is_array($double_decoded)) { + $info['dictionary'] = $double_decoded; + } else { + $info['dictionary'] = []; + } + } else { + $info['dictionary'] = []; + } + } else { + $info['dictionary'] = []; + } } return $info; } diff --git a/niucloud/app/service/admin/venue/VenueService.php b/niucloud/app/service/admin/venue/VenueService.php index b70dc056..27c8e5cd 100644 --- a/niucloud/app/service/admin/venue/VenueService.php +++ b/niucloud/app/service/admin/venue/VenueService.php @@ -57,7 +57,13 @@ class VenueService extends BaseAdminService $info = $this->model->field($field)->where([['id', "=", $id]])->with(['campus'])->findOrEmpty()->toArray(); $info['availability_status'] = strval($info['availability_status']); $info['time_range_type'] = strval($info['time_range_type']); - $info['fixed_time_ranges'] = json_decode($info['fixed_time_ranges'], true); + // 处理 fixed_time_ranges 字段的 JSON 解析,避免 null 值导致的错误 + if ($info['fixed_time_ranges'] === null || $info['fixed_time_ranges'] === '') { + $info['fixed_time_ranges'] = []; + } else { + $decoded = json_decode($info['fixed_time_ranges'], true); + $info['fixed_time_ranges'] = (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) ? $decoded : []; + } return $info; }