/* 字典模块 作者:AC 时间:2019/12/24 14:00 说明:所有其中的数据都不会显示在Storage中 一定程度上防止了泄漏 主要方法说明: AddKeyValue:添加键值对 DeleteValueByKey:根据键删除值 GetValueByKey:根据键获取值 ClearDictionary:清空字典内容 GetDictionary:获取字典 GetValueByKeyAndFun:根据键获取值 并且运行一个指定的函数(要求此函数必须返回这个键对应的值) 主要参数说明: dictionary:字典 httpDictionary: 字典操作 code:状态值 key:字典的键 value:字典的值 fun:外部函数(要求此函数必须返回这个键对应的值) 操作日志: 1.初步搭建 2019/12/14 14:00 AC */ /* 以下发出的功能均经过项目中的实际使用 在uni-app 中的微信小程序项目下 可以使用 避免使用Storage的一系列坑 不过此数据字典 会在每次用户关闭小程序或者关闭页面的时候清空. 仅仅可以用在小程序的优化访问中 一些静态数据的存储以及修改 以及一些临时数据的存储 需要在初始化的时候进行创建 详见APP.VUE 示例 */ var message=require("script/Message.js"); //初始化请求字典集合 var dictionary={}; //执行结果值 var value=""; var httpDictionary={ //添加/修改字典值-如果有这个key 则会覆盖其中原先的值 AddKeyValue:function(key,value){ dictionary[key]=value; }, //删除key的值 DeleteValueByKey:function(key){ return DeleteValue(key); }, //根据key获取值 GetValueByKey:function(key){ return GetValues(key); }, //清空字典的所有值 恢复成默认值 ClearDictionary:function(){ dictionary={}; }, //获取整个字典 GetDictionary:function(){ return dictionary; }, //获取字典的值 并且执行fun函数 如果fun是一个函数 结果fun必须返回一个字符串 GetValueByKeyAndFun:function(key,fun){ return typeof fun == "function"? fun(this.GetValueByKey(key)):this.GetValueByKey(key); } } //获取value值 function GetValues(key){ try{ //键为空 增加提示信息 if(key==""){ return message.SetMessage("A002","在执行获取字典内容的时候,键为空","",1); } console.log("值:"+dictionary[key]); return (typeof dictionary[key] == "undefined"?"":dictionary[key]); }catch(e){ return message.SetMessage("A001","在执行获取"+key+"的值的时候发生异常","",0); } } //尝试清除字典中的值 function DeleteValue(key){ try{ //键为空 增加提示信息 if(key==""){ return message.SetMessage("A002","在执行清除字典内容的时候,键为空",false,1); } dictionary[key]=""; return true; }catch(e){ return message.SetMessage("A001","在执行清除"+key+"的值的时候发生异常",false,0); } } //设置字典中的值 function SetValue(key,value){ try{ if(key==""){ return message.SetMessage("A002","在执行添加字典内容的时候,键为空",false,1); } dictionary[key]=value; return true; }catch(e){ return message.SetMessage("A001","在执行添加"+key+"的值的时候发生异常",false,0); } } module.exports=httpDictionary;