예제 #1
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()))  //参数缺少医生编号
                {
                    TmoShare.WriteLog(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"))
                {
                    TmoShare.WriteLog(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)
            {
                TmoShare.WriteLog(string.Format(strRecode, "err_wx_001(未知异常失败)", infoValue[0]) + e.Message);
                return("err_wx_001");
            }
        }