1 创建项目
2 开发项目
2.1 底部导航模块设计(原生)
2.1.1 引入
2.1.2 配置
{
"pages":[
"pages/home/home",
"pages/message/message",
"pages/profile/profile",
"pages/golist/golist"
],
"window":{
"backgroundTextStyle":"light",
"backgroundColor":"#000",
"navigationBarBackgroundColor": "#000",
"navigationBarTitleText": "本地生活",
"navigationBarTextStyle":"white",
"enablePullDownRefresh":true
},
"tabBar": {
"color": "#dddddd",
"selectedColor": "#16f2e7",
"backgroundColor": "#fff",
"borderStyle": "black",
"list": [
{
"pagePath": "pages/home/home",
"text": "主页",
"iconPath": "/tabs/img/home.png",
"selectedIconPath": "/tabs/img/home-active.png"
},
{
"pagePath": "pages/message/message",
"text": "消息",
"iconPath": "/tabs/img/message.png",
"selectedIconPath": "/tabs/img/message-active.png"
},
{
"pagePath": "pages/profile/profile",
"text": "我的",
"iconPath": "/tabs/img/profile.png",
"selectedIconPath": "/tabs/img/profile-active.png"
}
]
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
2.1.3 显示如下
2.2 底部导航模块设计(自定义)
- 使用vant-UI组件设计
- vant-UI官网链接
- 1 修改app.json文件
"custom": true,
- 2 引入自定义tabbar文件夹
custom-tab-bar
- 3 引入使用的UI组件库
vant
- 4 配置
custom-tab-bar/index.js
- 5 配置
custom-tab-bar/index.wxml
- 6 配置每个页面自定义跳转
index.js
2.2.1 引入
2.2.2 文件如下
app.json
文件
{
"pages":[
"pages/home/home",
"pages/message/message",
"pages/profile/profile",
"pages/golist/golist"
],
"window":{
"backgroundTextStyle":"light",
"backgroundColor":"#000",
"navigationBarBackgroundColor": "#000",
"navigationBarTitleText": "本地生活",
"navigationBarTextStyle":"white",
"enablePullDownRefresh":true
},
"tabBar": {
"custom": true,
"color": "#dddddd",
"selectedColor": "#16f2e7",
"backgroundColor": "#fff",
"borderStyle": "black",
"list": [
{
"pagePath": "pages/home/home",
"text": "主页",
"iconPath": "/tabs/img/home.png",
"selectedIconPath": "/tabs/img/home-active.png"
},
{
"pagePath": "pages/message/message",
"text": "消息",
"iconPath": "/tabs/img/message.png",
"selectedIconPath": "/tabs/img/message-active.png"
},
{
"pagePath": "pages/profile/profile",
"text": "我的",
"iconPath": "/tabs/img/profile.png",
"selectedIconPath": "/tabs/img/profile-active.png"
}
]
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
custom-tab-bar
文件夹下配置
1 custom-tab-bar/index.js
文件
Component({
data: {
selected: 0,
color: "#7A7E83",
selectedColor: "#3cc51f",
"list": [
{
"pagePath": "/pages/home/home",
"text": "主页",
"iconPath": "/tabs/img/home.png",
"selectedIconPath": "/tabs/img/home-active.png"
},
{
"pagePath": "/pages/message/message",
"text": "消息",
"iconPath": "/tabs/img/message.png",
"selectedIconPath": "/tabs/img/message-active.png"
},
{
"pagePath": "/pages/profile/profile",
"text": "我的",
"iconPath": "/tabs/img/profile.png",
"selectedIconPath": "/tabs/img/profile-active.png"
}
]
},
attached() {
},
methods: {
switchTab(e) {
wx.switchTab({
url:this.data.list[e.detail].pagePath
})
}
}
})
2 custom-tab-bar/index.wxml
文件
<!--miniprogram/custom-tab-bar/index.wxml-->
<van-tabbar active="{{ selected }}" bind:change="switchTab">
<van-tabbar-item wx:for="{{list}}" wx:key="index" class="my">
<image
slot="icon"
src="{{ item.iconPath }}"
mode="aspectFit"
/>
<image
slot="icon-active"
src="{{ item.selectedIconPath }}"
mode="aspectFit"
/>
{{item.text}}
</van-tabbar-item>
</van-tabbar>
3 custom-tab-bar/index.json
文件
{
"component": true,
"usingComponents": {
"van-tabbar": "/vant/tabbar/index",
"van-tabbar-item": "/vant/tabbar-item/index"
}
}
4 custom-tab-bar/index.json
文件
.my image{
width: 70rpx;
height: 70rpx;
}
其他文件夹下配置
pages/home/home.js
Page({
data: {
},
onLoad: function (options) {
if( typeof this.getTabBar==='function' && this.getTabBar){
this.getTabBar().setData(
{
selected:0
}
)
}
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})
pages/message/message.js
Page({
data: {
},
onLoad: function (options) {
if( typeof this.getTabBar==='function' && this.getTabBar){
this.getTabBar().setData(
{
selected:1
}
)
}
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})
pages/profile/profile.js
Page({
data: {
},
onLoad: function (options) {
if( typeof this.getTabBar==='function' && this.getTabBar){
this.getTabBar().setData(
{
selected:2
}
)
}
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})
2.2.3 如下
2.3 主页轮播图
1 接口启动
2 文件如下
app.js
中配置接口baseURL:'http://127.0.0.1:3000'
utils/req.js
编写请求文件home.js
请求数据home.wxml
编写接口home.wxss
编写样式
项目结构
1 app.js
App({
onLaunch: function () {
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
wx.login({
success: res => {
}
})
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: res => {
this.globalData.userInfo = res.userInfo
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
globalData: {
baseURL:'http://127.0.0.1:3000'
}
})
2 req.js
const app = getApp()
const request = (url, options) => {
wx.showLoading({title: '加载中...'})
return new Promise((resolve, reject) => {
wx.request({
url: `${app.globalData.baseURL}${url}`,
method: options.method,
data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
header: {
'Content-Type': 'application/json; charset=UTF-8',
'x-token': 'x-token'
},
success: resolve,
fail: reject,
complete() {
wx.hideLoading()
}
})
})
}
const get = (url, options = {}) => {
return request(url, { method: 'GET', data: options })
}
const post = (url, options) => {
return request(url, { method: 'POST', data: options })
}
const put = (url, options) => {
return request(url, { method: 'PUT', data: options })
}
const remove = (url, options) => {
return request(url, { method: 'DELETE', data: options })
}
module.exports = {
get,
post,
put,
remove
}
3 home.js
const http = require('../../utils/req')
Page({
data: {
swiper:[]
},
async getSwiper(){
const res = await http.get('/slides')
this.setData({
swiper:res.data
})
},
onLoad: function (options) {
if( typeof this.getTabBar==='function' && this.getTabBar){
this.getTabBar().setData(
{
selected:0
}
)
}
this.getSwiper()
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})
4 home.wxml
<!--pages/home/home.wxml-->
<!-- 轮播图 -->
<view>
<swiper
autoplay="true" circular indicator-dots class="my">
<block wx:for="{{swiper}}" wx:key='index'>
<swiper-item>
<navigator wx:if="{{item.link}}" url="{{item.link}}" >
<image src="{{item.image}}" mode="aspectFill" class="slide-image"/>
</navigator>
<image wx:else src="{{item.image}}" class="slide-image"/>
</swiper-item>
</block>
</swiper>
</view>
5 home.wxss
.my{
height: 280rpx;
}
.my image{
width: 750rpx;
}
2.4 主页九宫格
- 引入UI组件
home.json
- 请求数据
home.js
- 渲染页面
home.wxml
- 编写样式
home.wxss
1 home.json
{
"usingComponents": {
"van-grid": "/vant/grid/index",
"van-grid-item": "/vant/grid-item/index"
}
}
2 home.js
const http = require('../../utils/req')
Page({
data: {
swiper:[],
grids:[]
},
async getSwiper(){
const res = await http.get('/slides')
this.setData({
swiper:res.data
})
},
async getGirds(){
const res = await http.get('/categories')
this.setData({
grids:res.data
})
},
onLoad: function (options) {
if( typeof this.getTabBar==='function' && this.getTabBar){
this.getTabBar().setData(
{
selected:0
}
)
}
this.getSwiper()
this.getGirds()
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})
3 home.wxml
<!--pages/home/home.wxml-->
<!-- 轮播图 -->
<view>
<swiper
autoplay="true" circular indicator-dots class="my">
<block wx:for="{{swiper}}" wx:key='index'>
<swiper-item>
<navigator wx:if="{{item.link}}" url="{{item.link}}" >
<image src="{{item.image}}" mode="aspectFill" class="slide-image"/>
</navigator>
<image wx:else src="{{item.image}}" class="slide-image"/>
</swiper-item>
</block>
</swiper>
</view>
<!-- 九宫格数据 -->
<view>
<van-grid column-num="3">
<van-grid-item
icon="{{item.icon}}"
text="{{item.name}}"
wx:for="{{ grids }}"
wx:key="index"
url="/pages/golist/golist?id={{item.id}}" />
</van-grid>
</view>
4 home.wxss
.my{
height: 280rpx;
}
.my image{
width: 750rpx;
}
2.5 详情页面的编写
golist.json
引入UI组件golist.js
请求数据golist.wxml
编写结构golist.wxss
编写样式
1 golist.json
{
"usingComponents": {
"van-card": "/vant/card/index"
},
"enablePullDownRefresh":true
}
2 golist.js
const http = require('../../utils/req')
Page({
data: {
id:'',
detail:[],
page:1,
pageSize:20,
flag:false
},
getMore(){
this.data.page++
this.goDetails()
},
reset(){
this.setData({
detail:[],
page:0,
flag:false
})
this.goDetails()
},
async goDetails(){
if(this.data.flag===true) return
const res = await http.get(`/shops?categoryId=${this.data.id}&_page=${this.data.page}&_limit=${this.data.pageSize}`)
const flag = this.data.page * this.data.pageSize > res.header['X-Total-Count']
this.setData({detail:this.data.detail.concat(res.data),flag})
},
async getDetailId(){
const res = await http.get('/categories/'+this.data.id)
wx.setNavigationBarTitle({
title: res.data.name,
})
},
onLoad: async function (options) {
await this.setData({id:options.id})
await this.getDetailId()
await this.goDetails()
this.getMore()
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
this.reset()
wx.stopPullDownRefresh()
},
onReachBottom: function () {
this.getMore()
},
onShareAppMessage: function () {
}
})
3 golist.wxml
<!--pages/golist/golist.wxml-->
<view>
<van-card
title="{{item.name}}"
thumb="{{ item.images[0] }}"
wx:for="{{detail}}"
wx:key="index"
>
<view slot="price">
联系电话: {{item.phone}}
</view>
<view slot="desc">
地址: {{item.address}}
</view>
</van-card>
</view>
<view class="my">
<text hidden="{{flag}}">加载更多</text>
<text hidden="{{!flag}}">加载完毕</text>
</view>
4 golist.wxss
.my{
text-align: center;
}
3 项目总结
3.1 json文件
- 页面的json文件只能陪着项目
app.json
文件里面的属性
3.2 文件找不到
- 新引入的文件找不到,需要重新启动项目(不是重新编译)
- 新引入的文件找不到,可以重新新建目录
3.3 页面文件
- 页面文件在项目的
app.json
中直接添加即可,项目会自己创建文件
3.4 路由传参问题
- 直接传参即可(不需要使用字符串模板)
- 传参:
url="/pages/golist/golist?id={{item.id}}"
- 接收参数: 在
onload
方法中options.id
3.5 修改和获取data
- 修改变量
this.setData({name:'lay'})
- 获取变量
this.data.name
3.6 设置导航标题
- 修改:
wx.setNavigationBarTitle({ title: res.data.name })
3.7 自定义tabbar
- 在
app.json
中设置"custom": true,
- 在整个项目文件中引入
custom-tab-bar
文件夹 - 修改
custom-tab-bar
下的文件
项目源码
- 码云地址:https://gitee.com/lakerzhang/shop.git