waimai-app/pages/index/index.vue
2023-03-31 10:59:32 +08:00

157 lines
3.0 KiB
Vue

<template>
<view>
<view class="header">
<view class="my-location flex-center">
<text class="icon-location iconfont"></text>
<text class="address">通力大厦</text>
</view>
<view class="search-box">
<view class="search-input-wrapper flex-center">
<text class="iconfont icon-search"></text>
<input class="input" type="text" :placeholder="placeholder" />
</view>
</view>
</view>
<view class="foods-category">
<view class="category-item" v-for="c in categroyList" @click="loadFoodByCid(c.id)" :key="c.id">
<view class="cover">
<image :src="c.cover" mode=""></image>
</view>
<view class="title">{{c.id}}{{c.title}}</view>
</view>
</view>
<view class="foods-list">
<view class="foods-item" v-for="f in foodsList">
<image :src="f.cover" mode=""></image>
<view class="foods-name">{{f.title}}</view>
</view>
</view>
</view>
</template>
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue";
import { API_URL } from "../../service/api";
const foodsList = ref([])
const categroyList = ref<{
id : number; title : string; cover : string
}[]>([])
uni.request({
url: API_URL + '/api/category',
success(ret) {
categroyList.value = ret.data.data
// console.log(ret.data.data)
}
})
uni.request({
url: API_URL + '/api/food/recommend',
success(ret) {
foodsList.value = ret.data.data
// console.log(ret.data.data)
}
})
function loadFoodByCid(cid) {
uni.request({
url: API_URL + '/api/food/category/' + cid,
success(ret) {
foodsList.value = ret.data.data
// console.log(ret.data.data)
}
})
}
const foodsName = '鱼香肉丝,鱼香茄子,烂肉豇豆,麻婆豆腐'.split(',');
const placeholder = ref('搜索 鱼香肉丝 试试')
let timer : any, foodsIndex = 1;
onMounted(() => {
timer = setInterval(() => {
placeholder.value = `搜索 ${foodsName[foodsIndex]} 试试`
foodsIndex++;
if (foodsIndex >= foodsName.length) {
foodsIndex = 0;
}
}, 5000)
})
onUnmounted(() => {
clearInterval(timer)
})
</script>
<style lang="scss" scoped>
.header {
background-color: $uni-color-primary;
padding: 10px 15px;
color: white;
display: flex;
height: 30px;
line-height: 30px;
}
.my-location {
cursor: pointer;
background-color: rgba(0, 0, 0, 0.4);
border-radius: 50px;
padding: 0px 10rpx;
font-size: 12px;
max-width: 100px;
overflow: hidden;
}
.address {
margin-left: 2px;
margin-right: 5px;
}
.search-box {
flex: 1;
}
.icon-search {
color: #666;
font-size: 20px;
transform: translateY(1px);
margin-right: 5px;
}
.search-input-wrapper {
background-color: #fff;
border-radius: 40px;
height: 30px;
margin-left: 15px;
padding: 0 15px 0 5px;
.input {
width: 100%;
color: #666;
font-size: 14px;
}
}
// 分类
.foods-category {
overflow: hidden;
}
.category-item {
width: 25%;
text-align: center;
float: left;
font-size: 14px;
padding: 10px;
box-sizing: border-box;
image {
width: 40px;
height: 40px;
}
}
</style>