using System;
using System.Collections.Generic;
using Shared;
namespace HDL_ON.DAL.Server
{
///
/// 接口类的返回信息
///
public class IMessageCommon
{
///
/// 接口类的返回信息
///
private static IMessageCommon m_Current = null;
///
/// 接口类的返回信息
///
public static IMessageCommon Current
{
get
{
if (m_Current == null)
{
m_Current = new IMessageCommon();
//初始化全部的信息
m_Current.InitAllMsg();
}
return m_Current;
}
}
///
/// 是否在获取token
///
public bool isGetingToken;
///
/// 无视错误(访问云端时,此操作将不会弹出错误)
///
public bool IgnoreError = false;
/////
///// 全部信息
/////
//private Dictionary> dicMsg = null;
///
/// 通用错误码
///
private Dictionary stateCodeDic = null;
///
/// 初始化全部的信息
///
public void InitAllMsg()
{
//dicMsg = new Dictionary>();
#region 共通错误码
stateCodeDic = new Dictionary();
//与服务器通讯失败
stateCodeDic["NETWORK_ERROR"] = StringId.FailedRequestServer;
//数据异常
stateCodeDic["DATA_EXCEPTION"] = StringId.DataExceptionPleaseTryAgain;
#region 基础服务 新错误码 2020-12-07
//系统繁忙~请稍后再试~
stateCodeDic["1"] = StringId.SystemIsBusy;
//系统维护中~请稍后再试~
stateCodeDic["2"] = StringId.SystemMaintenance;
////服务异常
//stateCodeDic["3"] = StringId.SystemIsBusy;
//签名错误
stateCodeDic["4"] = StringId.SignatureError;
//验证码校验失败
stateCodeDic[StateCode.VERIFICATION_CODE_WRONG] = StringId.VerificationCodeWrong;
//验证码发送频繁,请稍后再试!
stateCodeDic["15"] = StringId.VerificationCodeSentFrequently;
//验证码发送失败
stateCodeDic["16"] = StringId.FailedToSendVerificationCode;
//会话超时,请更新token
stateCodeDic[StateCode.TOKEN_EXPIRED] = StringId.NotLogin;
//解析用户身份错误,请重新登录
stateCodeDic["10006"] = StringId.InvalidToken;
//登录失败,账号或者密码错误
stateCodeDic["10008"] = StringId.LoginFailed_AccountOrPasswordError;
//用户已经被禁用
stateCodeDic["10009"] = StringId.TheAccountHasBeenDisabled;
//账号不存在
stateCodeDic[StateCode.ACCOUNT_NOT_EXIST] = StringId.AccountNotExist;
//用户未登录
stateCodeDic["10011"] = StringId.InvalidToken;
//账号已存在
stateCodeDic["10012"] = StringId.AccountExist;
//手机号已注册
stateCodeDic["10013"] = StringId.AccountExist;
//邮箱已注册
stateCodeDic["10014"] = StringId.AccountExist;
//原密码错误
stateCodeDic["10015"] = StringId.OldPwdNoYes;
////绑定号码重复
//stateCodeDic["10016"] = StringId.10016;
////注册失败
//stateCodeDic["10017"] = StringId.10017;
////租户不存在
//stateCodeDic["10018"] = StringId.10018;
////枚举类型错误
//stateCodeDic["11003"] = StringId.11003;
//-------------------子账号模块-------------------
//子账号已经存在
stateCodeDic["10301"] = StringId.BindSubAccount_Exist;
//子账号不存在
stateCodeDic["10302"] = StringId.SubAccount_NoExists;
//不能把自己添加为成员
stateCodeDic["10303"] = StringId.BindSubAccount_SameAccount;
////相同的远程控制
//stateCodeDic["10304"] = StringId.uOperationFailed;
////子账号昵称已经存在
//stateCodeDic["10305"] = StringId.BindSubAccount_Exist;
//-------------------住宅模块-------------------
//当前住宅不属于该账号
stateCodeDic["10401"] = StringId.HomeIdAndTokenNoConsistent;
//住宅名称已存在
stateCodeDic["10402"] = StringId.HomeName_Exist;
////住宅ID已存在
//stateCodeDic["10403"] = StringId.HOME_ID_EXISTS;
//住宅不存在
stateCodeDic["10404"] = StringId.Home_NoExists;
////调用用户模块接口失败
//stateCodeDic["10213"] = HDL_ON.StringId;
#endregion
#endregion
}
///
/// 根据接口的状态码,翻译返回信息
///
/// 接口
/// 云端返回的数据
/// 请求参数
///
public string GetMsgByRequestName(string statuCode)
{
if (IgnoreError == true)
{
//无视错误
return null;
}
//共通码
if (stateCodeDic.ContainsKey(statuCode) == true)
{
return Language.StringByID(stateCodeDic[statuCode]);
}
//if (dicMsg.ContainsKey(requestName) == true && dicMsg[requestName].ContainsKey(statuCode) == true)
//{
// //在册的Msg
// return Language.StringByID(dicMsg[requestName][statuCode]);
//}
return Language.StringByID(StringId.FailedRequestServer) + "\n(" + statuCode + ")";
}
///
/// 弹窗提示网络请求错误信息
///
///
///
///
/// 补充的错误提示
public void ShowErrorInfoAlter(string statuCode, bool isTipStyle = true, int closeTime = 2, string tipStr = "")
{
try
{
//如果是token过期则刷新token
if (statuCode == StateCode.TOKEN_EXPIRED)
{
StartRefreshToken();
}
else
{
string mes = GetMsgByRequestName(statuCode);
if (mes == null) return;
if (!string.IsNullOrEmpty(tipStr))
{
mes = tipStr + "\n" + mes;
}
if (isTipStyle)
{
Application.RunOnMainThread(() =>
{
var tip = new Tip()
{
Text = mes,
CloseTime = closeTime,
Direction = AMPopTipDirection.None
};
tip.Show(MainPage.BaseView);
});
}
else
{
Application.RunOnMainThread(() =>
{
new Alert("", mes, Language.StringByID(StringId.Close)).Show();
});
}
}
}
catch { }
}
///
/// StartRefreshToken
///
public void StartRefreshToken()
{
if (isGetingToken) return;
new System.Threading.Thread(() =>
{
isGetingToken = true;
try
{
var success = new HttpServerRequest().RefreshToken();
if (success) {
Utlis.WriteLine("RefreshToken success");
}
}
catch
{
}
finally
{
isGetingToken = false;
}
})
{ IsBackground = true }.Start();
}
}
///
/// 常用状态码管理
///
public class StateCode
{
///
/// 请求成功的状态码
///
public const string SUCCESS = "0";
///
/// 验证码校验失败
///
public const string VERIFICATION_CODE_WRONG = "14";
///
/// 会话超时,请更新token
///
public const string TOKEN_EXPIRED = "10001";
///
/// 账号不存在的错误码
///
public const string ACCOUNT_NOT_EXIST = "10010";
}
}