request->post('account'); $password = $this->request->post('password'); if (!$account || !$password) { $this->error(__('Invalid parameters')); } $ret = $this->auth->login($account, $password); if ($ret) { $data = $this->auth->getUserinfo_smiple(); $this->success(__('Logged in successful'), $data); } else { $this->error($this->auth->getError()); } } /** * 手机验证码登录 * * @ApiMethod (POST) * @param string $mobile 手机号 * @param string $captcha 验证码 */ public function mobilelogin() { $mobile = $this->request->post('mobile'); $captcha = $this->request->post('captcha'); if (!$mobile || !$captcha) { $this->error(__('Invalid parameters')); } if (!Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('Mobile is incorrect')); } if (!Sms::check($mobile, $captcha, 'mobilelogin')) { $this->error(__('Captcha is incorrect')); } $user = \app\common\model\User::getByMobile($mobile); if ($user) { if ($user->status != 1) { $this->error(__('Account is locked')); } //如果已经有账号则直接登录 $ret = $this->auth->direct($user->id); } else { $this->error('不存在的用户'); } if ($ret) { Sms::flush($mobile, 'mobilelogin'); $data = $this->auth->getUserinfo_smiple(); $this->success(__('Logged in successful'), $data); } else { $this->error($this->auth->getError()); } } /** * 注册会员 * * @ApiMethod (POST) * @param string $username 用户名 * @param string $password 密码 * @param string $email 邮箱 * @param string $mobile 手机号 * @param string $code 验证码 */ public function register() { $username = $this->request->post('username'); $password = $this->request->post('password'); $email = $this->request->post('email'); $mobile = $this->request->post('mobile'); $code = $this->request->post('code'); if (!$username || !$password) { $this->error(__('Invalid parameters')); } if ($email && !Validate::is($email, "email")) { $this->error(__('Email is incorrect')); } if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('Mobile is incorrect')); } $ret = Sms::check($mobile, $code, 'register'); if (!$ret) { $this->error(__('Captcha is incorrect')); } $ret = $this->auth->register($username, $password, $email, $mobile, []); if ($ret) { $data = $this->auth->getUserinfo_smiple(); $this->success(__('Sign up successful'), $data); } else { $this->error($this->auth->getError()); } } /** * 退出登录 * @ApiMethod (POST) */ public function logout() { if (!$this->request->isPost()) { $this->error(__('Invalid parameters')); } $this->auth->logout(); $this->success(__('Logout successful')); } //真注销 public function cancleuser(){ if (!$this->request->isPost()) { $this->error(__('Invalid parameters')); } //退出im // $tenIm = new Tenim(); // $tenIm->loginoutim($this->auth->id); $data = [ 'status' => -1, 'mobile' => 'close_'.$this->auth->mobile, 'wechat_openid' => 'close_'.$this->auth->wechat_openid, // 'ios_user_id' => 'close_'.$this->auth->ios_user_id, ]; Db::name('user')->where('id',$this->auth->id)->update($data); $this->auth->logout(); $this->success('注销成功'); } //用户详细资料 public function userinfo(){ $info = $this->auth->getUserinfo(); $this->success(__('success'),$info); } /** * 修改会员个人信息 * * @ApiMethod (POST) * @param string $avatar 头像地址 * @param string $username 用户名 * @param string $nickname 昵称 * @param string $bio 个人简介 */ public function profile() { $field_array = ['nickname','avatar']; $data = []; foreach($field_array as $key => $field){ //前端传不了post,改了 /*if(!request()->has($field,'post')){ continue; }*/ if(!input('?'.$field)){ continue; } $newone = input($field); if($field == 'avatar'){ $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars'); } $data[$field] = $newone; } if(empty($data)){ $this->success(); } $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data); if($update_rs === false){ $this->error('修改资料失败'); } $this->success(); } /** * 修改手机号 * * @ApiMethod (POST) * @param string $mobile 手机号 * @param string $captcha 验证码 */ public function changemobile() { $user = $this->auth->getUser(); $mobile = $this->request->post('mobile'); $captcha = $this->request->post('captcha'); if (!$mobile || !$captcha) { $this->error(__('Invalid parameters')); } if (!Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('Mobile is incorrect')); } if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) { $this->error(__('Mobile already exists')); } $result = Sms::check($mobile, $captcha, 'changemobile'); if (!$result) { $this->error(__('Captcha is incorrect')); } $user->mobile = $mobile; $user->save(); Sms::flush($mobile, 'changemobile'); $this->success(); } //修改密码 public function changepwd() { $mobile = $this->auth->mobile; $captcha = $this->request->post('captcha'); $newpassword = input('newpassword',''); if (!$mobile || !$captcha || !$newpassword) { $this->error(__('Invalid parameters')); } if (!Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('Mobile is incorrect')); } $result = Sms::check($mobile, $captcha, 'changepwd'); if (!$result) { $this->error(__('Captcha is incorrect')); } Sms::flush($mobile, 'changepwd'); if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) { $this->error(__('Password must be 6 to 30 characters')); } $ret = $this->auth->changepwd($newpassword, '', true); if ($ret) { $this->success(__('Reset password successful')); } else { $this->error($this->auth->getError()); } } /** * 重置密码 * * @ApiMethod (POST) * @param string $mobile 手机号 * @param string $newpassword 新密码 * @param string $captcha 验证码 */ public function resetpwd() { $mobile = $this->request->post("mobile"); $captcha = $this->request->post("captcha"); $newpassword = $this->request->post("newpassword"); if (!$mobile || !$captcha || !$newpassword) { $this->error(__('Invalid parameters')); } if (!Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('Mobile is incorrect')); } $user = \app\common\model\User::getByMobile($mobile); if (!$user) { $this->error('不存在的用户'); }else{ if ($user->status != 1) { $this->error(__('Account is locked')); } } $ret = Sms::check($mobile, $captcha, 'resetpwd'); if (!$ret) { $this->error(__('Captcha is incorrect')); } Sms::flush($mobile, 'resetpwd'); if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) { $this->error(__('Password must be 6 to 30 characters')); } //模拟一次登录 $this->auth->direct($user->id); $ret = $this->auth->changepwd($newpassword, '', true); if ($ret) { $this->success(__('Reset password successful')); } else { $this->error($this->auth->getError()); } } }