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 }