Пример #1
0
 /// <summary>
 /// 创建一个EtpException实例,该实例封装Etp返回的错误消息。
 /// </summary>
 /// <param name="code">主错误码。</param>
 /// <param name="description">主错误描述。</param>
 /// <param name="subCode">子错误码。</param>
 /// <param name="subDescription">子错误描述。</param>
 public EtpException Create(string code, string description, string subCode = "", string subDescription = "")
 {
     EtpException etpException = null;
     int errCode;
     if (int.TryParse(code, out errCode))
     {
         //根据不同的错误类型创建不同的异常对象
         if (((errCode > 100 || errCode == 15) && subCode.StartsWith("isv.")) || errCode == 40 || errCode == 41)
         {
             //业务级异常
             etpException = EtpException.CreateBusinessException();
         }
         else if (((errCode < 100) && (!(errCode == 15 || errCode == 40 || errCode == 41))))
         {
             //一般是由于用户的请求不符合各种基本校验而引起的。用户遇到这些错误的返回首先检查应用的权限、频率等情况,然后参照文档检验一下传入的参数是否完整且合法.
             etpException = EtpException.CreatePlatformException(false);
         }
         else if (subCode.StartsWith("isp."))
         {
             //一般是由于服务端异常引起的。用户遇到这类错误需要隔一段时间再重试就可以解决。
             etpException = EtpException.CreatePlatformException(true);
         }
         else
         {
             etpException = EtpException.CreateApplicationException();
         }
     }
     else
     {
         etpException = EtpException.CreateBusinessException();
     }
     etpException.SetError(code, description);
     etpException.SetSubError(subCode, subDescription);
     return etpException;
 }
Пример #2
0
 /// <summary>
 /// 创建一个EtpException实例,该实例表示调用Api时发生了应用级异常。
 /// </summary>
 internal static EtpException CreateApplicationException()
 {
     EtpException etpException = new EtpException();
     //设置应用级异常
     etpException._errorType = eErrorType.ApplicationError;
     return etpException;
 }
Пример #3
0
        /// <summary>
        /// 创建一个EtpException实例,该实例封装Etp返回的错误消息。
        /// </summary>
        /// <param name="code">主错误码。</param>
        /// <param name="description">主错误描述。</param>
        /// <param name="subCode">子错误码。</param>
        /// <param name="subDescription">子错误描述。</param>
        public EtpException Create(string code, string description, string subCode = "", string subDescription = "")
        {
            EtpException etpException = null;

            //通过code判断不同级别EtpException实例
            if (code == "invalid_request")
            {
                etpException = EtpException.CreateApplicationException();
            }
            else if (subCode.Contains("090008"))
            {
                etpException = EtpException.CreatePlatformException(true);
            }
            else if (code == "403" || code == "400" || code == "unauthorized_client" || code == "401")
            {
                etpException = EtpException.CreatePlatformException(false);
            }
            else
            {
                etpException = EtpException.CreateBusinessException();
            }

            etpException.SetError(code, description);
            etpException.SetSubError(subCode, subDescription);
            return etpException;
        }
Пример #4
0
 /// <summary>
 /// 创建一个EtpException实例,该实例表示调用Api时发生了业务级异常。
 /// </summary>
 internal static EtpException CreateBusinessException()
 {
     EtpException etpException = new EtpException();
     //设置业务级异常
     etpException._errorType = eErrorType.BusinessError;
     return etpException;
 }
		/// <summary>
		/// 创建一个EtpException实例,该实例封装Etp返回的错误消息。
		/// </summary>
		/// <param name="code">主错误码。</param>
		/// <param name="description">主错误描述。</param>
		/// <param name="subCode">子错误码。</param>
		/// <param name="subDescription">子错误描述。</param>
		public EtpException Create(string code, string description, string subCode = "", string subDescription = ""){
            EtpException etpException = null;
            int error_code;
            if (code == "0000001")

            {
                //平台级异常
                etpException = EtpException.CreatePlatformException(true);
            }
            else if (code.StartsWith("00"))

            {
                //平台级异常
                etpException = EtpException.CreatePlatformException(false);
            }
            else if (int.TryParse(code, out error_code))

            {
                etpException = EtpException.CreateBusinessException();
            }
            else

            {
                //应用级异常
                etpException = EtpException.CreateApplicationException();
            }
            //设置主错误信息
            etpException.SetError(code, description);
            //设置子错误信息
            etpException.SetSubError(subCode, subDescription);

            return etpException;
        }
Пример #6
0
 /// <summary>
 /// 创建一个EtpException实例,该实例封装Etp返回的错误消息。
 /// </summary>
 /// <param name="code">主错误码。</param>
 /// <param name="description">主错误描述。</param>
 /// <param name="subCode">子错误码。</param>
 /// <param name="subDescription">子错误描述。</param>
 public EtpException Create(string code, string description, string subCode = "", string subDescription = "")
 {
     EtpException etpException = null;
     //平台级
     if (_platformErrorCode.Contains(code))
     {
         etpException = EtpException.CreatePlatformException(true);
         etpException.SetError(code, description);
         etpException.SetSubError(subCode, subDescription);
     }
     //应用级
     else if (_appErrorCode.Contains(code))
     {
         etpException = EtpException.CreateApplicationException();
         etpException.SetError(code, description);
         etpException.SetSubError(subCode, subDescription);
     }
     //业务级
     else if (_businessErrorCode.Contains(code))
     {
         etpException = EtpException.CreateBusinessException();
         etpException.SetError(code, description);
         etpException.SetSubError(subCode, subDescription);
     }
     return etpException;
 }
        /// <summary>
        /// 接受回调请求后向平台换取Token。
        /// </summary>
        /// <param name="application">平台应用。</param>
        /// <param name="callbackRequest">回调请求。</param>
        public override async Task<AuthorizationResult> GetTokenAsync(Application application, HttpRequest callbackRequest)
        {


            #region 请求参数
            //授权码
            string code = callbackRequest.QueryString["code"];
            //应用app  key
            string client_id = application.AppKey;
            //应用密匙
            string client_secret = application.Secret;
            //换取方式
            string grant_type = "authorization_code";
            //回调请求
            string redirect_uri = application.RedirectUrl;

            #endregion

            #region 生成请求URL
            //请求URL
            string url = application.Platform.TokenUrl;
            //请求参数拼接
            string para = string.Format("code={0}&app_key={1}&app_secret={2}&grant_type={3}&redirect_uri={4}",
                code, client_id, client_secret, grant_type, redirect_uri);

            #endregion

            #region 发送请求及处理结果

            //返回json字符串结果
            string json = await HttpHelp.GetStrAsync(url + "?" + para);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            if (json.Contains("data"))
            {
                //反序列化json字符串
                MeiLiShuoData meiLiShuoData = await Task.Factory.StartNew(() => { return Newtonsoft.Json.JsonConvert.DeserializeObject<MeiLiShuoData>(json); });
                MeiLiShuoJson meiLiShuoJson = meiLiShuoData.data;
                //填充数据返回
                return new AuthorizationResult()
                {
                    OpenId = meiLiShuoJson.Meilishuo_user_id,
                    Token = meiLiShuoJson.Access_token,
                    RefreshToken = meiLiShuoJson.Refresh_token,
                    TokenType = meiLiShuoJson.Token_type,
                    ExpireAt = DateTime.Now.AddMinutes(-1).AddSeconds(36000),
                    RefreshExpireAt = DateTime.FromFileTimeUtc(meiLiShuoJson.Re_expires_in),
                    UserName = meiLiShuoData.data.Meilishuo_shop_nick
                };

            }
            else//异常处理
            {
                //抛异常
                MeiLiShuoExecption ex = jss.Deserialize<MeiLiShuoExecption>(json);
                throw EtpException.Create(application.Platform.Name, ex.Error_code, ex.Message, "", "");
            }
            #endregion

        }
Пример #8
0
 /// <summary>
 /// 创建一个EtpException实例,该实例表示调用Api时发生了平台级异常。
 /// </summary>
 /// <param name="retriable">指示调用方是否可以重新尝试调用。</param>
 internal static EtpException CreatePlatformException(bool retriable)
 {
     EtpException etpException = new EtpException();
     //设置是否重试
     etpException._retriable = retriable;
     //设置平台及异常
     etpException._errorType = eErrorType.PlatformError;
     return etpException;
 }
        public EtpException Create(string code, string description, string subCode = "", string subDescription = "")
        {
            if (_codes[code] != null)
            {
                description = _codes[code];
            }
            var etpException = EtpException.CreateApplicationException();

            etpException.SetError(code, description);
            return(etpException);
        }
Пример #10
0
        /// <summary>
        /// 接受回调请求后向平台换取Token。
        /// </summary>
        /// <param name="application">应用信息。</param>
        /// <param name="callbackRequest">回调请求。</param>
        public override async Task<AuthorizationResult> GetTokenAsync(Application application, HttpRequest callbackRequest)
        {

            #region 获取Token请求参数

            //从请求报文中获取签名
            string code = callbackRequest.Params["Code"];
            //授权类型,使用authorization_code即可
            string grant_type = "authorization_code";
            //是否需要返回refresh_token,如果返回了refresh_token,原来获取的refresh_token也不会失效,除非超过半年有效期
            string need_refresh_token = "true";
            //app唯一标识,即appKey
            string client_id = application.AppKey;
            //app密钥
            string client_secret = application.Secret;
            //app入口地址
            string redirect_uri = application.RedirectUrl;

            #endregion

            #region 生成请求URL
            string url = application.Platform.TokenUrl.Replace("YOUR_APPKEY", application.AppKey);
            string para = string.Format("grant_type={0}&need_refresh_token={1}&client_id= {2}&client_secret= {3}&redirect_uri={4}&code={5}",
                grant_type, need_refresh_token, client_id, client_secret, redirect_uri, code);

            #endregion

            #region 发送请求及返回结果

            try
            {
                //发送请求报文
                string json = await HttpHelp.GetStrAsync(url + "?" + para);
                //判断是否返回异常
                if (json.Contains("error"))
                {
                    AlibabaException alibabaException = await Task.Factory.StartNew(() => { return JsonConvert.DeserializeObject<AlibabaException>(json); });
                    //抛出异常
                    if (string.IsNullOrWhiteSpace(alibabaException.Error))
                    {
                        throw EtpException.Create(application.Platform.Name, alibabaException.Error_code.ToString(), alibabaException.Error_message, "", "");
                    }
                    else
                    {
                        throw EtpException.Create(application.Platform.Name, alibabaException.Error, alibabaException.Error_description, "", "");
                    }
                }
                else
                {
                    //把返回的json字符串反序列化
                    AlibabaJson alibabaJson = await Task.Factory.StartNew(() => { return JsonConvert.DeserializeObject<AlibabaJson>(json); });
                    //填充AuthorizationResult数据
                    return new AuthorizationResult()
                    {
                        Token = alibabaJson.Access_token,
                        OpenId =alibabaJson.AliId,
                        RefreshToken = alibabaJson.Refresh_token,
                        UserName = alibabaJson.MemberId,
                        ExpireAt = DateTime.Now.AddMinutes(-1).AddSeconds(alibabaJson.Expires_in),
                        RefreshExpireAt = DateTime.Parse(alibabaJson.Refresh_token_timeout.Substring(0, 4) + "-" +
                        alibabaJson.Refresh_token_timeout.Substring(4, 2) + "-" +
                        alibabaJson.Refresh_token_timeout.Substring(6, 2) + " " +
                        alibabaJson.Refresh_token_timeout.Substring(8, 2) + ":" +
                        alibabaJson.Refresh_token_timeout.Substring(10, 2) + ":" +
                        alibabaJson.Refresh_token_timeout.Substring(12, 2))
                    };
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            #endregion
        }