自己常用的微信小程序的封装函数,开发速度up!up!

1.单提示函数(应用场景只想用户点击确认执行的时候)

function showModal(cont,flag,fn){
	  wx.showModal({
	    title: '提示',
	    content: cont,
	    showCancel:flag,
	    success(res) {
	      if (res.confirm) {
	        
	      } else if (res.cancel) {
	      }
	    },
      complete(){
        fn()
        wx.hideLoading()
      }
})
};
fn.showModal('单确定执行',false,()=>{})

2.post请求方式

function _post(url, data, success, fail) {
  wx.request({
    url: http + url,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    data: data,
    success (res) {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      console.log(res.data)
      if(!res.data.status && res.data.status!=0){
        //后台没有数据返回执行
        return;
      }else if(res.data.status == 600){
        //后台状态返回执行
        return
      }else if(res.data.status == 12){
        return
      }
      success(res);
    },
    fail (res) {
      fail(res);
    }
  });
}
fn._post('xxx/xx/xx',{data:data},res=>{},res=>{})

3.基础用户信息的获取

function baseLogin(fn){
  wx.login({
    success (res) {
      var code = res.code;
      wx.getUserInfo({
        success(res) {
          var encryptedData = res.encryptedData;
          var iv = res.iv;
          fn(encryptedData,code,iv)//这里获取到的数据可以去和后台换用户的资料 unionid/用户基本信息之类的
        },
        fail (res) {
          wx.reLaunch({
            url: '/pages/index/NearbyMerchants/nearShopList/wsq/wsq'
          })
        },
      })
    }
  })
}

4.获取自定义data-值

// 获取data-index 属性
function dataIndex(e){
  var a = e.currentTarget.dataset.index;
  var b = e.currentTarget.dataset.id;
  var d = e.currentTarget.dataset.type;
  var e = e.currentTarget.dataset.name;
  var c= [a,b,d,e];
  return c
}
let index = fn.dataIndex(e)[0]

5.图片上传的封装处理

function upImgAllSuccssAll(res,formData,success,url){
  var upImgBoxArr=res.tempFilePaths;
  var imgarr = [];
  for(var j in upImgBoxArr){
    if(res.tempFiles[j].size>2097152){
      http.showModal('有图片已超过2MB,您可以选择压缩图片以后重新上传',false,()=>{})
      continue;
    }
   var turnsImgUrl = wx.uploadFile({
      url: http+url,
      filePath: upImgBoxArr[j],
      name: 'fileData',//这里根据自己的实际情况改
      herder:{'content-type':'multipart/form-data;'},
      formData:formData,
      success: (resp) => {
        success(resp)
      },
    });
  }
}
fn.upImgAllSuccssAll(图片选择完成后的res,需要往后台传递的参数,成功后执行的方法,上传图片的路径)

6.检测左右滑动的封装方法

function moveFn(e,touchDotY,touchDot,time,fn,fn1,flag){
  let touchMove = e.touches[0].pageX;
  let touchMoveY = e.touches[0].pageY;
  // 检测是否点击以后上下滑动//清空时间数据
  if(Number(touchDotY)-Number(touchMoveY)>15 || Number(touchDotY)-Number(touchMoveY)<-15){
    time=0;
    return;
  }
  //向左滑动
  if(touchMove - touchDot <= -60 && !flag && time>=2) {
    fn()
  }
  //向右滑动
  if (touchMove - touchDot >= 60 && !flag && time>=2) {
    fn1()
  }
}

滑动一般都是三个事件的,下面是使用方法

// 初始点击
  touchstartFoot(e){
    var that = this;
    this.data.touchDot = e.touches[0].pageX;
    this.data.touchDotY = e.touches[0].pageY;
    this.data.interval = setInterval(function(){
      that.data.time+=1;
    },40);
  },
  // 结束点击
  touchendFoot(e){
    clearInterval(this.data.interval);
    this.data.time = 0;
    this.data.done = false;
  },
  // 移动删除
  delMove(e){
    var that = this;
    let touchDotY = this.data.touchDotY;
    let touchDot = this.data.touchDot;
    let time = this.data.time;
    let delstyle = that.data.delstyle;
    var index = http.dataIndex(e)[0];
    http.moveFn(e,touchDotY,touchDot,time,()=>{console.log('左滑动')},()=>{console.log('右滑动')},this.data.done)
  },

7.input内容获取

// 获取input框里面所有的数据
function inputArr(arr,e){
  var index = dataIndex(e)[0];
  var a = arr;
  var b = e.detail.value;
  a[index] = b;
  return a
}
fn.inputArr(存放input内容的数组,输入input的时候的e值{里面需包含当前输入的index用于定位})

8.支付的封装

// 支付的封装
function payFn(data,fn,fn2){
  var payT = data.jsApiParameters;
  wx.requestPayment({
     'timeStamp': payT.timeStamp,
     'nonceStr': payT.nonceStr,
     'package': payT.package,
     'signType': 'MD5',
     'paySign': payT.paySign,
     'success'(res){
        fn(res)
     },
     'fail'(res){
        fn2(res)
     }
  })
}

9.数字隐私处理

// 数字隐私处理
const passStrat = num => {
  var newTel = []
  for(let i = 0;i=3 && i<=7){
    aa = '*'
   }
   newTel.push(aa);
  }
  var newTelS = newTel.join("");
  return newTelS
}

10.倒计时

// 直接输出倒计时封装
const dayTime = bb => {
    var bb = bb
    var day = parseInt(bb/86400);
    var time = parseInt((bb-(day*86400))/3600);
    var min =  parseInt((bb-(time*3600+day*86400))/60)
    var sinTime = time*3600+min*60+day*86400
    var sinTimeb;
    var sin1 =  parseInt((bb-sinTime))
    var thisTime = addEge(day)+"天"+addEge(time)+":"+addEge(min)+":"+addEge(sin1);
    bb<=0?thisTime = "0天00:00:00":thisTime
    return thisTime
}
const addEge = a => {
  return a<10?a = "0"+a:a = a
}
// 输出数组倒计时封装
const dayTimeArr = bb => {
    var bb = bb
    var day = parseInt(bb/86400);
    var time = parseInt((bb-(day*86400))/3600);
    var min =  parseInt((bb-(time*3600+day*86400))/60)
    var sinTime = time*3600+min*60+day*86400
    var sinTimeb;
    var sin1 =  parseInt((bb-sinTime))
    var timeArr = [addEge(day),addEge(time),addEge(min),addEge(sin1)];
    if(bb<=0){
      timeArr = ["00","00","00","00"];
    }
    return timeArr
}

11.检测当前机型的长宽(部分安卓机匹配不准确,还是推荐使用节点匹配)

// 检测当前机型的长宽
const nowPhoneWH =()=>{
  var a = []
  var res = wx.getSystemInfoSync()
  a[0] = res.windowWidth;
  a[1] = res.windowHeight;
  return a
}

12.至少十八岁的事件选择输出

const formatTime18 = date => {
  const year = Number(date.getFullYear())-18
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}








你可能感兴趣的:(微信小程序)