微信小程序入门开发超详细笔记(三) 项目篇

1 创建项目

2 开发项目

2.1 底部导航模块设计(原生)

2.1.1 引入

2.1.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": {
    "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
// 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
// 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
// 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.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    baseURL:'http://127.0.0.1:3000'
  }
})
2 req.js

  const app = getApp()
  // const host = 'http://127.0.0.1:3000'

  const request = (url, options) => {
     // 请求之前提示加载中
    wx.showLoading({title: '加载中...'})
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${app.globalData.baseURL}${url}`,
        // url: host + 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,
        // success(request) {
        //   // console.log(request)
        //   if (request.statusCode === 200) {
        //     resolve(request.data)
        //   } else {
        //     reject(request.data)
        //   }
        // },
        // fail(error) {
        //   reject(error.data)
        // }
        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 })
  }

  // 不能声明DELETE(关键字)
  const remove = (url, options) => {
    return request(url, { method: 'DELETE', data: options })
  }

  module.exports = {
    get,
    post,
    put,
    remove
  }

3 home.js
// pages/home/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
/* pages/home/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

// pages/home/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

/* pages/home/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

// pages/golist/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

/* pages/golist/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
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值