frmAddMicroTemplateItem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace LYFZ.Software.MainBusiness.SMSManagement
  10. {
  11. public partial class frmAddMicroTemplateItem : LYFZ.Software.UI.SMSManagement.frmAddMicroTemplateItem
  12. {
  13. public frmAddMicroTemplateItem()
  14. {
  15. this.btnSave.Click += btnSave_Click;
  16. this.Shown += frmAddMicroTemplateItem_Shown;
  17. this.cboxTList.SelectedIndexChanged += cboxTList_SelectedIndexChanged;
  18. this.chkAutoGet.CheckedChanged += chkAutoGet_CheckedChanged;
  19. this.linkLabelURL.Click += linkLabelURL_Click;
  20. }
  21. void linkLabelURL_Click(object sender, EventArgs e)
  22. {
  23. try
  24. {
  25. System.Diagnostics.Process.Start(BLL.BLL_MicroInterface.GetGlobalReturnCodeURL());
  26. }
  27. catch { }
  28. }
  29. void frmAddMicroTemplateItem_Shown(object sender, EventArgs e)
  30. {
  31. modInterFace = bllInterface.GetModelMicroInterface(LYFZ.BLL.BLL_ErpCompanyInfo.CurrentCompanyInfoID);
  32. BindTemplateNo();
  33. if (this.SmsModel.ID > 0)
  34. {
  35. this.TemplateNo = (EnumPublic.MicroTemplate)this.SmsModel.ST_Category;
  36. this.cboxTList.SelectedItem = this.TemplateNo.ToString();
  37. this.txtSmsTID.Text = this.SmsModel.ST_Remark;
  38. this.lbTitle.Text = this.TemplateNo.ToString();
  39. this.lbContentExamples.Text = LYFZ.BLL.BLL_SMSTemplates.GetMicroTemplateExample(this.TemplateNo);
  40. this.txtSmsTNo.Text = LYFZ.WinAPI.CustomPublicMethod.ConvertEnumToDescription(this.TemplateNo);
  41. this.chkAutoGet.Checked = false;
  42. this.chkAutoGet.Enabled = false;
  43. this.cboxTList.Enabled = false;
  44. this.btnSave.Text = "保 存 修 改";
  45. }
  46. }
  47. void chkAutoGet_CheckedChanged(object sender, EventArgs e)
  48. {
  49. this.txtSmsTID.ReadOnly = this.chkAutoGet.Checked;
  50. }
  51. void cboxTList_SelectedIndexChanged(object sender, EventArgs e)
  52. {
  53. if (this.cboxTList.SelectedItem != null)
  54. {
  55. this.TemplateNo = (EnumPublic.MicroTemplate)Enum.Parse(typeof(EnumPublic.MicroTemplate), this.cboxTList.SelectedItem.ToString());
  56. this.lbTitle.Text = this.TemplateNo.ToString();
  57. this.lbContentExamples.Text = LYFZ.BLL.BLL_SMSTemplates.GetMicroTemplateExample(this.TemplateNo);
  58. this.txtSmsTNo.Text = LYFZ.WinAPI.CustomPublicMethod.ConvertEnumToDescription(this.TemplateNo);
  59. }
  60. }
  61. /// <summary>
  62. /// 当前消息模板
  63. /// </summary>
  64. EnumPublic.MicroTemplate _TemplateNo = EnumPublic.MicroTemplate.新订单通知;
  65. /// <summary>
  66. /// 当前消息模板
  67. /// </summary>
  68. public EnumPublic.MicroTemplate TemplateNo
  69. {
  70. get { return _TemplateNo; }
  71. set { _TemplateNo = value; }
  72. }
  73. /// <summary>
  74. /// 邦定消息模板
  75. /// </summary>
  76. void BindTemplateNo()
  77. {
  78. this.cboxTList.Items.Clear();
  79. this.cboxTList.DataSource = GetTemplateNoList();
  80. }
  81. List<string> GetTemplateNoList()
  82. {
  83. List<string> elist = new List<string>();
  84. elist.AddRange(Enum.GetNames(typeof(LYFZ.EnumPublic.MicroTemplate)));
  85. return elist;
  86. }
  87. LYFZ.DAL.DAL_SMSTemplates smsDal = new DAL.DAL_SMSTemplates();
  88. LYFZ.Model.Model_SMSTemplates smsModel = new Model.Model_SMSTemplates();
  89. /// <summary>
  90. /// 模板ID对象
  91. /// </summary>
  92. public LYFZ.Model.Model_SMSTemplates SmsModel
  93. {
  94. get { return smsModel; }
  95. set { smsModel = value; }
  96. }
  97. /// <summary>
  98. /// 客户端配置信息
  99. /// </summary>
  100. // LYFZ.Model.Model_Config mConfig = null;
  101. LYFZ.BLL.BLL_SMSTemplates mybllsmsT = new BLL.BLL_SMSTemplates();
  102. BLL.BLL_ErpMicroInterface bllInterface = new BLL.BLL_ErpMicroInterface();
  103. LYFZ.Model.Model_ErpMicroInterface modInterFace = new Model.Model_ErpMicroInterface();
  104. void btnSave_Click(object sender, EventArgs e)
  105. {
  106. this.SmsModel.ST_Remark = this.txtSmsTID.Text.Trim();
  107. if (!this.chkAutoGet.Checked && this.SmsModel.ST_Remark.Trim().Length <= 0)
  108. {
  109. ExMessageBoxCustom.Show("模板ID不能为空");
  110. return;
  111. }
  112. if (this.SmsModel.ID > 0)
  113. {
  114. if (smsDal.Update(this.SmsModel))
  115. {
  116. ExMessageBoxCustom.Show("修改微信模板ID成功。");
  117. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  118. }
  119. else
  120. {
  121. ExMessageBoxCustom.Show("修改微信模板ID失败,请关闭软件并重新启动后再试。");
  122. }
  123. }
  124. else
  125. {
  126. this.SmsModel.ST_Type = (int)EnumPublic.SmsTemplateType.微信模板ID;
  127. this.SmsModel.ST_Title = this.TemplateNo.ToString();
  128. this.SmsModel.ST_Category = (int)this.TemplateNo;
  129. this.SmsModel.ST_SMSContent = LYFZ.WinAPI.CustomPublicMethod.ConvertEnumToDescription(this.TemplateNo);
  130. this.SmsModel.ST_CreateDatetime = SDateTime.Now;
  131. this.SmsModel.ST_UpdateDatetime = SDateTime.Now;
  132. this.SmsModel.ST_DividedShop = LYFZ.BLL.BLL_ErpCompanyInfo.CurrentCompanyInfoID;
  133. bool isAutoGet = this.chkAutoGet.Checked;
  134. if (modInterFace.AppID == null || modInterFace.AppID.Trim().Length<=0)
  135. {
  136. MessageBoxCustom.Show("您还没有设置本店的公众号,请设置后重试");
  137. return;
  138. }
  139. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, System.ComponentModel.BackgroundWorker backgroundWorker)
  140. {
  141. backgroundWorker.ReportProgress(0, "正在检查数据...");
  142. if (!this.smsDal.ExistsMicroTemplate(this.SmsModel.ST_Category, LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.GetCompanyInfoID()))
  143. {
  144. if (isAutoGet)
  145. {
  146. backgroundWorker.ReportProgress(0, "正在下载模板...");
  147. // mConfig = new LYFZ.BLL.BLL_Config().GetModel_SysConfig();
  148. // int PortNumber = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.WebServerPort;
  149. // if (!LYFZ.WinAPI.CustomPublicMethod.CheckInternaNetworkAddress(mConfig.ServerHostOrIP) && LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.YS_EnableMapped)
  150. // {
  151. // PortNumber = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.YS_tAppport;
  152. // }
  153. //从公众号下载模板
  154. /* int clearAccessToken =0;//是否清容并刷新AccessToken
  155. if (this.chkaccess_token.Checked)
  156. {
  157. clearAccessToken = 1;
  158. }*/
  159. string resultStr = "";
  160. try
  161. {
  162. /*string url = LYFZ.BLL.BLL_MicroInterface.GetTemplatesIDUrl(mConfig.ServerHostOrIP, PortNumber, this.SmsModel.ST_SMSContent, this.SmsModel.ST_Category.ToString(), clearAccessToken);
  163. System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
  164. var result = client.GetAsync(url).Result;
  165. resultStr = result.Content.ReadAsStringAsync().Result;
  166. var retObj = Codeplex.Data.DynamicJson.Parse(resultStr);
  167. client.Dispose();*/
  168. // LYFZ.Model.Model_MicroInterface modelMIF = new BLL.BLL_MicroInterface().Model;
  169. string TemplatesNo = this.SmsModel.ST_SMSContent;
  170. EnumPublic.MicroTemplate TemplatesTitle = (EnumPublic.MicroTemplate)Convert.ToInt32(this.SmsModel.ST_Category.ToString());
  171. string getTemplatesUrl = "http://wx.lyfz.net/WeiXinAPP/WXServiceHandler.aspx?type=gettemplatesid&appid=" + modInterFace.AppID.Trim() + "&templatesno=" + TemplatesNo;
  172. string retTemplatesStr = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestGET(getTemplatesUrl, Encoding.UTF8);
  173. //"{ \"errcode\":-100,\"errmsg\":\"发送模板消息失败\", \"msgid\":\"\"}";
  174. if (retTemplatesStr.Contains("\"errcode\":"))
  175. {
  176. var retTemplatesID = Codeplex.Data.DynamicJson.Parse(retTemplatesStr);// LYFZ.Weixin.SDK.TemplateMessageAPI.GetTemplates(LYFZ.Weixin.SDK.Helpers.TokenHelper.AccessToken, this.SmsModel.ST_SMSContent);
  177. if (retTemplatesID.errmsg.ToString().ToLower() == "ok")
  178. {
  179. LYFZ.Model.Model_SMSTemplates modelsmsT = new Model.Model_SMSTemplates();
  180. modelsmsT.ST_Type = (int)EnumPublic.SmsTemplateType.微信模板ID;
  181. modelsmsT.ST_Title = TemplatesTitle.ToString();
  182. modelsmsT.ST_Category = (int)TemplatesTitle;
  183. modelsmsT.ST_SMSContent = TemplatesNo;
  184. modelsmsT.ST_Remark = retTemplatesID.template_id.ToString();
  185. modelsmsT.ST_CreateDatetime = DateTime.Now;
  186. modelsmsT.ST_UpdateDatetime = DateTime.Now;
  187. modelsmsT.ST_DividedShop = LYFZ.BLL.BLL_ErpCompanyInfo.CurrentCompanyInfoID;
  188. if (mybllsmsT.Add(modelsmsT))
  189. {
  190. ExMessageBoxCustom.Show("添加微信模板成功。", backgroundWorker: backgroundWorker);
  191. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  192. }
  193. else
  194. {
  195. ExMessageBoxCustom.Show("向数据库插入记录时失败,请稍后重试。", backgroundWorker: backgroundWorker);
  196. }
  197. }
  198. else
  199. {
  200. ExMessageBoxCustom.Show("添加微信模板失败,错误代码:" + retTemplatesID.ToString(), backgroundWorker: backgroundWorker);
  201. }
  202. }
  203. else {
  204. ExMessageBoxCustom.Show("添加微信模板失败,原因:" + retTemplatesStr, backgroundWorker: backgroundWorker);
  205. }
  206. }
  207. catch (Exception ex){
  208. ExMessageBoxCustom.Show("添加微信模板失败,错误代码:" + ex.Message + "\r\n" + resultStr, backgroundWorker: backgroundWorker);
  209. }
  210. }
  211. else
  212. {
  213. backgroundWorker.ReportProgress(0, "正在保存模板...");
  214. if (smsDal.Add(this.SmsModel))
  215. {
  216. ExMessageBoxCustom.Show("添加微信模板成功。", backgroundWorker: backgroundWorker);
  217. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  218. }
  219. else
  220. {
  221. ExMessageBoxCustom.Show("添加微信模板ID失败,请关闭软件并重新启动后再试。", backgroundWorker: backgroundWorker);
  222. }
  223. }
  224. }
  225. else
  226. {
  227. ExMessageBoxCustom.Show("要添加微信模板已经存在,如果你要更新模板,请删除后重试。", backgroundWorker: backgroundWorker);
  228. }
  229. });
  230. }
  231. }
  232. }
  233. }