diff --git a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs index 3fc24e6..ecf3c93 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs @@ -19,6 +19,8 @@ using ZR.Service.System; using Microsoft.Extensions.Options; using UAParser; using IPTools.Core; +using Infrastructure.Extensions; +using Microsoft.AspNetCore.Authorization; namespace ZR.Admin.WebApi.Controllers.System { @@ -80,7 +82,7 @@ namespace ZR.Admin.WebApi.Controllers.System } var user = sysLoginService.Login(loginBody, RecordLogInfo(httpContextAccessor.HttpContext)); - + List roles = roleService.SelectUserRoleListByUserId(user.UserId); //权限集合 eg *:*:*,system:user:list List permissions = permissionService.GetMenuPermission(user); @@ -105,9 +107,9 @@ namespace ZR.Admin.WebApi.Controllers.System //}).Wait(); var userid = HttpContext.GetUId(); var name = HttpContext.GetName(); - + CacheService.RemoveUserPerms(GlobalConstant.UserPermKEY + userid); - return SUCCESS(new { name , id = userid }); + return SUCCESS(new { name, id = userid }); } /// @@ -198,9 +200,41 @@ namespace ZR.Admin.WebApi.Controllers.System ipaddr = ipAddr, userName = context.GetName(), loginLocation = ip_info.Province + "-" + ip_info.City - }; + }; return sysLogininfor; } + + /// + /// 注册 + /// + /// + /// + [HttpPost("/register")] + [AllowAnonymous] + [Log(Title = "注册", BusinessType = Infrastructure.Enums.BusinessType.INSERT)] + public IActionResult Register([FromBody] RegisterDto dto) + { + SysConfig config = sysConfigService.GetSysConfigByKey("sys.account.register"); + if (config?.ConfigValue != "true") + { + return ToResponse(ResultCode.CUSTOM_ERROR, "当前系统没有开启注册功能!"); + } + SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff"); + if (sysConfig?.ConfigValue != "off" && CacheHelper.Get(dto.Uuid) is string str && !str.ToLower().Equals(dto.Code.ToLower())) + { + return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误"); + } + if (UserConstants.NOT_UNIQUE.Equals(sysUserService.CheckUserNameUnique(dto.Username))) + { + return ToResponse(ResultCode.CUSTOM_ERROR, $"保存用户{dto.Username}失败,注册账号已存在"); + } + SysUser user = sysUserService.Register(dto); + if (user.UserId > 0) + { + return SUCCESS(user); + } + return ToResponse(ResultCode.CUSTOM_ERROR, "注册失败,请联系管理员"); + } } } diff --git a/ZR.Common/Tools.cs b/ZR.Common/Tools.cs index 90d7a58..053802c 100644 --- a/ZR.Common/Tools.cs +++ b/ZR.Common/Tools.cs @@ -80,5 +80,38 @@ namespace ZR.Common return false; } } + + /// + /// 计算密码强度 + /// + /// 密码字符串 + /// + public static bool PasswordStrength(string password) + { + //空字符串强度值为0 + if (string.IsNullOrEmpty(password)) return false; + + //字符统计 + int iNum = 0, iLtt = 0, iSym = 0; + foreach (char c in password) + { + if (c >= '0' && c <= '9') iNum++; + else if (c >= 'a' && c <= 'z') iLtt++; + else if (c >= 'A' && c <= 'Z') iLtt++; + else iSym++; + } + + if (iLtt == 0 && iSym == 0) return false; //纯数字密码 + if (iNum == 0 && iLtt == 0) return false; //纯符号密码 + if (iNum == 0 && iSym == 0) return false; //纯字母密码 + + if (password.Length >= 6 && password.Length < 16) return true;//长度不大于6的密码 + + if (iLtt == 0) return true; //数字和符号构成的密码 + if (iSym == 0) return true; //数字和字母构成的密码 + if (iNum == 0) return true; //字母和符号构成的密码 + + return true; //由数字、字母、符号构成的密码 + } } } diff --git a/ZR.Model/System/Dto/RegisterDto.cs b/ZR.Model/System/Dto/RegisterDto.cs new file mode 100644 index 0000000..36898f4 --- /dev/null +++ b/ZR.Model/System/Dto/RegisterDto.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Text; + +namespace ZR.Model.System.Dto +{ + public class RegisterDto + { + /// + /// 用户名 + /// + [Required(ErrorMessage = "用户名不能为空")] + public string Username { get; set; } + + /// + /// 用户密码 + /// + [Required(ErrorMessage = "密码不能为空")] + public string Password { get; set; } + [Required(ErrorMessage = "确认密码不能为空")] + public string ConfirmPassword { get; set; } + /** + * 验证码 + */ + public string Code { get; set; } + + /** + * 唯一标识 + */ + public string Uuid { get; set; } = ""; + } +} diff --git a/ZR.Service/System/IService/ISysLoginService.cs b/ZR.Service/System/IService/ISysLoginService.cs index 5bd432f..3043962 100644 --- a/ZR.Service/System/IService/ISysLoginService.cs +++ b/ZR.Service/System/IService/ISysLoginService.cs @@ -9,6 +9,12 @@ namespace ZR.Service.System.IService { public interface ISysLoginService: IBaseService { + /// + /// 登录 + /// + /// + /// + /// public SysUser Login(LoginBodyDto loginBody, SysLogininfor logininfor); /// diff --git a/ZR.Service/System/IService/ISysUserService.cs b/ZR.Service/System/IService/ISysUserService.cs index 4c9d888..35848e5 100644 --- a/ZR.Service/System/IService/ISysUserService.cs +++ b/ZR.Service/System/IService/ISysUserService.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ZR.Model; +using ZR.Model; using ZR.Model.System; -using ZR.Repository; +using ZR.Model.System.Dto; namespace ZR.Service.System.IService { @@ -66,5 +61,12 @@ namespace ZR.Service.System.IService /// /// public int UpdatePhoto(SysUser user); + + /// + /// 注册 + /// + /// + /// + SysUser Register(RegisterDto dto); } } diff --git a/ZR.Service/System/SysUserService.cs b/ZR.Service/System/SysUserService.cs index eb69b9f..daa2202 100644 --- a/ZR.Service/System/SysUserService.cs +++ b/ZR.Service/System/SysUserService.cs @@ -1,10 +1,14 @@ +using Infrastructure; using Infrastructure.Attribute; +using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; +using ZR.Common; using ZR.Model; using ZR.Model.System; +using ZR.Model.System.Dto; using ZR.Repository.System; using ZR.Service.System.IService; @@ -156,5 +160,34 @@ namespace ZR.Service { return UserRepository.UpdatePhoto(user); } + + /// + /// 注册用户 + /// + /// + /// + public SysUser Register(RegisterDto dto) + { + //密码md5 + string password = NETCore.Encrypt.EncryptProvider.Md5(dto.Password); + if (!Tools.PasswordStrength(dto.Password)) + { + throw new CustomException("密码强度不符合要求"); + } + SysUser user = new() + { + Create_time = DateTime.Now, + UserName = dto.Username, + NickName = dto.Username, + Password = password, + Status = "0", + DeptId = 0, + Remark = "用户注册" + }; + + user.UserId = UserRepository.AddUser(user); + return user; + } + } } diff --git a/ZR.Vue/src/api/system/login.js b/ZR.Vue/src/api/system/login.js index 9adfb26..cd56aa7 100644 --- a/ZR.Vue/src/api/system/login.js +++ b/ZR.Vue/src/api/system/login.js @@ -9,9 +9,9 @@ export function login(username, password, code, uuid) { uuid } return request({ - url: '/login', - method: 'POST', - data: data, + url: '/login', + method: 'POST', + data: data, }) } @@ -26,8 +26,8 @@ export function getInfo() { // 退出方法 export function logout() { return request({ - url: '/LogOut', - method: 'POST' + url: '/LogOut', + method: 'POST' }) } @@ -38,3 +38,15 @@ export function getCodeImg() { method: 'get' }) } + +/** + * 注册 + * @returns + */ +export function register(data) { + return request({ + url: '/register', + method: 'post', + data: data + }) +} \ No newline at end of file diff --git a/ZR.Vue/src/router/index.js b/ZR.Vue/src/router/index.js index 09c040b..39422a0 100644 --- a/ZR.Vue/src/router/index.js +++ b/ZR.Vue/src/router/index.js @@ -38,6 +38,11 @@ export const constantRoutes = [{ path: '/login', component: (resolve) => require(['@/views/login'], resolve), hidden: true + }, + { + path: '/register', + component: (resolve) => require(['@/views/register'], resolve), + hidden: true }, { path: '/404', diff --git a/ZR.Vue/src/settings.js b/ZR.Vue/src/settings.js index ee93a3e..66b7df3 100644 --- a/ZR.Vue/src/settings.js +++ b/ZR.Vue/src/settings.js @@ -47,5 +47,6 @@ module.exports = { * The default is only used in the production env * If you want to also use it in dev, you can pass ['production', 'development'] */ - errorLog: 'production' + errorLog: 'production', + copyRight: 'Copyright ©2022 izhaorui.cn All Rights Reserved.' } \ No newline at end of file diff --git a/ZR.Vue/src/store/modules/user.js b/ZR.Vue/src/store/modules/user.js index 32ed9ef..8923977 100644 --- a/ZR.Vue/src/store/modules/user.js +++ b/ZR.Vue/src/store/modules/user.js @@ -45,10 +45,13 @@ const user = { setToken(res.data) //提交上面的mutaions方法 commit('SET_TOKEN', res.data) - resolve()//then处理 + resolve() //then处理 } else { - reject(res)//catch处理 + console.log('login error ' + res); + reject(res) //catch处理 } + }).catch(err => { + reject(err); }) }) }, @@ -69,7 +72,7 @@ const user = { commit('SET_NAME', data.user.nickName) commit('SET_AVATAR', avatar) - commit('SET_USERINFO', data.user)//新加 + commit('SET_USERINFO', data.user) //新加 resolve(res) }).catch(error => { reject(error) @@ -79,10 +82,10 @@ const user = { // 退出系统 LogOut({ commit, state }) { - console.log('退出登录') + console.log('退出登录') return new Promise((resolve, reject) => { logout().then((res) => { - removeToken()// 必须先移除token + removeToken() // 必须先移除token commit('SET_TOKEN', '') commit('SET_ROLES', []) commit('SET_PERMISSIONS', []) @@ -104,4 +107,4 @@ const user = { } } -export default user +export default user \ No newline at end of file diff --git a/ZR.Vue/src/utils/request.js b/ZR.Vue/src/utils/request.js index 0d18e3b..a4feaee 100644 --- a/ZR.Vue/src/utils/request.js +++ b/ZR.Vue/src/utils/request.js @@ -57,7 +57,7 @@ service.interceptors.response.use(res => { }) return Promise.reject('无效的会话,或者会话已过期,请重新登录。') - } else if (code == 0 || code == 1 || code == 110 || code == 101 || code == 403 || code == 500 || code == 429) { + } else if (code == 0 || code == 1 || code == 110 || code == 101 || code == 103 || code == 403 || code == 500 || code == 429) { Message({ message: msg, type: 'error' diff --git a/ZR.Vue/src/views/login.vue b/ZR.Vue/src/views/login.vue index 890e416..f716be7 100644 --- a/ZR.Vue/src/views/login.vue +++ b/ZR.Vue/src/views/login.vue @@ -26,122 +26,126 @@ 登 录 登 录 中... +
+ 还没有账号?立即注册 +
diff --git a/document/admin-mysql.sql b/document/admin-mysql.sql index 31a599c..d8da0fe 100644 --- a/document/admin-mysql.sql +++ b/document/admin-mysql.sql @@ -680,6 +680,7 @@ insert into sys_config values(2, '用户管理-账号初始密码', 'sys insert into sys_config values(3, '主框架页-侧边栏主题', 'sys.index.sideTheme', 'theme-dark', 'Y', 'admin', sysdate(), '', null, '深色主题theme-dark,浅色主题theme-light' ); insert into sys_config values(4, '账号自助-验证码开关', 'sys.account.captchaOnOff', 'true', 'Y', 'admin', sysdate(), '', null, '是否开启验证码功能(off、关闭,1、动态验证码 2、动态gif泡泡 3、泡泡 4、静态验证码)'); INSERT INTO `sys_config`(`configId`, `configName`, `configKey`, `configValue`, `configType`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`) VALUES (5, '本地文件上传访问域名', 'sys.file.uploadurl', 'http://localhost:8888', 'Y', '', '2022-04-10 10:11:27', '', NULL, NULL); +INSERT INTO `sys_config`(`configId`, `configName`, `configKey`, `configValue`, `configType`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`) VALUES (6, '开启注册功能', 'sys.account.register', 'true', 'Y', 'admin', '2022-04-14 00:00:00', 'admin', NULL, NULL); -- ---------------------------- -- 18、代码生成业务表 diff --git a/document/admin-sqlserver.sql b/document/admin-sqlserver.sql index c116fd4..ad28447 100644 --- a/document/admin-sqlserver.sql +++ b/document/admin-sqlserver.sql @@ -698,6 +698,7 @@ insert into sys_config values('用户管理-账号初始密码', 'sys.us insert into sys_config values('主框架页-侧边栏主题', 'sys.index.sideTheme', 'theme-dark', 'Y', 'admin', GETDATE(), '', null, '深色主题theme-dark,浅色主题theme-light' ); insert into sys_config values('账号自助-验证码开关', 'sys.account.captchaOnOff', '1', 'Y', 'admin', GETDATE(), '', null, '开启验证码功能(off、关闭,1、动态验证码 2、动态gif泡泡 3、泡泡 4、静态验证码)'); INSERT INTO sys_config VALUES('本地文件上传访问域名', 'sys.file.uploadurl', 'http://localhost:8888', 'Y', 'admin', GETDATE(), '', NULL, NULL); +INSERT INTO sys_config VALUES('开启注册功能', 'sys.account.register', 'true', 'Y', 'admin', GETDATE(), '', NULL, NULL); GO