第一次正式開發(fā)一個小程序檬寂,就從以下幾個方面來談一談小程序的開發(fā)過程和心得吧,主要說說這次項目中用到的功能捣作。
- 數(shù)據(jù)請求
這次的小程序,沒有太多的附加功能浦辨,所以數(shù)據(jù)以及對數(shù)據(jù)的處理是這次的主體工作,小程序向用戶提供API,供用戶向自己的服務(wù)器請求數(shù)據(jù),值得一提的是友浸,開發(fā)小程序之前峰尝,需要先在微信公眾平臺申請appID偏窝,并且綁定域名,域名必須是https協(xié)議武学,然后在小程序的開發(fā)工具的配置信息中完善信息祭往,請求的地址需要在前面綁定的域名下。這個項目中用到wx.request從服務(wù)器拉取數(shù)據(jù)火窒。
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();
}
}
});
- 數(shù)據(jù)緩存
這里使用數(shù)據(jù)緩存是因為需要做一個搜索功能硼补,就涉及到頁面之間的數(shù)據(jù)傳遞,放在地址中也是一種方法熏矿,借用一下localStorage也可以已骇,使用wx.setStorage將數(shù)據(jù)存儲到localStorage中,頁面跳轉(zhuǎn)之后票编,在從localStorage中讀取就可以了褪储,讀取數(shù)據(jù)的時候分同步讀取和異步讀取。
-
剪切板的應(yīng)用
借用小程序的API可以很方便的將任何信息復(fù)制到剪切板慧域,然后就可以粘貼了鲤竹。
wx.setClipboardData({ data: '【' + that.data.couponData.list[e.currentTarget.id].goodsTitle + '】復(fù)制這條信息,打開【手機(jī)淘寶】' + 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}) } });
-
模板
在這個項目中昔榴,頁面基本很相似辛藻,但有細(xì)微差別碘橘,所以就使用了模板,新建一個template/template.wxml吱肌,name屬性必須要設(shè)置痘拆。<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='請輸入關(guān)鍵詞找券' 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(); //調(diào)用
-
redirectTo & navigateTo
redirectTo是重定向至某頁面,navigateTo是打開新的頁面勇边,值得說明的一點是犹撒,使用navigateTo打開的頁面太多會導(dǎo)致小程序卡頓。
-
分享
Page({ onShareAppMessage: function () { return { title: 'your title粒褒!', path: '/xxxx/xxxx/xxxx', //分享之后回到這個頁面 success: function(res) { f(); //成功回調(diào)识颊; }, fail: function(res) { f(); //失敗回調(diào); } } } })
-
提高列表滑動的流暢性
簡而言之就是頁面滾動到哪里奕坟,列表中的圖片就顯示到哪里祥款,實現(xiàn)方法如下。
//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; //列表中的每一項有一個標(biāo)記是否加載圖片的的屬性月杉,默認(rèn)false刃跛,隨著頁面滾動,一個個變成true苛萎。 } } this.setData({"couponData.list":temp}); temp = null; }, }) //wxml文件中在scroll-view上綁定事件桨昙。 <scroll-view class="section" scroll-y="true" bindscroll='loadImg'></scroll-view>