frmMicroAppletAccountManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using LYFZ.BLL.MicroApplet;
  2. using LYFZ.Model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace LYFZ.Software.MainBusiness.MicroApplet
  10. {
  11. public class frmMicroAppletAccountManager:LYFZ.Software.MicroApplet.frmMicroAppletAccountManager
  12. {
  13. public frmMicroAppletAccountManager() {
  14. this.Load += FrmMicroAppletAccountManager_Load;
  15. this.Shown += FrmMicroAppletAccountManager_Shown;
  16. this.dataGridViewEx1.CellContentClick += DataGridViewEx1_CellContentClick;
  17. this.btnSave.Click += BtnSave_Click;
  18. this.btnReset.Click += BtnReset_Click;
  19. }
  20. private void BtnReset_Click(object sender, EventArgs e)
  21. {
  22. currentViewRow = null;
  23. this.txtAccount.Text = "";
  24. this.txtPassword.Text = "";
  25. this.txtRemarks.Text = "";
  26. this.comboBoxMdian.Enabled = true;
  27. }
  28. public bool isUpdate = false;
  29. private void BtnSave_Click(object sender, EventArgs e)
  30. {
  31. if (this.comboBoxMdian.SelectedItem != null)
  32. {
  33. LYFZ.Model.Model_ErpCompanyInfo comp = (LYFZ.Model.Model_ErpCompanyInfo)((ItemValue)this.comboBoxMdian.SelectedItem).Value;
  34. MicroAppletAccount account = null;
  35. if (currentViewRow != null)
  36. {
  37. account = (MicroAppletAccount)currentViewRow.Tag;
  38. }
  39. else {
  40. account = new MicroAppletAccount();
  41. account.ID = LYFZ.WinAPI.CustomPublicMethod.GenerateId().ToString();
  42. account.ShopID = comp.Company_DividedShop;
  43. if (this.MicroAppletAccountList.Any(a => a.ShopID.ToLower() == account.ShopID.ToLower()))
  44. {
  45. MessageBoxCustom.Show(String.Format("门店“{0}”小程序帐号已存在,不能重复添加",comp.Company_Name));
  46. return;
  47. }
  48. }
  49. account.Account = this.txtAccount.Text.Trim();
  50. account.Password = this.txtPassword.Text.Trim();
  51. account.Remarks = this.txtRemarks.Text.Trim();
  52. if (String.IsNullOrEmpty(account.Account) || String.IsNullOrEmpty(account.Password))
  53. {
  54. MessageBoxCustom.Show("帐号和密码不能为空");
  55. return;
  56. }
  57. bool loginState = false;
  58. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate (object obj, BackgroundWorker backgroundWorker)
  59. {
  60. try
  61. {
  62. backgroundWorker.ReportProgress(0, "正在验证授权...");
  63. MicroAppLoginReturn loginModel = frmMicroAppletAdmin.CompanyLoginReturnToken(account.Account, account.Password);
  64. if (loginModel.Code != 200)
  65. {
  66. MessageBoxCustom.Show("获取授权失败:" + loginModel.Message, backgroundWorker: backgroundWorker);
  67. }
  68. else {
  69. loginState = true;
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. MessageBoxCustom.Show(String.Format("获取授权时错误:{0}", ex.Message), backgroundWorker: backgroundWorker);
  75. }
  76. });
  77. if (!loginState)
  78. {
  79. return;
  80. }
  81. bool saveState = false;
  82. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate (object obj, BackgroundWorker backgroundWorker)
  83. {
  84. try
  85. {
  86. backgroundWorker.ReportProgress(0, "正在保存...");
  87. if (currentViewRow == null)
  88. {
  89. this.MicroAppletAccountList.Add(account);
  90. }
  91. saveState = configBll.SaveMicroAppletAccountList(this.MicroAppletAccountList);
  92. }
  93. catch (Exception ex)
  94. {
  95. MessageBoxCustom.Show(String.Format("加载帐号信息时错误:{0}", ex.Message), backgroundWorker: backgroundWorker);
  96. }
  97. });
  98. BtnReset_Click(null, null);
  99. if (saveState)
  100. {
  101. isUpdate = true;
  102. MessageBoxCustom.Show("保存成功");
  103. }
  104. else
  105. {
  106. MessageBoxCustom.Show("保存失败,请重试!");
  107. }
  108. FrmMicroAppletAccountManager_Shown(null, null);
  109. } else {
  110. MessageBoxCustom.Show("请选择门店");
  111. }
  112. }
  113. private void DataGridViewEx1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  114. {
  115. if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
  116. {
  117. switch (e.ColumnIndex)
  118. {
  119. case 3:
  120. SetMicroAppletAccount(this.dataGridViewEx1.Rows[e.RowIndex]);
  121. break;
  122. case 4:
  123. DelMicroAppletAccount(this.dataGridViewEx1.Rows[e.RowIndex]);
  124. break;
  125. }
  126. }
  127. }
  128. DataGridViewRow currentViewRow = null;
  129. void SetMicroAppletAccount(DataGridViewRow row)
  130. {
  131. currentViewRow = row;
  132. MicroAppletAccount account = (MicroAppletAccount)row.Tag;
  133. foreach (ItemValue item in this.comboBoxMdian.Items)
  134. {
  135. LYFZ.Model.Model_ErpCompanyInfo comp = (LYFZ.Model.Model_ErpCompanyInfo)item.Value;
  136. if (comp.Company_DividedShop.ToLower() == account.ShopID.ToLower())
  137. {
  138. this.comboBoxMdian.SelectedItem = item;
  139. break;
  140. }
  141. }
  142. this.comboBoxMdian.Enabled = false;
  143. this.txtAccount.Text = account.Account;
  144. this.txtPassword.Text = account.Password;
  145. this.txtRemarks.Text = account.Remarks;
  146. }
  147. void DelMicroAppletAccount(DataGridViewRow row)
  148. {
  149. MicroAppletAccount account = (MicroAppletAccount)row.Tag;
  150. this.MicroAppletAccountList.Remove(this.MicroAppletAccountList.Find(m => m.ID.ToLower() == account.ID.ToLower()));
  151. bool delState = false;
  152. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate (object obj, BackgroundWorker backgroundWorker)
  153. {
  154. try
  155. {
  156. backgroundWorker.ReportProgress(0, "正在删除...");
  157. delState = configBll.SaveMicroAppletAccountList(this.MicroAppletAccountList);
  158. }
  159. catch (Exception ex)
  160. {
  161. MessageBoxCustom.Show(String.Format("加载帐号信息时错误:{0}", ex.Message), backgroundWorker: backgroundWorker);
  162. }
  163. });
  164. if (delState)
  165. {
  166. isUpdate = true;
  167. FrmMicroAppletAccountManager_Shown(null, null);
  168. }
  169. else
  170. {
  171. MessageBoxCustom.Show("删除失败,请重试!");
  172. }
  173. }
  174. BLL.BLL_ErpSystemConfigure configBll=new BLL.BLL_ErpSystemConfigure();
  175. private void FrmMicroAppletAccountManager_Shown(object sender, EventArgs e)
  176. {
  177. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate (object obj, BackgroundWorker backgroundWorker)
  178. {
  179. try
  180. {
  181. backgroundWorker.ReportProgress(0, "正在加载帐号信息...");
  182. _MicroAppletAccountList = configBll.GetMicroAppletAccountList();
  183. }
  184. catch (Exception ex)
  185. {
  186. MessageBoxCustom.Show(String.Format("加载帐号信息时错误:{0}", ex.Message), backgroundWorker: backgroundWorker);
  187. }
  188. });
  189. this.dataGridViewEx1.Rows.Clear();
  190. foreach (MicroAppletAccount account in _MicroAppletAccountList)
  191. {
  192. DataGridViewRow row = new DataGridViewRow();
  193. row.CreateCells(this.dataGridViewEx1);
  194. row.Cells[0].Value = GetCompanyInfo(account.ShopID).Company_Name;
  195. row.Cells[1].Value = account.Account;
  196. row.Cells[2].Value = account.Remarks;
  197. row.Cells[3].Value = "编辑";
  198. row.Cells[4].Value = "删除";
  199. row.Tag = account;
  200. this.dataGridViewEx1.Rows.Add(row);
  201. }
  202. }
  203. List<MicroAppletAccount> _MicroAppletAccountList = new List<MicroAppletAccount>();
  204. List<MicroAppletAccount> MicroAppletAccountList
  205. {
  206. get { return _MicroAppletAccountList; }
  207. set { this._MicroAppletAccountList = value; }
  208. }
  209. string _PrimitiveID = "";
  210. /// <summary>
  211. /// 门店ID 每个门店只能添加一个小程序
  212. /// </summary>
  213. public string PrimitiveID
  214. {
  215. get { return _PrimitiveID; }
  216. set { _PrimitiveID = value; }
  217. }
  218. private void FrmMicroAppletAccountManager_Load(object sender, EventArgs e)
  219. {
  220. this.PrimitiveID = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.CurrentCompanyInfoID;
  221. BindMenDianList(this.comboBoxMdian,this.PrimitiveID);
  222. }
  223. LYFZ.Model.Model_ErpCompanyInfo GetCompanyInfo(string shopID)
  224. {
  225. if (_CompayList.Any(c => c.Company_DividedShop.ToLower() == shopID.ToLower()))
  226. {
  227. return _CompayList.Find(c => c.Company_DividedShop.ToLower() == shopID.ToLower());
  228. }
  229. else {
  230. return new Model.Model_ErpCompanyInfo();
  231. }
  232. }
  233. List<LYFZ.Model.Model_ErpCompanyInfo> _CompayList = new List<Model.Model_ErpCompanyInfo>();
  234. public List<LYFZ.Model.Model_ErpCompanyInfo> CompayList
  235. {
  236. get
  237. {
  238. if (_CompayList.Count <= 0)
  239. {
  240. _CompayList= new LYFZ.BLL.BLL_ErpCompanyInfo().GetModelList("1=1");
  241. }
  242. return _CompayList;
  243. }
  244. }
  245. public void BindMenDianList(ComponentLibrary.ComboBoxEx comBox,string dfShopID)
  246. {
  247. comBox.Items.Clear();
  248. foreach (LYFZ.Model.Model_ErpCompanyInfo com in CompayList)
  249. {
  250. ItemValue item = new ItemValue(com, com.Company_Name);
  251. comBox.Items.Add(item);
  252. if (dfShopID.Trim().Length > 0 && com.Company_DividedShop.ToLower() == dfShopID.ToLower())
  253. {
  254. comBox.SelectedItem = item;
  255. }
  256. else if (dfShopID.Trim().Length <= 0 && com.Company_DividedShop.ToLower() == LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.CurrentCompanyInfoID.ToLower())
  257. {
  258. comBox.SelectedItem = item;
  259. }
  260. }
  261. }
  262. }
  263. }