1. 程式人生 > >時間戳轉換成日期格式,以及倒計時函數封裝

時間戳轉換成日期格式,以及倒計時函數封裝

post方式 計算 tin nav 加載 拓展 檢測 請求參數 request

  1 // 加載配置文件
  2 const config = require(‘config.js‘);
  3 var app=getApp();
  4 module.exports = {
  5   /**
  6    * get方式請求,ulr是請求api號,token是登陸token,不用token就傳空,fn是函數成功的回調函數,data為向後臺傳遞的參數by:張濤20180303
  7    */
  8   GET: function (url = ‘‘,token=‘‘ ,data = {}, fn,fail) {
  9     wx.request({
 10       url
: config.API_HOST + url,//請求地址 11 method: ‘get‘,//請求方式 12 data: data,//請求參數 13 header: { "Content-Type": "application/json" ,‘token‘:token}, 14 success: function (res) { 15 // 判斷token是否失效 16 if (res.data.code==‘JWT00002‘||res.data.code==‘JWT00001‘||res.data.code==‘JWT00004‘||res.data.code==‘403‘||res.data.code==‘500‘) {
17 wx.navigateTo({ 18 url:‘/pages/login/login‘ 19 }) 20 return false; 21 } 22 fn(res); 23 }, 24 fail: function (res) { 25 // wx.showToast({ 26 // title: ‘請求服務器失敗,請稍後再試!‘, 27 // icon: ‘loading‘, 28 // duration: 2000
29 // }) 30 } 31 }); 32 }, 33 34 /** 35 * post方式請求 36 */ 37 POST: function (url = ‘‘,token=‘‘, data = {}, fn ,fail) { 38 wx.request({ 39 url: config.API_HOST + url,//請求地址 40 method: ‘post‘,//請求方式 41 data: data,//請求參數 42 header: { "Content-Type": "application/json",‘token‘:token}, 43 success: function (res) { 44 // 判斷token是否失效 如果失效就跳轉登錄頁面 45 if (res.data.code==‘JWT00002‘||res.data.code==‘JWT00001‘||res.data.code==‘JWT00004‘||res.data.code==‘403‘||res.data.code==‘500‘) { 46 wx.navigateTo({ 47 url:‘/pages/login/login‘ 48 }) 49 return false; 50 } 51 fn(res); 52 }, 53 fail: function (res) { 54 // wx.showToast({ 55 // title: ‘請求服務器失敗,請稍後再試!‘, 56 // icon: ‘loading‘, 57 // duration: 2000 58 // }) 59 } 60 }); 61 }, 62 /* 63 *時間戳格式修改公共函數 64 *timestamp為後臺傳遞的時間戳 65 *type為時間顯示的不同方式 66 *bol:判斷是否需要時分秒默認不要 67 *主要用來分割年月日 68 *後期可以擴展年月日時分秒。 69 *by:張濤 20180305 70 */ 71 72 setTime:function(timestamp,type,bol){ 73 var unixTimestamp = new Date(timestamp) ; 74 // 首先判斷是否需要時分秒 75 if (bol) { 76 //設置不同的格式 77 Date.prototype.toLocaleString = function() { 78 return this.getFullYear() + type + (this.getMonth() + 1) + type + this.getDate()+‘ ‘+ this.getHours() + ":" + this.getMinutes(); 79 }; 80 }else{ 81 //設置不同的格式 82 Date.prototype.toLocaleString = function() { 83 return this.getFullYear() + type + (this.getMonth() + 1) + type + this.getDate(); 84 }; 85 } 86 return unixTimestamp.toLocaleString(); 87 }, 88 // 時間戳倒計時函數,根據時間戳差值計算剩余時間 89 /* 90 *時間:timestamp(非毫秒級),fn回調函數,參數可定義 91 *暫時為天小時分鐘秒,後期可拓展by:張濤20180305 92 * 93 *第一種只進行倒計時解析 94 *第二種倒計時實時顯示 95 */ 96 downTime:function(timestamp,type,fn){ 97 // 只解析剩余時間 98 if (type==1) { 99 var time={ 100 day:‘‘, 101 hour:‘‘, 102 minute:‘‘, 103 second:‘‘ 104 } 105 time.day=Math.floor(timestamp / (24*3600)); 106 time.hour=Math.floor((timestamp-time.day*24*3600)/3600); 107 time.minute=Math.floor((timestamp-time.day*24*3600-time.hour*3600)/60); 108 time.second=Math.floor(timestamp-time.day*24*3600-time.hour*3600-time.minute*60); 109 return time; 110 }else if (type==2) { 111 var day,hour,minute,second,time; 112 // 解析剩余時間,並進行動態顯示 113 var timer = setInterval(function () { 114 timestamp--; 115 if (time == 0) { 116 clearInterval(timer) 117 }else{ 118 day=Math.floor(timestamp / (24*3600)); 119 hour=Math.floor((timestamp-day*24*3600)/3600); 120 minute=Math.floor((timestamp-day*24*3600-hour*3600)/60); 121 second=Math.floor(timestamp-day*24*3600-hour*3600-minute*60); 122 } 123 time={ 124 day:day, 125 hour:hour, 126 minute:minute, 127 second:second 128 } 129 //倒計時的回調函數(參數)天,時,分,秒 130 fn(time); 131 }, 1000) 132 } 133 }, 134 /* 135 *檢測用戶是否登錄的函數 136 * 137 */ 138 checkLogin:function(){ 139 if (app.globalData.loginInfo==‘‘||app.globalData.loginInfo==‘underfind‘||app.globalData.loginInfo==null) { 140 wx.navigateTo({ 141 url:‘/pages/login/login‘ 142 }) 143 // 阻止頁面邏輯繼續執行 144 return false; 145 } 146 return true; 147 } 148 149 }

時間戳轉換成日期格式,以及倒計時函數封裝