UNPKG

miue-ui

Version:

ali miniProgram UI components for vehicle.

79 lines (72 loc) 1.76 kB
/** * author:dansion@163.com * data:2022-04-25 * version:V1.0.0 * modfiny: * 1.0.1 */ const _key='PlayHistory'; const history={}; var _data=[]; var _max = 99; // 读取本地存储数据。 let _read=(key)=>{ return new Promise((resolve, reject) => { my.getStorage({ key: key, success: (res)=> { // 格式化统一日期和时间的 //res.data.forEach((ele) => { // ele.update=new Date(ele.update).toLocaleString(); //}) resolve(res); }, fail:(err)=>{ reject(res); } }) // }) } // 保存数据到本地。 let _save=(key,data)=>{ my.setStorage( { key:key, data:data, success:()=>{ /** 写入成功 */}, fail:()=>{/** 写入失败 */}, } ) } // 加载本地数据。 history.read=()=>{ ///console.warn("history loading......"); return _read(_key) } history.push=(item)=>{ // 如果item为空或是null ,则自动返回。 if(item==undefined || item ==null){ return true; } _read(_key).then( (res)=>{ // 当res 获取 有问题的处理机制。 let _data=res.data || []; // res.data =null ,undefine,空,的时,默认变更为 [] 数组对象。 // 添加时间字符串 let _now = new Date(); /* 2022-0-12 12:12:12 */ //let _now_str= `${_now.getFullYear()}-${_now.getMonth()+1}-${_now.getDate()} ${_now.getHours()}:${_now.getMinutes()}:${_now.getSeconds()}` let _now_str=_now.toLocaleString(); item.update=_now_str; _data.unshift(item); // 当数据超过定制长度之后,进行删除 if(_data.length > _max){ _data.pop(); } _save(_key,_data); }, (error)=>{} ); } export default history;