小程序模板網(wǎng)

微信小程序?qū)崙?zhàn)--高仿人民日報

發(fā)布時間:2018-04-25 09:34 所屬欄目:小程序開發(fā)教程

前言

開發(fā)的大致思路是:請求人民日報電子版網(wǎng)址,通過對響應(yīng)的html文檔進(jìn)行匹配,查找需要的資源(比如數(shù)字報圖片地址、每版標(biāo)題、每版的文章等)

新建項目

打開安裝好的“微信web開發(fā)者工具”,點擊“+”(右側(cè)左下),新建項目。填入相關(guān)信息

  • AppID:登錄微信公眾平臺可查看
  • 項目名稱:本項目的名字,自定義
  • 項目目錄:項目存放的位置,自定義
  • 創(chuàng)建QuickStart項目:勾選此項,開發(fā)工具會生成一個簡易的小程序demo

確定即可

修改配置文件app.json

app.json文件用來對微信小程序進(jìn)行全局配置,決定頁面文件的路徑、窗口表現(xiàn)、設(shè)置網(wǎng)絡(luò)超時時間、設(shè)置多 tab 等。

1.添加paper頁面
在"pages"的數(shù)組里,在第一個位置添加“pages/paper/paper”(第一個位置表示小程序打開時的首界面),添加保存后,會發(fā)現(xiàn)pages目錄下多了一個paper目錄
2.添加tabBar
打開app.json,添加“tabBar”屬性
3.修改導(dǎo)航欄標(biāo)題
修改“window”屬性下的“navigationBarTitleText”為fake人民日報讀報小程序

"pages":[
    "pages/paper/paper",
    "pages/index/index",
    "pages/logs/logs"
  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "fake人民日報讀報小程序",
    "navigationBarTextStyle":"black"
  },
  "tabBar": {
    "list": [
      {
        "pagePath": "pages/paper/paper",  
        "text": "版面"   
      },
      {
        "pagePath": "pages/index/index",
        "text": "目錄"
      }
    ],
    "selectedColor":"#589ad5"
  },
 "debug": true

注意:app.json文件中不能包含注釋

獲取版面數(shù)據(jù)

我們的想法是打開該小程序后,首先顯示的當(dāng)天人民日報電子版的第一版圖片,所以要知道該圖片的網(wǎng)絡(luò)地址,再通過小程序image組件的src屬性,將圖片顯示出來
1.分析url
打開人民日報電子版,(以2017.8.30號報紙為例)查看網(wǎng)址可以推測
http://paper.people.com.cn/rm...
2017-08/03 代表報紙的日期
nbs.D110000renmrb_01.htm 代表報紙的版面,01代表第1版
試著修改日期和代表版面的數(shù)字,證明了猜測
2.請求第一版的html文檔
打開utils目錄下的util.js文件,添加以下代碼

//獲取當(dāng)日年月日的數(shù)組
const todayDateArray = () => {
  var today = new Date();
  var year = today.getFullYear();
  var month = today.getMonth() + 1;//getMonth()返回0-11,與實際對應(yīng)的話需要+1
  var day = today.getDate();
  //小于10的,前面加0
  return [year, month, day].map(formatNumber);
}
module.exports = {
  todayDateArray: todayDateArray
}

修改app.js,添加如下代碼

//app.js
App({
  onLaunch: function () {
    // 展示本地存儲能力
    ...........

    // 登錄
    wx.login({
      success: res => {
        // 發(fā)送 res.code 到后臺換取 openId, sessionKey, unionId
      }
    })
    // 獲取用戶信息
    wx.getSetting({
     ........
    });

    //同步獲取系統(tǒng)信息
    try{
      var res = wx.getSystemInfoSync();
      this.globalData.systemInfo = res;
    }catch(error){console.log("同步獲取系統(tǒng)信息時失敗",error)}
  },
  
  globalData: {
    userInfo: null,
    systemInfo:null
  }
})

打開pages/paper/paper.js,修改

// pages/paper/paper.js
var app = getApp();
var todayDateArray = require('../../utils/util.js').todayDateArray;
const apiUrl = 'http://paper.people.com.cn/rmrb/html';  //接口地址
const imgUrl = 'http://paper.people.com.cn/rmrb';  //接口地址

Page({

  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    windowWidth: 0,
    windowHeight: 0,
    paperInfo:[]//報紙信息
  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  onLoad: function (options) {
    var self = this;
    
    //獲取設(shè)備窗口寬高
    if (app.globalData.systemInfo) {
      var systemInfo = app.globalData.systemInfo;
      self.setData({
        windowWidth: systemInfo.windowWidth,
        windowHeight: systemInfo.windowHeight
      });
    } else {
      //重新請求系統(tǒng)信息
    }
    //拼接當(dāng)日第一版url
    var todayArray = todayDateArray();
    var y_m = todayArray.slice(0, 2).join("-");
    var firstSection = 'nbs.D110000renmrb_01.htm';
    var url = [apiUrl, y_m, todayArray[2], firstSection].join('/');
    console.log("第一版url", url);
    //進(jìn)行網(wǎng)絡(luò)請求
    wx.request({
      url: url,
      success: function (res) {
        console.log(res.data);
        var html = res.data;
        //正則式-匹配版面圖片
        var pagePicImgReg = /<img[^>]+src=(.*)\s+border=0\s+usemap=#pagepicmap[^>]*>/i;
        //匹配結(jié)果
        var pagePicImgMatch = html.match(pagePicImgReg);
        var imgSrc = "";
        pagePicImgMatch && (imgSrc = pagePicImgMatch[1].replace('../../..', imgUrl));
        console.log("imgSrc", imgSrc);
        self.setData({
          paperInfo: [{ "imgSrc": imgSrc}]
        });
      }
    })
  },
})

說明:響應(yīng)的html文檔中,我們發(fā)現(xiàn),可利用的數(shù)據(jù)不僅僅是版面圖片,還有熱區(qū),版面列表,每版新聞列表等信息,大有可為
修改paper.wxml

<view class="page-container">
  <view class="paper-container">
    <swiper class='paper-swiper' style='width:{{windowWidth*2}}rpx;height:{{windowHeight*2}}rpx;' indicator-dots="true" indicator-active-color="#589ad5">
      <block wx:for="{{paperInfo}}" wx:key="*this">
        <swiper-item>
          <image style='width:{{windowWidth*2}}rpx;height:{{windowHeight*2}}rpx;' src="{{item.imgSrc}}"></image>
        </swiper-item>
      </block>

    </swiper>
  </view>
</view>

說明:由于后期會通過左右滑動切換版面的,所以用了swiper組件

編譯并預(yù)覽

首先勾“選不校驗安全域名、TLS 版本以及 HTTPS 證書”(開發(fā)工具的右上角->詳情)
顯示“模擬器”(開發(fā)工具左上角->頭像旁邊)
ctrl+b 開發(fā)工具中查看
點擊預(yù)覽,微信掃描二維碼,手機(jī)上查看效果(要打開調(diào)試,右上角button)



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://m.u-renovate.com/wxmini/doc/course/23986.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點擊咨詢
QQ在線咨詢
AI智能客服 ×