小程序相关封装

小程序相关封装

时间戳封装

文件 encapsulation.js

module.exports = {

  timestampToTime:(timestamp, connector, type)=> {

        var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000

        var Y = date.getFullYear() + connector;

	    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + connector;
	
	    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
	
	    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	
	    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
	
	    var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());

	    if (type == 'YMD') {
	
	      return Y + M + D;
	
	    } else {
	      return Y + M + D + h + m + s;
	    }
  },

}

文件 index.js

let enca = require(’…/…/public/encapsulation.js’) //路径自己改一下

onLoad:function(){
	   console.log(enca.timestampToTime(1234567890123, '-', 'YMDhms'))
}

showToast 封装

module.exports = {

// 提示 交互

  showToast : (title,icon) => {

	   wx.showToast({

 			  title: title,
	
		      icon: icon,

		      duration: 1500,
		
		      mask: true

	  });

  },

}

文件 index.js

let enca = require('../../public/encapsulation.js')   //路径自己改一下

onLoad:function(){

 enca.showToast("你好","none")

}

请求封装

const host = 'https:// ;//这个是你们的接口域名

function promise(url, params, method) {

    // 添加请求加载等待

    wx.showLoading({

      title: '加载中...'

    })

    return new Promise((resolve, reject) => {

        wx.request({

            url: `${url}`,

            data: params,

            method: method,

            header: {

                'content-type':'application/x-www-form-urlencoded'   //请求头

            },

          complete: (res) => {

            // 关闭等待动画

            wx.hideLoading()

                // 是否登录失效

                if(res.data.state == 0 && res.data.code == 97){

                    wx.showToast({

                        title: '登录失效,请重新登录',

                        icon: "none",

                        duration: 1500,

                    })

                    setTimeout(function(){

                        wx.reLaunch({

                            url: '/pages/user/index/index',  //失效后要跳转的页面

                        })

                    },1500)

                    return

                }

                resolve(res)

            },

            fail: reject

        })

    })

}

module.exports = {

    // 登录

    login: function (params) {

      return promise(host + '/login', params, 'POST')

            .then(res => res.data)

    },

}

index.js

onLoad:function(){

    api.login({

             // 需要给后台传的参数

           sort: 2,  

            start_pos: 0,

    }).then(res => {

      console.log(res)

    })

  },
  
   有哪里不足 下方欢迎评论 后期还会更新
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北境程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值