4、ABPZero系列教程之拼多多卖家工具 集成短信发送模块

4、ABPZero系列教程之拼多多卖家工具 集成短信发送模块

  ABPZero并不曾手机短信发送功能,现在我们来拼一个,为后面注册、登录作铺垫。

阿里云短信服务

率先需以阿里云开展短信服务,总是地址

开展后,在签约管理面临补充加一个签名

澳门新葡亰官网 1

于模板管理遭补充加一个模板,如下图所示

澳门新葡亰官网 2

末了得采用阿里云提供的.NET发送短信类库,下面可以直接生充斥自上传的类库,也可去官方下载,然后取本人所说之2个类库(aliyun-net-sdk-core.dll、aliyun-net-sdk-dysmsapi.dll)

直接下载文件:https://files.cnblogs.com/files/shensigzs/aliyun-net-sdk.zip (里面含有2独类库,把当时2个类库引用到Core项目)

.NET发送短信SDK:https://help.aliyun.com/document\_detail/59836.html?spm=5176.doc55284.6.573.GaNxg6

安装类库

一般来说图所示

采用的模块是Abp.Net.Sms,源码地址:https://github.com/berkaroad/Abp.Net.Sms

澳门新葡亰官网 3

 

Core项目增长实现

添加AliDayuSmsSender类,实现2单主意(一个合办、一个异步)

文本路径:D:\abp
version\aspnet-zero-3.4.0\aspnet-zero-3.4.0\src\MyCompanyName.AbpZeroTemplate.Core\Authorization\Users\AliDayuSmsSender.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Dependency;
using Abp.Net.Sms;
using Abp.UI;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using Castle.Core.Logging;

namespace MyCompanyName.AbpZeroTemplate.Authorization.Users
{
    public class AliDayuSmsSender : SmsSenderBase, ITransientDependency
    {
        private IClientProfile profile = null;
        public ILogger Logger { get; set; }
        public AliDayuSmsSender(ISmsSenderConfiguration configuration) : base(configuration)
        {
            Logger = NullLogger.Instance;
            profile = DefaultProfile.GetProfile("cn-hangzhou", configuration.GetAppKey(), configuration.GetAppSecret());
        }

        protected override void SendSms(SmsMessage sms)
        {

            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            try
            {
                //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                request.PhoneNumbers = sms.To;
                //必填:短信签名-可在短信控制台中找到
                request.SignName = sms.FreeSignName;
                //必填:短信模板-可在短信控制台中找到
                request.TemplateCode = string.IsNullOrEmpty(sms.TemplateCode)
                    ? _configuration.GetDefaultSmsTemplateCode()
                    : sms.TemplateCode;
                //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                request.TemplateParam = sms.TemplateParams;
                //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                //request.OutId = "yourOutId";
                //请求失败这里会抛ClientException异常
                SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
                Logger.Info("发送返回:" + sendSmsResponse.Message);
            }
            catch (ServerException e)
            {
                throw new UserFriendlyException("短信发送失败",
                    new Exception(string.Format("to:{0},errCode:{1},errMsg:{2}",
                        sms.To,
                        e.ErrorCode,
                        e.Message)));
            }
            catch (ClientException e)
            {
                throw new UserFriendlyException("短信发送失败",
                    new Exception(string.Format("to:{0},errCode:{1},errMsg:{2}",
                        sms.To,
                        e.ErrorCode,
                        e.Message)));
            }

        }

        protected override Task SendSmsAsync(SmsMessage sms)
        {
            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
            var task = new Task(() =>
            {
                IAcsClient acsClient = new DefaultAcsClient(profile);
                SendSmsRequest request = new SendSmsRequest();
                try
                {
                    //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                    request.PhoneNumbers = sms.To;
                    //必填:短信签名-可在短信控制台中找到
                    request.SignName = sms.FreeSignName;
                    //必填:短信模板-可在短信控制台中找到
                    request.TemplateCode = string.IsNullOrEmpty(sms.TemplateCode)
                        ? _configuration.GetDefaultSmsTemplateCode()
                        : sms.TemplateCode;
                    //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                    request.TemplateParam = sms.TemplateParams;
                    //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                    //request.OutId = "yourOutId";
                    //请求失败这里会抛ClientException异常
                    SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
                    Logger.Info("发送返回:" + sendSmsResponse.Message);
                }
                catch (ServerException e)
                {
                    throw new UserFriendlyException("短信发送失败",
                        new Exception(string.Format("to:{0},errCode:{1},errMsg:{2}",
                            sms.To,
                            e.ErrorCode,
                            e.Message)));
                }
                catch (ClientException e)
                {
                    throw new UserFriendlyException("短信发送失败",
                        new Exception(string.Format("to:{0},errCode:{1},errMsg:{2}",
                            sms.To,
                            e.ErrorCode,
                            e.Message)));
                }
            });


            task.Start();
            return task;
        }
    }
}

 

 

修改AppSettingProvider.cs,在return new[]里长如下代码

文本路径:D:\abpweb\PddSellerAssistant\PddSellerAssistant.Core\Configuration\AppSettingProvider.cs

return new[]
                   {
                        // Sms config
                        new SettingDefinition(SmsSettingNames.ServiceUrl,
                            ConfigurationManager.AppSettings[SmsSettingNames.ServiceUrl] ?? ""),
                        new SettingDefinition(SmsSettingNames.AppKey,
                            ConfigurationManager.AppSettings[SmsSettingNames.AppKey] ?? ""),
                        new SettingDefinition(SmsSettingNames.AppSecret,
                            ConfigurationManager.AppSettings[SmsSettingNames.AppSecret] ?? ""),
                        new SettingDefinition(SmsSettingNames.DefaultFreeSignName,
                            ConfigurationManager.AppSettings[SmsSettingNames.DefaultFreeSignName] ?? ""),
                        new SettingDefinition(SmsSettingNames.DefaultSmsTemplateCode,
                            ConfigurationManager.AppSettings[SmsSettingNames.DefaultSmsTemplateCode] ?? ""),
                       //Host settings

 

 修改web.config,添加如下配置节点

 <!-- 短信api配置开始-->
    <add key="Abp.Net.Sms.AppKey" value="" />
    <add key="Abp.Net.Sms.AppSecret" value="" />
    <add key="Abp.Net.Sms.DefaultFreeSignName" value="填写签名名称" />
    <add key="Abp.Net.Sms.DefaultSmsTemplateCode" value="填写模板ID" />
    <add key="Abp.Net.Sms.ServiceUrl" value="" />
    <!-- 短信api配置结束-->

 

 AppKey、AppSecret都好当阿里云后台获取

 

 修改AbpZeroTemplateCoreModule,代码修改如下

 文件路径:D:\abp
version\aspnet-zero-3.4.0\aspnet-zero-3.4.0\src\MyCompanyName.AbpZeroTemplate.Core\AbpZeroTemplateCoreModule.cs

 

if (DebugHelper.IsDebug)
            {
                //调试模式禁用邮件发送
                //IocManager.Register<IEmailSender, NullEmailSender>(DependencyLifeStyle.Transient);
                //调试模式禁用手机短信发送
                //IocManager.Register<ISmsSender, NullSmsSender>(DependencyLifeStyle.Transient);
            }

 

 可以见到我还统统注掉,因为自不怕使以调试模式下测试邮件、短信是否能发送。

 

[DependsOn(typeof(AbpZeroCoreModule),
       typeof(AbpZeroLdapModule),
       typeof(AbpAutoMapperModule),
       typeof(AbpNetSmsModule)
       )]

 

 此处是长短信模块依赖

Application项目

增长文件ISmsMessageService.cs

 文件路径:D:\abp
version\aspnet-zero-3.4.0\aspnet-zero-3.4.0\src\MyCompanyName.AbpZeroTemplate.Application\Authorization\Users\ISmsMessageService.cs

 

public interface ISmsMessageService : IApplicationService
    {
        void Send(string to, string templateCode, string templateParams);
        Task SendAsync(string to, string templateCode, string templateParams);
    }

 

 

再次补充加实现文件SmsMessageService.cs

文件路径:D:\abp
version\aspnet-zero-3.4.0\aspnet-zero-3.4.0\src\MyCompanyName.AbpZeroTemplate.Application\Authorization\Users\SmsMessageService.cs

public class SmsMessageService: ISmsMessageService
    {
        private readonly ISmsSender _smsSender;

        public SmsMessageService(ISmsSender smsSender)
        {
            _smsSender = smsSender;
        }

        public void Send(string to, string templateCode, string templateParams)
        {
            _smsSender.Send(to, templateCode, templateParams);
        }

        public async Task SendAsync(string to, string templateCode, string templateParams)
        {
            await _smsSender.SendAsync(to, templateCode, templateParams);
        }
    }

 

 测试短信发送

末,生成项目

浏览器打开:http://localhost:8088/swagger/ui/index(8088是IIS配置的端口)

找到app_smsMessageService服务进行测试

澳门新葡亰官网 4

 

澳门新葡亰官网 5

 

联合、异常且测试通过,至此手机短信发送模块并完成。

 

返回总目录

 

admin

网站地图xml地图