微信小程序結合后台數據管理實現商品數據的動態展示、維護


微信小程序給我們提供了一個很好的開發平台,可以用於展現各種數據和實現豐富的功能,本篇隨筆介紹微信小程序結合后台數據管理實現商品數據的動態展示、維護,介紹如何實現商品數據在后台管理系統中的維護管理,並通過小程序的請求Web API 平台獲取JSON數據在小程序界面上進行動態展示。

1、整體性的架構設計

我們整體性的架構設計,包含一個Web管理后台、一個Web API統一接口層、當然還有數據庫什么,另外還有一個小程序客戶端。整個架構體系還是以我之前隨筆介紹的《整合微信小程序的Web API接口層的架構設計》內容為藍本

整個體系以Web API為主提供服務,同時后台管理系統通過各種界面維護着數據的增刪改等基礎管理工作。

Web API的分層,我們可以通過下圖來了解具體的分層結構。

Web API 是一個統一的出口,因此會整合很多Web API控制器,以提供所有業務的接口,因此對Web API 控制器的管理就顯得很重要,這里建議引入Area區域進行管理控制器類,這種各個模塊就能夠很好分門別類的進行管理了。

如下圖所示是我們的Web API項目的控制器Area區域分類,把微信公眾號、企業號、小程序、基礎框架、第三方接口、CRM等內容進行不同的划分。

而后台管理系統,我們通過下面的來了解整體功能,整個后台管理系統使用了Bootstrap的框架進行前端處理。

各種賬號的維護如下所示。

  

2、后台管理系統的數據維護

前面介紹了,后台管理和Web API層是分開的,它們的數據最終都是集中在一個數據庫上,實現我們所要的數據集中化管理。

我們言歸正題,介紹如何實現商品數據的后台管理,數據數據我們分為幾種類型,方便在前端界面展示。

商品編輯界面包括對基礎信息的修改、封面和Banner圖片的維護、以及商品多個展示圖片、商品詳細介紹的內容維護,如下界面所示。

除了商品的封面圖片以及Banne圖片外,我們在小程序的商品詳細界面里面,需要在頂端展示多個可以滾動的圖片效果,那么我們需要維護商品的圖片,如下界面所示。

當然商品的詳細信息需要一個富文本的編輯器來進行圖片文字的編輯處理,如下界面所示。

HTML圖文的編輯,我們這里是用SummerNote插件來進行處理,這個控件的使用非常方便,另外通過修改onImageUpload回調函數,可以實現圖片的隨時上傳處理。

        function initEditor() {
            $('#Note').summernote({
                lang: 'zh-CN',       // default: 'en-US'
                height: 600,         // set editor height                
                minHeight: null,    // set minimum height of editor
                maxHeight: null,    // set maximum height of editor
                focus: true,         // set focus to editable area after initializing summe
                callbacks: {
                    onImageUpload: function (files) { //the onImageUpload API  
                        img = sendFile(files[0]);
                    }
                }
            });
        }
        //提交文件到服務器處理
        function sendFile(file) {
            data = new FormData();
            data.append("file", file);
            //增加額外的參數
            data.append("folder", '商品信息');
            data.append("guid", $("#ID").val());

            $.ajax({
                data: data,
                type: "POST",
                url: "/FileUpload/Upload",
                cache: false,
                contentType: false,
                processData: false,
                success: function (json) {
                    var data = $.parseJSON(json);
                    var url = data.urls[0];
                    $("#Note").summernote('insertImage', url, 'image name'); // the insertImage API  
                }
            });
        }

 

3、小程序整合Web API接口實現數據展示

上面介紹了管理后台的數據維護,我們就是基於上面的數據模型,在小程序上實現商品數據的展示的。

下圖是小程序的商品展示首圖,其中包括了頂部Banner欄目、中間的商品分類、底部的商品信息展示幾部分。

其中Banner欄目的是一個swiper界面組件,商品類型使用了scroll-view來展示,而商品信息則是使用普通的View處理即可。

 整個界面的視圖部分代碼如下所示。

<!--pages/product/index.wxml-->
<!--1px = 750/320 = 2.34rpx;-->
<view class="container">
   <view class="swiper-container">
        <swiper class="swiper_box" 
                    autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" bindchange="swiperchange">
            <block wx:for="{{banners}}">
                <swiper-item>
                    <image bindtap="tapBanner" data-id="{{item.ID}}" src="{{item.Banner}}" class="slide-image" width="750rpx" height="562.5rpx"/>
                </swiper-item>
            </block>
        </swiper>
        <view class="dots">  
            <block wx:for="{{banners}}" wx:key="unique">  
                <view class="dot{{index == swiperCurrent ? ' active' : ''}}"></view>  
            </block>  
        </view>  
   </view>
   
    <view class="type-container">
        <scroll-view class="type-navbar" scroll-x="true" style="width: 100%">
            <view class="type-box" wx:for-items="{{categories}}">
                <view id="{{item.id}}" class="type-navbar-item {{activeCategoryId == item.id ? 'type-item-on' : ''}}" bindtap="tabClick">
                    {{item.name}}
                </view>
            </view>
        </scroll-view>
    </view>
    <view class="goods-container">
        <view class="goods-box" wx:for-items="{{goods}}" wx:key="{{index}}" bindtap="toDetailsTap" data-id="{{item.ID}}">
           <view class="img-box">
              <image src="{{item.Picture}}" class="image"/>
           </view>
           <view class="goods-title">{{item.ProductName}}</view>
        </view>
    </view>
    <view hidden="{{loadingMoreHidden ? true : false}}" class="no-more-goods">沒有更多啦</view>
</view>

其中小程序的數據是通過后台的JS文件實現數據的加載綁定的。

  /**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {    
    var that = this;

    this.getCategorys();
    this.getTopBanners();
    this.getGoodsList(0);
  },

其中上面的幾個函數就是分別通過Web API來獲取對應的JSON數據的,函數代碼如下所示。

 //獲取頂部Banner的數據
  getTopBanners : function() {
    //獲取產品列表
    var url = config.product_api;//'http://localhost:27206/api/Framework/Product/list'
    var data ={
      status : 1, //推薦
      pageindex : 1,
      pagesize: 10 
    }
    app.utils.get(url, data).then(res=> {
      this.setData({
        banners : res.list
      })
    });
  },
  //獲取商品類型
  getCategorys : function() {
    var url = config.category_api;//'http://localhost:27206/api/Framework/Product/GetProductType'
    app.utils.get(url, {}).then(res=> {
      var that = this;
      var categories = [{id:0, name:"全部"}];
      for(var i=0;i<res.length;i++){
        categories.push(res[i]);
      }
      that.setData({
        categories:categories,
        activeCategoryId:0
      });
    });
  },
  //獲取商品列表
  getGoodsList: function (categoryId) {
    if (categoryId == 0) {
      categoryId = "";
    }
    this.setData({
      goods: [],
      loadingMoreHidden: true
    });
    //獲取產品列表
    var url = config.product_api;//'http://localhost:27206/api/Framework/Product/list'
    var data = {
      type: categoryId,
      status: '', //所有狀態
      pageindex: 1,
      pagesize: 50
    }
    app.utils.get(url, data).then(res => {
      this.setData({
        goods: res.list,
        loadingMoreHidden: false,
      })
    });
  },

如果你對上面請求數據的代碼

    app.utils.get(url, data).then(res=> {
      this.setData({
        banners : res.list
      })
    });

有疑問,你可以參考我的隨筆《在微信小程序的JS腳本中使用Promise來優化函數處理》了解Promise插件的使用過程,這里通過引入Promise以及JS的模塊化方式,可以直接重用這些通用的JS函數,

 

而詳細部分內容,則是需要滾動展示商品的多個圖片,另外還需要展示詳細的HTML內容,HTML內容的展示使用富文本轉化插件wxParse即可實現,這部分在隨筆《在微信小程序中使用富文本轉化插件wxParse》有詳細的使用介紹。

商品詳細內容的視圖代碼如下所示。

<import src="../../utils/wxParse/wxParse.wxml" />
<view class="container"> 
   <view class="swiper-container">
        <swiper class="swiper_box" 
            autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" bindchange="swiperchange">
            <block wx:for="{{goodsDetail.pics}}">
                <swiper-item>
                    <image src="{{item.pic}}" class="slide-image" width="355" height="150"/>
                </swiper-item>
            </block>
        </swiper>
        <view class="dots">  
            <block wx:for="{{goodsDetail.pics}}" wx:key="unique">  
                <view class="dot{{index == swiperCurrent ? ' active' : ''}}"></view>  
            </block>  
        </view>  
   </view>
   <view class="goods-info">
        <view class="goods-title">{{goodsDetail.ProductName}}</view>
        <view class="goods-price" hidden="true">¥ {{goodsDetail.Price}}</view>
        <view class="goods-text">{{goodsDetail.Description}}</view>
   </view>
    <view class="goods-des-info">
        <view class="label-title">商品介紹</view>
        <view class="goods-text">
            <template is="wxParse" data="{{wxParseData:article.nodes}}"/>
         </view>
    </view>

</view>

其中后台的JS主要負責詳細信息的獲取和綁定工作。

  onLoad: function (e) {
    var that = this;

    //獲取商品詳細信息
    var url = config.product_detail_api;//'http://localhost:27206/api/Framework/Product/getdetail';
    var data = {id: e.id};
    app.utils.get(url, data).then(res => { 
        console.log(res);
        that.data.goodsDetail = res;
        that.setData({
          goodsDetail:res
        });
        WxParse.wxParse('article', 'html', res.Note, that, 5);
    });
  },

 

最后來段視頻了解下整體性的功能展示。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM