玄武短信接口和移动MAS短信接口的API封装

来源:互联网 发布:人工智能在智能交通 编辑:程序博客网 时间:2024/05/16 19:00
直接上代码,关键点:

133行的敏感词过滤

176行的6位扩展码写入

 

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Web;  5 using System.Web.UI;  6 using System.Web.UI.WebControls;  7 using CVTE.Utils;  8 using ImApiDotNet;  9 using CVTE.ServiceModel; 10 using System.Configuration; 11 using System.Threading; 12 using System.Text; 13  14 namespace CVTE.Portal 15 { 16     public partial class SmsSend : System.Web.UI.Page 17     { 18         private String[] retunvalues = new String[] { "成功", "连接数据库失败", "数据库关闭失败", "数据库插入错误", "数据库删除错误", "数据库查询错误", "参数错误", "API标识非法", "消息内容太长", "没有初始化或初始化失败", "API接口处于暂停(失效)状态", "短信网关未连接" }; 19         private String[] initvalues = new String[] { "成功", "连接失败", "用户名或密码错误", "密码错误", "接口编码不存在" }; 20         private ImApiDotNet.APIClient apiclient; 21  22         //是否联通号码,用于MAS过滤。在玄武短信中已不用考虑这个问题。 23         bool isLianTong(string mobile) 24         { 25             bool result = false; 26             if (mobile.StartsWith("186") || mobile.StartsWith("185") || mobile.StartsWith("156") || mobile.StartsWith("155") 27                 || mobile.StartsWith("131") || mobile.StartsWith("132") || mobile.StartsWith("130") || mobile.StartsWith("145")) 28                 result = true; 29  30             return result; 31         } 32  33         protected void Page_Load(object sender, EventArgs e) 34         { 35             if (!IsPostBack) 36             { 37                 try 38                 { 39                     if (Request["mobile"] == null) 40                         return; 41                     if (Request["context"] == null) 42                         return; 43                     if (Request["context"].Length < 1) 44                         return; 45                     if (Request["AppName"] == null) 46                         return; 47  48                     String context = Request["context"].Replace("&", "").Replace("+", "").Replace("#", ""); 49  50                     String mobileStr = Request["mobile"]; 51                     if (mobileStr == "") 52                     { 53                         return; 54                     } 55                     string smsProvider = ConfigurationManager.AppSettings["SmsProvider"]; 56  57                     if (Request["SmsProvider"] == null) 58                     { 59                         if (smsProvider.Equals("MAS")) 60                         { 61                             SendByMas(context, mobileStr); 62                         } 63  64                         if (smsProvider.Equals("MOS")) 65                         { 66                             //MOS的发送号码是以空格分隔的,和mas的半角逗号不同 67                             mobileStr = mobileStr.Replace(";", " ").Replace(",", " "); 68                             SendByMos(context, mobileStr); 69                         } 70                     } 71                     else if (Request["SmsProvider"] == "MOS") 72                     { 73                         mobileStr = mobileStr.Replace(";", " ").Replace(",", " "); 74                         SendByMos(context, mobileStr); 75                     } 76  77                 } 78                 catch (Exception ex) 79                 { 80                     //MessageBox.Show(this.Page, ex.Message); 81                 } 82             } 83  84         } 85  86         /// <summary> 87         /// 玄武短信发送 88         /// </summary> 89         /// <param name="context">短信内容</param> 90         /// <param name="mobileStr">以,分隔的手机号码</param> 91         private void SendByMos(String context, String mobileStr) 92         { 93             string AppName = ""; 94  95             if (Request["AppName"] == null) 96             { 97                 AppName = "CVTalk"; 98             } 99             else100             {101                 AppName = Request["AppName"];102             }103 104             string Phone6ID = "0";105             //发送内容106             string Context = "";107 108             PhoneDal phoneDal = new PhoneDal();109             try110             {111                 string fromPhone = "";112                 if (Request["from"] != null)113                 {114                     if (Request["from"].ToLower() != "sysmessage")115                     {116                         fromPhone = phoneDal.GetPhone(Request["from"]);117                     }118                 }119 120                 //MOS只能识别GB2312,所以要对utf-8进行UrlDecode转码121                 string IMMoshost = ConfigurationManager.AppSettings["MosURL"] + @"username=XXXXXX&password=XXXXXX&";122                 string contextUrlDecode = System.Web.HttpUtility.UrlDecode(context, System.Text.Encoding.UTF8);123 124                 //关键词库为空,则从数据库把词库导入内存125                 if (SensitiveWords.GetWords().Count == 0)126                 {127                     SensitiveWords.SetWords(SensitiveWordsDal.GetAllWords());128                 }129 130                 int keyMatchCount = 0;131                 string keys = "";132 133                 //开始关键词过滤,通过内存查询134                 foreach (string word in SensitiveWords.GetWords())135                 {136                     if (contextUrlDecode != null)137                     {138                         if (contextUrlDecode.Contains(word))139                         {140                             StringBuilder sb = new StringBuilder();141                             foreach (char ch in word)142                             {143                                 sb.Append(ch + "~");144                             }145                             keyMatchCount++;146                             keys += word + " ";147                             context = context.Replace(word, sb.ToString());148                         }149                     }150                 }151 152                 if (keyMatchCount > 0)153                 {154                     string alertMSG = "亲,您先前发送的短信包含运营商敏感字:“ " + keys + " ” 我们修改了您的短信内容后重新发送。修改后短信内容:" + context;155                     string IMhost = "http://xxxxxxxx.cn:8989/IMMessageAPI/im/toim.do?tousername=" + Request["from"] + "&message=" + alertMSG + "&subject=CVTalk短信异常";156                     WebHelper.GetWebData(IMhost);157                     contextUrlDecode = System.Web.HttpUtility.UrlDecode(context, System.Text.Encoding.UTF8);158 159                 }160                 string contextSuccess = System.Web.HttpUtility.UrlEncode(contextUrlDecode, System.Text.Encoding.GetEncoding("GB2312"));161 162                 if (Request["to"] != null)163                 {164                     if (Request["cnName"] != null && fromPhone != null)165                     {166                         Context = "CVTalk" + HttpUtility.UrlEncode(Request["cnName"], System.Text.Encoding.GetEncoding("GB2312")) + fromPhone + " :" + contextSuccess + ConfigurationSettings.AppSettings["smsAD"];167                     }168                     else169                         Context = contextSuccess;170                 }171                 else172                 {173                     Context = contextSuccess;174                 }175 176                 if (string.IsNullOrEmpty(fromPhone))177                 {178                     //fromPhone为空,则一定是系统发送的短信,不予处理回复,统一设置扩展码为0179                     Phone6ID = "0";180                 }181                 else//查询到用户的手机号182                 {183                     //获取用户的手机号后6位,这里是为了定位用户,有100万分之一的重复风险184                     Phone6ID = fromPhone.Substring(5);185                 }186 187                 string result = WebHelper.GetWebData(IMMoshost + "to=" + mobileStr + "&text=" + Context + "&subid=" + Phone6ID + "&msgtype=1");188                 //对第一次发送失败,进行第二次补发189                 if (result != "0")190                 {191                     Thread.Sleep(20);192                     //第二次发送193                     string result2 = WebHelper.GetWebData(IMMoshost + "to=" + mobileStr + "&text=" + Context + "&subid=" + Phone6ID + "&msgtype=1");194                     Response.Write(result2);195                     Response.End();196                 }197                 Response.Write(result);198                 Response.End();199             }200             catch (Exception ex)201             {202                 //Response.Write(ex.Message);203             }204         }205 206         /// <summary>207         /// 移动MAS短信发送208         /// </summary>209         /// <param name="context">短信内容</param>210         /// <param name="mobileStr">以,或;分隔的手机号码</param>211         /// <returns></returns>212         private string SendByMas(String context, String mobileStr)213         {214             string AppName = "";215 216             if (Request["AppName"] == null)217             {218                 AppName = "CVTalk";219             }220             else221             {222                 AppName = Request["AppName"];223             }224 225             apiclient = new APIClient();226 227             int con = apiclient.init("172.xx.xx.xx", "bpm", "bpm", "BPM", "mas");228             con = System.Math.Abs(con);229 230             mobileStr = mobileStr.Replace("", ",");231 232             int sm = 1;233             //if (Request["cnName"] != null)234             Random r = new Random();235             int smsID = r.Next(0, 999999);//产生0-999999之间的整数,用于无后六位号码的情况236             int Phone6ID = 0;237             //只支持发送一个手机号码238             string Context = "";239 240             PhoneDal phoneDal = new PhoneDal();241             string fromPhone = phoneDal.GetPhone(Request["from"]);242 243             if (Request["to"] != null)244             {245                 if (Request["cnName"] != null)246                     Context = "CVTalk" + Request["cnName"] + fromPhone + " :" + context + ConfigurationSettings.AppSettings["smsAD"];247                 else248                     Context = context;249 250             }251             else252             {253                 Context = context;254             }255 256             string[] mobiles = mobileStr.Split(',');257             if (string.IsNullOrEmpty(fromPhone) || mobiles.Length > 1)258             {259                 //sm = apiclient.sendSM(mobiles, Context, smsID);260                 for (int i = 0; i < mobiles.Length; i++)261                 {262                     //如果是联通号码263                     if (isLianTong(mobiles[i]))264                     {265                         sm = apiclient.sendSM(mobiles[i], Context + "回复TD退订", smsID);266                     }267                     else268                     {269                         sm = apiclient.sendSM(mobiles[i], Context, smsID);270                     }271 272                 }273             }274             else275             {276 277                 //sm = apiclient.sendSM(mobileStr.Split(','), Context, smsID);278                 //发送者号码的后6位279                 //smsID和srcID 没有办法分离,如果分置,将无法回复,因为将溯源至srcID,而不是smsID。280                 //smsID和srcID就必须设置为一样的!这样可以让发送者固定发送号码,但这样发送者给所有人的smsID都将将相同,281                 //造成的结果是,第一次发送可以成功,第一次回复是可以成功,但第三次交互(发送者再次回复),信息会回复错乱282                 //(这是因为在发送者给多人发送信息后,smsID是一样的,第三次回复将无法准确定位)283                 //目前采用后6位为ID的方法,因为最多只能拿6位,如果重复将异常。284 285                 Phone6ID = Convert.ToInt32(fromPhone.Substring(5));286                 sm = apiclient.sendSM(mobiles, Context, Phone6ID);287                 smsID = Phone6ID;288             }289 290             apiclient.release();291 292             CVTE.Model.IMSms model = new CVTE.Model.IMSms();293             model.AppName = AppName;294             model.FromUserName = Request["from"];295             model.ToUserName = Request["to"];296             model.ID = Guid.NewGuid().ToString();297 298             System.Text.Encoding utf8 = System.Text.Encoding.UTF8;299             byte[] key = Convert.FromBase64String("YzJjZGxmZ2hcamtsvW5vcHbycXXXXXXXXXX");300             byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };      //当模式为ECB时,IV无用301             byte[] data = utf8.GetBytes(context);302 303             byte[] byteEncode = DES3.Des3EncodeECB(key, iv, data);304             string strEncode = Convert.ToBase64String(byteEncode);305             //加密306             model.Msg = sm.ToString() + "-" + strEncode;307 308             model.SendTime = System.DateTime.Now;309             model.SmsID = smsID;// Convert.ToInt32(mobileStr.Split(',')[0].Substring(3)); srcID;// 310             model.ToMoblie = Request["mobile"];311             model.FromUserCNName = Request["cnName"];312 313             CVTE.BLL.IMSms bll = new CVTE.BLL.IMSms();314             bll.Add(model);315 316             Response.Write(retunvalues[sm]);317             Response.End();318             return mobileStr;319         }320     }321 }

 

0 0
原创粉丝点击