- 關於短消息:
先研究Nopcommerce自帶的短消息配置/短信管理,首先,這個是插件模式的使用:
項目結構很典型,MVC,帶有View,就是有配置界面的,然後RouteProvider.cs:
- routes.MapRoute("Plugin.SMS.Clickatell.Configure",
- "Plugins/SMSClickatell/Configure",
- new { controller = "SmsClickatell", action = "Configure" },
- new[] { "Nop.Plugin.SMS.Clickatell.Controllers" }
再有就是插件暴露什麼信息:
Group: SMS providers FriendlyName: Clickatell SMS Provider SystemName: Mobile.SMS.Clickatell Version: 1.00 SupportedVersions: 2.30 Author: nopCommerce team DisplayOrder: 1 FileName: Nop.Plugin.SMS.Clickatell.dll
SMScontroller 通過Plugin過濾SMS plugin然後通過Grid View
http://localhost:3861/Admin/SMS/Providers
- public ActionResult Providers() { if (!_permissionService.Authorize(StandardPermissionProvider.ManageSmsProviders)) return AccessDeniedView(); var smsProvidersModel = new List<SmsProviderModel>(); var smsProviders = _smsService.LoadAllSmsProviders(); foreach (var smsProvider in smsProviders) { var tmp1 = smsProvider.ToModel(); tmp1.IsActive = smsProvider.IsSmsProviderActive(_smsSettings); smsProvidersModel.Add(tmp1); } var gridModel = new GridModel<SmsProviderModel> { Data = smsProvidersModel, Total = smsProvidersModel.Count() }; return View(gridModel); }
- // .ClientTemplate("<a href=\"ConfigureProvider?systemName=<#= SystemName #>\">" + T("Admin.Configuration.SMSProviders.Configure").Text + "</a>")
通過超鏈接,選擇編輯,在URL看到的還是ConfigureProvider?systemName=Mobile.SMS.Clickatell
- public ActionResult ConfigureProvider(string systemName)
- {
- if (!_permissionService.Authorize(StandardPermissionProvider.ManageSmsProviders))
- return AccessDeniedView();
- var smsProvider = _smsService.LoadSmsProviderBySystemName(systemName);
- if (smsProvider == null)
- throw new ArgumentException("No SMS provider found with the specified system name", "systemName");
- var model = smsProvider.ToModel();
- string actionName, controllerName;
- RouteValueDictionary routeValues;
- smsProvider.GetConfigurationRoute(out actionName, out controllerName, out routeValues);
- model.ConfigurationActionName = actionName;
- model.ConfigurationControllerName = controllerName;
- model.ConfigurationRouteValues = routeValues;
- return View(model);
- }
通過ConfigureProvider的View中加載了Plugin的View(可以參考文件Presentation\Nop.Web\Administration\Views\Sms\ConfigureProvider.cshtml):
@if (!String.IsNullOrEmpty(Model.ConfigurationActionName)) { @Html.Action(Model.ConfigurationActionName, Model.ConfigurationControllerName, Model.ConfigurationRouteValues); }
如果需要增加自己的SMS 組件,可參考同樣的流程:
====
發送消息的流程在這插件中控制:
1.添加某個Web Service引用,因爲網站通過該Service來發送消息,最後結算也是通過消息數目結算的
2.在XXXProvider中完成SMSsend
- public bool SendSms(string text)
- {
- try
- {
- using (var svc = new PushServerWSPortTypeClient(new BasicHttpBinding(), new EndpointAddress("http://api.clickatell.com/soap/webservice_vs.php")))
- {
- string authRsp = svc.auth(Int32.Parse(_clickatellSettings.ApiId), _clickatellSettings.Username, _clickatellSettings.Password);
- if (!authRsp.ToUpperInvariant().StartsWith("OK"))
- {
- throw new NopException(authRsp);
- }
- //認證通過
- string ssid = authRsp.Substring(4);//ssid作爲參數一部分
- string[] sndRsp = svc.sendmsg(ssid,
- Int32.Parse(_clickatellSettings.ApiId), _clickatellSettings.Username,
- _clickatellSettings.Password, new string[1] { _clickatellSettings.PhoneNumber }, //發送接收的電話號碼,如果要羣發要改寫一個方法
- String.Empty, text, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- String.Empty, 0, String.Empty, String.Empty, String.Empty, 0);
- if (!sndRsp[0].ToUpperInvariant().StartsWith("ID"))
- {
- throw new NopException(sndRsp[0]);
- }
- return true;
- }
- } public bool SendSmses(string text, string[] PhoneNumbers)
- {
- foreach (string PhNo in PhoneNumbers)
- {
- SendSms(PhNo);
- }
- return true;
- }