@@ -57,10 +57,7 @@ import petItem from "@/components/pet/item";
5757import luckyItem from " @/components/pet/lucky" ;
5858import { clone , debounce } from " lodash" ;
5959import { isPhone } from " @/utils/index" ;
60- import Type from " @/assets/data/pet_type.json" ;
61- import Source from " @/assets/data/pet_source.json" ;
62- import { __imgPath } from " @/utils/config" ;
63- import { getPets , getPetLucky , getSliders , getMapList } from " @/service/pet" ;
60+ import { getPets , getPetSearchOptions , getPetLucky , getSliders , getMapList } from " @/service/pet" ;
6461import dayjs from " @/plugins/day" ;
6562
6663export default {
@@ -75,8 +72,8 @@ export default {
7572 return {
7673 tabsData: {}, // 标签筛选数据
7774 active: " " , // 当前激活的分类ID
78- Type, // 宠物类型配置
79- Source, // 宠物来源配置
75+ Type: [], // 宠物类型配置
76+ Source: [], // 宠物来源配置
8077 list: [], // 单分类宠物列表
8178 page: 1 , // 当前页码
8279 per_page: 50 , // 每页显示数量
@@ -89,13 +86,9 @@ export default {
8986 showAllList: false , // 是否显示单分类全部列表
9087 mapList: [], // 地图列表
9188 count: 0 , // 每行卡片数
89+ searchReady: false , // 筛选项是否已加载
9290 // 分类宠物列表(首页按分类展示)
93- list_type: [
94- { class: 1 , type: 1 , name: " 水族" , list: [] },
95- { class: 2 , type: 2 , name: " 禽鸟" , list: [] },
96- { class: 3 , type: 3 , name: " 走兽" , list: [] },
97- { class: 4 , type: 4 , name: " 机关" , list: [] },
98- ],
91+ list_type: [],
9992 };
10093 },
10194 computed: {
@@ -142,9 +135,10 @@ export default {
142135 this .getPetLucky ();
143136 this .getMapList ();
144137 this .showCount (1 );
145- // 首次加载数据
146- this .$nextTick (() => {
147- this .getPetListInit ();
138+ this .getPetSearchOptions ().finally (() => {
139+ this .$nextTick (() => {
140+ this .getPetListInit ();
141+ });
148142 });
149143
150144 this .handleResize = debounce (() => {
@@ -156,6 +150,43 @@ export default {
156150 window .removeEventListener (" resize" , this .handleResize );
157151 },
158152 methods: {
153+ /**
154+ * 获取宠物筛选项
155+ * 种类对应宠物 Class 字段,来源对应宠物 Source 字段
156+ */
157+ getPetSearchOptions () {
158+ return getPetSearchOptions ()
159+ .then ((res ) => {
160+ const data = Array .isArray (res .data ) ? res .data : [];
161+ const typeOptions = data
162+ .filter ((item ) => item .Type === 1 && item .TypeName )
163+ .map ((item ) => ({
164+ class: item .ID ,
165+ type: item .ID ,
166+ name: item .TypeName ,
167+ }));
168+ const sourceOptions = data
169+ .filter ((item ) => item .Type === 2 && item .TypeName )
170+ .map ((item ) => ({
171+ source: item .ID ,
172+ name: item .TypeName ,
173+ }));
174+
175+ this .Type = [{ class: " " , type: 0 , name: " 所有种类" }, ... typeOptions];
176+ this .Source = [{ source: " " , name: " 所有途径" }, ... sourceOptions];
177+ this .list_type = typeOptions .map ((item ) => ({
178+ ... item,
179+ list: [],
180+ }));
181+ })
182+ .catch ((err ) => {
183+ console .error (" 获取宠物筛选项失败" , err);
184+ })
185+ .finally (() => {
186+ this .searchReady = true ;
187+ });
188+ },
189+
159190 /**
160191 * 获取地图列表
161192 * 用于筛选组件中的地图下拉框
@@ -175,6 +206,7 @@ export default {
175206 * @returns {Boolean} 是否无结果
176207 */
177208 isNoRes () {
209+ if (! this .searchReady || this .loading ) return false ;
178210 if (this .params .Class ) {
179211 return this .list .length === 0 ;
180212 }
@@ -190,7 +222,6 @@ export default {
190222 this .page = 1 ;
191223 document .documentElement .scrollTop = 0 ;
192224 this .typeName = this .getTypeName ();
193- this .Type = [... Type];
194225 // 同步更新 tabsData 中的 Class
195226 this .tabsData = { ... this .tabsData , Class: val };
196227 },
@@ -331,7 +362,6 @@ export default {
331362 if (newClass !== this .active ) {
332363 this .active = newClass;
333364 this .typeName = this .getTypeName ();
334- this .Type = [... Type];
335365 document .documentElement .scrollTop = 0 ;
336366 }
337367
0 commit comments