Esempio n. 1
0
        /// <summary>
        /// 获取配置节点BOOL类型信息(1-True,else-False) 如果没有词配置项,则自动创建并赋默认值
        /// </summary>
        /// <param name="key">配置项关键字</param>
        /// <param name="defaultValue">配置项默认值</param>
        ///<param name="createKeyAuto">自动创建配置项</param>
        /// <returns></returns>
        public static bool GetConfigBool(string key, bool defaultValue = false, bool createKeyAuto = false)
        {
            bool   result = defaultValue;
            string cfgVal = GetConfigString(key);

            if (string.IsNullOrWhiteSpace(cfgVal))
            {
                if (createKeyAuto)
                {
                    if (cfgVal == null)
                    {
                        ConfigHelper.Addconfig(key, defaultValue ? "1" : "0");
                    }
                    else
                    {
                        ConfigHelper.UpdateConfig(key, defaultValue ? "1" : "0");
                    }
                }
            }
            else
            {
                result = cfgVal.Trim() == "1";
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 得到AppSettings中的配置int信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetConfigInt(string key, int defaultValue = 0, bool createKeyAuto = false)
        {
            int    result = defaultValue;;
            string cfgVal = GetConfigString(key);

            if (!string.IsNullOrWhiteSpace(cfgVal))
            {
                try
                {
                    result = int.Parse(cfgVal);
                }
                catch
                {
                    TmoShare.WriteLog("读取配置文件int类型失败 节点:" + key + " ,值:" + cfgVal);
                }
            }
            else
            {
                if (createKeyAuto)
                {
                    if (cfgVal == null)
                    {
                        ConfigHelper.Addconfig(key, defaultValue.ToString());
                    }
                    else
                    {
                        ConfigHelper.UpdateConfig(key, defaultValue.ToString());
                    }
                }
            }
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// 获取用户配置信息 如果没有词配置项,则自动创建并赋默认值
        /// </summary>
        /// <param name="key">配置项关键字</param>
        /// <param name="DefaultValue">配置项默认值</param>
        ///<param name="CreateKeyAuto">自动创建配置项</param>
        /// <returns></returns>
        public static string GetConfigString(string Key, string DefaultValue, bool CreateKeyAuto)
        {
            string val = ConfigHelper.GetConfigString(Key);

            if (string.IsNullOrWhiteSpace(val))
            {
                if (CreateKeyAuto)
                {
                    if (val == null)
                    {
                        ConfigHelper.Addconfig(Key, DefaultValue);
                    }
                    else
                    {
                        ConfigHelper.UpdateConfig(Key, DefaultValue);
                    }
                }
                return(DefaultValue);
            }
            else
            {
                return(val);
            }
        }
Esempio n. 4
0
        /// <summary>
        ///获得模板ID
        /// </summary>
        /// <param name="infoValue">0-医生编号 1-模板库中的短ID</param>
        /// <returns></returns>
        public static string WXGetTemplateSetID(object[] infoValue)
        {
            string strRecode = "获得微信模板ID失败!-原因:{0}-当前医生:{1}";

            try
            {
                if (string.IsNullOrEmpty(infoValue[0].ToString()))  //参数缺少医生编号
                {
                    LogHelper.Log.Error(string.Format(strRecode, "err_wx_002(参数中未找到医生编号)", "null"));
                    return("err_wx_002");
                }
                string doc_code          = infoValue[0].ToString(); //取得参数中的值
                string template_id_short = infoValue[1].ToString();

                #region 发送命令
send:
                string AccessToken = WeChatHelper.WXGetAccessToken(new object[] { "admin" }, true);
                if (string.IsNullOrEmpty(AccessToken) || AccessToken.StartsWith("err"))
                {
                    LogHelper.Log.Error(string.Format(strRecode, "err_wx_accessToken(获取accessToken失败:(" + AccessToken + "))", doc_code));
                    return("err_wx_accessToken");
                }
                string     url      = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=" + AccessToken;
                string     postdata = "{\"template_id_short\":\"" + template_id_short + "\"}";
                WebRequest request  = WebRequest.Create(url);
                request.Method      = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                Stream writer = request.GetRequestStream();
                byte[] pdarry = Encoding.UTF8.GetBytes(postdata);
                writer.Write(pdarry, 0, pdarry.Length);
                writer.Close();
                #endregion
                #region 取得命令返回值
                WebResponse  response = request.GetResponse();
                Stream       rpReader = response.GetResponseStream();
                StreamReader reader   = new StreamReader(rpReader, Encoding.UTF8);
                string       rtStr    = reader.ReadToEnd();
                reader.Close();
                rpReader.Close();
                response.Close();
                rtStr = rtStr.Replace("&lt;", "<").Replace("&gt;", ">");
                #endregion

                #region 结果处理
                if (string.IsNullOrEmpty(rtStr))
                {
                    return("err_wx_003");
                }
                var nodes = TmoCommon.JsonHelper.JSONToObject <Dictionary <string, string> >(rtStr);
                if (nodes == null || nodes.Count == 0)
                {
                    return("err_json_converter");
                }
                if (nodes.ContainsKey("errcode"))
                {
                    string errcode = nodes["errcode"].Trim();
                    if (errcode == "0")
                    {
                        if (nodes.ContainsKey("template_id"))
                        {
                            string          template_id = nodes["template_id"].Trim();
                            WxTemplateID    template    = null;
                            BinaryFormatter bf          = new BinaryFormatter();
                            string          configValue = ConfigHelper.GetConfigString("wx_templateid");
                            if (!string.IsNullOrEmpty(configValue))
                            {
                                MemoryStream msr = new MemoryStream(Encoding.UTF8.GetBytes(configValue));
                                template = (WxTemplateID)bf.Deserialize(msr);
                                msr.Close();
                            }
                            if (template == null)
                            {
                                template = new WxTemplateID(TmoShare.WX_APP_ID);
                            }
                            template.AddTemplate(template_id_short, template_id);
                            byte[]       tmp = new byte[1024];
                            MemoryStream msw = new MemoryStream(tmp);
                            bf.Serialize(msw, template);
                            msw.Close();
                            configValue = Encoding.UTF8.GetString(tmp);
                            if (!ConfigHelper.UpdateConfig("wx_templateid", configValue, true))
                            {
                                ConfigHelper.GetConfigString("wx_templateid", configValue, true);
                            }
                            return(template_id); //成功
                        }
                        else
                        {
                            return("err_success");
                        }
                    }
                    else if (errcode == "40001" || errcode == "40002") //access_token问题
                    {
                        AccessToken = WeChatHelper.WXGetAccessToken(new object[] { "admin" }, false);
                        goto send;
                    }
                    else
                    {
                        return("err_" + errcode + "_" + nodes["errmsg"]);
                    }
                }
                else
                {
                    return("err_errcode_null");
                }
                #endregion
            }
            catch (Exception e)
            {
                LogHelper.Log.Error(string.Format(strRecode, "err_wx_001(未知异常失败)", infoValue[0]), e);
                return("err_wx_001");
            }
        }