📄 sms_sendgroup.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DBLibrary;
using SMS_Lib;
using System.Text;
using System.Text.RegularExpressions;
public partial class SMS_SMS_SendGroup : SMS_PageBase
{
private DBLibrary.DBClass db = new DBClass();
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
SendGroup();
}
}
private bool IsRegEx(string regExValue, string itemValue)
{
try
{
Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
if (regex.IsMatch(itemValue))
{
return true;
}
else
{
return false;
}
}
catch (Exception)
{
return false;
}
finally
{
}
}
private void SendGroup()
{
string memo = "";
string flag = "0";
string nRet="";
if (Request["smemo"] != null && Request["smemo"].ToString().Trim()!="")
{
memo =Request["smemo"].ToString().Trim();
}
if(Request["stype"] !=null && Request["stype"].ToString().Trim() !="")
{
flag = Request["stype"].ToString().Trim();
}
if (memo.Trim().Length <= 0)
{
Response.Write(JSAlert("发送的内容不能为空!"));
return;
}
StringBuilder sb = new StringBuilder();
string filename = Up_Txt();
if (filename == null || filename == "")
{
Response.Write(JSAlert("上传失败,必须是TXT文件!"));
return;
}
System.IO.StreamReader sr = new System.IO.StreamReader(filename);
string receive = "";
int Fail = 0;
int Success = 0;
int nMobile = 0;
int nUNiom = 0;
int nCNC = 0;
while((receive = sr.ReadLine())!=null)
{
//号码验证
if (!NString.ChcekStr(receive, "0123456789"))
{
Fail++;
continue;
}
//正则验证,暂时关闭发送小灵通
//不符合号码格式过滤 移动
if (receive.ToString().Trim().StartsWith("1"))
{
if (IsRegEx("(^15[8|9]{1}[0-9]{8}$)|(^13[4-9]{1}[0-9]{8}$)", receive.ToString().Trim()))
{
nMobile++;
}
else
{
if (IsRegEx("(^153[0-9]{8}$)|(^13[0-3]{1}[0-9]{8}$)", receive.ToString().Trim()))
{
nUNiom++;
}
else
{
Fail++;
continue;
}
}
}
else if (receive.ToString().Trim().StartsWith("0")) //小灵通
{
if (IsRegEx("^0[0-9]{10,11}$", receive.ToString().Trim()))
{
nCNC++;
}
else
{
Fail++;
continue;
}
}
else
{
Fail++;
continue;
}
//成功号码累计
Success++;
sb.Append(receive);
sb.Append("|");
}
sr.Close();
try
{
if (sb.Length <= 0)
{
//删除无效上传文件
System.IO.File.Delete(filename);
Response.Write(JSAlert("发送失败,没有找到有效号码!"));
return;
}
}
catch
{
Response.Write(JSAlert("删除文件时,出现异常,稍后再试!"));
return;
}
//-写入历史记录------------------------------------------------------------------------------
int g_id = 0;
int total_send = 0;
if (memo.Length <= 70)
{
total_send = Success;
}
else
{
if (memo.Length % 70 == 0)
{
total_send = (memo.Length / 70) * Success;
}
else
{
total_send = (memo.Length / 70+1) * Success;
}
}
System.Data.SqlClient.SqlParameter[] pam =
{
db.MakeInputParameter("@sms_id",SqlDbType.BigInt,8,this.sms_id),
db.MakeInputParameter("@sms_memo",SqlDbType.NVarChar,1500,memo),
db.MakeInputParameter("@sms_count",SqlDbType.BigInt,8,total_send)
};
try
{
g_id = db.RunProcedureForInt("SMS_Web_AddGroupSMS", pam);
}
catch
{
Response.Write(JSAlert("出现异常,稍后再试!"));
return;
}
//-------------------------------------------------------------------------------------------
string sendmsg = "";
int sended = 0;
string []re_list =sb.ToString().Trim().Split('|');
for (int j = 0; j < re_list.Length; j++) //手机号数组
{
if(re_list[j].Trim().Length<=0)
{
continue;
}
for (int i = 0; i < memo.Length; i += 70) //内容拆分
{
if (memo.Length > 70)
{
if ((memo.Length - i) < 70)
{
sendmsg = memo.Substring(i, memo.Length - i);
}
else
{
sendmsg = memo.Substring(i, 70);
}
}
else
{
sendmsg = memo;
}
if (sendmsg.Trim().Length <= 0)
{
continue;
}
sended++;
//发送到网关
System.Data.SqlClient.SqlParameter[] para =
{
db.MakeInputParameter("@smsid",SqlDbType.BigInt,8,this.sms_id),
db.MakeInputParameter("@receiver",SqlDbType.NVarChar,50,re_list[j].Trim()),
db.MakeInputParameter("@flag",SqlDbType.Int,4,flag),
db.MakeInputParameter("@memo",SqlDbType.NVarChar,200,sendmsg.Trim()),
db.MakeInputParameter("@remark",SqlDbType.NVarChar,100,this.sms_id+"_1_"+g_id.ToString())
};
try
{
// int snRet = 0;
int snRet = db.RunProcedureForInt("SMS_Web_AddNewSMS", para);
if (snRet == 10)
{
nRet = "发送终止,余额不足。已处理:" + sended.ToString() + "条,已过滤无效号码:" + Fail.ToString() + "条!";
Response.Write(JSAlert(nRet));
return ;
}
}
catch
{
nRet = "发送错误,出现异常。已处理:" + sended.ToString() + "条,已过滤无效号码:" + Fail.ToString() + "条!";
Response.Write(JSAlert(nRet));
return ;
}
}
}
nRet = @"操作已成功。已处理有效总数:" + sended.ToString() + "条,其中\\r";
nRet += @"移动号码:" + nMobile.ToString() + "个.\\r";
nRet += @"联通号码:" + nUNiom.ToString()+ "个.\\r";
nRet += @"小灵通号码:" + nCNC.ToString() + "个.\\r";
nRet += @"提取无效号码:" + Fail.ToString() + "个";
//string putjs = "<script>alert('" + nRet + "');";
Response.Write(JSAlert(nRet,"SMS_SendGroup.aspx"));
}
private string Up_Txt()
{
HttpFileCollection files = HttpContext.Current.Request.Files;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
string[] file_name = new string[files.Count];
string NewPath_Name="";
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
HttpPostedFile postedFile = files[0];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
if (fileExtension != ".txt")
{
return null;
}
strMsg.Append("客户端文件地址" + postedFile.FileName + "<br>");
NewPath_Name = System.Web.HttpContext.Current.Request.MapPath("~/").ToString()+"UpFile/";
System.Threading.Thread.Sleep(10);
file_name[0] = DBLibrary.NString.GetStringFromTime(DateTime.Now.ToString()+DateTime.Now.Millisecond.ToString() + fileExtension);
NewPath_Name += file_name[0];
//Page.Server.MachineName.ToString();
postedFile.SaveAs(NewPath_Name);
}
}
catch (System.Exception ex)
{
return null;
}
return NewPath_Name;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -