Login.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Carbon\Carbon;
  7. use hg\apidoc\annotation\Method;
  8. use hg\apidoc\annotation\Param;
  9. use hg\apidoc\annotation\Returned;
  10. use hg\apidoc\annotation\Title;
  11. /**
  12. * @Title("用户登录注册接口")
  13. */
  14. class Login extends Auth
  15. {
  16. protected $noNeedLogin=['in','register','sendsms','findpwd','sms'];
  17. /**
  18. * @Title("手机号或用户名+密码登陆")
  19. * @Method("post")
  20. * @Param("phone",desc="手机号或用户名")
  21. * @Param("password",desc="密码")
  22. * @Returned("token.token",desc="token")
  23. */
  24. public function in()
  25. {
  26. $data = $this->_vali([
  27. //'phone.mobile' => '手机号码格式错误!',
  28. 'phone.require' => '登录名不能为空!',
  29. 'password.require' => '登录密码不能为空!',
  30. ],'post');
  31. $map = ['deleted' => 0, 'phone|username' => $data['phone']];
  32. $user = DataUser::mk()->where($map)->findOrEmpty();
  33. if ($user->isEmpty()) $this->error('该手机号还没有注册哦!');
  34. if (empty($user['status'])) $this->error('该用户账号状态异常!');
  35. if (md5($data['password']) === $user['password']) {
  36. $this->success('手机登录成功!', UserAdminService::set($map, [], $this->type, true));
  37. } else {
  38. $this->error('账号登录失败,请稍候再试!');
  39. }
  40. }
  41. /**
  42. * @Title("用户统一注册入口")
  43. * @Method("post")
  44. * @Param("nickname",desc="昵称")
  45. * @Param("phone",desc="手机号")
  46. * @Param("verify",desc="验证码")
  47. * @Param("password",desc="密码")
  48. */
  49. public function register()
  50. {
  51. $data = $this->_vali([
  52. 'region_province.default' => '',
  53. 'region_city.default' => '',
  54. 'region_area.default' => '',
  55. 'username.default' => '',
  56. 'phone.mobile' => '手机格式错误!',
  57. 'phone.require' => '手机不能为空!',
  58. 'nickname.require' => '昵称必须!',
  59. 'verify.require' => '验证码不能为空!',
  60. 'password.require' => '登录密码不能为空!',
  61. ]);
  62. if (!MessageService::instance()->checkCode($data['verify'], $data['phone'],2)) {
  63. $this->error('手机短信验证失败!');
  64. }
  65. $map = ['phone' => $data['phone'], 'deleted' => 0];
  66. if (DataUser::mk()->where($map)->count() > 0) {
  67. $this->error('手机号已注册,请使用其它手机号!');
  68. }
  69. $data['password'] = md5($data['password']);
  70. $user = UserAdminService::set($map, $data, $this->type, true);
  71. empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user);
  72. }
  73. /**
  74. * @Title("发送短信验证码")
  75. * @Param("phone",desc="手机号")
  76. * @Param("type",desc="1登陆2注册3找回密码")
  77. */
  78. public function sendsms()
  79. {
  80. $data = $this->_vali([
  81. 'phone.mobile' => '手机号格式错误!',
  82. 'phone.require' => '手机号不能为空!',
  83. 'type.require' => '类型不能为空!',
  84. 'type.in:1,2,3' => '类型有误!',
  85. ]);
  86. $needLogin=[];
  87. if(in_array($data['type'],$needLogin) && !$this->uuid){
  88. $this->error('请登录');
  89. }
  90. MessageService::instance()->sendCode($data['phone'],$data['type']);
  91. $this->success('');
  92. }
  93. /**
  94. * @Title("找回密码")
  95. * @Method("post")
  96. * @Param("phone",desc="手机号")
  97. * @Param("verify",desc="验证码")
  98. * @Param("password",desc="密码")
  99. */
  100. public function findpwd(){
  101. $data=$this->_vali([
  102. 'phone.mobile'=>'手机号必须',
  103. 'phone.require'=>'手机号必须',
  104. 'verify.require'=>'验证码必须',
  105. 'password.require'=>'密码必须',
  106. ],'post');
  107. if (!MessageService::instance()->checkCode($data['verify'], $data['phone'],3)) {
  108. $this->error('手机短信验证失败!');
  109. }
  110. $user=DataUser::where('phone',$data['phone'])->find();
  111. if(!$user){
  112. $this->error('用户不存在');
  113. }
  114. $user['password']=md5($data['password']);
  115. $user->save();
  116. $this->success('success');
  117. }
  118. /**
  119. * @Title ("验证码登陆")
  120. * @Method ("post")
  121. * @Param("phone",desc="手机号")
  122. * @Param("verify",desc="验证码")
  123. * @Returned("registered",type="boolean",desc="是否已注册")
  124. */
  125. public function sms(){
  126. $data=$this->_vali([
  127. 'phone.mobile'=>'手机号必须',
  128. 'phone.require'=>'手机号必须',
  129. 'verify.require'=>'验证码必须',
  130. ],'post');
  131. if (!MessageService::instance()->checkCode($data['verify'], $data['phone'],1)) {
  132. $this->error('手机短信验证失败!');
  133. }
  134. $user=DataUser::where('phone',$data['phone'])->find();
  135. if(!$user){
  136. $this->error('用户未注册',[
  137. 'registered'=>false
  138. ]);
  139. }
  140. if(!$user['status']){
  141. $this->error('用户被禁用');
  142. }
  143. $user = UserAdminService::set(['id'=>$user['id']], [], $this->type, true);
  144. $this->success('',$user);
  145. }
  146. }