Login.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\login;
  12. use app\dict\member\MemberLoginTypeDict;
  13. use app\service\api\captcha\CaptchaService;
  14. use app\service\api\login\ConfigService;
  15. use app\service\api\login\LoginService;
  16. use core\base\BaseController;
  17. use Exception;
  18. use think\Response;
  19. class Login extends BaseController
  20. {
  21. /**
  22. * 登录
  23. * @return Response
  24. */
  25. public function login()
  26. {
  27. $data = $this->request->params([
  28. ['username', ''],
  29. ['password', ''],
  30. ]);
  31. //校验登录注册配置
  32. ( new ConfigService() )->checkLoginConfig(MemberLoginTypeDict::USERNAME);
  33. //参数验证
  34. //验证码验证
  35. $result = (new LoginService())->account($data['username'], $data['password']);
  36. if (!$result) {
  37. //账号密码错误, 重置验证码
  38. return fail('ACCOUNT_OR_PASSWORD_ERROR');
  39. }
  40. return success($result);
  41. }
  42. /**
  43. * 登出
  44. * @return Response
  45. */
  46. public function logout()
  47. {
  48. (new LoginService)->logout();
  49. return success('MEMBER_LOGOUT');
  50. }
  51. /**
  52. * 创建验证码
  53. * @return Response
  54. */
  55. public function captcha()
  56. {
  57. return success((new CaptchaService())->create());
  58. }
  59. /**
  60. * 发送手机验证码
  61. * @param $type
  62. * @return Response
  63. * @throws Exception
  64. */
  65. public function sendMobileCode($type)
  66. {
  67. $data = $this->request->params([
  68. ['mobile', ''],
  69. ]);
  70. return success((new LoginService())->sendMobileCode($data['mobile'], $type));
  71. }
  72. /**
  73. * 手机号登录
  74. * @return Response
  75. */
  76. public function mobile()
  77. {
  78. $data = $this->request->params([
  79. ['mobile', ''],
  80. ]);
  81. //校验登录注册配置
  82. ( new ConfigService() )->checkLoginConfig(MemberLoginTypeDict::MOBILE);
  83. return success((new LoginService())->mobile($data['mobile']));
  84. }
  85. /**
  86. * 重置密码
  87. * @return Response
  88. */
  89. public function resetPassword()
  90. {
  91. $data = $this->request->params([
  92. ['mobile', ''],
  93. ['password', '']
  94. ]);
  95. //参数验证
  96. $this->validate($data, 'app\validate\member\Member.reset_password');
  97. (new LoginService())->resetPassword($data['mobile'], $data['password']);
  98. return success('PASSWORD_RESET_SUCCESS');
  99. }
  100. }