You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

74 line
1.9 KiB

  1. /*
  2. * @Description: 校验用户信息是否符合规则
  3. * @Author: hai-27
  4. * @Date: 2020-02-25 15:43:27
  5. * @LastEditors: hai-27
  6. * @LastEditTime: 2020-02-27 02:04:35
  7. */
  8. module.exports = {
  9. /**
  10. * 校验用户信息是否符合规则
  11. * @param {Object} ctx
  12. * @param {string} userName
  13. * @param {string} password
  14. * @return:
  15. */
  16. checkUserInfo: (ctx, userName = '', password = '') => {
  17. // userName = userName ? userName : '';
  18. // password = password ? password : '';
  19. // 判断是否为空
  20. if (userName.length === 0 || password.length === 0) {
  21. ctx.body = {
  22. code: '002',
  23. msg: '用户名或密码不能为空'
  24. }
  25. return false;
  26. }
  27. // 用户名校验规则
  28. const userNameRule = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/;
  29. if (!userNameRule.test(userName)) {
  30. ctx.body = {
  31. code: '003',
  32. msg: '用户名不合法(以字母开头,允许5-16字节,允许字母数字下划线)'
  33. }
  34. return false;
  35. }
  36. // 密码校验规则
  37. const passwordRule = /^[a-zA-Z]\w{5,17}$/;
  38. if (!passwordRule.test(password)) {
  39. ctx.body = {
  40. code: '003',
  41. msg: '密码不合法(以字母开头,长度在6~18之间,只能包含字母、数字和下划线)'
  42. }
  43. return false;
  44. }
  45. return true;
  46. },
  47. /**
  48. * 校验用户名是否符合规则
  49. * @param {type}
  50. * @return:
  51. */
  52. checkUserName: (ctx, userName = '') => {
  53. // 判断是否为空
  54. if (userName.length === 0) {
  55. ctx.body = {
  56. code: '002',
  57. msg: '用户名不能为空'
  58. }
  59. return false;
  60. }
  61. // 用户名校验规则
  62. const userNameRule = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/;
  63. if (!userNameRule.test(userName)) {
  64. ctx.body = {
  65. code: '003',
  66. msg: '用户名不合法(以字母开头,允许5-16字节,允许字母数字下划线)'
  67. }
  68. return false;
  69. }
  70. return true;
  71. }
  72. }