本节:从服务器获取数据,传送到前端。
1.div,for循环输出数组。
2.script:定义服务器要传的参数;连接接口,定义空数组,存放数据,循环赋值。
// 从服务器获取数据
loadmenu() {
var that = this; //1.指向上面的数据
uni.request({ //2.对服务器请求数据
url: `http://www.yhjtp.com:8761/menu/menu/listmenu/${this.categoryId}/100/1`,
//3.连接的接口
success: (res) => { //4.成功之后返回的数据
console.log(res);
// this.text = 'request success';
that.goods = []; //5.定义一个数组,来接收服务器的内容
for (let g of res.data.data.menuPage.records) { //6.循环输出从服务器获取的数据数组
that.goods.push({ //7.放到上面定义的数组,循环赋值
id: g.id, //
categoryId: g.categoryId,
img: "http://www.yhjtp.com:8761/" + g.cover, //图片要服务器的网址+指针指向
price: g.price,
desc: g.menuDesc,
count: 0,
})}; //同级for这里用分号
that.categoryList = []; //1.定义数组,接收服务器的内容
for (let c of res.data.data.categoryList) { //2.循环数组
that.categoryList.push({
id: c.id, //3.赋值
name: c.name,
})
}},
})
},
最后,原代码。
<template>
<view>
<view class="" style="background-color:#f7f7f7 ;">
<view style="height: 110rpx;width: 750rpx;line-height: 110rpx;background-color: #FFFFFF;">
藤原肠粉店
</view>
<view>
<view class="page-section swiper">
<view>
<swiper :indicator-dots="indicatorDots" :indicator-color="indicatorColor"
:indicator-active-color="indicatorActiveColor" :autoplay="autoplay" :interval="interval"
:duration="duration">
<swiper-item>
<view>
<image style="width: 750rpx; height: 422rpx; " src="../../../static/1.jpg" mode="">
</image>
</view>
</swiper-item>
<swiper-item>
<view>
<image style="width: 750rpx; height: 422rpx; " src="../../../static/1.jpg" mode="">
</image>
</view>
</swiper-item>
<swiper-item>
<view>
<image style="width: 750rpx; height: 422rpx; " src="../../../static/1.jpg" mode="">
</image>
</view>
</swiper-item>
</swiper>
</view>
</view>
</view>
<view class="qianggoouqu" style="margin-top: 10rpx;background-color: #FFFFFF;">
<view class="" style="font-weight: 600;padding-bottom: 5rpx;">
抢购专区
</view>
<view class="">
<scroll-view class="nav1" scroll-x="true" style="">
<view class="" style="background-color:#FFFFFF;display: flex;width:1800rpx;height: 330rpx;">
<view class="" style="padding-left: 20rpx;margin-top: 10rpx;">
<image src="../../../static/1.jpg" mode="" style="width: 270rpx;height: 160rpx;">
</image>
<view class="tc">
一人吃超值套餐
</view>
<view class="price" style="display: flex;font-size: 35rpx;">
¥9.9 <del class="" style="color:#e6e6e6; margin-left: 5rpx;">
¥4.9
</del>
</view>
</view>
<view class="" style="padding-left: 20rpx;margin-top: 10rpx;">
<image src="../../../static/1.jpg" mode="" style="width: 270rpx;height: 160rpx;">
</image>
<view class="tc">
一人吃超值套餐
</view>
<view class="price" style="display: flex;font-size: 35rpx;">
¥9.9 <del class="" style="color:#e6e6e6; margin-left: 5rpx;">
¥4.9
</del>
</view>
</view>
<view class="" style="padding-left: 20rpx;margin-top: 10rpx;">
<image src="../../../static/1.jpg" mode="" style="width: 270rpx;height: 160rpx;">
</image>
<view class="tc">
一人吃超值套餐
</view>
<view class="price" style="display: flex;font-size: 35rpx;">
¥9.9 <del class="" style="color:#e6e6e6; margin-left: 5rpx;">
¥4.9
</del>
</view>
</view>
<view class="" style="padding-left: 20rpx;margin-top: 10rpx;">
<image src="../../../static/1.jpg" mode="" style="width: 270rpx;height: 160rpx;">
</image>
<view class="tc">
一人吃超值套餐
</view>
<view class="price" style="display: flex;font-size: 35rpx;">
¥9.9 <del class="" style="color:#e6e6e6; margin-left: 5rpx;">
¥4.9
</del>
</view>
</view>
<view class="" style="padding-left: 20rpx;margin-top: 10rpx;">
<image src="../../../static/1.jpg" mode="" style="width: 270rpx;height: 160rpx;">
</image>
<view class="tc">
一人吃超值套餐
</view>
<view class="price" style="display: flex;font-size: 35rpx;">
¥9.9 <del class="" style="color:#e6e6e6; margin-left: 5rpx;">
¥4.9
</del>
</view>
</view>
<view class="" style="padding-left: 20rpx;margin-top: 10rpx;">
<image src="../../../static/1.jpg" mode="" style="width: 270rpx;height: 160rpx;">
</image>
<view class="tc">
一人吃超值套餐
</view>
<view class="price" style="display: flex;font-size: 35rpx;">
¥9.9 <del class="" style="color:#e6e6e6; margin-left: 5rpx;">
¥4.9
</del>
</view>
</view>
</view>
</scroll-view>
</view>
</view>
<view class="" style="margin-top: 10rpx;background-color: #FFFFFF;height: 80rpx;display: flex;">
<view @click="showGoodsByCategoryId(category.id)" v-for="(category,index) in categoryList"
:key="'c-' +index" style="margin-left: 20rpx;line-height: 80rpx;display:flex;
position: relative;">
<!-- :key="'c-' +index" 防止循环重复,不然电脑会以为只有一个循环 -->
{{category.id}}--{{category.name}}
<view v-if="category.count > 0" class="yuan2" style="position: absolute;top: 0rpx;right: -20rpx;">
{{category.count}}
</view>
<!-- <div style="position: relative;" v-for="(cate, index) in category"
:key="index">
{{ cate.name }}
</div> -->
</view>
</view>
<view class="" style="margin-top: 10rpx;background-color: #FFFFFF;height: 1200rpx;">
<view v-for="(good,index) in goods" style="padding-top: 30rpx;display: flex;align-items: center; ">
<div v-if="good.categoryId == showCategoryId">
<div>
<image style="width: 131rpx;height: 125rpx;margin-left: 20rpx;" :src="good.img"></image>
<!-- 冒号是变量,不是冒号的src是直接的路径 -->
</div>
<view style="margin-left: 10rpx;">
<div>{{good.name}}</div>
<div>{{good.desc}}</div>
<div>¥{{good.price}}</div>
</view>
<view class="" style="display: flex;margin-left:300rpx;margin-top:-100rpx">
<view class="btn1" @click="alterGoods(good,1)">+</view>
<view class="">
{{good.count}}
</view>
<view class="btn2" @click="alterGoods(good,-1)">-</view>
</view>
</div>
</view>
</view>
</view>
<view class="countlanmu">
<view class="" style="flex: 1;padding-left: 30rpx;">
<view class="">
总价:
{{totalprice.toFixed(2)}}
</view>
数量
{{totalcount}}
</view>
<view class="js" @click="settlement()">
去结算
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
// 1.任何变量的先加都要放在data里面
categoryList: [{
id: 1,
name: "烧腊",
count: 0,
},
{
id: 2,
name: "奶茶",
count: 0,
},
{
id: 3,
name: "米饭",
count: 0,
},
],
categoryId: 1,
pagesize: 1,
pagenum: 10,
showCategoryId: 1,
totalprice: 0,
totalcount: 0,
indicatorDots: true,
autoplay: true,
interval: 3000,
duration: 500,
indicatorColor: "#292b40",
indicatorActiveColor: "#ffffff",
goods: [{
id: 1,
name: "曲奇饼",
price: 0.5,
desc: "我很好吃",
img: "../../../static/3.png",
count: 0,
categoryId: 1,
},
{
id: 2,
name: "奥利啊",
price: 0.5,
desc: "我很好吃",
count: 0,
img: "../../../static/1.jpg",
categoryId: 2,
},
{
id: 3,
name: "奥利啊",
price: 0.5,
desc: "我很好吃",
count: 0,
img: "../../../static/1.jpg",
categoryId: 3,
},
{
id: 4,
name: "奥利啊",
price: 0.5,
desc: "我很好吃",
count: 0,
img: "../../../static/1.jpg",
categoryId: 2,
},
]
}
},
created: function() {
// 4.最开始就运行这个程序,就是计算器
this.loadcategoryList();
this.loadmenu();
},
methods: {
// 从服务器获取数据
loadmenu() {
var that = this; //1.指向上面的数据
uni.request({ //2.对服务器请求数据
url: `http://www.yhjtp.com:8761/menu/menu/listmenu/${this.categoryId}/100/1`,
//3.连接的接口
success: (res) => { //4.成功之后返回的数据
console.log(res);
// this.text = 'request success';
that.goods = []; //5.定义一个数组,来接收服务器的内容
for (let g of res.data.data.menuPage.records) { //6.循环输出从服务器获取的数据数组
that.goods.push({ //7.放到上面定义的数组,循环赋值
id: g.id, //
categoryId: g.categoryId,
img: "http://www.yhjtp.com:8761/" + g.cover, //图片要服务器的网址+指针指向
price: g.price,
desc: g.menuDesc,
count: 0,
})
}; //同级for这里用分号
that.categoryList = []; //1.定义数组,接收服务器的内容
for (let c of res.data.data.categoryList) { //2.循环数组
that.categoryList.push({
id: c.id, //3.赋值
name: c.name,
})
}
},
})
},
settlement() {
let orderInfo = { //1.因为服务器要接收数组和userID,所以要先定义好,然后再接收
menuInfoDtoList: [],
userId: 1,
}
for (let g of this.goods) { //2.循环商品数组
orderInfo.menuInfoDtoList.push({ // 3.接收到之后,传递进数组
menuId: g.id,
num: g.count
});
}
console.log(orderInfo);
uni.request({ //1.连接接口
url: 'http://www.yhjtp.com:8761/menu/order/confirm', //仅为示例,并非真实接口地址。
data: orderInfo, //传递的数组
method: "POST", //传递的方法
header: {
'custom-header': 'hello' //自定义请求头信息,服务器没有要header的时候,可以不传
},
success: (res) => { //es6语法写法 2.成功之后的返回告知的文字
console.log(res.data);
this.text = 'request success'; //传递成功之后,输出的文字。
}
});
},
alterGoods(good, val) {
// val是加减的参数
// good.count = good.count + val;
good.count = Math.max(good.count + val, 0);
// 大于0的时候才赋值
// 寻找分类的对象
// 增加对应商品,会在对应分类增加对应的数量。商品有分类的id,分类也有id,相等就可以互相对应。
let curCategory = {}; //新定义的数组
for (let c of this.categoryList) {
// 循环分类列表,如果商品id和分类id一致,就把这个id参数赋值给新定义的数组
if (c.id == good.categoryId) {
curCategory = c;
}
curCategory.count = 0; //先把商品分类总数,小红点里面的值,定义为0
//this.categoryList[0].count = 0; //先把红点里面的内容定义为0
this.totalcount = 0;
this.totalprice = 0;
for (let g of this.goods) {
// 再次循环商品
if (curCategory.id == g.categoryId) {
// 如果商品的id等于新定义数组的id,就计算各个分类数组的总数,商品的数量加上对于分类的数量
curCategory.count = curCategory.count + g.count;
// this.curCategory[0].count = this.categoryList[0].count + g.count;
// 红点里面的内容,加商品的内容,不断累加
}
this.totalcount += g.count; //数量
this.totalprice += g.count * g.price; //价格
}
}
},
showGoodsByCategoryId(categoryId) {
this.showCategoryId = categoryId;
},
loadcategoryList() { //显示商品列表
var that = this;
// 方便指向,访问上面的数组,就是把上面之前的东西赋值到that上,就不需要再引入一遍,只需要that即可指到之前的对象
uni.request({
url: 'http://www.yhjtp.com:8761/menu/category/list/', //仅为示例,并非真实接口地址。
success: (res) => {
console.log(res.data);
that.categoryList = res.data.data; //接收到的分类列表
that.loadmenu(that.categoryList[0].id);
for (let i of that.categoryList) {
i["count"] = 0;
// 定义一个变量count,默认值为0,因为服务器没有count变量,所以要定义一个,然后在根据上面加就加
}
}
});
}
},
}
</script>
<style>
.countlanmu {
position: fixed;
bottom: 0rpx;
height: 100rpx;
display: flex;
background-color: #FFFFFF;
width: 750rpx;
}
,
.js {
padding-right: 30rpx;
flex: 1;
background-color: #c09969;
width: 150rpx;
height: 80rpx;
border-radius: 50rpx;
line-height: 80rpx;
text-align: center;
}
,
.btn1 {
height: 50rpx;
width: 50rpx;
background-color: #6d8dc4;
border-radius: 50%;
text-align: center;
color: #FFFFFF;
font-weight: 600;
margin-right: 10rpx;
}
,
.btn2 {
margin-left: 10rpx;
height: 50rpx;
width: 50rpx;
background-color: #6d8dc4;
border-radius: 50%;
text-align: center;
color: #FFFFFF;
font-weight: 600;
}
,
.yuan2 {
height: 40rpx;
width: 40rpx;
background-color: #ff0000;
border-radius: 50%;
text-align: center;
line-height: 40rpx;
color: #FFFFFF;
font-size: 16rpx;
}
,
.banner {
width: 750rpx;
height: 422rpx;
background-color: #0f0f27;
}
.banner .swiper {
width: 750rpx;
height: 422rpx !important;
}
.banner .swiper-item {
width: 750rpx;
height: 422rpx !important;
}
.banner .swiper-item image {
display: block;
width: 750rpx;
height: 422rpx !important;
}
</style>