统一传输格式,对后期开发有很大的帮助,扩展也很方便。
底层数据定义格式
1 public class JsonParam 2 { 3 /// <summary> 4 /// 成功=ok ;失败=no 5 /// </summary> 6 private string status; 7 public string Message { get; set; } 8 public object Data { get; set; } 9 public string Status { get { return status.ToLower(); } set { status = value; } } 10 public string Layer { get { return LayerMessage(); } } 11 12 public bool IsSuccess { get => isSuccess; } 13 14 private bool isSuccess; 15 16 public JsonParam() 17 { 18 this.Status = "no"; 19 isSuccess = false; 20 } 21 22 /// <summary> 23 /// 设置失败 24 /// </summary> 25 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 26 public void SetDefeate(bool _IsTranslate = true) 27 { 28 SetDefeate("操作失败!", _IsTranslate); 29 } 30 /// <summary> 31 /// 设置成功 32 /// </summary> 33 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 34 public void SetSuccess(bool _IsTranslate=true) 35 { 36 SetSuccess("Success", "", _IsTranslate); 37 } 38 /// <summary> 39 /// 设置成功 40 /// </summary> 41 /// <param name="msg"></param> 42 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 43 public void SetSuccess(string msg, bool _IsTranslate = true) 44 { 45 SetSuccess(msg, "", _IsTranslate); 46 } 47 /// <summary> 48 /// 设置成功 49 /// </summary> 50 /// <param name="data"></param> 51 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 52 public void SetSuccess(object data, bool _IsTranslate = true) 53 { 54 SetSuccess("Success", data, _IsTranslate); 55 } 56 /// <summary> 57 /// 设置成功 58 /// </summary> 59 /// <param name="msg"></param> 60 /// <param name="data"></param> 61 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 62 public void SetSuccess(string msg, object data, bool _IsTranslate = true) 63 { 64 this.Status = "ok"; 65 isSuccess = true; 66 this.Message = LangString(msg, _IsTranslate); 67 this.Data = data; 68 } 69 /// <summary> 70 /// 设置失败 71 /// </summary> 72 /// <param name="msg"></param> 73 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 74 public void SetDefeate(string msg, bool _IsTranslate = true) 75 { 76 this.Message = LangString(msg, _IsTranslate); 77 this.isSuccess = false; 78 } 79 80 string LayerMessage() 81 { 82 string p; 83 if (Status == "ok") 84 { 85 //p = string.Format("layui.use('layer', function () { layer.msg('{0}', { icon: 0 } ); });", Message); 86 p = "layui.use('layer', function () { var layer=layui.layer; layer.msg('" + Message + "', { icon: 0 } ); });"; 87 } 88 else 89 { 90 //p =string.Format("layui.use('layer', function () { layer.msg('{0}', { icon: 2 } ); });",Message); 91 p = "layui.use('layer', function () { var layer=layui.layer; layer.msg('" + Message + "', { icon: 2 } ); });"; 92 } 93 94 return p; 95 } 96 public string LangString(string key, bool _IsTranslate = true) 97 { 98 if (!_IsTranslate) return key; 99 return LanguageClient.GetSingle().GetValue(key); 100 } 101 }
然后,就是逻辑层,辅助与界面来往
1 public class JsonModel 2 { 3 /// <summary> 4 /// 成功=ok ;失败=no 5 /// </summary> 6 private JsonParam res; 7 private JsonResult jsonResult; 8 public JsonModel() 9 { 10 res = new JsonParam(); 11 jsonResult = new JsonResult(); 12 } 13 /// <summary> 14 /// 设置失败 *支持多语言* 15 /// </summary> 16 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 17 /// <returns></returns> 18 public JsonResult Defeate(bool _IsTranslate=true) 19 { 20 return Defeate("操作失败!",_IsTranslate); 21 } 22 /// <summary> 23 /// 设置成功 *支持多语言* 24 /// </summary> 25 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 26 /// <returns></returns> 27 public JsonResult Success(bool _IsTranslate=true) 28 { 29 res.SetSuccess(_IsTranslate); 30 jsonResult.Data = res; 31 return jsonResult; 32 } 33 /// <summary> 34 /// 设置成功 *支持多语言* 35 /// </summary> 36 /// <param name="msg"></param> 37 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 38 /// <returns></returns> 39 public JsonResult Success(string msg, bool _IsTranslate=true) 40 { 41 res.SetSuccess(msg,_IsTranslate); 42 jsonResult.Data = res; 43 return jsonResult; 44 } 45 /// <summary> 46 /// 设置成功 *支持多语言* 47 /// </summary> 48 /// <param name="data"></param> 49 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 50 /// <returns></returns> 51 public JsonResult Success(object data, bool _IsTranslate = true) 52 { 53 res.SetSuccess(data,_IsTranslate); 54 jsonResult.Data = res; 55 return jsonResult; 56 } 57 /// <summary> 58 /// 设置成功 *支持多语言* 59 /// </summary> 60 /// <param name="msg"></param> 61 /// <param name="data"></param> 62 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 63 /// <returns></returns> 64 public JsonResult Success(string msg,object data, bool _IsTranslate = true) 65 { 66 res.SetSuccess(msg,data,_IsTranslate); 67 jsonResult.Data = res; 68 return jsonResult; 69 } 70 71 /// <summary> 72 /// 设置失败 *支持多语言* 73 /// </summary> 74 /// <param name="msg"></param> 75 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 76 /// <returns></returns> 77 public JsonResult Defeate(string msg, bool _IsTranslate = true) 78 { 79 res.SetDefeate(msg,_IsTranslate); 80 jsonResult.Data = res; 81 return jsonResult; 82 } 83 /// <summary> 84 /// 发生异常 *支持多语言* 85 /// </summary> 86 /// <returns></returns> 87 public JsonResult Exception() 88 { 89 return Defeate( "服务器错误!"); 90 } 91 /// <summary> 92 /// 自定义响应结果 *支持多语言* 93 /// </summary> 94 /// <param name="param"></param> 95 /// <param name="_IsTranslate">是否进行翻译(默认翻译)</param> 96 /// <returns></returns> 97 public JsonResult SetResult(JsonParam param, bool _IsTranslate = true) 98 { 99 if (param == null) return Defeate(_IsTranslate); 100 jsonResult.Data = param; 101 return jsonResult; 102 } 103 104 105 }
这里是MVC上的JSON传输