网络编程 
首页 > 网络编程 > 浏览文章

微信小程序工具函数封装

(编辑:jimmy 日期: 2024/11/27 浏览:3 次 )

本文实例为大家分享了微信小程序工具函数封装的具体代码,供大家参考,具体内容如下

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: '加载数据失败',
  })
 }) 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:JavaScript制作3D旋转相册
下一篇:js实现3D照片墙效果
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap