微信小程序> 小程序常用的一些公用方法封装-网站封装小程序方法-网页封装小程序

小程序常用的一些公用方法封装-网站封装小程序方法-网页封装小程序

浏览量:1891 时间: 来源:jeremy-09
1.

1、ajax请求

constwxPromisifyfn(obj{})newPromise((resolve,reject){obj.successresresolve(res)//成功(可在里面加处理,如登录超时等)obj.failresreject(res)//失败fn(obj)})constajaxFns(url,data,method,contentType)wxPromisify(wx.request)({url:url,data:data,method:method?method:'POST',header:{cookie:getApp().globalData.token?getApp().globalData.token:'','content-type':contentType?contentType:'application/x-www-form-urlencoded'}})例子:ajaxFns(url,data,method,contentType).then(res{})2.

2、时间格式转换

constformatTime(date,type){letyeardate.getFullYear()//年letmonthdate.getMonth()+1//月letdaydate.getDate()//日lethourdate.getHours()//时letminutedate.getMinutes()//分letseconddate.getSeconds()//秒letgetMillisecondsdate.getMilliseconds()//毫秒if(type1){//2019-12-1212:12:12return[year,month,day].map(formatNumber).join('-')+''+[hour,minute,second].map(formatNumber).join(':')}elseif(type2){//2019年12月12日if(month10)month'0'+monthif(day10)day'0'+dayreturnyear+'年'+month+'月'+day+'日'}elseif(type3){//时间戳123456789returndate.getTime()}}constformatNumbern{nn.toString()returnn[1]?n:'0'+n}例子:formatTime(newDate(),type)3.

3、页面跳转

constgotoPage(url,param){wx.navigateTo({url:parseUrl(url,param)})}例子:gotoPage(url,{param:param})constgotoTab(url,param){wx.reLaunch({url:parseUrl(url,param)})}例子:gotoTab(url,{param:param})constbackPage(val){//返回堆栈上级页面层数注:redirectTo页面不会将页面加入堆栈wx.navigateBack({delta:val})}例子:backPage(num)//返回几层就输多少constparseUrl(url,param){if(typeof(param)!"object")returnurlvarstr''for(letiinparam){str+(''+i+''+param[i])}if(url.indexOf('?')-1){returnurl+str}else{returnurl+'?'+str.substr(1)}}4.

4、排序

constsortFns(arr,param){//数组排序参数returnarr.sort(compare(param))}constcompare(property){return(a,b){varvalue1a[property]varvalue2b[property]returnvalue1-value2}}例子:sortFns(arr,'param')5.

5、缓存

constsetStorage(key,data){//存wx.setStorageSync(key,data)}constgetStorage(key){//取returnwx.getStorageSync(key)}constremoveStorage(key){//删wx.removeStorageSync(key)}6.

6、url截取参数

constgetUrlParam(url,param){letregnewRegExp("(^|)"+param+"([^]*)(|$)");//构造一个含有目标参数的正则表达式对象letindexurl.indexOf("?")letrurl.substring(index).substr(1).match(reg)//匹配目标参数if(r!null)returnunescape(r[2])returnnull//返回参数值}7.

7、提供2个数生成区间数组

constrangeArray(start,end)Array.apply(null,{length:end-start+1}).map((v,i)i+start)8.

8、保留小数

consttoMoney(num)num.toFixed(2).split('').reverse().join('').replace(/(d{3})/g,'$1,').replace(/,$/,'').split('').reverse().join('')9.

9、菊花遮罩

constshowLoading(title){wx.showLoading({title:title,mask:true})}consthideLoading(){wx.hideLoading()}constshowToast(title,icon'none'){wx.showToast({title,icon,duration:1500,mask:true})}consthideToast(){wx.hideToast()}10.

10、数字转中文汉字

consttoChinesNum(num){constchangeNum['零','一','二','三','四','五','六','七','八','九']constunit["","十","百","千","万"]numparseInt(num)letgetWan(temp){  letstrArrtemp.toString().split("").reverse()  letnewNum""  for(vari0;istrArr.length;i++){newNum(i0strArr[i]0?"":(i0strArr[i]0strArr[i-1]0?"":changeNum[strArr[i]]+(strArr[i]0?unit[0]:unit[i])))+newNum} returnnewNum}letoverWanMath.floor(num/10000)letnoWannum%10000if(noWan.toString().length4)noWan"0"+noWanreturnoverWan?getWan(overWan)+"万"+getWan(noWan):getWan(num)}

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎