// 日期格式化 import {encrypt,decrypt} from '../utils/des2.js' import constant from './constant.js'; /** * 格式化时间 * formatDateTimeStr(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) => { if(!date){ date = new 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 formatDate = (date?: Date) => { if(!date){ date = new Date(); } const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); let datestr = [year, month, day].map(formatNumber).join('-') 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 } export const dateChange=(num = 1,date?:Date) =>{ let dateStr = "" if (!date) { date = new Date();//没有传入值时,默认是当前日期 dateStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(); } dateStr += " 00:00:00";//设置为当天凌晨12点 let dateNum = Date.parse(dateStr)/1000;//转换为时间戳 dateNum += (86400) * num;//修改后的时间戳 var newDate = new Date(dateNum* 1000);//转换为时间 return newDate.getFullYear() + '-' + (newDate.getMonth() + 1) + '-' + newDate.getDate(); } // 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 LOGIN_URL = "http://192.168.1.107:8085"; export const BASE_URL = 'http://zydh.gexuejy.com:8088/' //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 genToken = (wechatID?:string)=>{ let randomStr = getRandomString(20) let nowStr = formatTime(); let userid = wx.getStorageSync(constant.WECHATID) if(wechatID){ userid = wechatID; } if(!userid){ wx.showModal({title:"token生成失败,请联系管理员!",showCancel:false}) } let token = randomStr.concat(";",nowStr,";",userid); // console.log("token",token) let tokenEncode = encrypt(token); return tokenEncode; } /** * 随机生成字符串 * @param len 指定生成字符串长度 */ function getRandomString(len:number){ let _charStr = 'abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789', min = 0, max = _charStr.length-1, _str = ''; //定义随机字符串 变量 //判断是否指定长度,否则默认长度为15 len = len || 15; //循环生成字符串 for(var i = 0, index; i < len; i++){ index = (function(randomIndexFunc, i){ return randomIndexFunc(min, max, i, randomIndexFunc); })(function(min, max, i, _self){ let indexTemp = Math.floor(Math.random()*(max-min+1)+min), numStart = _charStr.length - 10; if(i==0&&indexTemp >=numStart){ indexTemp = _self(min, max, i, _self); } return indexTemp ; }, i); _str += _charStr[index]; } return _str; }