Login.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\data\controller\api;
  3. use app\data\model\DataUser;
  4. use app\data\service\MessageService;
  5. use app\data\service\UserAdminService;
  6. use hg\apidoc\annotation\Method;
  7. use hg\apidoc\annotation\Param;
  8. use hg\apidoc\annotation\Title;
  9. /**
  10. * @Title("用户登录注册接口")
  11. */
  12. class Login extends Auth
  13. {
  14. protected $noNeedLogin=['in','register','sendsms','findpwd'];
  15. /**
  16. * @Title("手机号或用户名+密码登陆")
  17. * @Method("post")
  18. * @Param("phone",desc="手机号或用户名")
  19. * @Param("password",desc="密码")
  20. */
  21. public function in()
  22. {
  23. $data = $this->_vali([
  24. //'phone.mobile' => '手机号码格式错误!',
  25. 'phone.require' => '登录名不能为空!',
  26. 'password.require' => '登录密码不能为空!',
  27. ],'post');
  28. $map = ['deleted' => 0, 'phone|username' => $data['phone']];
  29. $user = DataUser::mk()->where($map)->findOrEmpty();
  30. if ($user->isEmpty()) $this->error('该手机号还没有注册哦!');
  31. if (empty($user['status'])) $this->error('该用户账号状态异常!');
  32. if (md5($data['password']) === $user['password']) {
  33. $this->success('手机登录成功!', UserAdminService::set($map, [], $this->type, true));
  34. } else {
  35. $this->error('账号登录失败,请稍候再试!');
  36. }
  37. }
  38. /**
  39. * @Title("用户统一注册入口")
  40. * @Method("post")
  41. * @Param("nickname",desc="昵称")
  42. * @Param("phone",desc="手机号")
  43. * @Param("verify",desc="验证码")
  44. * @Param("password",desc="密码")
  45. */
  46. public function register()
  47. {
  48. $data = $this->_vali([
  49. 'region_province.default' => '',
  50. 'region_city.default' => '',
  51. 'region_area.default' => '',
  52. 'username.default' => '',
  53. 'phone.mobile' => '手机格式错误!',
  54. 'phone.require' => '手机不能为空!',
  55. 'nickname.require' => '昵称必须!',
  56. 'verify.require' => '验证码不能为空!',
  57. 'password.require' => '登录密码不能为空!',
  58. ]);
  59. if (!MessageService::instance()->checkVerifyCode($data['verify'], $data['phone'])) {
  60. $this->error('手机短信验证失败!');
  61. }
  62. $map = ['phone' => $data['phone'], 'deleted' => 0];
  63. if (DataUser::mk()->where($map)->count() > 0) {
  64. $this->error('手机号已注册,请使用其它手机号!');
  65. }
  66. $data['password'] = md5($data['password']);
  67. $user = UserAdminService::set($map, $data, $this->type, true);
  68. empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user);
  69. }
  70. /**
  71. * @Title("发送短信验证码")
  72. * @Param("phone",desc="手机号")
  73. * @Param("type",desc="1登陆2注册3找回密码")
  74. */
  75. public function sendsms()
  76. {
  77. $data = $this->_vali([
  78. 'phone.mobile' => '手机号格式错误!',
  79. 'phone.require' => '手机号不能为空!',
  80. 'type.require' => '类型不能为空!',
  81. 'type.in:1,2,3' => '类型有误!',
  82. ]);
  83. $needLogin=[];
  84. if(in_array($data['type'],$needLogin) && !$this->uuid){
  85. $this->error('请登录');
  86. }
  87. [$state, $message, $data] = MessageService::instance()->sendVerifyCode($data['phone']);
  88. $state ? $this->success($message, $data) : $this->error($message, $data);
  89. }
  90. /**
  91. * @Title("找回密码")
  92. * @Method("post")
  93. * @Param("phone",desc="手机号")
  94. * @Param("verify",desc="验证码")
  95. * @Param("password",desc="密码")
  96. */
  97. public function findpwd(){
  98. $data=$this->_vali([
  99. 'phone.mobile'=>'手机号必须',
  100. 'phone.require'=>'手机号必须',
  101. 'verify.require'=>'验证码必须',
  102. 'password.require'=>'密码必须',
  103. ]);
  104. if (!MessageService::instance()->checkVerifyCode($data['verify'], $data['phone'])) {
  105. $this->error('手机短信验证失败!');
  106. }
  107. $user=DataUser::where('phone',$data['phone'])->find();
  108. if(!$user){
  109. $this->error('用户不存在');
  110. }
  111. }
  112. }