wxr
2023-06-06 592974441a4df95fffd9167c90192da1a390b1c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using System.Collections.Generic;
using Shared;
 
namespace HDL_ON.DAL.Server
{
    /// <summary>
    /// 接口类的返回信息
    /// </summary>
    public class IMessageCommon
    {
 
        /// <summary>
        /// 接口类的返回信息
        /// </summary>
        private static IMessageCommon m_Current = null;
        /// <summary>
        /// 接口类的返回信息
        /// </summary>
        public static IMessageCommon Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new IMessageCommon();
                    //初始化全部的信息
                    m_Current.InitAllMsg();
                }
                return m_Current;
            }
        }
 
        /// <summary>
        /// 是否在获取token
        /// </summary>
        public bool isGetingToken;
 
        /// <summary>
        /// 无视错误(访问云端时,此操作将不会弹出错误)
        /// </summary>
        public bool IgnoreError = false;
 
        ///// <summary>
        ///// 全部信息
        ///// </summary>
        //private Dictionary<string, Dictionary<string, int>> dicMsg = null;
 
        /// <summary>
        /// 通用错误码
        /// </summary>
        private Dictionary<string, int> stateCodeDic = null;
 
 
        /// <summary>
        /// 初始化全部的信息
        /// </summary>
        public void InitAllMsg()
        {
            //dicMsg = new Dictionary<string, Dictionary<string, int>>();
            #region 共通错误码
            stateCodeDic = new Dictionary<string, int>();
            //与服务器通讯失败
            stateCodeDic["NETWORK_ERROR"] = StringId.FailedRequestServer;
            //数据异常
            stateCodeDic["DATA_EXCEPTION"] = StringId.DataExceptionPleaseTryAgain;
            //与服务器通讯失败
            stateCodeDic[StateCode.NETWORK_ERROR] = StringId.FailedRequestServer;
            //数据异常
            stateCodeDic[StateCode.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["9"] = StringId.RequestFailedParameterException;
            //验证码校验失败
            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["10007"] = StringId.LoggedOnOtherDevices;
            //登录失败,账号或者密码错误
            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.DuplicateBindingNumber;
            //注册失败
            stateCodeDic["10017"] = StringId.RegistrationFailedPleaseTryAgain;
            ////租户不存在
            //stateCodeDic["10018"] = StringId.10018;
            //密码错误次数过多,账号被锁定!
            stateCodeDic["10019"] = StringId.AccountLoginLock;
            //没有权限
            stateCodeDic["10105"] = StringId.PermissionDenied;
            //验证码已经发送
            stateCodeDic["10207"] = StringId.VerificationCodePrompt;
            //发送验证码错误
            stateCodeDic["10210"] = StringId.VerificationCodeWrong;
 
 
 
 
            ////枚举类型错误
            //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["10503"] = StringId.PleaseBindTheGatewayFirst;
 
            ////调用用户模块接口失败
            //stateCodeDic["10213"] = HDL_ON.StringId;
 
            //*********网关*********
            //网关不存在
            stateCodeDic["10506"] = StringId.GatewayDoesNotExist;
 
 
            //控制失败
            stateCodeDic["14007"] = StringId.ControlFailure;
            //网关离线
            stateCodeDic["14005"] = StringId.GatewayOffline;
            stateCodeDic["124005"] = StringId.GatewayOffline;
            //设备离线
            stateCodeDic["14006"] = StringId.DeviceOffline;
 
            //*********场景/自动化*********
            //场景已存在
            stateCodeDic["14501"] = StringId.SceneAlreadyExists;
            //自动化已存在
            stateCodeDic["14502"] = StringId.AutomationAlreadyExists;
 
            //*********设备*********
            //设备目标重复
            stateCodeDic["10801"] = StringId.DuplicateDeviceTarget;
            //没有远程控制权限
            stateCodeDic["10802"] = StringId.NoRemoteControlPermission;
            //设备不能为空
            stateCodeDic["10803"] = StringId.TheDeviceCannotBeEmpty;
            //用户没有设备的权限
            stateCodeDic["10804"] = StringId.NotHavePermissionToTheDevice;
            //设备不存在
            stateCodeDic["10805"] = StringId.TheDeviceDoesNotExist;
 
            //控制失败
            stateCodeDic["20000"] = StringId.ControlFailure;
            //网关离线
            stateCodeDic["20001"] = StringId.GatewayOffline;
            //设备离线
            stateCodeDic["20002"] = StringId.DeviceOffline;
            //spk不支持该功能
            stateCodeDic["20101"] = StringId.SpkNotSupportThisFeature;
            //spk功能不支持此功能值
            stateCodeDic["20102"] = StringId.SpkNotSupportThisFunctionValue;
            
 
 
            #endregion
 
            #endregion
 
 
        }
 
        /// <summary>
        /// 根据接口的状态码,翻译返回信息
        /// </summary>
        /// <param name="requestName">接口</param>
        /// <param name="revertObj">云端返回的数据</param>
        /// <param name="pra">请求参数</param>
        /// <returns></returns>
        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 + ")";
        }
 
 
        /// <summary>
        /// 弹窗提示网络请求错误信息
        /// </summary>
        /// <param name="requestName"></param>
        /// <param name="statuCode"></param>
        /// <param name="isTipStyle"></param>
        /// <param name="tipStr">补充的错误提示</param>
        public void ShowErrorInfoAlter(string statuCode, bool isTipStyle = true, int closeTime = 2, string tipStr = "",bool showAllMsg = true)
        {
            try
            {
                //如果是token过期则刷新token
                if (statuCode == StateCode.TOKEN_EXPIRED)
                {
                    StartRefreshToken();
                }
                else
                {
                    string mes = GetMsgByRequestName(statuCode);
                    if (mes == null) return;
                    if (!string.IsNullOrEmpty(tipStr))
                    {
                        if (showAllMsg)
                        {
                            mes = tipStr + "\n" + mes;
                        }else
                        {
                            mes = tipStr;
                        }
                    }
                    if (isTipStyle)
                    {
                        Application.RunOnMainThread(() =>
                        {
                            var tip = new Tip()
                            {
                                MaxWidth = Application.GetRealWidth(300),
                                Text = mes,
                                CloseTime = closeTime,
                                Direction = AMPopTipDirection.None
                            };
                            tip.Show(MainPage.BaseView);
                        });
                    }
                    else
                    {
                        Application.RunOnMainThread(() =>
                        {
                            new Alert("", mes+$" \r\nCode:{statuCode}", Language.StringByID(StringId.Close)).Show();
                        });
                    }
                }
            }
            catch { }
        }
 
        /// <summary>
        /// StartRefreshToken
        /// </summary>
        public void StartRefreshToken()
        {
            if (isGetingToken) return;
 
            new System.Threading.Thread(() =>
            {
                isGetingToken = true;
 
                try
                {
                    var success = new HttpServerRequest().RefreshToken();
                    if (success == StateCode.SUCCESS) {
                        Utlis.WriteLine("RefreshToken success");
                    }
                }
                catch
                {
                }
                finally
                {
                    isGetingToken = false;
                }
            })
            { IsBackground = true }.Start();
 
        }
    }
 
    /// <summary>
    /// 常用状态码管理
    /// </summary>
    public class StateCode
    {
        /// <summary>
        /// 网络请求异常
        /// APP自定义错误码
        /// </summary>
        public const string NETWORK_ERROR = "-1";
        /// <summary>
        /// 数据解析错误
        /// APP自定义错误码
        /// </summary>
        public const string DATA_EXCEPTION = "-2";
        /// <summary>
        /// 请求成功的状态码
        /// </summary>
        public const string SUCCESS = "0";
        /// <summary>
        /// 验证码校验失败
        /// </summary>
        public const string VERIFICATION_CODE_WRONG = "14";
        /// <summary>
        /// 会话超时,请更新token
        /// </summary>
        public const string TOKEN_EXPIRED = "10001";
        /// <summary>
        /// 账号不存在的错误码
        /// </summary>
        public const string ACCOUNT_NOT_EXIST = "10010";
        /// <summary>
        /// 登录锁定,密码错误次数过多
        /// 密码输入错误超过10次,请30分钟后重试!
        /// </summary>
        public const string AccountLoginLock = "10019";
        /// <summary>
        /// 密码错误
        /// </summary>
        public const string PasswrodError = "100008";
        
 
 
    }
}