溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

微信小程序商城開發(fā)中如今搭建https框架

發(fā)布時間:2021-02-03 10:48:27 來源:億速云 閱讀:130 作者:小新 欄目:移動開發(fā)

小編給大家分享一下微信小程序商城開發(fā)中如今搭建https框架,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

本篇文章給大家?guī)淼膬热菔顷P于微信小程序商城開發(fā)之https框架的搭建以及頂部和底部導航的實現(xiàn),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

之前的小程序商城系列已經(jīng)更新到購物車模塊了但是很多讀者反映如何能夠更接近于實戰(zhàn)場景,動態(tài)的獲取數(shù)據(jù)并展示出來!那么經(jīng)過這段時間的準備我們開始來做新的微商城版本,該版本是完全按照工作場景來開發(fā)的。

小程序https域名配置

登錄注冊好的微信小程序官方賬號并登錄平臺——>設置——>開發(fā)設置,如下圖所示:

微信小程序商城開發(fā)中如今搭建https框架

創(chuàng)建小程序項目并封裝ajax請求

創(chuàng)建小程序項目可以參考文章微信小程序電商實戰(zhàn)-入門篇

新建ajax.js
#目錄結構-pages
--utils
---ajax.js
聲明api 全局變量調用地址
const api = 'https://100boot.cn/wxShop/';
封裝request請求
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
配置開發(fā)者key

打開utils/util.js,增加key

module.exports = {
  formatTime: formatTime,
  key: '開發(fā)者key'
}

微信小程序微商城:開發(fā)者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');

頁面初始化導航數(shù)據(jù)

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

頁面初始化加載導航數(shù)據(jù)函數(shù)

/**
  * 生命周期函數(shù)--監(jiān)聽頁面加載
  */

onLoad: function (options) {    
    var that = this;    
    //加載navbar導航條
    that.navbarShow();
  },

ajax獲取導航數(shù)據(jù)

navbarShow:function(success){    
    var that = this;
    ajax.request({      
        method: 'GET',      
        url: 'home/navBar?key=' + utils.key,      
        success: data => {
        that.setData({          
            navbars: data.result
        })        
        console.log(data.result)
      }
    })
  },
實現(xiàn)效果如下

微信小程序商城開發(fā)中如今搭建https框架

以上是“微信小程序商城開發(fā)中如今搭建https框架”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI