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

直接上代码,关键点:

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             else
100             {
101                 AppName = Request["AppName"];
102             }
103 
104             string Phone6ID = "0";
105             //发送内容
106             string Context = "";
107 
108             PhoneDal phoneDal = new PhoneDal();
109             try
110             {
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                     else
169                         Context = contextSuccess;
170                 }
171                 else
172                 {
173                     Context = contextSuccess;
174                 }
175 
176                 if (string.IsNullOrEmpty(fromPhone))
177                 {
178                     //fromPhone为空,则一定是系统发送的短信,不予处理回复,统一设置扩展码为0
179                     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             else
221             {
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                 else
248                     Context = context;
249 
250             }
251             else
252             {
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                     else
268                     {
269                         sm = apiclient.sendSM(mobiles[i], Context, smsID);
270                     }
271 
272                 }
273             }
274             else
275             {
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-data-picker 是 uni-app 中用于展示日期、时间、选择器的组件,它并没有直接调用接口的功能。通常情况下,你需要在组件的事件回调函数中调用接口获取数据,然后将数据传递给 uni-data-picker 进行展示。 例如,如果你需要在 uni-data-picker 中展示省市区三级联动的数据,可以按照以下步骤进行: 1. 编写一个获取省市区数据的接口,返回数据格式如下: ```json { "code": 0, "msg": "", "data": { "provinces": [ { "id": 1, "name": "浙江省", "cities": [ { "id": 11, "name": "杭州市", "areas": [ { "id": 111, "name": "西湖区" }, { "id": 112, "name": "余杭区" } ] }, { "id": 12, "name": "宁波市", "areas": [ { "id": 121, "name": "海曙区" }, { "id": 122, "name": "江东区" } ] } ] }, { "id": 2, "name": "江苏省", "cities": [ { "id": 21, "name": "南京市", "areas": [ { "id": 211, "name": "玄武区" }, { "id": 212, "name": "白下区" } ] }, { "id": 22, "name": "苏州市", "areas": [ { "id": 221, "name": "姑苏区" }, { "id": 222, "name": "相城区" } ] } ] } ] } } ``` 2. 在页面中使用 uni-data-picker 组件,并绑定选择器的事件回调函数,例如: ```html <template> <view> <uni-data-picker :columns="columns" @change="onChange" ></uni-data-picker> </view> </template> ``` ```javascript export default { data() { return { columns: [ { values: [], defaultIndex: 0 }, { values: [], defaultIndex: 0 }, { values: [], defaultIndex: 0 } ] }; }, mounted() { // 页面加载完成时,调用接口获取省市区数据,并设置到选择器中 this.getAreaData().then((data) => { const provinces = data.provinces; const cities = provinces[0].cities; const areas = cities[0].areas; this.columns[0].values = provinces.map((p) => p.name); this.columns[1].values = cities.map((c) => c.name); this.columns[2].values = areas.map((a) => a.name); }); }, methods: { getAreaData() { // 调用获取省市区数据的接口 return new Promise((resolve, reject) => { uni.request({ url: 'https://your-api-server.com/area', success(res) { if (res.statusCode === 200 && res.data.code === 0) { resolve(res.data.data); } else { reject(new Error(res.data.msg || '获取数据失败')); } }, fail(err) { reject(err); } }); }); }, onChange(e) { const { columnIndex, value } = e.detail; const provinces = this.columns[0].values; const cities = this.columns[1].values; const areas = this.columns[2].values; switch (columnIndex) { case 0: // 当选择省份时,更新城市和区域的数据 const province = provinces[value]; const provinceData = this.getProvinceData(province); const cityData = provinceData.cities; const areaData = cityData[0].areas; this.columns[1].values = cityData.map((c) => c.name); this.columns[2].values = areaData.map((a) => a.name); this.columns[0].defaultIndex = value; this.columns[1].defaultIndex = 0; this.columns[2].defaultIndex = 0; break; case 1: // 当选择城市时,更新区域的数据 const city = cities[value]; const cityData2 = this.getCityData(city); const areaData2 = cityData2.areas; this.columns[2].values = areaData2.map((a) => a.name); this.columns[1].defaultIndex = value; this.columns[2].defaultIndex = 0; break; case 2: // 当选择区域时,更新选中的值 this.columns[2].defaultIndex = value; break; default: break; } }, getProvinceData(name) { const data = this.areaData.provinces; return data.find((p) => p.name === name); }, getCityData(name) { const provinceName = this.columns[0].values[this.columns[0].defaultIndex]; const provinceData = this.getProvinceData(provinceName); const cities = provinceData.cities; return cities.find((c) => c.name === name); }, getAreaData() { // 调用获取省市区数据的接口 return new Promise((resolve, reject) => { uni.request({ url: 'https://your-api-server.com/area', success(res) { if (res.statusCode === 200 && res.data.code === 0) { this.areaData = res.data.data; resolve(this.areaData); } else { reject(new Error(res.data.msg || '获取数据失败')); } }, fail(err) { reject(err); } }); }); } } }; ``` 在上述代码中,我们在页面加载完成时调用了接口获取省市区数据,并将数据设置到选择器中。当用户选择省份、城市、区域时,我们根据用户的选择更新其他列的数据,并最终将用户选择的值保存下来。需要注意的是,我们使用 Promise 对象来异步获取接口数据,确保数据加载完成后再进行后续操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值