Login.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\serve;
  12. use app\controller\admin\AuthController;
  13. use app\validate\admin\serve\ServeValidate;
  14. use app\Request;
  15. use app\services\message\sms\SmsAdminServices;
  16. use crmeb\services\CacheService;
  17. use app\services\serve\ServeServices;
  18. use think\facade\App;
  19. /**
  20. * 服务登录
  21. * Class Login
  22. * @package app\controller\admin\v1\serve
  23. */
  24. class Login extends AuthController
  25. {
  26. public function __construct(App $app, ServeServices $services)
  27. {
  28. parent::__construct($app);
  29. $this->services = $services;
  30. }
  31. /**
  32. * 发送验证码
  33. * @param string $phone
  34. * @return mixed
  35. */
  36. public function captcha(string $phone)
  37. {
  38. $this->validate(['phone' => $phone], ServeValidate::class, 'phone');
  39. return $this->success('发送成功', $this->services->user()->code($phone));
  40. }
  41. /**
  42. * 验证验证码
  43. * @param string $phone
  44. * @param $code
  45. * @return mixed
  46. */
  47. public function checkCode()
  48. {
  49. [$phone, $verify_code] = $this->request->postMore([
  50. ['phone', ''],
  51. ['verify_code', ''],
  52. ], true);
  53. $this->validate(['phone' => $phone], ServeValidate::class, 'phone');
  54. return $this->success('success', $this->services->user()->checkCode($phone, $verify_code));
  55. }
  56. /**
  57. * 注册服务
  58. * @param Request $request
  59. * @param SmsAdminServices $services
  60. * @return mixed
  61. */
  62. public function register(Request $request, SmsAdminServices $services)
  63. {
  64. $data = $request->postMore([
  65. ['phone', ''],
  66. ['account', ''],
  67. ['password', ''],
  68. ['verify_code', ''],
  69. ]);
  70. $data['account'] = $data['phone'];
  71. $this->validate($data, ServeValidate::class);
  72. $data['password'] = md5($data['password']);
  73. $res = $this->services->user()->register($data);
  74. if ($res) {
  75. $services->updateSmsConfig($data['account'], md5($data['account'] . md5($data['password'])));
  76. return $this->success('注册成功');
  77. } else {
  78. return $this->fail('注册失败');
  79. }
  80. }
  81. /**
  82. * 平台登录
  83. * @return mixed
  84. * @throws \Psr\SimpleCache\InvalidArgumentException
  85. */
  86. public function login(SmsAdminServices $services)
  87. {
  88. [$account, $password] = $this->request->postMore([
  89. ['account', ''],
  90. ['password', '']
  91. ], true);
  92. $this->validate(['account' => $account, 'password' => $password], ServeValidate::class, 'login');
  93. $password = md5($account . md5($password));
  94. $res = $this->services->user()->login($account, $password);
  95. if ($res) {
  96. CacheService::redisHandler()->set('sms_account', $account);
  97. $services->updateSmsConfig($account, $password);
  98. return $this->success('登录成功', $res);
  99. } else {
  100. return $this->fail('登录失败');
  101. }
  102. }
  103. }