点击上方蓝字"小黑在哪里"关注我吧
定义特征
应用特征
用户数量
社交登录
前言
上一篇提到了ABP功能管理(特征管理),它来自ABP的FeatureManagement模块,ABP官方文档貌似还没有这个模块的相关说明,但是个人感觉这个模块非常实用,下面就简单介绍一个特征管理的基本应用。
开始
在租户管理中,有一个“管理功能”按钮,默认是没有数据的,界面上也没有地方维护。
特征管理简单来说就是在同一套系统中为不同的租户提供一些差异化的功能。比如免费用户,提供的是基础功能,VIP用户则会多一些高级功能。
定义特征
在Application.Contracts项目中添加Features文件夹。
src\Xhznl.HelloAbp.Application.Contracts\Features\HelloAbpFeatures.cs:
publicclassHelloAbpFeatures{publicconststringGroupName="HelloAbp";publicconststringSocialLogins=GroupName+".SocialLogins";publicconststringUserCount=GroupName+".UserCount";}
src\Xhznl.HelloAbp.Application.Contracts\Features\HelloAbpFeatureDefinitionProvider.cs:
publicclassHelloAbpFeatureDefinitionProvider:FeatureDefinitionProvider{publicoverridevoidDefine(IFeatureDefinitionContextcontext){vargroup=context.AddGroup(HelloAbpFeatures.GroupName);group.AddFeature(HelloAbpFeatures.SocialLogins,"true",L("Feature:SocialLogins"),valueType:newToggleStringValueType());group.AddFeature(HelloAbpFeatures.UserCount,"10",L("Feature:UserCount"),valueType:newFreeTextStringValueType(newNumericValueValidator(1,1000)));}privatestaticLocalizableStringL(stringname){returnLocalizableString.Create<HelloAbpResource>(name);}}
以上代码添加了2个特征:SocialLogins,UserCount。
SocialLogins(社交登录),valueType为ToggleStringValueType,意味着它是个勾选框,默认值为"true"。
UserCount(用户数量),valueType为FreeTextStringValueType,意味着它是个输入框,默认值为"10"。
现在可以为不同租户设置不同的特征值。
应用特征
特征值定义好了,接下来就是如何应用了,首先看一下用户数量如何控制。
用户数量
目前用户是通过/identity/users
接口来添加的,那么我们重写这个接口对应的服务方法就好了。关于重写服务可以参考:重写服务[1]
对应的ABP源码在:abp\modules\identity\src\Volo.Abp.Identity.Application\Volo\Abp\Identity\IdentityUserAppService.cs中。
在我们的Application项目中添加一个服务类继承IdentityUserAppService
,重写CreateAsync
方法,使用FeatureChecker
获取到特征值,然后做个用户数量校验即可。
src\Xhznl.HelloAbp.Application\Identity\HelloIdentityUserAppService.cs:
[RemoteService(IsEnabled=false)][Dependency(ReplaceServices=true)][ExposeServices(typeof(IIdentityUserAppService),typeof(IdentityUserAppService))]publicclassHelloIdentityUserAppService:IdentityUserAppService,IHelloIdentityUserAppService{privatereadonlyIStringLocalizer<HelloAbpResource>_localizer;publicHelloIdentityUserAppService(IdentityUserManageruserManager,IIdentityUserRepositoryuserRepository,IIdentityRoleRepositoryroleRepository,IStringLocalizer<HelloAbpResource>localizer):base(userManager,userRepository,roleRepository){_localizer=localizer;}publicoverrideasyncTask<IdentityUserDto>CreateAsync(IdentityUserCreateDtoinput){varuserCount=(awaitFeatureChecker.GetOrNullAsync(HelloAbpFeatures.UserCount)).To<int>();varcurrentUserCount=awaitUserRepository.GetCountAsync();if(currentUserCount>=userCount){thrownewUserFriendlyException(_localizer["Feature:UserCount.Maximum",userCount]);}returnawaitbase.CreateAsync(input);}}
下面可以将某租户的用户数量设置一下,测试是否有效果:
这样,就实现了对不同租户用户数量的限制。
社交登录
特征值也可以在前端使用,在/abp/application-configuration
中就可以获取到。
拿到特征值,前端也可以做一些差异化功能,比如这里的是否支持社交登录。
关于Feature就简单介绍到这里,本项目源码放在:/xiajingren/HelloAbp
另外非常感谢热心小伙@jonny-xhl[2]给添加的设置模块(来自EasyAbp的Abp.SettingUi[3])。
最后
本文只是对Feature的最基本介绍,关于Feature,还有很多实用的API方法,基于Feature可以满足很多定制化需求,想深入了解的话可以看下Abp.FeatureManagement源码。
感谢@jonny-xhl的pr。
参考资料
[1]
重写服务:https://docs.abp.io/zh-Hans/abp/latest/Customizing-Application-Modules-Overriding-Services
[2]
jonny-xhl:/jonny-xhl
[3]
Abp.SettingUi:/EasyAbp/Abp.SettingUi
如果本文对您有用,
不妨点个“在看”或者转发朋友圈支持一下