Browse Source

因小程序不支持uni默认的动态tabbar, 因此将tabbar单独抽离出来做成组件

h5_css
蟑螂恶霸 3 years ago
parent
commit
c86bceb640
  1. 1
      App.vue
  2. 130
      components/tabbar/tabbar.vue
  3. 24
      pages.json
  4. 67
      pages/index/index.vue
  5. 11
      pages/knowledge/knowledge.vue
  6. 22
      pages/my/my.vue
  7. 11
      pages/scan/scan.vue

1
App.vue

@ -6,6 +6,7 @@
}, },
onShow: function() { onShow: function() {
console.log('App Show') console.log('App Show')
uni.hideTabBar();
}, },
onHide: function() { onHide: function() {
console.log('App Hide') console.log('App Hide')

130
components/tabbar/tabbar.vue

@ -0,0 +1,130 @@
<template>
<view class="tab_bar">
<view class="tabbarBox">
<view class="handleBox" v-for="(item,index) in tabBarList" :key="index">
<view class="menuBox" @click="goPages(item.pageIndex)">
<view class="menuIcon">
<image v-if="item.type!=selectIndex" class="img" :src="item.iconPath"></image>
<image v-else class="img" :src="item.selectIconPath"></image>
</view>
<view class="menuName">
<text :class="item.type==selectIndex?'TextColor':'Text'" >{{item.tabbarName}}</text>
</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
page: {
type: String,
default: "homeIndex"
}
},
watch: {
page: {
handler(value) {
this.selectIndex = value;
},
immediate: true,
deep: true
}
},
data() {
return {
selectIndex: "",
tabBarList: [],
}
},
//uniappvue
created() {
//tabbar
// this.tabBarList = [{
// "tabbarName":"", //tababr
// "iconPath":"", //tabbar icon
// "selectIconPath":"", //tabbar icon
// "pageIndex":"" //
// }]
this.listTabbar();
},methods: {
listTabbar() {
this.$api.listTabbar({})
.then(res => {
console.log(res)
for (var i = 0; i < res.data.length; i++) {
if (res.data[i].hide == 1) {
this.tabBarList.push({
"tabbarName": res.data[i].title,
"iconPath": res.data[i].selectedIconPath,
"selectIconPath": res.data[i].selectedIconPath,
"pageIndex": res.data[i].pagePath,
"type": res.data[i].type
});
}
}
}).catch((e) => console.log(e)); //
},
//tabble
goPages(pageIndex) {
uni.switchTab({
url:"/"+pageIndex
})
},
},
}
</script>
<style lang="scss">
.tab_bar {
width: 100vw;
height: 120rpx;
position: fixed;
bottom: 30rpx;
/* 模糊大小就是靠的blur这个函数中的数值大小 */
backdrop-filter: blur(10px);
border-radius: 60rpx;
.tabbarBox {
display: flex;
margin-top: 10rpx;
justify-content: space-evenly;
.handleBox {
width: 20vw;
height: 110rpx;
.menuBox {
padding: 0rpx 20rpx;
width: 120rpx;
height: 98%;
text-align: center;
.img {
width: 50rpx;
height: 50rpx;
}
}
}
}
}
.Text {
font-size: 24rpx;
font-family: Cochin, serif;
font-weight: 900;
}
.TextColor {
@extend .Text;
color: #d81e06;
}
</style>

24
pages.json

@ -60,23 +60,23 @@
"tabBar": { "tabBar": {
"selectedColor": "#009eff", "selectedColor": "#009eff",
"list": [{ "list": [{
"pagePath": "pages/index/index", "pagePath": "pages/index/index"
"text": "首页", // "text": "首页",
"iconPath": "static/tab_icons/home.png" // "iconPath": "static/tab_icons/home.png"
}, },
{ {
"pagePath": "pages/scan/scan", "pagePath": "pages/scan/scan"
"text": "扫一扫", // "text": "扫一扫",
"iconPath": "static/tab_icons/cate.png" // "iconPath": "static/tab_icons/cate.png"
}, },
{ {
"pagePath": "pages/knowledge/knowledge", "pagePath": "pages/knowledge/knowledge"
"text": "知识库", // "text": "知识库",
"iconPath": "static/tab_icons/knownledge.png" // "iconPath": "static/tab_icons/knownledge.png"
}, { }, {
"pagePath": "pages/my/my", "pagePath": "pages/my/my"
"text": "我的", // "text": "我的",
"iconPath": "static/tab_icons/my.png" // "iconPath": "static/tab_icons/my.png"
} }
] ]
} }

67
pages/index/index.vue

@ -16,15 +16,19 @@
<image :src="item" class="scene-img"></image> <image :src="item" class="scene-img"></image>
</view> </view>
</view> </view>
<tabbar :page="page"></tabbar>
</view> </view>
</template> </template>
<script> <script>
import tabbar from "@/components/tabbar/tabbar.vue";
import { mapMutations, mapState } from 'vuex' import { mapMutations, mapState } from 'vuex'
export default { export default {
data() { data() {
return { return {
page: "",
// //
topImg: "", topImg: "",
leftImg: "", leftImg: "",
@ -39,65 +43,13 @@
...mapState('m_user', ['token']) ...mapState('m_user', ['token'])
}, },
onLoad() { onLoad() {
uni.setTabBarItem({
index: 1,
"visible": false
});
uni.setTabBarItem({
index: 2,
"visible": false
});
uni.setTabBarItem({
index: 3,
"visible": false
});
uni.setTabBarItem({
index: 4,
"visible": false
});
this.listTabbar();
this.getSwiperList(); this.getSwiperList();
this.checkLogin() // this.checkLogin()
}, },
methods: { onShow() {
listTabbar() { this.page= "home"
this.$api.listTabbar({})
.then(res => {
console.log(res)
for (var i = 0; i < res.data.length; i++) {
if (res.data[i].hide == 1) {
if (res.data[i].type == "home") {
uni.setTabBarItem({
index: 0,
text: res.data[i].title,
"visible": true
});
}
if (res.data[i].type == "scan") {
uni.setTabBarItem({
index: 1,
text: res.data[i].title,
"visible": true
});
}
if (res.data[i].type == "know") {
uni.setTabBarItem({
index: 2,
text: res.data[i].title,
"visible": true
});
}
if (res.data[i].type == "my") {
uni.setTabBarItem({
index: 3,
text: res.data[i].title,
"visible": true
});
}
}
}
}).catch((e) => console.log(e)); //
}, },
methods: {
async getSwiperList() { async getSwiperList() {
@ -132,7 +84,8 @@
} }
}, },
} },
components: {tabbar}
} }
</script> </script>

11
pages/knowledge/knowledge.vue

@ -1,16 +1,23 @@
<template> <template>
<view> <view>
知识库页面 知识库页面
<tabbar :page="page"></tabbar>
</view> </view>
</template> </template>
<script> <script>
import tabbar from "@/components/tabbar/tabbar.vue";
export default { export default {
data() { data() {
return { return {
page: "",
}; };
} },
onShow() {
this.page= "know"
},
components: {tabbar}
} }
</script> </script>

22
pages/my/my.vue

@ -45,14 +45,21 @@
</view> </view>
</view> </view>
</view> </view>
<tabbar :page="page"></tabbar>
</view> </view>
</template> </template>
<script> <script>
import { mapState, mapMutations } from 'vuex' import tabbar from "@/components/tabbar/tabbar.vue";
import {
mapState,
mapMutations
} from 'vuex'
export default { export default {
data() { data() {
return { return {
page: "",
avatarUrl: uni.$http.baseUrl + "/profile//2024/01/18/6147d64a3b94d7b3d3cc35aaf8cbfe06.png", avatarUrl: uni.$http.baseUrl + "/profile//2024/01/18/6147d64a3b94d7b3d3cc35aaf8cbfe06.png",
// //
swiperList: [], swiperList: [],
@ -64,6 +71,9 @@
this.getSwiperList() this.getSwiperList()
this.getFunctionList() this.getFunctionList()
}, },
onShow() {
this.page = "my"
},
computed: { computed: {
...mapState('m_user', ['userinfo']) ...mapState('m_user', ['userinfo'])
}, },
@ -85,7 +95,9 @@
const { const {
data: res data: res
} = await uni.$http.post('/small/customImage/listAll',{page:"my"}) } = await uni.$http.post('/small/customImage/listAll', {
page: "my"
})
if (!res.success) return uni.$showMsg() if (!res.success) return uni.$showMsg()
res.data.forEach(item => { res.data.forEach(item => {
@ -111,6 +123,9 @@
navClickHandler(item) { navClickHandler(item) {
}, },
components: {
tabbar
}
} }
</script> </script>
@ -181,6 +196,7 @@
} }
} }
} }
.function-img { .function-img {
width: 30px; width: 30px;
height: 30px; height: 30px;
@ -194,6 +210,7 @@
padding: 0 10px; padding: 0 10px;
line-height: 45px; line-height: 45px;
} }
swiper { swiper {
height: 230rpx; height: 230rpx;
@ -205,4 +222,3 @@
} }
} }
</style> </style>

11
pages/scan/scan.vue

@ -2,20 +2,26 @@
<view> <view>
<view class="video-wrapper" id="video-container"> <view class="video-wrapper" id="video-container">
</view> </view>
<tabbar :page="page"></tabbar>
</view> </view>
</template> </template>
<script> <script>
import tabbar from "@/components/tabbar/tabbar.vue";
export default { export default {
data() { data() {
return { return {
page: "",
}; };
}, },
onReady(){ onReady(){
this.QR(); this.QR();
}, },
onShow() {
this.page= "scan"
},
methods:{ methods:{
QR() { QR() {
uni.scanCode({ uni.scanCode({
@ -28,7 +34,8 @@
} }
}) })
}, },
} },
components: {tabbar}
} }
</script> </script>

Loading…
Cancel
Save