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

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

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?