ErrorMessage.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. namespace ZcPeng.weixin.PublicAccount
  6. {
  7. /// <summary>
  8. /// 错误信息
  9. /// </summary>
  10. public class ErrorMessage
  11. {
  12. /// <summary>
  13. /// 成功对应的错误码
  14. /// </summary>
  15. public const int SuccessCode = 0;
  16. /// <summary>
  17. /// 成功
  18. /// </summary>
  19. public static readonly ErrorMessage Success = new ErrorMessage(SuccessCode, "请求成功");
  20. /// <summary>
  21. /// 异常
  22. /// </summary>
  23. public static readonly ErrorMessage Exception = new ErrorMessage(ExceptionCode, "请求异常");
  24. /// <summary>
  25. /// 程序异常对应的错误代码
  26. /// </summary>
  27. public const int ExceptionCode = -99999;
  28. /// <summary>
  29. /// 错误字典
  30. /// </summary>
  31. public static Dictionary<int, string> ErrorDictionary = new Dictionary<int, string>()
  32. {
  33. {-1 , "系统繁忙"},
  34. {0 , "请求成功"},
  35. {40001 , "获取access_token时AppSecret错误,或者access_token无效"},
  36. {40002 , "不合法的凭证类型"},
  37. {40003 , "不合法的OpenID"},
  38. {40004 , "不合法的媒体文件类型"},
  39. {40005 , "不合法的文件类型"},
  40. {40006 , "不合法的文件大小"},
  41. {40007 , "不合法的媒体文件id"},
  42. {40008 , "不合法的消息类型"},
  43. {40009 , "不合法的图片文件大小"},
  44. {40010 , "不合法的语音文件大小"},
  45. {40011 , "不合法的视频文件大小"},
  46. {40012 , "不合法的缩略图文件大小"},
  47. {40013 , "不合法的APPID"},
  48. {40014 , "不合法的access_token"},
  49. {40015 , "不合法的菜单类型"},
  50. {40016 , "不合法的按钮个数"},
  51. {40017 , "不合法的按钮个数"},
  52. {40018 , "不合法的按钮名字长度"},
  53. {40019 , "不合法的按钮KEY长度"},
  54. {40020 , "不合法的按钮URL长度"},
  55. {40021 , "不合法的菜单版本号"},
  56. {40022 , "不合法的子菜单级数"},
  57. {40023 , "不合法的子菜单按钮个数"},
  58. {40024 , "不合法的子菜单按钮类型"},
  59. {40025 , "不合法的子菜单按钮名字长度"},
  60. {40026 , "不合法的子菜单按钮KEY长度"},
  61. {40027 , "不合法的子菜单按钮URL长度"},
  62. {40028 , "不合法的自定义菜单使用用户"},
  63. {40029 , "不合法的oauth_code"},
  64. {40030 , "不合法的refresh_token"},
  65. {40031 , "不合法的openid列表"},
  66. {40032 , "不合法的openid列表长度"},
  67. {40033 , "不合法的请求字符,不能包含\\uxxxx格式的字符"},
  68. {40035 , "不合法的参数"},
  69. {40038 , "不合法的请求格式"},
  70. {40039 , "不合法的URL长度"},
  71. {40050 , "不合法的分组id"},
  72. {40051 , "分组名字不合法"},
  73. {41001 , "缺少access_token参数"},
  74. {41002 , "缺少appid参数"},
  75. {41003 , "缺少refresh_token参数"},
  76. {41004 , "缺少secret参数"},
  77. {41005 , "缺少多媒体文件数据"},
  78. {41006 , "缺少media_id参数"},
  79. {41007 , "缺少子菜单数据"},
  80. {41008 , "缺少oauthcode"},
  81. {41009 , "缺少openid"},
  82. {42001 , "access_token超时"},
  83. {42002 , "refresh_token超时"},
  84. {42003 , "oauth_code超时"},
  85. {43001 , "需要GET请求"},
  86. {43002 , "需要POST请求"},
  87. {43003 , "需要HTTPS请求"},
  88. {43004 , "需要接收者关注"},
  89. {43005 , "需要好友关系"},
  90. {44001 , "多媒体文件为空"},
  91. {44002 , "POST的数据包为空"},
  92. {44003 , "图文消息内容为空"},
  93. {44004 , "文本消息内容为空"},
  94. {45001 , "多媒体文件大小超过限制"},
  95. {45002 , "消息内容超过限制"},
  96. {45003 , "标题字段超过限制"},
  97. {45004 , "描述字段超过限制"},
  98. {45005 , "链接字段超过限制"},
  99. {45006 , "图片链接字段超过限制"},
  100. {45007 , "语音播放时间超过限制"},
  101. {45008 , "图文消息超过限制"},
  102. {45009 , "接口调用超过限制"},
  103. {45010 , "创建菜单个数超过限制"},
  104. {45015 , "回复时间超过限制"},
  105. {45016 , "系统分组,不允许修改"},
  106. {45017 , "分组名字过长"},
  107. {45018 , "分组数量超过上限"},
  108. {46001 , "不存在媒体数据"},
  109. {46002 , "不存在的菜单版本"},
  110. {46003 , "不存在的菜单数据"},
  111. {46004 , "不存在的用户"},
  112. {47001 , "解析JSON/XML内容错误"},
  113. {48001 , "api功能未授权"},
  114. {50001 , "用户未授权该api"},
  115. //客服管理接口错误码
  116. {61450 , "系统错误(system error)"},
  117. {61451 , "参数错误(invalid parameter)"},
  118. {61452 , "无效客服账号(invalid kf_account)"},
  119. {61453 , "账号已存在(kf_account exsited)"},
  120. {61454 , "账号名长度超过限制(前缀10个英文字符)(invalid kf_acount length)"},
  121. {61455 , "账号名包含非法字符(英文+数字)(illegal character in kf_account)"},
  122. {61456 , "账号个数超过限制(10个客服账号)(kf_account count exceeded)"},
  123. {61457 , "无效头像文件类型(invalid file type)"},
  124. {61458 , "客户正在被其他客服接待(customer accepted by xxx@xxxx)"},
  125. {61459 , "客服不在线(kf offline)"},
  126. {61500 , "日期格式错误"},
  127. {61501 , "日期范围错误"},
  128. //语义理解接口错误码
  129. {7000000 , "请求正常,无语义结果"},
  130. {7000001 , "缺失请求参数"},
  131. {7000002 , "signature 参数无效"},
  132. {7000003 , "地理位置相关配置 1 无效"},
  133. {7000004 , "地理位置相关配置 2 无效"},
  134. {7000005 , "请求地理位置信息失败"},
  135. {7000006 , "地理位置结果解析失败"},
  136. {7000007 , "内部初始化失败"},
  137. {7000008 , "非法 appid(获取密钥失败)"},
  138. {7000009 , "请求语义服务失败"},
  139. {7000010 , "非法 post 请求"},
  140. {7000011 , "post 请求 json 字段无效 "},
  141. {7000030 , "查询 query 太短"},
  142. {7000031 , "查询 query 太长"},
  143. {7000032 , "城市、经纬度信息缺失"},
  144. {7000033 , "query 请求语义处理失败"},
  145. {7000034 , "获取天气信息失败"},
  146. {7000035 , "获取股票信息失败"},
  147. {7000036 , "utf8 编码转换失败"},
  148. {9001001 , "POST数据参数不合法"},
  149. {9001002 , "远端服务不可用"},
  150. {9001003 , "Ticket不合法"},
  151. {9001004 , "获取摇周边用户信息失败"},
  152. {9001005 , "获取商户信息失败"},
  153. {9001006 , "获取OpenID失败"},
  154. {9001007 , "上传文件缺失"},
  155. {9001008 , "上传素材的文件类型不合法"},
  156. {9001009 , "上传素材的文件尺寸不合法"},
  157. {9001010 , "上传失败"},
  158. {9001020 , "帐号不合法"},
  159. {9001021 , "已有设备激活率低于50%,不能新增设备"},
  160. {9001022 , "设备申请数不合法,必须为大于0的数字"},
  161. {9001023 , "已存在审核中的设备ID申请"},
  162. {9001024 , "一次查询设备ID数量不能超过50"},
  163. {9001025 , "设备ID不合法"},
  164. {9001026 , "页面ID不合法"},
  165. {9001027 , "页面参数不合法"},
  166. {9001028 , "一次删除页面ID数量不能超过10"},
  167. {9001029 , "页面已应用在设备中,请先解除应用关系再删除"},
  168. {9001030 , "一次查询页面ID数量不能超过50"},
  169. {9001031 , "时间区间不合法"},
  170. {9001032 , "保存设备与页面的绑定关系参数错误"},
  171. {9001033 , "门店ID不合法"},
  172. {9001034 , "设备备注信息过长"},
  173. {9001035 , "设备申请参数不合法"},
  174. {9001036 , "查询起始值begin不合法"}
  175. };
  176. /// <summary>
  177. /// 错误码
  178. /// </summary>
  179. public int errcode { get; set; }
  180. /// <summary>
  181. /// 错误提示
  182. /// </summary>
  183. public string errmsg { get; set; }
  184. /// <summary>
  185. /// 获取是否为成功消息
  186. /// </summary>
  187. /// <returns></returns>
  188. public bool IsSuccess
  189. {
  190. get
  191. {
  192. return errcode == SuccessCode;
  193. }
  194. }
  195. /// <summary>
  196. /// 获取汉字错误提示
  197. /// </summary>
  198. /// <returns></returns>
  199. public string ChineseMessage
  200. {
  201. get
  202. {
  203. return GetChineseMessage(errcode, errmsg);
  204. }
  205. }
  206. /// <summary>
  207. /// 获取汉字错误提示
  208. /// </summary>
  209. /// <param name="errcode">错误码</param>
  210. /// <param name="errmsg">错误提示</param>
  211. /// <returns>返回汉字错误提示</returns>
  212. internal static string GetChineseMessage(int errcode, string errmsg = null)
  213. {
  214. string msg = errmsg;
  215. if (ErrorDictionary.ContainsKey(errcode))
  216. msg = ErrorDictionary[errcode];
  217. return msg;
  218. }
  219. /// <summary>
  220. /// 构造函数
  221. /// </summary>
  222. /// <param name="_errcode">错误码</param>
  223. /// <param name="_errmsg">错误提示</param>
  224. public ErrorMessage(int _errcode, string _errmsg)
  225. {
  226. errcode = _errcode;
  227. errmsg = _errmsg;
  228. }
  229. /// <summary>
  230. /// 构造函数
  231. /// </summary>
  232. /// <param name="e">异常</param>
  233. public ErrorMessage(Exception e)
  234. {
  235. errcode = ExceptionCode;
  236. errmsg = string.Format("异常源:{0}\r\n错误描述:{1}\r\n堆栈:{2}", e.Source, e.Message, e.StackTrace);
  237. }
  238. /// <summary>
  239. /// 返回错误消息字符串
  240. /// </summary>
  241. /// <returns></returns>
  242. public override string ToString()
  243. {
  244. return string.Format("编码:{0}\r\n提示:{1}", errcode, ChineseMessage ?? "");
  245. }
  246. /// <summary>
  247. /// 返回JSON字符串
  248. /// </summary>
  249. /// <returns></returns>
  250. public string ToJson()
  251. {
  252. return JsonConvert.SerializeObject(this);
  253. }
  254. /// <summary>
  255. /// 从JSON字符串解析错误消息
  256. /// </summary>
  257. /// <param name="json">JSON字符串</param>
  258. /// <returns>返回错误消息</returns>
  259. internal static ErrorMessage Parse(string json)
  260. {
  261. var em = JsonConvert.DeserializeAnonymousType(json, new { errcode = 0, errmsg = "ok" });
  262. return new ErrorMessage(em.errcode, em.errmsg);
  263. }
  264. /// <summary>
  265. /// 尝试从JSON字符串解析错误消息
  266. /// </summary>
  267. /// <param name="json">JSON字符串</param>
  268. /// <param name="msg">如果解析成功,返回错误消息;否则,返回null。</param>
  269. /// <returns></returns>
  270. internal static bool TryParse(string json, out ErrorMessage msg)
  271. {
  272. bool success = false;
  273. msg = null;
  274. try
  275. {
  276. msg = Parse(json);
  277. success = true;
  278. }
  279. catch { }
  280. return success;
  281. }
  282. /// <summary>
  283. /// 判断JSON字符串是否为错误消息
  284. /// </summary>
  285. /// <param name="json">JSON字符串</param>
  286. /// <returns>如果是,返回true;否则,返回false。</returns>
  287. internal static bool IsErrorMessage(string json)
  288. {
  289. JObject jo = JObject.Parse(json);
  290. JToken jt;
  291. return jo.TryGetValue("errcode", out jt) && jo.TryGetValue("errmsg", out jt);
  292. }
  293. }
  294. }