极乐门资源网 Design By www.ioogu.com

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

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.ioogu.com
极乐门资源网 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com

评论“微信小程序工具函数封装”

暂无微信小程序工具函数封装的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。