第一次正式開發一個小程序,就從以下幾個方面來談一談小程序的開發過程和心得吧,主要說說這次項目中用到的功能。
- 數據請求
這次的小程序,沒有太多的附加功能,所以數據以及對數據的處理是這次的主體工作,小程序向用戶提供API,供用戶向自己的服務器請求數據,值得一提的是,開發小程序之前,需要先在微信公眾平臺申請appID,并且綁定域名,域名必須是https協議,然后在小程序的開發工具的配置信息中完善信息,請求的地址需要在前面綁定的域名下。這個項目中用到wx.request從服務器拉取數據。
wx.request({
url: that.data.couponData.requestUrl,
data: that.data.couponData.queryData,
header: {
'content-type': 'application/json'
},
success: function(res) {
var list = res.data.goodsList;
console.log(res.data);
for(var i in list) {
list[i].quanUsedNum = parseInt(list[i].quanTotalNum) - parseInt(list[i].quanRemainNum);
list[i].isImgRendered = false;
}
list[0].isImgRendered = list[1].isImgRendered = list[2].isImgRendered = list[3].isImgRendered = true;
that.setData({"couponData.totalPage":res.data.totalPage});
that.setData({"couponData.list":that.data.couponData.list.concat(list)});
that.setData({"couponData.loadmore":!that.data.couponData.loadmore});
that.setData({"couponData.queryData.pageNum":parseInt(that.data.couponData.queryData.pageNum) + 1});
if(that.data.couponData.queryData.pageNum > that.data.couponData.totalPage) {
that.setData({"couponData.isAction":false});
}
if(that.data.couponData.list.length < 1) {
that.setData({"couponData.nodata":true});
}
if(f) {
f();
}
}
});
- 數據緩存
這里使用數據緩存是因為需要做一個搜索功能,就涉及到頁面之間的數據傳遞,放在地址中也是一種方法,借用一下localStorage也可以,使用wx.setStorage將數據存儲到localStorage中,頁面跳轉之后,在從localStorage中讀取就可以了,讀取數據的時候分同步讀取和異步讀取。
-
剪切板的應用
借用小程序的API可以很方便的將任何信息復制到剪切板,然后就可以粘貼了。
wx.setClipboardData({ data: '【' + that.data.couponData.list[e.currentTarget.id].goodsTitle + '】復制這條信息,打開【手機淘寶】' + that.data.couponData.list[e.currentTarget.id].twoInOneKouling, success: function(res) { that.setData({"couponData.copyTip":true,"couponData.Kouling":that.data.couponData.list[e.currentTarget.id].twoInOneKouling}) } });
-
模板
在這個項目中,頁面基本很相似,但有細微差別,所以就使用了模板,新建一個template/template.wxml,name屬性必須要設置。<template name='navsearch'> <view class='nav-search'> <view class='nav-search__container space-between'> <view class='nav-search__search' wx:if='{{isSearch}}'></view> <input class='nav-search__input' placeholder='請輸入關鍵詞找券' name='queryStr' value="{{queryStr}}" bindfocus='toggleSearch' bindconfirm='doQuery' bindinput="syncQuery"/> <view class='nav-search__delete' wx:if='{{!isSearch}}' bindtap='deleteAll'></view> <view class='nav-search__btn center' wx:if='{{!isSearch}}' bindtap='doQuery'>搜索</view> </view> <view class='nav-filter' bindtap='toggleFilter'></view> </view> </template> <!--在其他文件中使用模板--> <import src="/template/template.wxml" /> <template is='navsearch' data="{{...couponData}}"></template>
-
模塊化
對于公共的js可以寫在一個專門的js文件中,然后使用module.exports暴露接口。
通用的js文件使用require引入。var common = require('../../common/common.js'); ... common.f(); //調用
-
redirectTo & navigateTo
redirectTo是重定向至某頁面,navigateTo是打開新的頁面,值得說明的一點是,使用navigateTo打開的頁面太多會導致小程序卡頓。
-
分享
Page({ onShareAppMessage: function () { return { title: 'your title!', path: '/xxxx/xxxx/xxxx', //分享之后回到這個頁面 success: function(res) { f(); //成功回調; }, fail: function(res) { f(); //失敗回調; } } } })
-
提高列表滑動的流暢性
簡而言之就是頁面滾動到哪里,列表中的圖片就顯示到哪里,實現方法如下。
//js文件 Page({ loadImg:function(e) { //計算接下來加載哪幾張 var index = Math.floor((e.detail.scrollTop - 8)/259.5); var temp = this.data.couponData.list; //完整的列表 var min = Math.max(index * 2,0),max = Math.min(index * 2 + 8,temp.length); for(var i = min; i < max; i ++) { if(temp[i] && !temp[i].isImgRendered) { temp[i].isImgRendered = true; //列表中的每一項有一個標記是否加載圖片的的屬性,默認false,隨著頁面滾動,一個個變成true。 } } this.setData({"couponData.list":temp}); temp = null; }, }) //wxml文件中在scroll-view上綁定事件。 <scroll-view class="section" scroll-y="true" bindscroll='loadImg'></scroll-view>