diff --git a/src/pages/category/category.vue b/src/pages/category/category.vue index 13352ba..b34c367 100644 --- a/src/pages/category/category.vue +++ b/src/pages/category/category.vue @@ -45,23 +45,31 @@ export default { scrollTop: 0 } }, - onLoad(){ - this.getCategories() + async onLoad(){ + // 数据本地存储 + let message = null + const cate = uni.getStorageSync("categories") + if(cate){ + // 数据超过10秒, 从新从接口拿数据并更新到本地 + if(Data.now() - cate.time > 1000 * 10){ + message = (await this.$u.api.getCategories()).message + uni.setStorageSync("categories", { time: Date.now(), list: message }); + }else { + // 没有过期 + message = cate.list + } + }else{ + message = (await this.$u.api.getCategories()).message + uni.setStorageSync("categories", { time: Date.now(), list: message }); + } + // 左列表获取 + this.menus = message.map(v=>(v.cat_name)) + // 右列表获取 + this.goods = message[this.menuCurrent].children + // 数据存到全局 + categoryData = message }, - methods: { - // 获取详情信息 - async getCategories(){ - const { message } = await this.$u.api.getCategories() - // 把接口所有的数据存到全局变量上 - categoryData = message - console.log(categoryData); - // 左列表获取 - this.menus = message.map(v=>(v.cat_name)) - // console.log(this.menu) - // 右列表获取 - this.goods = message[this.menuCurrent].children - // console.log(this.goods); - }, + methods: { // 点击左菜单 onMenuTap(menuCurrent) { this.menuCurrent = menuCurrent @@ -69,9 +77,7 @@ export default { // 一直为0没变化, 小程序不会发现 this.scrollTop = Math.random() / 1000; } - } - } @@ -146,9 +152,6 @@ export default { image { width: 70%; } - .goods_name{ - - } } } }