登录注册功能实现

用户模型dao操作

  1. /**
  2. * 数据库创建用户
  3. * @param {object} model 用户数据模型
  4. * @return {object} mysql执行结果
  5. */
  6. async create ( model ) {
  7. let result = await dbUtils.insertData( 'user_info', model )
  8. return result
  9. },
  10. /**
  11. * 查找一个存在用户的数据
  12. * @param {obejct} options 查找条件参数
  13. * @return {object|null} 查找结果
  14. */
  15. async getExistOne(options ) {
  16. let _sql = `
  17. SELECT * from user_info
  18. where email="${options.email}" or name="${options.name}"
  19. limit 1`
  20. let result = await dbUtils.query( _sql )
  21. if ( Array.isArray(result) && result.length > 0 ) {
  22. result = result[0]
  23. } else {
  24. result = null
  25. }
  26. return result
  27. },
  28. /**
  29. * 根据用户名和密码查找用户
  30. * @param {object} options 用户名密码对象
  31. * @return {object|null} 查找结果
  32. */
  33. async getOneByUserNameAndPassword( options ) {
  34. let _sql = `
  35. SELECT * from user_info
  36. where password="${options.password}" and name="${options.name}"
  37. limit 1`
  38. let result = await dbUtils.query( _sql )
  39. if ( Array.isArray(result) && result.length > 0 ) {
  40. result = result[0]
  41. } else {
  42. result = null
  43. }
  44. return result
  45. },
  46. /**
  47. * 根据用户名查找用户信息
  48. * @param {string} userName 用户账号名称
  49. * @return {object|null} 查找结果
  50. */
  51. async getUserInfoByUserName( userName ) {
  52. let result = await dbUtils.select(
  53. 'user_info',
  54. ['id', 'email', 'name', 'detail_info', 'create_time', 'modified_time', 'modified_time' ])
  55. if ( Array.isArray(result) && result.length > 0 ) {
  56. result = result[0]
  57. } else {
  58. result = null
  59. }
  60. return result
  61. },

业务层操作

  1. /**
  2. * 创建用户
  3. * @param {object} user 用户信息
  4. * @return {object} 创建结果
  5. */
  6. async create( user ) {
  7. let result = await userModel.create(user)
  8. return result
  9. },
  10. /**
  11. * 查找存在用户信息
  12. * @param {object} formData 查找的表单数据
  13. * @return {object|null} 查找结果
  14. */
  15. async getExistOne( formData ) {
  16. let resultData = await userModel.getExistOne({
  17. 'email': formData.email,
  18. 'name': formData.userName
  19. })
  20. return resultData
  21. },
  22. /**
  23. * 登录业务操作
  24. * @param {object} formData 登录表单信息
  25. * @return {object} 登录业务操作结果
  26. */
  27. async signIn( formData ) {
  28. let resultData = await userModel.getOneByUserNameAndPassword({
  29. 'password': formData.password,
  30. 'name': formData.userName})
  31. return resultData
  32. },
  33. /**
  34. * 根据用户名查找用户业务操作
  35. * @param {string} userName 用户名
  36. * @return {object|null} 查找结果
  37. */
  38. async getUserInfoByUserName( userName ) {
  39. let resultData = await userModel.getUserInfoByUserName( userName ) || {}
  40. let userInfo = {
  41. // id: resultData.id,
  42. email: resultData.email,
  43. userName: resultData.name,
  44. detailInfo: resultData.detail_info,
  45. createTime: resultData.create_time
  46. }
  47. return userInfo
  48. },
  49. /**
  50. * 检验用户注册数据
  51. * @param {object} userInfo 用户注册数据
  52. * @return {object} 校验结果
  53. */
  54. validatorSignUp( userInfo ) {
  55. let result = {
  56. success: false,
  57. message: '',
  58. }
  59. if ( /[a-z0-9\_\-]{6,16}/.test(userInfo.userName) === false ) {
  60. result.message = userCode.ERROR_USER_NAME
  61. return result
  62. }
  63. if ( !validator.isEmail( userInfo.email ) ) {
  64. result.message = userCode.ERROR_EMAIL
  65. return result
  66. }
  67. if ( !/[\w+]{6,16}/.test( userInfo.password ) ) {
  68. result.message = userCode.ERROR_PASSWORD
  69. return result
  70. }
  71. if ( userInfo.password !== userInfo.confirmPassword ) {
  72. result.message = userCode.ERROR_PASSWORD_CONFORM
  73. return result
  74. }
  75. result.success = true
  76. return result
  77. }

controller 操作

  1. /**
  2. * 登录操作
  3. * @param {obejct} ctx 上下文对象
  4. */
  5. async signIn( ctx ) {
  6. let formData = ctx.request.body
  7. let result = {
  8. success: false,
  9. message: '',
  10. data: null,
  11. code: ''
  12. }
  13. let userResult = await userInfoService.signIn( formData )
  14. if ( userResult ) {
  15. if ( formData.userName === userResult.name ) {
  16. result.success = true
  17. } else {
  18. result.message = userCode.FAIL_USER_NAME_OR_PASSWORD_ERROR
  19. result.code = 'FAIL_USER_NAME_OR_PASSWORD_ERROR'
  20. }
  21. } else {
  22. result.code = 'FAIL_USER_NO_EXIST',
  23. result.message = userCode.FAIL_USER_NO_EXIST
  24. }
  25. if ( formData.source === 'form' && result.success === true ) {
  26. let session = ctx.session
  27. session.isLogin = true
  28. session.userName = userResult.name
  29. session.userId = userResult.id
  30. ctx.redirect('/work')
  31. } else {
  32. ctx.body = result
  33. }
  34. },
  35. /**
  36. * 注册操作
  37. * @param {obejct} ctx 上下文对象
  38. */
  39. async signUp( ctx ) {
  40. let formData = ctx.request.body
  41. let result = {
  42. success: false,
  43. message: '',
  44. data: null
  45. }
  46. let validateResult = userInfoService.validatorSignUp( formData )
  47. if ( validateResult.success === false ) {
  48. result = validateResult
  49. ctx.body = result
  50. return
  51. }
  52. let existOne = await userInfoService.getExistOne(formData)
  53. console.log( existOne )
  54. if ( existOne ) {
  55. if ( existOne .name === formData.userName ) {
  56. result.message = userCode.FAIL_USER_NAME_IS_EXIST
  57. ctx.body = result
  58. return
  59. }
  60. if ( existOne .email === formData.email ) {
  61. result.message = userCode.FAIL_EMAIL_IS_EXIST
  62. ctx.body = result
  63. return
  64. }
  65. }
  66. let userResult = await userInfoService.create({
  67. email: formData.email,
  68. password: formData.password,
  69. name: formData.userName,
  70. create_time: new Date().getTime(),
  71. level: 1,
  72. })
  73. console.log( userResult )
  74. if ( userResult && userResult.insertId * 1 > 0) {
  75. result.success = true
  76. } else {
  77. result.message = userCode.ERROR_SYS
  78. }
  79. ctx.body = result
  80. },

api路由操作

  1. const router = require('koa-router')()
  2. const userInfoController = require('./../controllers/user-info')
  3. const routers = router
  4. .get('/user/getUserInfo.json', userInfoController.getLoginUserInfo)
  5. .post('/user/signIn.json', userInfoController.signIn)
  6. .post('/user/signUp.json', userInfoController.signUp)

前端用react.js实现效果

登录模式
project-result-01
注册模式
project-result-01