微信小程序商城实战(一)https框架搭建及导航栏实现

实现效果:

在这里插入图片描述

小程序https域名配置

登录注册好的微信小程序官方账号并登录平台——>开发——>开发设置,如下图所示:
在这里插入图片描述
备注:https://100boot.cn 是已经认证过的域名,大家可以放心使用。

创建小程序项目并封装ajax请求

创建小程序项目可以参考文章微信小程序电商实战-入门篇

新建ajax.js

声明api 全局变量调用地址

const api = 'https://100boot.cn/wxShop/';

封装request请求

const request = (opt)=>{
  wx.request({
    method: opt.method || 'GET',
    url: api + opt.url,
    header: { 'content-type': 'application/json' },
    data: opt.data,
    success: function (res) {
      if (res.data.code == 100) {
        if (opt.success) {
          opt.success(res.data)
        }
      } else {
        console.error(res);
        wx.showToast({
          title: res.data.message,
        })
      }
    }
  })
}
  
module.exports.request = request;

配置开发者key
打开utils/util.js,增加key

module.exports = {
  formatTime: formatTime,
  key: '开发者key'
}

微信小程序微商城:开发者key获取

app.json

{
  "pages": [
    "pages/home/home",
    "pages/cart/cart",
    "pages/detail/detail",
    "pages/classify/classify",
    "pages/mine/mine",
    "pages/index/index",
    "pages/logs/logs"
  ],
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#f0145a",
    "navigationBarTitleText": "微商城",
    "backgroundColor": "#f0145a"
  },
  "tabBar": {
    "color": "#858585",
    "selectedColor": "#f0145a",
    "backgroundColor": "#ffffff",
    "borderStyle": "#000",
    "list": [
      {
        "pagePath": "pages/home/home",
        "iconPath": "images/home.png",
        "selectedIconPath": "images/home_select.png",
        "text": "首页"
      },
      {
        "pagePath": "pages/classify/classify",
        "iconPath": "images/classify.png",
        "selectedIconPath": "images/classify_select.png",
        "text": "分类"
      },
      {
        "pagePath": "pages/cart/cart",
        "iconPath": "images/cart.png",
        "selectedIconPath": "images/cart_select.png",
        "text": "购物车"
      },
      {
        "pagePath": "pages/mine/mine",
        "iconPath": "images/mine.png",
        "selectedIconPath": "images/mine_select.png",
        "text": "我的"
      }
    ]
  }
}

app.wxss

.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
}

home.wxml

<!--导航条-->  
<view class="navbar">  
  <text wx:for="{{navbars}}" data-idx="{{index}}" class="item {{currentTab==index ? 'active' : ''}}" wx:key="unique" bindtap="navbarTap">{{item.navbarName}}</text>  
</view>

home.wxss

page{  
  display: flex;  
  flex-direction: column;  
  height: 100%;  
}  
.navbar{  
  flex: none;  
  display: flex;  
  background: #fff;  
}  
.navbar .item{  
  position: relative;  
  flex: auto;  
  text-align: center;  
  line-height: 80rpx;  
  font-size:14px;
}  
/* 顶部导航字体颜色 */
.navbar .item.active{  
  color: #f0145a;  
}  
/* 顶部指示条属性 */
.navbar .item.active:after{  
  content: "";  
  display: block;  
  position: absolute;  
  bottom: 0;  
  left: 0;  
  right: 0;  
  height: 6rpx;  
  background: #f0145a;  
}

home.js
引用ajax和utils公共js

const ajax = require('../../utils/ajax.js');
const utils = require('../../utils/util.js');

页面初始化导航数据

data: {
    navbars:null,
    currentTab: 0,
  },

页面初始化加载导航数据函数

/**
   * 生命周期函数--监听页面加载
   */
onLoad: function (options) {
    var that = this;
    //加载navbar导航条
    that.navbarShow();
  },

ajax获取导航数据

navbarShow:function(success){
    var that =this;
    ajax.request({
      method:'GET',
      url:'home/navBar?key='+utils.key,
      success:function(res){
        console.log(res.result);
        that.setData({
          navbars: res.result
        })
      }
    })

  }

导航栏切换

navbarTap:function(e){
    console.log(e)
    var that = this;
    that.setData({
     currentTab :e.currentTarget.dataset.idx
    })
    
  }

转载自 https://www.jianshu.com/p/14722c41fdee

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值