示例#1
0
        /// <summary>
        /// 【电信】发送短信
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="templateParam"></param>
        /// <param name="tempateId"></param>
        private static void SendSmsByDx(string mobile, Dictionary <string, object> templateParam, int tempateId)
        {
            string token = GetDxSmsAccessToken(0);

            if (string.IsNullOrEmpty(token))
            {
                return;
            }
            Dictionary <string, string> paras = new Dictionary <string, string>();

            paras.Add("acceptor_tel", mobile);
            paras.Add("access_token", token);
            paras.Add("app_id", ConfigUtil.SmsAppId_dx);
            paras.Add("template_id", tempateId.ToString());
            paras.Add("template_param", JsonUtil.Serialize(templateParam));
            paras.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            try
            {
                string res = NetUtil.SendPostRequest(SendDxSmsApi, paras);
                LogUtil.Debug(string.Format("发送手机短信:{0},{1}", mobile, JsonUtil.Serialize(templateParam)));
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex.ToString());
            }
        }
示例#2
0
        /// <summary>
        /// 获取登录Token
        /// </summary>
        /// <param name="tryNum"></param>
        /// <returns></returns>
        private static string GetDxSmsAccessToken(int tryNum)
        {
            string key = "smstoken";

            if (_dXSmsToken.ContainsKey(key))
            {
                var kv = _dXSmsToken[key];
                if (kv.Item2 > DateTime.Now)
                {
                    return(kv.Item1);
                }
            }
            Dictionary <string, string> paras = new Dictionary <string, string>();

            paras.Add("grant_type", "client_credentials");
            paras.Add("app_id", ConfigUtil.SmsAppId_dx);
            paras.Add("app_secret", ConfigUtil.SmsAppSecret_dx);
            try
            {
                string res = NetUtil.SendPostRequest(GetDxSmsTokenApi, paras);
                Dictionary <string, string> resDic = JsonUtil.Deserialize <Dictionary <string, string> >(res);
                if (resDic.ContainsKey("access_token"))
                {
                    _dXSmsToken.Clear();
                    _dXSmsToken.Add(key, new Tuple <string, DateTime>(resDic["access_token"], DateTime.Now.AddSeconds(ConvertUtil.ToInt(resDic["expires_in"], 0))));
                    return(resDic["access_token"]);
                }
                else
                {
                    LogUtil.Debug(resDic["res_message"]);
                    return(string.Empty);
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex.ToString());
                System.Threading.Thread.Sleep(200);
                if (tryNum < 3)
                {
                    return(GetDxSmsAccessToken(++tryNum));
                }
                else
                {
                    return(string.Empty);
                }
            }
        }