123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace app\data\controller\api;
- use app\data\model\DataUser;
- use app\data\service\MessageService;
- use app\data\service\UserAdminService;
- use Carbon\Carbon;
- use hg\apidoc\annotation\Method;
- use hg\apidoc\annotation\Param;
- use hg\apidoc\annotation\Returned;
- use hg\apidoc\annotation\Title;
- /**
- * @Title("用户登录注册接口")
- */
- class Login extends Auth
- {
- protected $noNeedLogin=['in','register','sendsms','findpwd','sms'];
- /**
- * @Title("手机号或用户名+密码登陆")
- * @Method("post")
- * @Param("phone",desc="手机号或用户名")
- * @Param("password",desc="密码")
- * @Returned("token.token",desc="token")
- */
- public function in()
- {
- $data = $this->_vali([
- //'phone.mobile' => '手机号码格式错误!',
- 'phone.require' => '登录名不能为空!',
- 'password.require' => '登录密码不能为空!',
- ],'post');
- $map = ['deleted' => 0, 'phone|username' => $data['phone']];
- $user = DataUser::mk()->where($map)->findOrEmpty();
- if ($user->isEmpty()) $this->error('该手机号还没有注册哦!');
- if (empty($user['status'])) $this->error('该用户账号状态异常!');
- if (md5($data['password']) === $user['password']) {
- $this->success('手机登录成功!', UserAdminService::set($map, [], $this->type, true));
- } else {
- $this->error('账号登录失败,请稍候再试!');
- }
- }
- /**
- * @Title("用户统一注册入口")
- * @Method("post")
- * @Param("nickname",desc="昵称")
- * @Param("phone",desc="手机号")
- * @Param("verify",desc="验证码")
- * @Param("password",desc="密码")
- */
- public function register()
- {
- $data = $this->_vali([
- 'region_province.default' => '',
- 'region_city.default' => '',
- 'region_area.default' => '',
- 'username.default' => '',
- 'phone.mobile' => '手机格式错误!',
- 'phone.require' => '手机不能为空!',
- 'nickname.require' => '昵称必须!',
- 'verify.require' => '验证码不能为空!',
- 'password.require' => '登录密码不能为空!',
- ]);
- if (!MessageService::instance()->checkCode($data['verify'], $data['phone'],2)) {
- $this->error('手机短信验证失败!');
- }
- $map = ['phone' => $data['phone'], 'deleted' => 0];
- if (DataUser::mk()->where($map)->count() > 0) {
- $this->error('手机号已注册,请使用其它手机号!');
- }
- $data['password'] = md5($data['password']);
- $user = UserAdminService::set($map, $data, $this->type, true);
- empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user);
- }
- /**
- * @Title("发送短信验证码")
- * @Param("phone",desc="手机号")
- * @Param("type",desc="1登陆2注册3找回密码")
- */
- public function sendsms()
- {
- $data = $this->_vali([
- 'phone.mobile' => '手机号格式错误!',
- 'phone.require' => '手机号不能为空!',
- 'type.require' => '类型不能为空!',
- 'type.in:1,2,3' => '类型有误!',
- ]);
- $needLogin=[];
- if(in_array($data['type'],$needLogin) && !$this->uuid){
- $this->error('请登录');
- }
- MessageService::instance()->sendCode($data['phone'],$data['type']);
- $this->success('');
- }
- /**
- * @Title("找回密码")
- * @Method("post")
- * @Param("phone",desc="手机号")
- * @Param("verify",desc="验证码")
- * @Param("password",desc="密码")
- */
- public function findpwd(){
- $data=$this->_vali([
- 'phone.mobile'=>'手机号必须',
- 'phone.require'=>'手机号必须',
- 'verify.require'=>'验证码必须',
- 'password.require'=>'密码必须',
- ],'post');
- if (!MessageService::instance()->checkCode($data['verify'], $data['phone'],3)) {
- $this->error('手机短信验证失败!');
- }
- $user=DataUser::where('phone',$data['phone'])->find();
- if(!$user){
- $this->error('用户不存在');
- }
- $user['password']=md5($data['password']);
- $user->save();
- $this->success('success');
- }
- /**
- * @Title ("验证码登陆")
- * @Method ("post")
- * @Param("phone",desc="手机号")
- * @Param("verify",desc="验证码")
- * @Returned("registered",type="boolean",desc="是否已注册")
- */
- public function sms(){
- $data=$this->_vali([
- 'phone.mobile'=>'手机号必须',
- 'phone.require'=>'手机号必须',
- 'verify.require'=>'验证码必须',
- ],'post');
- if (!MessageService::instance()->checkCode($data['verify'], $data['phone'],1)) {
- $this->error('手机短信验证失败!');
- }
- $user=DataUser::where('phone',$data['phone'])->find();
- if(!$user){
- $this->error('用户未注册',[
- 'registered'=>false
- ]);
- }
- if(!$user['status']){
- $this->error('用户被禁用');
- }
- $user = UserAdminService::set(['id'=>$user['id']], [], $this->type, true);
- $this->success('',$user);
- }
- }
|