Esempio n. 1
0
        /// <summary>
        /// 设置标签
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public OperateResultDto SetTag(SetTagBucketParam request)
        {
            new SetTagBucketParamValidator().Validate(request).Check(HttpStatus.Err.Name);
            var                 scheme      = Core.Tools.GetScheme(_qiNiuConfig, request.PersistentOps.IsUseHttps);
            RestClient          restClient  = new RestClient($"{scheme}uc.qbox.me");
            string              bucket      = Core.Tools.GetBucket(_qiNiuConfig, request.PersistentOps.Bucket);
            RestRequest         restRequest = new RestRequest($"/bucketTagging?bucket={bucket}", Method.PUT);
            GetManageTokenParam param       =
                new GetManageTokenParam($"{scheme}uc.qbox.me/bucketTagging?bucket={bucket}");

            restRequest.AddHeader("Authorization",
                                  $"{_storageProvider.GetManageToken(param)}");
            restRequest.AddParameter("application/json; charset=utf-8;", _jsonProvider.Serializer(new
            {
                Tags = request.Tags
            }),
                                     ParameterType.RequestBody);
            try
            {
                string response = restClient.Execute(restRequest).Content;
                return(GetResponse(response, () => new OperateResultDto(true,
                                                                        "success"),
                                   resultResponse =>
                                   new OperateResultDto(false, $"{resultResponse.Error}|{resultResponse.ErrorCode}")));
            }
            catch (Exception ex)
            {
                this._logger?.LogError(ex.ExtractAllStackTrace());
                return(new OperateResultDto(false, $"lose {ex.Message}"));
            }
        }
        /// <summary>
        /// 发送语音短信
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public SmsResponseDto <SendSmsResponseDto> SendVoiceSms(SendVoiceSmsParam param)
        {
            new SendVoiceSmsParamValidator().Validate(param).Check(HttpStatus.Err.Name);
            CommonRequest request =
                base.GetRequest("dyvmsapi.aliyuncs.com", "SingleCallByTts", "2017-05-25", "cn-hangzhou");

            request.AddQueryParameters("CalledNumber", param.Phone);
            request.AddQueryParameters("CalledShowNumber", param.CalledShowNumber);
            request.AddQueryParameters("TtsCode", param.TemplateCode);
            request.AddQueryParameters("PlayTimes", param.PlatTimes.ToString()); //播放次数
            request.AddQueryParameters("Volume", param.Volume.ToString());       //播放音量
            Dictionary <string, string> data = new Dictionary <string, string>();

            param.Content.ForEach(item => { data.Add(item.Key, item.Value); });
            request.AddQueryParameters("TtsParam", _jsonProvider.Serializer(data));
            try
            {
                CommonResponse response = GetClient().GetCommonResponse(request);
                if (response != null)
                {
                    var res = _jsonProvider
                              .Deserialize <SendVoiceSmsResponseDto>(
                        response.Data);
                    if (res != null)
                    {
                        SmsCode smsCode = SmsCodeMap.Where(x => x.Key == res.Code).Select(x => x.Value)
                                          .FirstOrDefault();

                        if (smsCode != default(SmsCode))
                        {
                            return(new SmsResponseDto <SendSmsResponseDto>()
                            {
                                Code = smsCode,
                                Msg = smsCode == SmsCode.Ok ? "success" : "lose",
                                Extend = new SendSmsResponseDto(param.Phone, new SendSmsExtend()
                                {
                                    BizId = smsCode == SmsCode.Ok
                                        ? _jsonProvider
                                            .Deserialize <SendVoiceSmsSuccessResponseDto>(
                                        response.Data).CallId
                                        : "",
                                    RequestId = res.RequestId,
                                    Msg = res.Message
                                })
                            });
                        }
                    }
                }
            }
            catch (ServerException e)
            {
            }

            return(new SmsResponseDto <SendSmsResponseDto>()
            {
                Code = SmsCode.Unknown,
                Msg = "发送异常"
            });
        }
Esempio n. 3
0
 public ContentResult Healthy2()
 {
     _userRepository.Add(new Users()
     {
         Name = "zhenlei520",
         Age  = 18
     });
     _userRepository.UnitOfWork.Commit();
     return(Content(_jsonProvider.Serializer(_userQuery.GetQueryable().ToList())));
 }
Esempio n. 4
0
        private Task HandleExceptionAsync(HttpContext context, int statusCode, string msg)
        {
            var data = new ApiErrResult(statusCode, msg);

            var result = _jsonProvider.Serializer(data);

            context.Response.ContentType = "application/json;charset=utf-8";

            return(context.Response.WriteAsync(result));
        }
        /// <summary>
        /// 指定短信列表发送短信
        /// </summary>
        /// <param name="phoneNumbers">手机号</param>
        /// <param name="templateCode">短信模板</param>
        /// <param name="content">内容</param>
        /// <param name="loseAction">失败回调函数</param>
        /// <returns></returns>
        public bool Send(List <string> phoneNumbers, string templateCode, object content,
                         Action <SendSmsLoseDto> loseAction = null)
        {
            Dictionary <string, string> commonParam = Util.BuildCommonParam(_smsConfig.AccessKey);

            commonParam.Add("Action", "SendSms");
            commonParam.Add("Version", "2017-05-25");
            commonParam.Add("RegionId", "cn-hangzhou");
            commonParam.Add("PhoneNumbers", phoneNumbers.ConvertListToString(','));
            commonParam.Add("SignName", _smsConfig.SignName);
            commonParam.Add("TemplateCode", templateCode);
            commonParam.Add("TemplateParam", _jsonProvider.Serializer(content));

            string sign = Util.CreateSign(commonParam, _smsConfig.EncryptionKey);

            commonParam.Add("Signature", sign);
            RestRequest request = new RestRequest(Method.GET);

            foreach (var key in commonParam.Keys)
            {
                request.AddQueryParameter(key, commonParam[key]);
            }

            var             response = _restClient.Execute(request);
            SendSmsResponse result   = _xmlProvider.Deserialize <SendSmsResponse>(response.Content);

            if (result.Code == "OK")
            {
                return(true);
            }

            loseAction?.Invoke(new SendSmsLoseDto()
            {
                PhoneList = phoneNumbers,
                Msg       = "短信发送失败",
                SubMsg    = response.Content,
                Code      = result.Code
            });
            return(false);
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="config"></param>
        /// <param name="code"></param>
        /// <param name="errCode">错误码</param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        public LoginResultConfig Login(WxConfig config, string code, int?errCode = null)
        {
            LoginResultConfig loginResult = new LoginResultConfig();

            if (config.Type == WebChatType.ThirdPartyLogins.Id)
            {
                if (string.IsNullOrEmpty(code))
                {
                    throw new BusinessException("登录失败,授权异常", errCode ?? HttpStatus.Err.Id);
                }

                WxUserInfo wxUserInfo = JsonConvert.DeserializeObject <WxUserInfo>(code);
                if (wxUserInfo == null)
                {
                    throw new BusinessException("登录失败,授权异常", errCode ?? HttpStatus.Err.Id);
                }

                loginResult.Success = !string.IsNullOrEmpty(wxUserInfo.Openid) &&
                                      !string.IsNullOrEmpty(wxUserInfo.Unionid);

                loginResult.Error    = "";
                loginResult.OpenId   = wxUserInfo.Openid;
                loginResult.Unionid  = wxUserInfo.Unionid;
                loginResult.AppId    = config.AppId;
                loginResult.UserInfo = wxUserInfo;
            }
            else
            {
                //                string getAccessTokenUrl = "sns/oauth2/access_token?appid=" + appConfig.AppId + "&secret=" +
                //                                           appConfig.AppSecret + "&code=" +
                //                                           code + "&grant_type=authorization_code";

                string getAccessTokenUrl;
                if (config.Type == WebChatType.MWeb.Id)
                {
                    getAccessTokenUrl = "sns/oauth2/access_token?appid=" + config.AppId + "&secret=" +
                                        config.AppSecret + "&code=" +
                                        code + "&grant_type=authorization_code";
                }
                else
                {
                    getAccessTokenUrl =
                        $"sns/jscode2session?appid={config.AppId}&secret={config.AppSecret}&js_code={code}&grant_type=authorization_code";
                }

                string getAccessTokenResponse = WebChatJsSdkCommon.RestClient
                                                .Execute(new RestRequest(getAccessTokenUrl
                                                                         ))
                                                .Content;


                GetAccessTokenResultConfig getAccessTokenResult =
                    JsonConvert.DeserializeObject <GetAccessTokenResultConfig>(getAccessTokenResponse);


                loginResult.Success = !string.IsNullOrEmpty(getAccessTokenResult.Openid) &&
                                      !string.IsNullOrEmpty(getAccessTokenResult.Unionid);

                loginResult.Error   = getAccessTokenResult.Errmsg;
                loginResult.OpenId  = getAccessTokenResult.Openid;
                loginResult.Unionid = getAccessTokenResult.Unionid;
                loginResult.AppId   = config.AppId;


                if (loginResult.Success)
                {
                    string getUserInfoResponse = WebChatJsSdkCommon.RestClient
                                                 .Execute(new RestRequest("sns/userinfo?access_token=" + getAccessTokenResult.AccessToken +
                                                                          "&openid=" + loginResult.OpenId + "")).Content;


                    WxUserInfo wxUserInfo = JsonConvert.DeserializeObject <WxUserInfo>(getUserInfoResponse);

                    if (string.IsNullOrEmpty(wxUserInfo.Errmsg) && string.IsNullOrEmpty(wxUserInfo.Errcode))
                    {
                        loginResult.UserInfo = wxUserInfo;
                    }
                    else
                    {
                        loginResult.UserInfo = null;
                    }
                }

                if (!loginResult.Success)
                {
                    _logger.LogError("[微信登陆失败]\r\n请求参数:" + getAccessTokenUrl + "\r\n返回参数:" +
                                     _jsonProvider.Serializer(getAccessTokenResult, true));
                }
            }

            return(loginResult);
        }
Esempio n. 7
0
        /// <summary>
        /// 将对象序列化成JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        private string ConvertJson <T>(T value)
        {
            string result = value is string?value.ToString() : _jsonProvider.Serializer(value);

            return(result);
        }