SMS(短讯服务中心)[二]-----webservice

来源:互联网 发布:248rr新域名 编辑:程序博客网 时间:2024/04/29 04:16

 上次说到用一个webservice来建立与外界系统的整合。主要是接收手机号码,群组,工号,windows帐号。 

 

结构如下图:

 

主要代码如下:

using System;using System.Collections;using System.Text.RegularExpressions;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Data;using System.Data.SqlClient;using System.Web.UI.WebControls;using System.Net.Mail;using System.Net;using System.IO;using System.Configuration;using AdGroup.DBObject;using AdGroup.SMS.Utility;[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]// 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。// [System.Web.Script.Services.ScriptService]public class Service : System.Web.Services.WebService{    public Service () {        //如果使用設計的元件,請取消註解下行程式碼         //InitializeComponent();     }    [WebMethod(Description = "簡訊發送到用戶ArrayList")]    public RetArray sendSMSArrayList(Mobile m)    {        Account acc= checkCompet(m.User1,m.Password);        string returnStr = acc.Message;        RetArray ret = new RetArray();        ret.phoneret = new RetArray.Phoneret[m.phone.Length];        if (m.phone.Length < 1)        {            ret.Type = "W";            ret.Message = "未添加接收人";            ret.Mid = "";            return ret;        }        else        {            for (int i = 0; i < m.phone.Length; i++)            {                ret.phoneret[i] = new RetArray.Phoneret();                ret.phoneret[i].Type = "";                ret.phoneret[i].Message = "";                ret.phoneret[i].UserTo = m.phone[i].UserTo;                ret.phoneret[i].UserToMobile = m.phone[i].UserToMobile;            }        }        if (m.ObjTime == "") m.ObjTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");        if (returnStr.Trim() != "")        {            //用戶或密碼不正確的情況              ret.Type = "E";            ret.Message = returnStr;            for (int i = 0; i < m.phone.Length; i++)            {                ret.phoneret[i].Type = "E";                ret.phoneret[i].Message = returnStr;                ret.phoneret[i].Did = "";            }        }        else        {            #region Type=A             if (m.Type.ToUpper() == "A")            {                returnStr = "";                string MID = getNewMID();                string DID;                string userTo;                if (m.phone.Length > 1) userTo = "手機多用戶";                else userTo = "手機單用戶";                addSMSLogMaster(MID, m.User1, userTo, "A", m.phone.Length.ToString(), m.SmsTitle, m.SmsContent, m.ObjTime, "");//新增LogMaster                ret.Mid = MID;                ret.Type = "S";                ret.Message = "發送成功";                if (m.SmsContent != "")//簡訊內容為空,只記Log                 {                    for (int i = 0; i < m.phone.Length; i++)                    {                      DID = getNewDID();                      if (!checkMobile(m.phone[i].UserToMobile.ToString()))                        {                          ret.Type = "W";                          ret.Message = "手機號碼不正確 ";                          ret.phoneret[i].Type = "W";                          ret.phoneret[i].Message = "手機號碼不正確";                          ret.phoneret[i].Did = DID;                          addSMSLogDetail(DID, MID, m.User1, m.phone[i].UserTo, m.phone[i].UserToMobile, m.ObjTime, "手機號碼不正確!");                          updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), m.phone[i].UserTo+"手機號碼不正確!只記錄Log不發送!");                          updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", "只記錄Log不發送!");                        }                        else                        {                            if (Convert.ToDateTime(m.ObjTime) <= DateTime.Now) //預約時間是否到了                            {                                returnStr = sendOutSMS(DID, MID, m.User1,m.phone[i].UserTo, m.phone[i].UserToMobile, m.ObjTime, m.SmsTitle, m.SmsContent,acc);                                if (returnStr.IndexOf("發送成功") > 0)                                {                                    ret.phoneret[i].Type = "S";                                    ret.phoneret[i].Message = "發送成功";                                    ret.phoneret[i].Did = DID;                                }                                else                                {                                    ret.Type = "E";                                    ret.Message = returnStr;                                    ret.phoneret[i].Type = "E";                                    ret.phoneret[i].Message = returnStr;                                    ret.phoneret[i].Did = DID;                                }                                                                }                            else                            {                                // ret.Type = "W";                                ret.Message = "預約成功 ";                                ret.phoneret[i].Message = "預約成功";                                ret.phoneret[i].Did = DID;                                addSMSLogDetail(DID, MID, m.User1, m.phone[i].UserTo, m.phone[i].UserToMobile, m.ObjTime, m.phone[i].UserTo + "預約成功");                                //updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), userTo1[i].ToString() + "預約成功");                                //updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", userTo1[i].ToString() + "預約成功");                            }                        }                                           }                }                else // 簡訊內容為空                {                    ret.Type = "W";                    ret.Message = "簡訊內容為空 ";                    for (int i = 0; i < m.phone.Length; i++)                    {                        DID = getNewDID();                        ret.phoneret[i].Type = "W";                        ret.phoneret[i].Message = "簡訊內容為空";                        ret.phoneret[i].Did = DID;                                           addSMSLogDetail(DID, MID, m.User1, m.phone[i].UserTo, m.phone[i].UserToMobile, m.ObjTime, "簡訊內容為空!");                        updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "簡訊內容為空!只記錄Log不發送!");                        updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", "只記錄Log不發送!");                     }                }            }            #endregion Type=A             #region Type=B             else if (m.Type.ToUpper() == "B")            {               // returnStr = sendSMSByPERNR(user1, userTo, userToMobile, objTime, smsTitle, smsContent);            }                #endregion             #region Type=C             else if (m.Type.ToUpper() == "C")            {                //returnStr = sendSMSByAD(user1, userTo, userToMobile, objTime, smsTitle, smsContent);            }                #endregion             #region Type=Other             else            {                returnStr = "請選擇正確的類型,A:手機號碼 B: 用戶工號 C:AD帳號";                ret.Type = "W";                ret.Message = returnStr;            }            #endregion         }        return ret;    }    [WebMethod(Description = "簡訊發送到群組ArrayList")]    public RetArrayGroup sendSMSGroupArrayList(Group g)    {        Account acc = checkCompet(g.User1, g.Password);        string returnStr = acc.Message;        RetArrayGroup ret = new RetArrayGroup();               if (g.ObjTime == "") g.ObjTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");        if (returnStr != "")        {            //用戶或密碼不正確的情況              ret.Type = "E";            ret.Message = returnStr;                    }        else        {            #region Type=A            //if (m.Type.ToUpper() == "A")            //{            //    returnStr = "";            string MID = getNewMID();            string DID;            string userTo;            string cond = " where Groupid='";            if (g.Groupid.Length < 1)            {                ret.Type = "E";                ret.Message = "未添加接收群組";                return ret;            }            else            {                for (int i = 0; i < g.Groupid.Length; i++)                {                    if (i == g.Groupid.Length - 1)                        cond += g.Groupid[i].Groupid + "' ";                    else                        cond += g.Groupid[i].Groupid + "' or Groupid='";                }            }            DataTable dt = getGroupUser(cond);            ret.groupret = new RetArrayGroup.Groupret[dt.Rows.Count];            if (g.Groupid.Length > 1) userTo = "群組多用戶";            else userTo = "群組單用戶";            addSMSLogMaster(MID, g.User1, userTo, "D", dt.Rows.Count.ToString(), g.SmsTitle, g.SmsContent, g.ObjTime, "");//新增LogMaster            ret.Mid = MID;            ret.Type = "S";            ret.Message = "發送成功";            if (dt.Rows.Count > 0)            {                for (int i = 0; i < dt.Rows.Count; i++)                {                    ret.groupret[i] = new RetArrayGroup.Groupret();                    ret.groupret[i].Did = "";                    ret.groupret[i].Groupid = dt.Rows[i]["GroupID"].ToString();                    ret.groupret[i].Message = "";                    ret.groupret[i].UserTo = dt.Rows[i]["UserID"].ToString();                    ret.groupret[i].Type = "";                    #region smsContent 判定                    if (g.SmsContent != "")//簡訊內容為空,只記Log                    {                        DID = getNewDID();                        if (!checkMobile(dt.Rows[i]["Mobile"].ToString()))                        {                            ret.Type = "W";                            ret.Message = "手機號碼不正確 ";                            ret.groupret[i].Type = "W";                            ret.groupret[i].Message = "手機號碼不正確";                            ret.groupret[i].Did = DID;                            addSMSLogDetail(DID, MID, g.User1, dt.Rows[i]["UserID"].ToString(), dt.Rows[i]["Mobile"].ToString(), g.ObjTime, "手機號碼不正確!");                            updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), dt.Rows[i]["UserID"].ToString() + "手機號碼不正確!只記錄Log不發送!");                            updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", "只記錄Log不發送!");                        }                        else                        {                            if (Convert.ToDateTime(g.ObjTime) <= DateTime.Now) //預約時間是否到了                            {                                returnStr = sendOutSMS(DID, MID, g.User1, dt.Rows[i]["UserID"].ToString(), dt.Rows[i]["Mobile"].ToString(), g.ObjTime, g.SmsTitle, g.SmsContent,acc);                                if (returnStr.IndexOf("發送成功") > 0)                                {                                    ret.groupret[i].Type = "S";                                    ret.groupret[i].Message = "發送成功";                                    ret.groupret[i].Did = DID;                                }                                else                                {                                    ret.Type = "E";                                    ret.Message = returnStr;                                    ret.groupret[i].Type = "E";                                    ret.groupret[i].Message = returnStr;                                    ret.groupret[i].Did = DID;                                }                            }                            else                            {                                // ret.Type = "W";                                ret.Message = "預約成功 ";                                ret.groupret[i].Message = "預約成功";                                ret.groupret[i].Did = DID;                                addSMSLogDetail(DID, MID, g.User1, dt.Rows[i]["UserID"].ToString(), dt.Rows[i]["Mobile"].ToString(), g.ObjTime, dt.Rows[i]["UserID"].ToString() + "預約成功");                            }                        }                    }                    else // 簡訊內容為空                    {                        ret.Type = "W";                        ret.Message = "簡訊內容為空 ";                                                DID = getNewDID();                        ret.groupret[i].Type = "W";                        ret.groupret[i].Message = "簡訊內容為空";                        ret.groupret[i].Did = DID;                        addSMSLogDetail(DID, MID, g.User1, dt.Rows[i]["UserID"].ToString(), dt.Rows[i]["Mobile"].ToString(), g.ObjTime, "簡訊內容為空!");                        updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "簡訊內容為空!只記錄Log不發送!");                        updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", "只記錄Log不發送!");                                            }                    #endregion                }//            }            else//user dt.Rows.Count <1            {                ret.Type = "W";                ret.Message = "該群組無人員 ";                return ret;            }            //}            #endregion Type=A            #region Type=B            //else if (m.Type.ToUpper() == "B")            //{            //    // returnStr = sendSMSByPERNR(user1, userTo, userToMobile, objTime, smsTitle, smsContent);            //}            //#endregion            //#region Type=C            //else if (m.Type.ToUpper() == "C")            //{            //    //returnStr = sendSMSByAD(user1, userTo, userToMobile, objTime, smsTitle, smsContent);            //}            //#endregion            //#region Type=Other            //else            //{            //    returnStr = "請選擇正確的類型,A:手機號碼 B: 用戶工號 C:AD帳號";            //    ret.Type = "W";            //    ret.Message = returnStr;            //}            #endregion        }        return ret;    }    [WebMethod(Description = "簡訊發送到用戶")]     public string sendSMS(string user1, string password,string type, string userTo,  string userToMobile, string objTime, string smsTitle, string smsContent)    {        Account acc = checkCompet(user1, password);        string returnStr = acc.Message;        if (objTime == "") objTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");        if (returnStr != "")        {            returnStr = "用戶或密碼不正確的情況";        }        else        {            if (type.ToUpper() == "A")            {                returnStr = sendSMSByMobile(user1, userTo, userToMobile, objTime, smsTitle, smsContent,acc);             }            else if (type.ToUpper() == "B")            {                returnStr = sendSMSByPERNR(user1, userTo, userToMobile, objTime, smsTitle, smsContent);            }            else if (type.ToUpper() == "C")            {                 returnStr = sendSMSByAD(user1, userTo, userToMobile, objTime, smsTitle, smsContent);             }            else            {                returnStr = "請選擇正確的類型,A:手機號碼 B: 用戶工號 C:AD帳號";            }        }        return returnStr;    }    [WebMethod(Description = "簡訊發送到群組")]    public string sendSMSGroup(string user1, string password, string userTo,  string objTime,  string smsTitle, string smsContent)    {        Account acc = checkCompet(user1, password);        string returnStr = acc.Message;        if (objTime == "") objTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");        string MID = getNewMID();        string DID;        if (returnStr != "")        {            returnStr = "用戶或密碼不正確的情況";        }        else        {            DataTable dt = DBUtility.GetDataTableBySql("select U.userid,U.Mobile from TB_UserData U,(select * from TB_GroupMapping where GroupID='" + userTo + "')G where U.UserID=G.UserID");            if (dt == null )            {                returnStr = "請先確定是否該群組";            }            else            {                if (dt.Rows.Count < 1)                {                    returnStr = "請先為該群組選定人員";                }                else                {                    addSMSLogMaster(MID, user1, userTo, "D", dt.Rows.Count.ToString(), smsTitle, smsContent, objTime, "");//新增LogMaster                    for (int i = 0; i < dt.Rows.Count; i++)                    {                        DID = getNewDID();                        if (smsContent == "")                        {                            returnStr = dt.Rows[i]["userid"].ToString() + "簡訊內容為空。";                            addSMSLogDetail(DID, MID, user1, dt.Rows[i]["userid"].ToString(), dt.Rows[i]["Mobile"].ToString(), objTime, "簡訊內容為空!");                            updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "簡訊內容為空!只記錄Log不發送!");                            updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", "只記錄Log不發送!");                        }                        else                        {                            if (checkMobile(dt.Rows[i]["Mobile"].ToString()))                            {                                if (Convert.ToDateTime(objTime) <= DateTime.Now) //預約時間是否到了                                    returnStr +=  sendOutSMS(DID, MID, user1, dt.Rows[i]["userid"].ToString(), dt.Rows[i]["Mobile"].ToString(), objTime, smsTitle, smsContent,acc);                                else                                {                                    returnStr += dt.Rows[i]["userid"].ToString() + "預約成功。";                                    addSMSLogDetail(DID, MID, user1, dt.Rows[i]["userid"].ToString(), dt.Rows[i]["Mobile"].ToString(), objTime, dt.Rows[i]["userid"].ToString() + "預約成功");                                }                            }                            else                            {                                returnStr += dt.Rows[i]["userid"].ToString() + "手機號碼不正確。";                                addSMSLogDetail(DID, MID, user1, dt.Rows[i]["userid"].ToString(), dt.Rows[i]["Mobile"].ToString(), objTime, "手機號碼不正確!");                                updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), dt.Rows[i]["userid"].ToString() + "手機號碼不正確!只記錄Log不發送!");                                updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", "只記錄Log不發送!");                            }                        }                    }// end dt for                 }// end else dt.Rows.Count<1            }//end else dt null        }        return returnStr;    }    [WebMethod(Description = "手機號碼簡訊發送到用戶")]    private string sendSMSByMobile(string user1, string userTo, string userToMobile, string objTime, string smsTitle, string smsContent,Account acc)    {        string returnStr = "";        string[] userTo1 = userTo.ToString().Split(',');        string[] userToMobile1 = userToMobile.ToString().Split(',');        if (userTo1.Length == 1) userTo1 = userTo.ToString().Split(',');        if (userToMobile1.Length == 1) userToMobile1 = userToMobile.ToString().Split(',');        string MID = getNewMID();        string DID;        if (userTo1.Length == userToMobile1.Length)        {            if (userTo1.Length > 1)                 userTo = "手機多用戶";            else                 userTo = "手機單用戶";            addSMSLogMaster(MID, user1, userTo, "A", userToMobile1.Length.ToString(),smsTitle,smsContent,objTime, "");//新增LogMaster            for (int i = 0; i < userTo1.Length; i++)            {                DID = getNewDID();                if (checkMobile(userToMobile1[i].ToString()))                {                    if (smsContent == "")//簡訊內容為空,只記Log                    {                        returnStr += userTo1[i].ToString() + "簡訊內容為空,";                        addSMSLogDetail(DID, MID, user1, userTo1[i].ToString(), userToMobile1[i].ToString(), objTime, "簡訊內容為空!");                        updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "簡訊內容為空!只記錄Log不發送!");                        updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", "只記錄Log不發送!");                    }                    else                    {                        if (Convert.ToDateTime(objTime) <= DateTime.Now) //預約時間是否到了                            returnStr += sendOutSMS(DID, MID, user1, userTo1[i].ToString(), userToMobile1[i].ToString(), objTime, smsTitle, smsContent,acc);                        else                        {                            returnStr += userTo1[i].ToString() + "預約成功。";                            addSMSLogDetail(DID, MID, user1, userTo1[i].ToString(), userToMobile1[i].ToString(), objTime, userTo1[i].ToString() + "預約成功");                        }                    }                }                else                {                    returnStr += userTo1[i].ToString() + "手機號碼不正確。";                    addSMSLogDetail(DID, MID, user1, userTo1[i].ToString(), userToMobile1[i].ToString(),  objTime,  "手機號碼不正確!");                    updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), userTo1[i].ToString()+"手機號碼不正確!只記錄Log不發送!");                    updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", "只記錄Log不發送!");                }            }                   }        else        {            returnStr = "接收人數和接收手機號碼數不一致";        }          return returnStr;    }    [WebMethod(Description = "工號簡訊發送到用戶")]    private string sendSMSByPERNR(string user1, string userTo, string userToMobile, string objTime, string smsTitle, string smsContent)    {        string returnStr = "";        //string[] userTo1 = userTo.ToString().Split(',');        //string[] userToMobile1 = userToMobile.ToString().Split(',');        //string MID = getNewDID();        //string DID;        //if (userTo1.Length == userToMobile1.Length)        //{        //    if (userTo1.Length > 1) userTo = "手機多用戶"; else userTo = "手機單用戶";        //    addSMSLogMaster(MID, user1, userTo, "A", "1", userToMobile1.Length.ToString(), "");//新增LogMaster        //    for (int i = 0; i < userTo1.Length; i++)        //    {        //        DID = getNewDID();        //        if (checkMobile(userToMobile1[i].ToString()))        //        {        //            if (smsContent == "")//簡訊內容為空,只記Log        //            {        //                returnStr += userTo1[i].ToString() + "簡訊內容為空,";        //                addSMSLogDetail(DID, MID, user1, userTo1[i].ToString(), userToMobile1[i].ToString(), smsTitle, smsContent, objTime,  "1", "0", "簡訊內容為空");        //            }        //            else        //            {        //                //if(objTime<DateTime.Now)  //預約時間是否到了        //                returnStr += sendOutSMS(DID, MID, user1, userTo1[i].ToString(), userToMobile[i].ToString(), objTime, smsTitle, smsContent);        //            }        //        }        //        else        //        {        //            returnStr += userTo1[i].ToString() + "手機號碼不正確,";        //            addSMSLogDetail(DID, MID, user1, userTo1[i].ToString(), userToMobile1[i].ToString(), smsTitle, smsContent, objTime, "1", "0", "手機號碼不正確");        //        }        //    }        //    if (returnStr == "")        //    {        //        updataMaster(MID, "全部發送成功");        //        returnStr = "發送成功";        //    }        //    else        //        updataMaster(MID, returnStr);        //}        //else        //{        //    returnStr = "接收人數和接收手機號碼數不一致";        //}        return returnStr;    }    [WebMethod(Description = "AD帳號簡訊發送到用戶")]    private string sendSMSByAD(string user1, string userTo, string userToMobile, string objTime, string smsTitle, string smsContent)    {        string returnStr = "";        //string[] userTo1 = userTo.ToString().Split(',');        //string[] userToMobile1 = userToMobile.ToString().Split(',');        //string MID = getNewDID();        //string DID;        //if (userTo1.Length == userToMobile1.Length)        //{        //    if (userTo1.Length > 1) userTo = "手機多用戶"; else userTo = "手機單用戶";        //    addSMSLogMaster(MID, user1, userTo, "A", "1", userToMobile1.Length.ToString(), "");//新增LogMaster        //    for (int i = 0; i < userTo1.Length; i++)        //    {        //        DID = getNewDID();        //        if (checkMobile(userToMobile1[i].ToString()))        //        {        //            if (smsContent == "")//簡訊內容為空,只記Log        //            {        //                returnStr += userTo1[i].ToString() + "簡訊內容為空,";        //                addSMSLogDetail(DID, MID, user1, userTo1[i].ToString(), userToMobile1[i].ToString(), smsTitle, smsContent, objTime,  "1", "0", "簡訊內容為空");        //            }        //            else        //            {        //                //if(objTime<DateTime.Now)  //預約時間是否到了        //                returnStr += sendOutSMS(DID, MID, user1, userTo1[i].ToString(), userToMobile[i].ToString(), objTime, smsTitle, smsContent);        //            }        //        }        //        else        //        {        //            returnStr += userTo1[i].ToString() + "手機號碼不正確,";        //            addSMSLogDetail(DID, MID, user1, userTo1[i].ToString(), userToMobile1[i].ToString(), smsTitle, smsContent, objTime, "1", "0", "手機號碼不正確");        //        }        //    }        //    if (returnStr == "")        //    {        //        updataMaster(MID, "全部發送成功");        //        returnStr = "發送成功";        //    }        //    else        //        updataMaster(MID, returnStr);        //}        //else        //{        //    returnStr = "接收人數和接收手機號碼數不一致";        //}        return returnStr;    }        [WebMethod(Description = "發送SMS")]    private string sendOutSMS(string DID,string MID,string user1, string userTo, string userToMobile, string objTime,  string smsTitle, string smsContent,Account acc)    {        string userName = acc.Userid;        string passWord = acc.Password;        string returnStr = "";   //狀態  1:成功  0:err     !:exception err        string sms_url;        string trans;                                  try                {//隐藏了具体实现代码  sorry
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();                    Stream resst = res.GetResponseStream();                    StreamReader sr = new StreamReader(resst);                    returnStr = userTo + "發送成功";                    sr.Close();                    resst.Close();                            }                catch (Exception e)                {                    returnStr = "錯誤:" + e.Message;                }                #region 從網站中讀取發送狀態來確定是否成功                              #endregion                if (returnStr == userTo + "發送成功")                {                    //if (gbdat != "Test")                    addSMSLogDetail(DID,MID,user1, userTo, userToMobile,objTime,"");                    updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), userTo+"發送成功");                    updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "1", userTo+"發送成功",acc.Id);                }                else                {                    addSMSLogDetail(DID, MID, user1, userTo, userToMobile, objTime,  "");                    updataSMSLogMaster(MID, "1", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), userTo+"發送失敗:" + returnStr);                    updataSMSLogDetail(DID, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "1", "0", userTo+"發送失敗:" + returnStr,acc.Id);                }                   return returnStr;    }    [WebMethod(Description = "Log記錄MARSTER")]    private void addSMSLogMaster(string MID, string user1, string userTo, string type, string count,string smsTitle,string smsContent,string objTime, string mark)    {        string modiTime = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");        string modiUser = "SMSService";                System.Text.StringBuilder sqlSB = new System.Text.StringBuilder(512);        sqlSB.Append(" insert into TB_LogMaster (MID,user1,userto,type,count,smsTitle,smsContent,objTime,mark,modi_user,modi_time)values('");        sqlSB.Append(MID + "','");        sqlSB.Append(user1 + "','");        sqlSB.Append(userTo + "','");        sqlSB.Append(type + "','");        sqlSB.Append(count + "','");        sqlSB.Append(smsTitle+"','");        sqlSB.Append(smsContent + "','");        sqlSB.Append(objTime+"','");        sqlSB.Append(mark + "','");        sqlSB.Append(modiUser + "','");        sqlSB.Append(modiTime + "')");                        DBUtility.ExecuteNonQuery(sqlSB.ToString());            }    [WebMethod(Description = "Log記錄DETAIL")]    private void addSMSLogDetail(string DID,string MID, string user1, string userTo, string userToMobile, string objTime, string mark)    {       string  modiTime = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");       string modiUser = "SMSService";       string accTime = modiTime;        System.Text.StringBuilder sqlSB = new System.Text.StringBuilder(512);        sqlSB.Append(" insert into TB_LogDetail (DID,MID,user1,userto,usertoMobile,objTime,mark,modi_user,modi_time)values('");        sqlSB.Append(DID + "','");        sqlSB.Append(MID + "','");        sqlSB.Append(user1 + "','");        sqlSB.Append(userTo + "','");        sqlSB.Append(userToMobile + "','");        sqlSB.Append(objTime+"','");               sqlSB.Append(mark + "','");        sqlSB.Append(modiUser + "','");        sqlSB.Append(modiTime + "')");        DBUtility.ExecuteNonQuery(sqlSB.ToString());           }    [WebMethod(Description = "Log記錄更新SMSLogMaster")]    private void updataSMSLogMaster(string MID,string states,string accTime, string mark)    {        DBUtility.ExecuteNonQuery("update TB_LogMaster set mark=mark+'" + mark + "', accTime='" + accTime + "', states='" + states + "' where MID='" + MID + "'");    }    [WebMethod(Description = "Log記錄更新SMSLogDetail")]    private void updataSMSLogDetail(string DID, string accTime, string states, string isok, string mark)    {        DBUtility.ExecuteNonQuery("update TB_LogDetail set mark=mark+'" + mark + "', isok='" + isok + "', accTime='" + accTime + "', states='" + states + "' where DID='" + DID + "'");    }    private void updataSMSLogDetail(string DID, string accTime,string states,string isok, string mark,string accid)    {        DBUtility.ExecuteNonQuery("update TB_LogDetail set mark=mark+'" + mark + "', isok='" + isok + "', AccId='" + accid + "', accTime='" + accTime + "', states='" + states + "' where DID='" + DID + "'");    }    [WebMethod(Description="取得Log記錄數MID")]    private string getNewMID()    {        DataTable dt;        string sqlCommand = "SELECT MAX(MID) MID FROM TB_LogMaster Where MID like'" + DateTime.Now.ToString("yyyyMMdd") + "%' ";        dt = DBUtility.GetDataTableBySql(sqlCommand);        if (dt.Rows[0]["MID"].ToString() != "")            return (Convert.ToInt64(dt.Rows[0]["MID"]) + 1).ToString();        else            return (DateTime.Now.ToString("yyyyMMdd") + "0001").ToString();    }    [WebMethod(Description = "取得Log記錄數DID")]    private string getNewDID()    {        DataTable dt;        string sqlCommand = "SELECT MAX(DID) DID FROM TB_LogDetail Where DID like'" + DateTime.Now.ToString("yyyyMMdd") + "%' ";        dt = DBUtility.GetDataTableBySql(sqlCommand);        if (dt.Rows[0]["DID"].ToString() != "")            return (Convert.ToInt64(dt.Rows[0]["DID"]) + 1).ToString();        else            return (DateTime.Now.ToString("yyyyMMdd") + "0001").ToString();    }    [WebMethod(Description = "Mail通知")]    private bool sendEMail(string strMail, string strSubject, string strbody, string strform, string cc)    {        try        {            MailMessage message = new MailMessage();            message.Body = strbody;            //寄件者            if (strform.Trim() == "")            {                MailAddress ma = new MailAddress("SMS@mail.adgroup.com.tw");                message.From = ma;                message.Subject = strSubject;            }            else            {                MailAddress ma = new MailAddress(strform);                message.From = ma;                message.Subject = strSubject;            }            if (cc != "")            {                message.CC.Add(cc);            }            //收件者            message.To.Add(strMail);            //mailserver            SmtpClient sc = new SmtpClient("mail.adgroup.com.tw");            sc.Send(message);            return true;        }        catch        {            return false;            throw;        }    }    [WebMethod(Description = "取得群組用戶的手機號碼")]    private DataTable getGroupUser(string cond)    {        DataTable dt;        string sqlStr = "select G.GroupID+':'+U.UserID as UserID, U.Mobile,G.GroupID from TB_Userdata U ,(select userid,groupid from TB_GroupMapping G " + cond + ")G where U.Userid=G.Userid";        dt = DBUtility.GetDataTableBySql(sqlStr);               return dt;    }    [WebMethod(Description = "用戶權限檢測")]    private Account checkCompet(string u,string pw)    {        DataTable dt;        Account returnAcc = new Account();        string sqlStr = "select TB_Account.* from TB_UserData inner join TB_Account on TB_UserData.canPst=TB_Account.id where TB_UserData.userid='" + u + "' and TB_UserData.UserPW='" + pw + "' ";        dt = DBUtility.GetDataTableBySql(sqlStr);        if (dt.Rows.Count > 0)        {            if (!string.IsNullOrEmpty(dt.Rows[0]["id"].ToString()))            {                if (dt.Rows[0]["id"].ToString() != "0")                {                    returnAcc.Id = dt.Rows[0]["id"].ToString();                    returnAcc.Userid = dt.Rows[0]["userid"].ToString();                    returnAcc.Password = dt.Rows[0]["password"].ToString();                    returnAcc.Message = "";                }                else                    returnAcc.Message = "帳號"+u+"沒有發送簡訊的權限";            }            else            {                returnAcc.Message = "帳號"+u+"沒有設置關聯的出口帳號";            }        }        else        {            returnAcc.Message = "帳號名為:" + u + "不存在";        }        return returnAcc;    }    [WebMethod(Description = "手機號碼檢測")]    private bool checkMobile(string s)    {        if (s == "" || s == null)        {            return false;        }        else        {            string reguCN = "^[1][3,5,8][0-9]{9}{1}quot;;            //string reguTW = "^[0][9][0-9]{8}{1}quot;;            if (Regex.Match(s, reguCN, RegexOptions.Compiled).Success)             {                 return true;             }             else             {                if (s.Length == 10)                {                    if (s.StartsWith("09"))                    {                        return true;                    }                    else                    {                        return false;                    }                }                else                {                    return false;                }            }        }    }}


 

原创粉丝点击