// 日期格式化 /** * 格式化时间 * 调用formatDate(strDate, 'yyyy-MM-dd'); * @param strDate(中国标准时间、时间戳等) * @param strFormat(返回格式) */ export const formatDateTimeStr=(strDate: any, strFormat?: any)=>{ if (!strDate){ return; } if (!strFormat){ strFormat = 'yyyy-MM-dd HH:mm:ss'; } switch (typeof strDate) { case 'string': strDate = new Date(strDate.replace(/-/, '/')); break; case 'number': strDate = new Date(strDate); break; } if (strDate instanceof Date){ const dict: any = { yyyy: strDate.getFullYear(), M: strDate.getMonth() + 1, d: strDate.getDate(), H: strDate.getHours(), m: strDate.getMinutes(), s: strDate.getSeconds(), MM: ('' + (strDate.getMonth() + 101)).substr(1), dd: ('' + (strDate.getDate() + 100)).substr(1), HH: ('' + (strDate.getHours() + 100)).substr(1), mm: ('' + (strDate.getMinutes() + 100)).substr(1), ss: ('' + (strDate.getSeconds() + 100)).substr(1), }; return strFormat.replace(/(yyyy|MM?|dd?|HH?|mm?|ss?)/g, function () { return dict[arguments[0]]; }); } } export const formatTime = (date: 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(':') ) } export const formatDateStr = (date?: Date) => { if(!date){ date = new Date(); } const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); // return ( // [year, month, day].map(formatNumber).join('/') + // ' ' + // [hour, minute, second].map(formatNumber).join(':') // ) let datestr = [year, month, day].map(formatNumber).join('-'); console.log(datestr); return datestr; } export const formatTimeStr = (date?: Date) => { if(!date){ date = new Date(); } const hour = date.getHours(); const minute = 0; // return ( // [year, month, day].map(formatNumber).join('/') + // ' ' + // [hour, minute, second].map(formatNumber).join(':') // ) let datestr = [hour, minute].map(formatNumber).join(':'); console.log(datestr); return datestr; } // 数字格式化 const formatNumber = (n: number) => { const s = n.toString() return s[1] ? s : '0' + s } // URL参数处理 const getQueryString = (params: any):string => { let paramStrings = []; for (let key in params) { paramStrings.push(key + '=' + params[key]); } return paramStrings.length > 0 ? '?' + paramStrings.join('&') : ''; } // Base URL (注意:不要以 / 结尾) export const BASE_URL = "http://192.168.207.1:8085"; //export const BASE_URL = "https://gl.tsrlsf.com:13180"; export const buttonClicked= (self:any) =>{ self.setData({ buttonClicked: true }) setTimeout(function () { self.setData({ buttonClicked: false }) }, 1500) } // 发送请求 export const fetch = (option: any):Promise => { return new Promise((resolve, reject) => { // URL 处理 let url = option.url || ''; option.url = (url.indexOf('http://') != -1 || url.indexOf('https://') != -1) ? url : BASE_URL + url; // 处理 QueryString if (option.params) { let queryString = getQueryString(option.params); option.url = option.url + queryString; } // 请求头 let headers = option.headers || {}; let token = wx.getStorageSync('SYS_TOKEN'); // 填充 TOKEN if (!token) { wx.showToast({ title: '当前用户尚未登录', icon: 'none' }); reject(); } headers['Authorization'] = "Bearer " + token; option.header = headers; // 兼容性处理:umi request 的请求头参数是带s的(headers),小程序的是不带s的(header) // 发起请求 wx.request({ ...option, success: (res:any) => { if (res.data.code === 10010) { wx.showToast({ title: '用户凭证过期,请重新登录', icon: "none" }); } // 同时兼容回调函数和 Promise 两种写法 if (option.success) { if (option.getReponse) { option.success(res); } else { option.success(res.data); } } if (option.getReponse) { resolve(res); } else { resolve(res.data); } }, fail: (err) => { // 同时兼容回调函数和 Promise 两种写法 if (option.fail) { option.fail(err); } reject(err); } }) }); } export const wxLogin = ()=>{ wx.qy.login({ success: res => { console.log(res.code) // 发起登录请求 wx.request({ method: 'POST', url: BASE_URL + '/api/login', data: { weChatCode: res.code }, success: (res: any) => { let result = res && res.data; wx.hideLoading(); if (result.code === 0) { // 登录成功,暂存 TOKEN wx.setStorageSync('SYS_TOKEN', result.data); wx.showToast({ title: '登录成功', icon: 'none' }); } else if (result.code === 10013) { wx.showToast({ icon: "none", title: result.msg }); wx.navigateTo({ url: '/pages/report/detail/reportDetail' }); } else { wx.showToast({ title: `登录失败 [${result.code}] ${result.msg}`, icon: 'none' }); } }, fail: (err) => { console.error('登录失败 [2]', err); wx.hideLoading(); wx.showToast({ title: '登录失败 [2] 网络异常', icon: 'none' }); } }); // 发送 res.code 到后台换取 openId, sessionKey, unionId }, }) }