当前位置:健康减肥瘦身 > 瘦身减肥 > ABPZero体系教程之拼多多商家工具

ABPZero体系教程之拼多多商家工具

文章作者:瘦身减肥 上传时间:2019-12-02

  ABPZero并从未手提式有线电话机短信发送功效,现在我们来集成三个,为前面注册、登入作铺垫。

Ali云短信服务

第豆蔻梢头必要在Ali云开通短信服务,三番五次地址

开展后,在签定管理中加多三个签订

图片 1

在模板管理中加多多个模板,如下图所示

图片 2

末段索要选拔Ali云提供的.NET发送短信类库,上边可以向来下载作者上传的类库,也得以去官方下载,然后提取本身所说的2个类库(aliyun-net-sdk-core.dll、aliyun-net-sdk-dysmsapi.dll)

直白下载文件: (里面饱含2个类库,把那2个类库援引到Core项目)

.NET发送短信SDK:

设置类库

正如图所示

接受的模块是Abp.Net.Sms,源码地址:

图片 3

 

Core项目拉长实现

增多AliDayuSmsSender类,实现2个点子(三个同台、二个异步)

文本路线:D:abp versionaspnet-zero-3.4.0aspnet-zero-3.4.0srcMyCompanyName.AbpZeroTemplate.CoreAuthorizationUsersAliDayuSmsSender.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:abpwebPddSellerAssistantPddSellerAssistant.CoreConfigurationAppSettingProvider.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都能够在Ali云后台获取

 

 修正AbpZeroTemplateCoreModule,代码校正如下

 文件路线:D:abp versionaspnet-zero-3.4.0aspnet-zero-3.4.0srcMyCompanyName.AbpZeroTemplate.CoreAbpZeroTemplateCoreModule.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 versionaspnet-zero-3.4.0aspnet-zero-3.4.0srcMyCompanyName.AbpZeroTemplate.ApplicationAuthorizationUsersISmsMessageService.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 versionaspnet-zero-3.4.0aspnet-zero-3.4.0srcMyCompanyName.AbpZeroTemplate.ApplicationAuthorizationUsersSmsMessageService.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);
        }
    }

 

 测量检验短信发送

末尾,生成项目

浏览器打开:

找到app_smsMessageService服务进行测量检验

图片 4

 

图片 5

 

同步、格外都测量检验通过,至此手提式有线电电话机短信发送模块集成完毕。

 

归来总目录

 

本文由健康减肥瘦身发布于瘦身减肥,转载请注明出处:ABPZero体系教程之拼多多商家工具

关键词: