FrmSendSms.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.SmsSend
  10. {
  11. public partial class FrmSendSms : LYFZ.Software.UI.SMSManagement.SmsSend.FrmSendSms
  12. {
  13. private string _Phone;
  14. /// <summary>
  15. /// 手机号
  16. /// </summary>
  17. public string Phone
  18. {
  19. get { return _Phone; }
  20. set { _Phone = value.Replace('/', ','); }
  21. }
  22. int SumCount = 0; //短信条数
  23. private static int _Works64 = 67; //67字数分割点
  24. /// <summary>
  25. /// 67字数分割点
  26. /// </summary>
  27. public static int Works64
  28. {
  29. get { return FrmSendSms._Works64; }
  30. set { FrmSendSms._Works64 = value; }
  31. }
  32. // int nTxetMsg = 70; //最后分割点
  33. public FrmSendSms()
  34. {
  35. this.Load += new EventHandler(FrmSendSms_Load);
  36. radN_M.Click += new EventHandler(radN_M_Click);
  37. this.radN.Click += new EventHandler(radN_Click);
  38. this.radM.Click += new EventHandler(radM_Click);
  39. this.btnContentSelection.Click += new EventHandler(btnContentSelection_Click);
  40. this.btnSendSMS.Click += new EventHandler(btnSendSMS_Click);
  41. this.richNr.TextChanged += new EventHandler(richNr_TextChanged);
  42. this.richTelPhone.MouseDoubleClick += richTelPhone_MouseDoubleClick;
  43. this.richTelPhone.TextChanged += richTelPhone_TextChanged;
  44. }
  45. void richTelPhone_TextChanged(object sender, EventArgs e)
  46. {
  47. richNr_TextChanged(sender, e);
  48. }
  49. void richTelPhone_MouseDoubleClick(object sender, MouseEventArgs e)
  50. {
  51. this.richTelPhone.ReadOnly = !this.richTelPhone.ReadOnly;
  52. }
  53. public void SetDefaultSmsText(string text)
  54. {
  55. this.richNr.Text = text.Trim();
  56. }
  57. #region 加载
  58. /// <summary>
  59. ///
  60. /// </summary>
  61. /// <param name="i">短信内容总字数</param>
  62. /// <param name="j">短信条数</param>
  63. /// <returns></returns>
  64. public static string GetlblNameTextString(int i, int j)
  65. {
  66. string Smsa_AccountName = "";
  67. if (frmSMSTemplateSet.CurrentSmsAccount != null) {
  68. Smsa_AccountName = frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName;
  69. i = i + frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName.Length;
  70. }
  71. return "短信内容(手机67字/条)(现" + i.ToString() + "字 加上签名:" + Smsa_AccountName + ",共" + j.ToString() + "条短信)";
  72. }
  73. protected void FrmSendSms_Load(object sender, EventArgs e)
  74. {
  75. try
  76. {
  77. lblName.Text = GetlblNameTextString(0, 0);
  78. richTelPhone.Text = Phone.TrimEnd(',');
  79. #region 检查手机合法性
  80. string phone = richTelPhone.Text;
  81. //筛选重复的数据
  82. string[] sj = phone.Split(',');
  83. List<string> strReturn = new List<string>();
  84. foreach (string name in sj)
  85. {
  86. if (strReturn.Contains(name) == false) strReturn.Add(name);
  87. }
  88. string[] Sx = strReturn.ToArray();
  89. //文本框获取新值
  90. string Lj = "";
  91. for (int i = 0; i < Sx.Length; i++)
  92. {
  93. Lj += Sx[i] + ",";
  94. }
  95. richTelPhone.Text = Lj.TrimEnd(',');
  96. #endregion
  97. int count = richTelPhone.Text.Length - richTelPhone.Text.Replace(",", "").Length;
  98. txtPhoneCount.Text = Convert.ToInt32(count + 1).ToString();
  99. }
  100. catch (Exception ex) { MessageBoxCustom.Show(ex.Message); }
  101. }
  102. #endregion
  103. #region 男+女
  104. protected void radN_M_Click(object sender, EventArgs e)
  105. {
  106. txtPhoneCount.Text = "";
  107. richTelPhone.Text = "";
  108. richTelPhone.Text = Phone.TrimEnd(',');
  109. int count = richTelPhone.Text.Length - richTelPhone.Text.Replace(",", "").Length;
  110. txtPhoneCount.Text = Convert.ToInt32(count + 1).ToString();
  111. }
  112. #endregion
  113. #region
  114. protected void radN_Click(object sender, EventArgs e)
  115. {
  116. if (!string.IsNullOrEmpty(Phone))
  117. {
  118. txtPhoneCount.Text = "";
  119. richTelPhone.Text = "";
  120. LYFZ.BLL.BLL_ErpCustomer bll = new BLL.BLL_ErpCustomer();
  121. string[] sz = Phone.TrimEnd(',').Split(',');
  122. for (int i = 0; i < sz.Length; i++)
  123. {
  124. DataTable dt = bll.View_tb_ErpCustomer("Cus_Telephone='" + sz[i] + "'").Tables[0];
  125. if (dt.Rows[0]["sex"].ToString() == "男")
  126. {
  127. richTelPhone.Text = sz[i];
  128. int count = richTelPhone.Text.Length - richTelPhone.Text.Replace(",", "").Length;
  129. txtPhoneCount.Text = Convert.ToInt32(count + 1).ToString();
  130. }
  131. }
  132. }
  133. }
  134. #endregion
  135. #region
  136. protected void radM_Click(object sender, EventArgs e)
  137. {
  138. if (!string.IsNullOrEmpty(Phone))
  139. {
  140. txtPhoneCount.Text = "";
  141. richTelPhone.Text = "";
  142. LYFZ.BLL.BLL_ErpCustomer bll = new BLL.BLL_ErpCustomer();
  143. string[] sz = Phone.TrimEnd(',').Split(',');
  144. for (int i = 0; i < sz.Length; i++)
  145. {
  146. DataTable dt = bll.View_tb_ErpCustomer("Cus_Telephone='" + sz[i] + "'").Tables[0];
  147. if (dt.Rows[0]["sex"].ToString() == "女")
  148. {
  149. richTelPhone.Text = sz[i];
  150. int count = richTelPhone.Text.Length - richTelPhone.Text.Replace(",", "").Length;
  151. txtPhoneCount.Text = Convert.ToInt32(count + 1).ToString();
  152. }
  153. }
  154. }
  155. }
  156. #endregion
  157. #region 选择
  158. protected void btnContentSelection_Click(object sender, EventArgs e)
  159. {
  160. frmSelectSmsInfo selectInfo = new frmSelectSmsInfo();
  161. if (selectInfo.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  162. {
  163. if (selectInfo.SmsModel.ID > 0)
  164. {
  165. this.richNr.Text = selectInfo.SmsModel.ST_SMSContent;
  166. SumCount = GetSmsSizeCount(selectInfo.SmsModel.ST_SMSContent, Works64);
  167. lblName.Text = GetlblNameTextString(selectInfo.SmsModel.ST_SMSContent.Length, SumCount * richTelPhone.Text.Trim().Split(',').Length);
  168. }
  169. }
  170. //LYFZ.Software.MainBusiness.SMSManagement.ShortSmsMessage.FrmSMSContent set = new ShortSmsMessage.FrmSMSContent();
  171. //set.ShowDialog();
  172. //string Nr = set.NR;
  173. //if (!string.IsNullOrEmpty(Nr))
  174. //{
  175. // richNr.Text = Nr;
  176. // #region 获取67字数
  177. // SumCount = GetSmsSizeCount(Nr.ToString(), Works64);
  178. // lblName.Text = GetlblNameTextString(Nr.Length, SumCount);// "短信内容(手机67字/条)(现" + Nr.Length + "字,共" + SumCount + "条短信)";
  179. // #endregion
  180. //}
  181. }
  182. /// <summary>
  183. /// 计算短信条数
  184. /// </summary>
  185. /// <param name="NR">短信内容</param>
  186. /// <param name="MSG_LENGTH">每条短信字数</param>
  187. /// <returns></returns>
  188. public static int GetSmsSizeCount(string NR, int MSG_LENGTH)
  189. {
  190. int nNumberOfTextMsg = 0;
  191. int nWCharLen = NR.Length;
  192. if (frmSMSTemplateSet.CurrentSmsAccount != null)
  193. {
  194. nWCharLen = NR.Length + frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName.Length;
  195. }
  196. if (MSG_LENGTH != 0)
  197. {
  198. // 魔数MSG_LENGTH = 67:
  199. // 当短信字符数超过70时,短信服务商会分为两部分;
  200. // 1.将前67个字符为一条短信;
  201. // 2.将67后的为另一部分,若仍大于70,再次执行1;
  202. // 3.若短信小于等于70,则为一条短信;
  203. nNumberOfTextMsg = nWCharLen / MSG_LENGTH;
  204. if (nWCharLen % MSG_LENGTH > 0)
  205. nNumberOfTextMsg++;
  206. }
  207. return nNumberOfTextMsg;
  208. }
  209. #endregion
  210. #region 内容输入事件
  211. protected void richNr_TextChanged(object sender, EventArgs e)
  212. {
  213. #region 获取64字数
  214. string Nr = richNr.Text;
  215. lblName.Text = GetlblNameTextString(Nr.Length, GetSmsSizeCount(Nr.ToString(), Works64) * richTelPhone.Text.Trim().Trim(',').Split(',').Length);//"短信内容(手机67字/条)(现" + Nr.Length + "字,共" + GetSmsSizeCount(Nr.ToString(), Works64) + "条短信)";
  216. this.txtPhoneCount.Text = richTelPhone.Text.Trim().Trim(',').Split(',').Length.ToString();
  217. #endregion
  218. }
  219. #endregion
  220. #region 发送
  221. protected void btnSendSMS_Click(object sender, EventArgs e)
  222. {
  223. AddSendSMS(richTelPhone.Text, richNr.Text);
  224. }
  225. /// <summary>
  226. /// 发送短信
  227. /// </summary>
  228. /// <param name="Phones"></param>
  229. /// <param name="smsContent"></param>
  230. public static void AddSendSMS(string Phones, string smsContent)
  231. {
  232. try
  233. {
  234. #region 判断不能为空
  235. if (string.IsNullOrEmpty(Phones))
  236. {
  237. MessageBoxCustom.Show("手机号码不能为空!");
  238. return;
  239. }
  240. if (string.IsNullOrEmpty(smsContent))
  241. {
  242. MessageBoxCustom.Show("短信内容不能为空!");
  243. return;
  244. }
  245. #endregion
  246. #region 检查手机合法性
  247. string phone = Phones.Trim().Trim(',').Replace(",,", ",");
  248. string ret = LYFZ.WinAPI.CustomPublicMethod.ValidationPhones(phone);
  249. if (ret.Trim().Length > 0)
  250. {
  251. MessageBoxCustom.Show("手机号无效:" + ret);
  252. return;
  253. }
  254. //筛选重复的数据
  255. string[] sj = phone.Split(',');
  256. StringBuilder phoneString = new StringBuilder();
  257. foreach (string name in sj)
  258. {
  259. if (!phoneString.ToString().Contains(name))
  260. phoneString.Append(name + ",");
  261. }
  262. phone = phoneString.ToString().Trim(',');
  263. #endregion
  264. #region 发送
  265. LYFZ.BLL.BLL_SMSTemplates smsTemplates = new BLL.BLL_SMSTemplates();
  266. if (phone.Trim().Length > 0)
  267. {
  268. if (phone.Trim().Length >= 60 && (!smsContent.Contains("回TD退订") && !smsContent.Contains("退订回TD") && !smsContent.Contains("退订回N") && !smsContent.Contains("回N退订")))
  269. {
  270. string tempSms = smsContent + "回TD退订";
  271. int nWCharLen =0;//签名长度
  272. int tempcount = tempSms.Length;
  273. int tempSumCount = 0;
  274. if (frmSMSTemplateSet.CurrentSmsAccount != null)
  275. {
  276. nWCharLen =frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName.Length;
  277. if (!smsContent.StartsWith(frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName) && !smsContent.EndsWith(frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName))
  278. {
  279. tempcount = tempcount + nWCharLen;
  280. tempSms = tempSms + frmSMSTemplateSet.CurrentSmsAccount.Smsa_AccountName;
  281. }
  282. }
  283. tempSumCount = tempcount / Works64;
  284. if (tempcount % Works64 > 0)
  285. tempSumCount++;
  286. if (MessageBoxCustom.Show("系统检查到您发送的短信为“群发(营销类)短信”,且未加入“回TD退订”字样,系统将自动加上该字样,此时实际短信字数为 " + tempcount + " 个字, " + (tempSumCount* phone.Split(',').Length).ToString() + " 条,是否确定发送此短信?", msgBoxButton: MessageBoxButtons.YesNo) == DialogResult.Yes)
  287. {
  288. smsContent = smsContent + "回TD退订";
  289. }
  290. else {
  291. return;
  292. }
  293. }
  294. string retMsg = smsTemplates.GenerateManuallySendSMS(phone, smsContent.Trim(), SDateTime.Now, LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.LoginUserModel.User_Name
  295. , LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.GetCompanyInfoID());
  296. MessageBoxCustom.Show(retMsg);
  297. }
  298. else
  299. {
  300. MessageBoxCustom.Show("没有找到有效的手机号");
  301. }
  302. #endregion
  303. }
  304. catch (Exception ex) { MessageBoxCustom.Show(ex.Message); }
  305. }
  306. #region 验证手机
  307. private bool CheckPhoneNumber(string number)
  308. {
  309. return System.Text.RegularExpressions.Regex.IsMatch(number, @"^(1[3,5,8][0-9])\d{8}$");
  310. }
  311. #endregion
  312. #endregion
  313. }
  314. }