小程序获取页面的当前实例方法getCurrentPages()

小程中this.setData({},callback)方法是异步方法

1.小程序中的请求

习惯了jQ中的ajax,方式简洁,在微信中的请求过于繁琐,自己动手简单的写下

#创建ajax.js
function ajaxFactory(){
    let configData = {
        taoke_id:1,  //ext.taoke_id,
        client_id:'pcselection',
        page_size:10
    }
    //get方法
    function get(url,data,callback){
        data = Object.assign({},configData,data);
        if('' == url){
            let err = new Error('请输入请求url');
            throw err;
            return ;
        }
        wx.request({
            url: url,
            data: data,
            header: {
                'content-type': 'application/x-www-form-urlencoded'  //序列化
            },
            method:'GET',
            dataType:'json',
            success: function(res) {
                if(callback){
                    callback(res.data);
                }
            },
            fail:function(log){
                console.log(log);
            }
        });
    }
    //post方法
    function post(url,data,callback){
        data = Object.assign({},configData,data);
        if('' == url){
            let err = new Error('请输入请求url');
            throw err;
            return ;
        }
        wx.request({
            url: url,
            data: data,
            header: {
                'content-type': 'application/x-www-form-urlencoded' //序列化
            },
            method:'POST',
            dataType:'json',
            success: function(res) {
                if(callback){
                    callback(res.data);
                }
            },
            fail:function(log){
                console.log(log);
            }
        });
    }

    return {
        get:get,
        post:post
    }
}

export const ajax = {
    get:ajaxFactory().get,
    post:ajaxFactory().post
}

在如a.js中使用

import { ajax } from "path/ajax.js";  //path就是路径

//post请求
ajax.post('http:www.baidu.com',{
	key1:value1,
	key2:value2
},data => {
	//返回成功后处理的回调
});

//get请求
ajax.get('http:www.baidu.com',{
	key1:value1,
	key2:value2
},data =>{
    //返回成功后处理的回调
});

有没有回到jQ的用法,现在可以大喊一声,我 胡汉三又回来了!

2.获得当前页面url

#01.获取当前页面的url
export function getCurrentPageUrl(){
    let pages = getCurrentPages();    //获取加载的页面
    let currentPage = pages[pages.length-1];    //获取当前页面的对象
    let url = currentPage.route;    //当前页面url
    return url;
}

#02.获取当前页带参数的url
export function getCurrentPageUrlWithArgs(){
    let pages = getCurrentPages();    //获取加载的页面
    let currentPage = pages[pages.length-1];    //获取当前页面的对象
    let url = currentPage.route;    //当前页面url
    let options = currentPage.options;    //如果要获取url中所带的参数可以查看options

    //拼接url的参数
    var urlWithArgs = url + '?';
    for(let key in options){
        let value = options[key];
        urlWithArgs += key + '=' + value + '&';
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length-1);

    return urlWithArgs;
}

#03.获取当前页面参数
export function getCurrentPageParam(){
    let pages = getCurrentPages();    //获取加载的页面
    let currentPage = pages[pages.length-1];    //获取当前页面的对象
    let options = currentPage.options;    //如果要获取url中所带的参数可以查看options
    return options;
}

3.小程序倒计时

const countDown = (self,endData) =>{
  let endTime = endData * 1000;
  let nowTime =  Date.now();

  if(0 == endTime - nowTime ){
    self.setData({endData:'已结束'});
    return false;
  }else{
    let changeTime = (endTime - nowTime) - 1000,
        tmp = msTransformDate(changeTime),
        day = tmp.day,
        h = 10 >tmp.h?'0' + tmp.h:tmp.h,
        m = 10 >tmp.m?'0'+ tmp.m:tmp.m,
        s = 10 > tmp.s?'0' + tmp.s:tmp.s;
    let text = day +''+h+'小时 '+m+'分钟 '+s+'';
    self.setData({endData:text});
  }
  setTimeout(function(){
    countDown(self,endData);
  },1000);
}
const msTransformDate = ms =>{
  let day = parseInt(ms/1000/60/60/24),
      h = parseInt((ms-(day*24*60*60*1000))/1000/60/60),
      m = parseInt((ms - (day*24*60*60*1000) - (h*60*60*1000))/1000/60),
      s = parseInt((ms - (day*24*60*60*1000) - (h*60*60*1000) - (m*60*1000))/1000);
  return {
    day:day,
    h:h,
    m:m,
    s:s
  }
}