// 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 app = getApp(); const PATH = app.globalData.path; module.exports = { // formatTime, request: request, post: post, get: get, toast: toast, formatDate: formatDate } /** * POST请求 * @param {*} options */ function post(options) { wx.showLoading({ title: '加载中...', mask: true }); let header = { 'Authorization': app.globalData.Authorization, } if (options.header) { options.header = Object.assign(options.header, header); } console.info(options.header) wx.request({ url: PATH + options.url, method: 'POST', data: options.data || {}, header: options.header || header, success: options.success || function (res) { console.info(res); wx.hideLoading(); }, fail: options.fail || function (e) { console.info(e); wx.hideLoading(); wx.showToast({ title: '请求失败', icon: 'error' }); } }); } /** * GET请求 * @param {*} options */ function get(options) { wx.showLoading({ title: '加载中...', mask: true }); let header = { 'Authorization': app.globalData.Authorization, } if (options.header) { options.header = Object.assign(options.header, header); } console.info(options.header) console.info(header) wx.request({ url: PATH + options.url, method: 'GET', data: options.data || {}, header: options.header || header, success: options.success || function (res) { console.info(res); wx.hideLoading(); }, fail: options.fail || function (e) { console.info(e); wx.hideLoading(); wx.showToast({ title: '请求失败', icon: 'error' }); } }); } /** * 请求 * @param {*} options */ function request(options) { wx.showLoading({ title: '加载中...', mask: true }); let header = { 'Authorization': app.globalData.Authorization, } if (options.header) { options.header = Object.assign(options.header, header); } console.info(options.header) wx.request({ url: PATH + options.url, method: options.method || 'GET', data: options.data || {}, header: options.header || header, success: options.success || function (res) { console.info(res) wx.hideLoading(); }, fail: options.fail || function (e) { console.info(e) wx.hideLoading(); wx.showToast({ title: '请求失败', icon: 'error' }); } }); } function toast(msg, icon) { wx.showToast({ title: msg || 'title', icon: icon || 'none', duration: 3000 }); } function formatDate(date, fmt) { if (date) { date = new Date(date); } else { date = new Date(); } fmt = fmt || 'yyyy-MM-dd HH:mm'; let ret; const opt = { "y+": date.getFullYear().toString(), // 年 "M+": (date.getMonth() + 1).toString(), // 月 "d+": date.getDate().toString(), // 日 "H+": date.getHours().toString(), // 时 "m+": date.getMinutes().toString(), // 分 "s+": date.getSeconds().toString() // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 }; for (let k in opt) { ret = new RegExp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0"))) }; }; return fmt; }