frmMicroLetterPlatformInterface.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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.OAShopManagement
  10. {
  11. public partial class frmMicroLetterPlatformInterface : LYFZ.Software.UI.OAShopManagement.frmMicroLetterPlatformInterface
  12. {
  13. BLL.BLL_ErpMicroInterface bllInterface = new BLL.BLL_ErpMicroInterface();
  14. string _PrimitiveID = "";
  15. /// <summary>
  16. /// 门店ID 每个门店只能添加一个公众号
  17. /// </summary>
  18. public string PrimitiveID
  19. {
  20. get { return _PrimitiveID; }
  21. set { _PrimitiveID = value; }
  22. }
  23. public string MenuData = "";
  24. public frmMicroLetterPlatformInterface()
  25. {
  26. this.btnAuthorizationAndBind.Click += btnAuthorizationAndBind_Click;
  27. this.btnClose.Click += btnClose_Click;
  28. this.Load += frmMicroLetterPlatformInterface_Load;
  29. this.btnDetected.Click += btnDetected_Click;
  30. this.btnCustommenuManager.Click += btnCustommenuManager_Click;
  31. this.btnAutoReply.Click += btnAutoReply_Click;
  32. }
  33. LYFZ.BLL.BLL_ErpSystemConfigure config = new BLL.BLL_ErpSystemConfigure();
  34. LYFZ.Model.ModelAPPInterface AppModel = new Model.ModelAPPInterface();
  35. void btnAuthorizationAndBind_Click(object sender, EventArgs e)
  36. {
  37. try
  38. {
  39. string myDomainName = AppModel.AppEnId+modFace.AppID;
  40. System.Diagnostics.Process.Start(BLL.BLL_MicroInterface.GetAuthorizationAndBindUrl(modFace.AppID, myDomainName));
  41. }
  42. catch { }
  43. }
  44. void btnAutoReply_Click(object sender, EventArgs e)
  45. {
  46. frmMicroLetterAutoReply frmAutorly = new frmMicroLetterAutoReply();
  47. frmAutorly.Show();
  48. }
  49. void btnCustommenuManager_Click(object sender, EventArgs e)
  50. {
  51. frmCustommenuManager frmcmanager = new frmCustommenuManager();
  52. frmcmanager.ID = modFace.ID;
  53. frmcmanager.ShowDialog();
  54. MenuData = frmcmanager.MenuData;
  55. }
  56. List<LYFZ.Model.Model_ErpCompanyInfo> compayList = new List<Model.Model_ErpCompanyInfo>();
  57. public List<LYFZ.Model.Model_ErpCompanyInfo> CompayList
  58. {
  59. get { return compayList; }
  60. set { compayList = value; }
  61. }
  62. private delegate void UpdateControl();
  63. /// <summary>
  64. /// 保存接口信息
  65. /// </summary>
  66. void Save_MicroInterface()
  67. {
  68. try
  69. {
  70. /* modelMIF.MicroName = this.txtPublicNumberName.Text.Trim();
  71. modelMIF.MicroNumber = this.txtMicroSignal.Text.Trim();
  72. modelMIF.PrimitiveID = "";// this.txtPrimitiveID.Text.Trim();
  73. modelMIF.APPEnterpriseID = this.txtAPPEnterpriseID.Text.Trim();
  74. modelMIF.AppID = this.txtAppID.Text.Trim();
  75. modelMIF.AppSecret = "";//this.txtAppSecret.Text.Trim();
  76. modelMIF.Token = this.txtToken.Text.Trim();
  77. modelMIF.EncodingAESKey = "";//this.txtEncodingAESKey.Text.Trim();
  78. //modelMIF.InterfaceUrl = LYFZ.BLL.BLL_MicroInterface.GetMicroServerAddressUrl(LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.SDomainName).Trim();
  79. //if (LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.IsBranchStore)
  80. //{
  81. // modelMIF.InterfaceUrl = LYFZ.BLL.BLL_MicroInterface.GetMicroServerAddressUrl(LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.MainStoreDomainName).Trim(); ;
  82. //}
  83. // modelMIF.VisitDomainName = this.txtVisitDomainName.Text.Trim().ToLower().Replace("http://", "");
  84. modelMIF.IsThirdPartyPlatformURL = false;//this.cbxIsThirdPartyPlatformURL.Checked;
  85. modelMIF.ThirdPartyPlatformURL = "";//this.txtThirdPartyPlatformURL.Text.Trim();*/
  86. /* if (this.cbxIsThirdPartyPlatformURL.Checked)
  87. {
  88. if (modelMIF.ThirdPartyPlatformURL.Trim().Length <= 0 || !modelMIF.ThirdPartyPlatformURL.Trim().ToLower().Contains("http://"))
  89. {
  90. MessageBoxCustom.Show("请输入正确的第三方平台接口URL地址!"); return;
  91. }
  92. }*/
  93. string cCompanyID = "";
  94. if (this.comboBoxMdian.SelectedItem == null)
  95. {
  96. MessageBoxCustom.Show("请选择所属门店");
  97. return;
  98. }
  99. LYFZ.Model.Model_ErpCompanyInfo selectCom = (LYFZ.Model.Model_ErpCompanyInfo)((ItemValue)this.comboBoxMdian.SelectedItem).Value;
  100. cCompanyID = selectCom.Company_DividedShop;
  101. modFace.AppID = this.txtAppID.Text.Trim();
  102. modFace.MicroName = this.txtPublicNumberName.Text.Trim();
  103. modFace.MicroNumber = this.txtMicroSignal.Text.Trim();
  104. modFace.PrimitiveID = cCompanyID;
  105. if (String.IsNullOrEmpty(AppModel.AppUrlPath.Trim()) || String.IsNullOrEmpty(AppModel.AppEnId.Trim()))
  106. {
  107. MessageBoxCustom.Show("APP接口还未注册授权");
  108. return;
  109. }
  110. if (modFace.AppID.Trim().Length < 1)
  111. {
  112. MessageBoxCustom.Show("请输入公众号的 APPID 可在公众号后查看");
  113. }
  114. else if (AppModel.AppUrlPath.IndexOf('.') <= 0 && AppModel.AppUrlPath.Length > 0)
  115. {
  116. MessageBoxCustom.Show("APP接口地址名格式不正确");
  117. }
  118. else
  119. {
  120. bool modFaceOK = false;
  121. if (modFace.ID > 0)
  122. {
  123. modFaceOK = bllInterface.Update(modFace);
  124. }
  125. else {
  126. if (bllInterface.Exists("PrimitiveID", cCompanyID))
  127. {
  128. MessageBoxCustom.Show("选择门店“" + selectCom.Company_Name + "”已经添加了公众号,每个门店只为绑定一个公众号");
  129. return;
  130. }
  131. modFaceOK = bllInterface.Add(modFace);
  132. }
  133. if (modFaceOK)
  134. {
  135. // MessageBoxCustom.Show("保存成功");
  136. modFace = (LYFZ.Model.Model_ErpMicroInterface)bllInterface.GetModelObject("PrimitiveID", cCompanyID);
  137. Detecting();
  138. }
  139. else
  140. {
  141. MessageBoxCustom.Show("保存失败请重试");
  142. }
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. MessageBoxCustom.Show("保存出错:" + ex.Message);
  148. }
  149. }
  150. /// <summary>
  151. /// 是否正在检测
  152. /// </summary>
  153. static bool isDetecting = false;
  154. void btnDetected_Click(object sender, EventArgs e)
  155. {
  156. Save_MicroInterface();
  157. }
  158. void showDetectedInfo(string info, Color color)
  159. {
  160. try
  161. {
  162. if (this.InvokeRequired)
  163. {
  164. this.Invoke(new UpdateControl(delegate()
  165. {
  166. this.labDetectedInfo.ForeColor = color;
  167. this.labDetectedInfo.Text = info;
  168. }));
  169. }
  170. else {
  171. this.labDetectedInfo.ForeColor = color;
  172. this.labDetectedInfo.Text = info;
  173. }
  174. }
  175. catch { }
  176. isDetecting = false;
  177. }
  178. LYFZ.BLL.BLL_ErpCompanyInfo comInfoDal = new BLL.BLL_ErpCompanyInfo();
  179. /// <summary>
  180. /// 检测接口状态
  181. /// </summary>
  182. void Detecting()
  183. {
  184. if (!isDetecting)
  185. {
  186. this.btnAuthorizationAndBind.Enabled = false;
  187. System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
  188. {
  189. showDetectedInfo("保存成功,正在检测请稍等...", Color.DarkOrange);
  190. isDetecting = true;
  191. string url = LYFZ.BLL.BLL_MicroInterface.GetPostInfoUrl();
  192. LYFZ.Model.Model_ErpCompanyInfo compModel = comInfoDal.GetModel("Company_DividedShop",modFace.PrimitiveID);
  193. if (compModel.ID <= 0)
  194. {
  195. showDetectedInfo("获取本店信息失败...", Color.Red);
  196. return;
  197. }
  198. // int PortNumber = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.WebServerPort;
  199. string CompanyName = "未知客户";
  200. if (compModel.ID > 0)
  201. {
  202. //店名
  203. CompanyName = compModel.Company_Name + "_" + compModel.Company_Manager + LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.GetSoftwareVersionDescription();
  204. }
  205. string JMGDomainName =AppModel.AppEnId+modFace.AppID;
  206. string ShellDomainName = AppModel.AppUrlPath.Trim();
  207. string AppID = modFace.AppID.Trim();
  208. string MicroNumber = modFace.MicroNumber;
  209. string postData = LYFZ.BLL.BLL_MicroInterface.GetPostServerAddressData(JMGDomainName, ShellDomainName, System.Web.HttpUtility.UrlEncode(CompanyName), AppModel.AppEnId, AppID, MicroNumber, System.Web.HttpUtility.UrlEncode(modFace.MicroName));
  210. string retMsg = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestPOST(url, postData);
  211. if (!retMsg.Trim().ToLower().Contains("Success".ToLower()))
  212. {
  213. showDetectedInfo("连接微信服务器平台失败,请检查网络是否正常后重试...", Color.Red);
  214. LYFZ.BLL.BLL_ErpSystemLogs.WriteSystemLog(EnumPublic.SystemLogsType.系统日志,
  215. retMsg+" 提交数据:"+ postData,
  216. "system",
  217. "system");
  218. // LYFZ.BLL.BLL_FileLogs.WriteMainLogs("连接微信服务器平台失败:" + retMsg);
  219. // LYFZ.BLL.BLL_FileLogs.WriteMainLogs("向微信服务器平台提交的数据:" + postData);
  220. return;
  221. }
  222. string getTestMsg = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestGET(AppModel.AppUrlPath + "/Default.aspx",Encoding.UTF8);
  223. if (!getTestMsg.ToLower().Contains("WEBAPI".ToLower()))
  224. {
  225. showDetectedInfo("检测到APP接口服务器没有正常工作...", Color.Red);
  226. return;
  227. }
  228. showDetectedInfo("手机APP和微信接口服务已正常工作...", Color.Green);
  229. this.Invoke(new UpdateControl(delegate()
  230. {
  231. this.btnAuthorizationAndBind.Enabled = true;
  232. }));
  233. }));
  234. t.Start();
  235. }
  236. else
  237. {
  238. MessageBoxCustom.Show("正在检测,请耐心等待!");
  239. }
  240. }
  241. LYFZ.BLL.BLL_MicroInterface bllMIF = new BLL.BLL_MicroInterface();
  242. LYFZ.Model.Model_ErpMicroInterface modFace=new Model.Model_ErpMicroInterface();
  243. void frmMicroLetterPlatformInterface_Load(object sender, EventArgs e)
  244. {
  245. // LYFZ.WindowsServiceHandling.MessageService.WeixinConfigRegister();
  246. // mConfig = new LYFZ.BLL.BLL_Config().GetModel_SysConfig();
  247. /* string WebMobileUrl =LYFZ.BLL.BLL_MicroInterface.GetWebAPPGoToUrl(LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.SDomainName.ToString());
  248. if (LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.IsBranchStore)
  249. {
  250. WebMobileUrl = LYFZ.BLL.BLL_MicroInterface.GetWebAPPGoToUrl(LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.MainStoreDomainName.ToString());
  251. }
  252. this.picQrCodeImage.Image = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.CreateCode_Choose(WebMobileUrl, "Byte", "M", 8, 4);*/
  253. if (!String.IsNullOrEmpty(PrimitiveID))
  254. {
  255. modFace = bllInterface.GetModelMicroInterface(PrimitiveID);
  256. //(LYFZ.Model.Model_ErpMicroInterface)bllInterface.GetModelObject("PrimitiveID", PrimitiveID);
  257. MenuData = modFace.MenuData;
  258. this.txtPublicNumberName.Text = modFace.MicroName.Trim();
  259. this.txtMicroSignal.Text = modFace.MicroNumber.Trim();
  260. this.txtAppID.Text = modFace.AppID.Trim();
  261. }
  262. BindMenDianList();
  263. AppModel = config.GetModelAPPInterface();
  264. showDetectedInfo("请点击“检测APP和微信接口”按钮检测接口服务是否正常", Color.DarkOrange);
  265. }
  266. void BindMenDianList()
  267. {
  268. this.comboBoxMdian.Items.Clear();
  269. foreach (LYFZ.Model.Model_ErpCompanyInfo com in compayList)
  270. {
  271. ItemValue item = new ItemValue(com, com.Company_Name);
  272. this.comboBoxMdian.Items.Add(item);
  273. if (PrimitiveID.Trim().Length > 0 && com.Company_DividedShop.ToLower() == PrimitiveID.ToLower())
  274. {
  275. this.comboBoxMdian.SelectedItem = item;
  276. }
  277. else if (PrimitiveID.Trim().Length <= 0 && com.Company_DividedShop.ToLower() == LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.CurrentCompanyInfoID.ToLower())
  278. {
  279. this.comboBoxMdian.SelectedItem = item;
  280. }
  281. }
  282. }
  283. void btnClose_Click(object sender, EventArgs e)
  284. {
  285. /*
  286. //测试代码
  287. try
  288. {
  289. LYFZ.BLL.BLL_SMSTemplates smsTemplates = new BLL.BLL_SMSTemplates();
  290. dynamic tMParameters = new System.Dynamic.ExpandoObject();
  291. tMParameters.XXX = "小小";
  292. tMParameters.XXXNO = "201505081446052981";
  293. tMParameters.MemberNo = "1360001";
  294. tMParameters.ConsumptionAmount = "100.00元";
  295. tMParameters.Time = SDateTime.Now;
  296. tMParameters.PointsIncrease = 0;
  297. tMParameters.ConsumptionType = "订购";
  298. tMParameters.ConsumptionStore = "门站";
  299. smsTemplates.GenerateEventTemplateMessage(EnumPublic.MicroSendEvent.现金卡消费后X分钟发提醒消息, tMParameters);
  300. }
  301. catch
  302. { }*/
  303. this.Close();
  304. }
  305. /// <summary>
  306. /// 上传文件到服务器
  307. /// </summary>
  308. /// <param name="SubPath"></param>
  309. /// <param name="fullFilePath"></param>
  310. /// <returns></returns>
  311. private bool UploadFileToServer(string yuname, string fullFilePath)
  312. {
  313. try
  314. {
  315. Random random = new Random();
  316. Bitmap bmp= LYFZ.BLL.SelectPhotoHandling.GeneratePreviewBitmap(fullFilePath, 800, 600);
  317. string newfilename = yuname + System.IO.Path.GetExtension(fullFilePath);
  318. if (LYFZ.BLL.SelectPhotoHandling.SaveIamge(bmp, System.IO.Path.GetDirectoryName(fullFilePath) + newfilename))
  319. {
  320. string url = "http://wx.lyfz.net:8086/WeiXinAPP/UploadPhoto.aspx?yuname=" + yuname + "&filename=" + newfilename + "&t=" + random.Next();
  321. System.Net.WebClient myWebClient = new System.Net.WebClient();
  322. myWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
  323. byte[] by = myWebClient.UploadFile(url, "POST", fullFilePath);
  324. string content = System.Text.Encoding.UTF8.GetString(by);
  325. if (content == "Success")
  326. {
  327. myWebClient.Dispose();
  328. myWebClient = null;
  329. try
  330. {
  331. System.IO.File.Delete(newfilename);
  332. }
  333. catch { }
  334. return true;
  335. }
  336. else
  337. {
  338. return false;
  339. }
  340. }
  341. else {
  342. return false;
  343. }
  344. }
  345. catch
  346. {
  347. return false;
  348. }
  349. }
  350. }
  351. }