不好意思打錯(cuò)了,是小工具
其實(shí)我是故意打錯(cuò)的
在此集成了各種小工具
廢話不多說(shuō),直接上代碼
1、分頁(yè)查詢
數(shù)據(jù)過(guò)多怎么辦?一個(gè)分頁(yè)就解決,麻麻再也不用擔(dān)心我的工作啦分頁(yè)思路:
把數(shù)據(jù)放入可滾動(dòng)視圖區(qū)域內(nèi),數(shù)據(jù)過(guò)多則視圖可以滾動(dòng),當(dāng)滾到底部的時(shí)候觸發(fā)hotActivityLoadMore方法帶上頁(yè)數(shù)請(qǐng)求下一頁(yè)數(shù)據(jù)
-
//bindscrolltolower的意思是拉到底部
-
-
<scroll-view scroll-y="truze" style="height: 1080rpx;" bindscrolltolower="hotActivityLoadMore">
-
-
<view wx:for="{{list}}" wx:for-item="item" >
-
-
循環(huán)展示的內(nèi)容體
-
-
</view>
-
-
</scroll-view>
js代碼
-
// pages/distribution/my_agent.js
-
-
var util = require('../../../utils/util.js')
-
-
var app = getApp()
-
-
Page({
-
-
data: {
-
-
myAgentList: {
-
-
hasMore: true,
-
-
list: [],
-
-
listSize: 0
-
-
},
-
-
startNum: 1,//頁(yè)數(shù)
-
-
},
-
-
onLoad: function (options) {
-
-
//加載頁(yè)面
-
-
var _that = this
-
-
var _url = '請(qǐng)求的地址';
-
-
wx.request({
-
-
url: _url,
-
-
data: [{
-
-
id: agentCode,
-
-
agentTel: '',
-
-
pageNum: 1,//請(qǐng)求第一頁(yè)
-
-
}],
-
-
method: 'post',
-
-
success: function (res) {
-
-
console.log(res.data)
-
-
-
-
if (res.data.code == '00') {
-
-
var listSize = 后臺(tái)返回的數(shù)據(jù).length;
-
-
var newData = _that.data.myAgentList.list;
-
-
newData = 后臺(tái)返回的數(shù)據(jù);
-
-
var newList = {};
-
-
//當(dāng)長(zhǎng)度小于10表示沒(méi)有下頁(yè)數(shù)據(jù)了
-
-
if (listSize < 10) {
-
-
newList.hasMore = falseo?;
-
-
} elseo? {
-
-
newList.hasMore = true;
-
-
}
-
-
newList.list = newData;
-
-
newList.listSize = listSize;
-
-
_that.seo?tData({ myAgentList: newList });
-
-
-
-
_that.seo?tData({
-
-
startNum: 2
-
-
})
-
-
} else {
-
-
util.msg("錯(cuò)誤", res.data.msg)
-
-
console.log("網(wǎng)絡(luò)通,數(shù)據(jù)不成功")
-
-
}
-
-
},
-
-
fail: function (res) {
-
-
console.log(res.data)
-
-
util.msg("錯(cuò)誤", "通訊失敗")
-
-
}
-
-
})
-
-
},
-
-
hotActivityLoadMore: function (e) {
-
-
var _that = this;
-
-
//判斷是否還有下一頁(yè)
-
-
if (_that.data.myAgentList.hasMore == true) {
-
-
var pageNum = _that.data.startNum
-
-
var _url = '請(qǐng)求的地址';
-
-
wx.request({
-
-
url: _url,
-
-
data: [{
-
-
id: agentCode,
-
-
linkTel: seek,
-
-
pageNum: pageNum //第N頁(yè)
-
-
}],
-
-
method: 'post',
-
-
success: function (res) {
-
-
console.log(res.data)
-
-
if (res.data.code == "00") {
-
-
console.log("網(wǎng)絡(luò)通,數(shù)據(jù)成功")
-
-
var listSize = 后臺(tái)返回的數(shù)據(jù).length;
-
-
var newData = _that.data.myAgentList.list;
-
-
//concat是在原有基礎(chǔ)上進(jìn)行增加
-
-
newData = newData.concat(后臺(tái)返回的數(shù)據(jù));
-
-
var newList = {};
-
-
//當(dāng)長(zhǎng)度等于于0表示當(dāng)前沒(méi)有數(shù)據(jù),停止
-
-
if (listSize == 0) {
-
-
newList.hasMore = false;
-
-
return;
-
-
//當(dāng)長(zhǎng)度小于10表示沒(méi)有下頁(yè)數(shù)據(jù)了
-
-
} else if (listSize <= 10) {
-
-
newList.hasMore = false;
-
-
} else {
-
-
newList.hasMore = true;
-
-
}
-
-
newList.list = newData;
-
-
newList.listSize = listSize;
-
-
_that.setData({ myAgentList: newList });
-
-
-
-
_that.setData({
-
-
startNum: pageNum + 1 //頁(yè)數(shù)加1
-
-
})
-
-
} else {
-
-
util.msg("錯(cuò)誤", res.data.msg)
-
-
console.log("網(wǎng)絡(luò)通,數(shù)據(jù)不成功")
-
-
}
-
-
-
-
},
-
-
fail: function (res) {
-
-
console.log(res.data)
-
-
util.msg("錯(cuò)誤", "通訊失敗")
-
-
console.log("失敗")
-
-
}
-
-
})
-
-
}
-
-
}
-
-
})
2、防止用戶多次提交,使用的遮罩層
實(shí)現(xiàn)很簡(jiǎn)單,按鈕加上disabled屬性,用true和false控制。js
-
data: {
-
-
hidden: true, //等待的展示與隱藏的控制
-
-
buthidden: false //按鈕的可用和不可用的控制
-
-
}
-
-
primary: function (e) {
-
-
//顯示等待、禁用按鈕,后臺(tái)返回錯(cuò)誤則將true和false對(duì)調(diào)一下就是隱藏等待、啟用按鈕
-
-
this.setData({
-
-
hidden: false,
-
-
buthidden: true
-
-
})
-
-
}
wxml
-
-
-
<loading hidden="{{hidden}}">
-
-
注冊(cè)中...
-
-
</loading>
-
-
<button class="next_step" type="primary" disabled="{{buthidden}}" bindtap="primary">下一步</button>
3、切割字符串(很少會(huì)用)
代碼和js一樣
-
var dateList = 需要切割的字符串.split("分隔符");
-
-
var arr = []
-
-
for (var i in dateList) {
-
-
arr = arr.concat(dateList);
-
-
console.log(arr)
-
-
}
4、保留2位小數(shù)
數(shù)據(jù).toFixed(2)
tip:數(shù)據(jù)必須是數(shù)字類型,不能是Str類型
5、跳轉(zhuǎn)傳值
在跳轉(zhuǎn)的時(shí)候用kv的形式進(jìn)行傳值
-
<navigator url="../../agent_index?k=v&k=v">
-
</navigator>
第二個(gè)頁(yè)面在onLoad里面打印options就可以看到上個(gè)頁(yè)面?zhèn)鬟^(guò)來(lái)的值
6、獲取當(dāng)前定位信息
-
//自動(dòng)獲取當(dāng)前位置
-
-
//獲取當(dāng)前位置經(jīng)緯度
-
-
wx.getLocation({
-
-
type: 'wgs84',
-
-
success: function (res) {
-
-
// 成功返回函數(shù)
-
-
var longitude = res.longitude
-
-
var latitude = res.latitude
-
-
//發(fā)送請(qǐng)求通過(guò)百度經(jīng)緯度反查地址信息
-
-
wx.request({
-
-
//百度地圖經(jīng)緯度反查路徑
-
-
url: 'http://api.map.baidu.com/geocoder/v2/?ak=btsVVWf0TM1zUBEbzFz6QqWF&location=' + latitude + ',' + longitude + '&output=json&pois=0',
-
-
data: {}, method: "get",
-
-
header: { 'Content-Type': 'application/json' },
-
-
success: function (ops) {
-
-
//成功返回一個(gè)result集合
-
-
console.log(ops)
-
-
//把地址放入需要展示的地方
-
-
_that.setData({
-
-
merchAddr: ops.data.result.formatted_address
-
-
})
-
-
}, fail: function () {
-
-
util.msg("提示", "定位失敗,請(qǐng)手動(dòng)輸入店鋪地址")
-
-
}
-
-
})
-
-
}
-
-
})
-