import { host } from './config' const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}` } const formatNumber = n => { n = n.toString() return n[1] ? n : `0${n}` } const http = (url, method, data, isLoading = true) => { if (isLoading) { wx.showLoading({ title: '加载中' }); } return new Promise(function (resolve, reject) { let token = getApp().globalData.token // wx.getStorage({ // key: 'token', // success (res) { // // console.log(res.data) // let token = res.data // }, // fail (res) { // console.log(res) // wx.hideLoading(); // } // }) let header = { 'token': token } if (token) { wx.request({ url: host + url, data: data, method: method, header, success: res => { if (isLoading) { wx.hideLoading(); } if (200 == res.data.code) { resolve(res.data); } else if (10008 === res.data.code && res.data.msg === "未注册!") { resolve(res.data) } else { wx.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }) } }, fail: res => { wx.hideLoading(); } }) } else { wx.showToast({ title: '未登录\r\n点击个人中心头像登录', icon: 'none', duration: 2000 }) } }) } module.exports = { formatTime, http }