本文实例为大家分享了微信小程序工具函数封装的具体代码,供大家参考,具体内容如下
const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } const formatNumber = n => { n = n.toString() return n[1] "8888": wx.navigateTo({ url: '../hotbook/hotbook"9999": { wx.navigateTo({ url: '../book-detail/book-detail"", WordToHexValue_temp = "", lByte, lCount; for (lCount = 0; lCount <= 3; lCount++) { lByte = (lValue > (lCount * 8)) & 255; WordToHexValue_temp = "0" + lByte.toString(16); WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2); } return WordToHexValue; } function Utf8Encode(string) { var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if ((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c 12) | 224); utftext += String.fromCharCode(((c 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; } function requestLoading(url, params, message) { let promise = new Promise((resolve, reject) => { wx.showNavigationBarLoading() if (message != "") { wx.showLoading({ title: message }) } var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; params['openid'] = wx.getStorageSync('openid'); var newParams = this.objKeySort(params); newParams['unionid'] = wx.getStorageSync('unionid'); newParams['antime'] = timestamp; newParams['key'] = this.jiamiParams(newParams); wx.request({ url: url, data: newParams, header: { 'content-type': 'application/x-www-form-urlencoded' }, method: 'post', success: function (res) { wx.hideNavigationBarLoading() if (message != "") { wx.hideLoading() } if (res.statusCode == 200) { resolve(res.data) } else { reject(res) } }, fail: function (res) { wx.hideNavigationBarLoading() if (message != "") { wx.hideLoading() } reject('网络出错!') } }) }) return promise } module.exports = { http: http, formatTime: formatTime, requestLoading: requestLoading, tiaozhuan: tiaozhuan, tiaozhuanAd: tiaozhuanAd, objKeySort: objKeySort, jiamiParams: jiamiParams }
这个工具函数中用promise封装了微信小程序数据请求的方法:单独拿出来(但不能单独使用哦,因为还需要借助上面的一些工具函数)
function requestLoading(url, params, message) { let promise = new Promise((resolve, reject) => { wx.showNavigationBarLoading() if (message != "") { wx.showLoading({ title: message }) } var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; params['openid'] = wx.getStorageSync('openid'); var newParams = this.objKeySort(params); newParams['unionid'] = wx.getStorageSync('unionid'); newParams['antime'] = timestamp; newParams['key'] = this.jiamiParams(newParams); wx.request({ url: url, data: newParams, header: { 'content-type': 'application/x-www-form-urlencoded' }, method: 'post', success: function (res) { wx.hideNavigationBarLoading() if (message != "") { wx.hideLoading() } if (res.statusCode == 200) { resolve(res.data) } else { reject(res) } }, fail: function (res) { wx.hideNavigationBarLoading() if (message != "") { wx.hideLoading() } reject('网络出错!') } }) }) return promise }
使用方法:
util1.requestLoading(url, param, "正在加载数据").then(res => { if (res.status == 1) { let bannerList = res.data that.setData({ bannerList }); } }).catch(()=>{ wx.showToast({ title: '加载数据失败', }) })
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年12月25日
2024年12月25日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]