| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace App\Http\Controllers\V1;
- use App\Models\Job;
- use App\Models\User;
- use App\Services\Api\CommonService;
- use App\Services\Api\UserService;
- use App\Services\JPushService;
- use App\Services\SmsServer;
- use Cache;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Validator;
- class AuthController extends Controller
- {
- //注册
- public function register(Request $request)
- {
- $account = $request->input('account', '');
- $password = $request->input('password', '');
- $passwords = $request->input('passwords', '');
- $captcha = $request->input('captcha', '');
- $captcha_key = $request->input('captcha_key','');
- $validator = Validator::make($request->all(), [
- 'account' => 'required',
- 'name' => 'required|alpha_num',
- 'email' => 'required',
- 'password' => 'required|min:6',
- 'passwords' => 'required|min:6',
- ]);
- if ($validator->fails()) {
- return $this->error($validator->errors()->first());
- }
- if(!captcha_api_check($captcha,$captcha_key)){
- return $this->error("图形验证码错误!");
- }
- if($password != $passwords){
- return $this->error('密码不一致!');
- }
- // 查询用户是否存在
- $user = User::query()
- ->where('account','=',$account)
- ->orWhere('email','=',$request->email)
- ->first();
- if($user){
- return $this->error('账号已存在或邮箱已被注册!');
- }
- if (CommonService::is_email($request->email)){ // 邮箱格式
- if(!EmailController::isEmailCodeRight($request->email,$request->code)){
- return $this->error("验证码错误或已过期,请重新发送!");
- }
- }else{
- return $this->error('账号格式不正确!');
- }
- $user = \App::make('getUserInstance'); //在 app/Providers/AppServiceProvider.php 里面可以创一个单例模式
- $user->name = $request->name; // 姓名
- $user->account = $request->account; // 账号
- $user->nickname = $request->account; // 账号
- $user->email = $request->email; // 邮箱
- $user->password = $password; //这个不是直接存密码,User模型中使用了修改器
- $user->register_ip = request()->ip();
- $user->save();
- return $this->success('创建成功!');
- }
- //账号密码登录
- public function login(Request $request)
- {
- $account = $request->input('account');
- $password = $request->input('password');
- $jpush_reg_id = $request->input('jpush_reg_id');
- if (!$user = User::query()->where('account','=',$account)->orWhere('email','=',$account)->first()) {
- return $this->error('账号不存在');
- }
- $credentials1 = ['account' => $account, 'password' => $password];
- $credentials2 = ['email' => $account, 'password' => $password];
- if (!auth('api')->attempt($credentials1) && !auth('api')->attempt($credentials2)) {
- return $this->error('密码错误!');
- }
- // $user->nickname = $user->nickname.'@huabook.net';
- $data = $this->doLogin($user, $jpush_reg_id);
- return $this->success($data);
- }
- //短信验证码登录
- public function loginBySmsCode(Request $request)
- {
- try {
- if (!$user = User::query()->where(['mobile' => $request->mobile])->first()) {
- return $this->error('账号不存在');
- }
- //手机验证码验证
- SmsServer::checkSmsCodeByVerifyKey($request->mobile, $request->smsCode);
- //如果登录类型和 openid 不为空
- $type = $request->type;
- if (isset($type) && !empty($type)) {
- if ($type == 'weixin') {
- if ($user->wx_openid != '') {
- return $this->error('已经绑定微信');
- }
- $user->wx_openid = $request->openid;
- $user->save();
- }
- }
- $data = $this->doLogin($request->mobile, $request->post('jpush_reg_id', ''));
- } catch (\Exception $exception) {
- return $this->error($exception);
- }
- return $this->success($data);
- }
- //执行登录
- public function doLogin($user, $jpush_reg_id = null)
- {
- if (!empty($jpush_reg_id)) {
- //清除登陆过本设备的账号设备id
- User::query()->where('jpush_reg_id', $jpush_reg_id)->update(['jpush_reg_id' => '']);
- //当前登录用户绑定设备
- $user->jpush_reg_id = $jpush_reg_id;
- //清除别名
- JPushService::deleteAlias('user_id_' . $user->id);
- //设置极光推送别名
- JPushService::updateAlias($user->jpush_reg_id, 'user_id_' . $user->id);
- }
- $user->online = 1;
- $user->last_login_time = date('Y-m-d H:i:s');
- $user->last_login_ip = request()->ip();
- if (!$user->save()) {
- return $this->error('登录失败!');
- }
- $token = Auth::guard('api')->fromUser($user);
- $userInfo = UserService::getUserInfoById($user->id);
- $data = [
- 'token' => "Bearer " . $token,
- 'user_info' => $userInfo,
- ];
- return $data;
- }
- //用户是否存在
- public function isUserExist($account)
- {
- $user = User::where(['mobile' => $account])
- ->orWhere(['email' => $account])
- ->first();
- if (!$user) {
- return false;
- }
- return $user;
- }
- //忘记密码
- public function forgetPassword(Request $request)
- {
- $account = $request->input('account', '');
- $captcha = $request->input('captcha', '');
- $captcha_key = $request->input('captcha_key','');
- $validator = Validator::make($request->all(), [
- 'account' => 'required',
- 'captcha' => 'required',
- 'captcha_key' => 'required',
- ]);
- if ($validator->fails()) {
- return $this->error($validator->errors()->first());
- }
- if(!captcha_api_check($captcha,$captcha_key)){
- return $this->error("图形验证码错误!");
- }
- // 查询用户是否存在
- $user = User::query()
- ->where('account','=',$account)
- ->first();
- if(!$user){
- return $this->error('账号不存在!');
- }
- if($user->status == 0){
- return $this->error('账号已被禁用!');
- }
- // 随机生成密码
- $password = rand(100000, 999999);
- $content = '您找回的密码为系统重新生成:'.$password.',登录后请自行修改!';
- $res = EmailController::sendNotice($user->email,'找回密码通知',$content);
- if(!$res){
- return $this->error("找回密码失败!");
- }
- $user->password = $password; // 处理过加密的
- $user->save();
- return $this->success('',0,'我们已将密码发至您的电子邮件!');
- }
- //找回ID
- public function findId(Request $request)
- {
- $email = $request->input('email', '');
- $captcha = $request->input('captcha', '');
- $captcha_key = $request->input('captcha_key','');
- $validator = Validator::make($request->all(), [
- 'captcha' => 'required',
- 'captcha_key' => 'required',
- 'email' => 'required',
- ]);
- if ($validator->fails()) {
- return $this->error($validator->errors()->first());
- }
- if(!captcha_api_check($captcha,$captcha_key)){
- return $this->error("图形验证码错误!");
- }
- // 查询用户是否存在
- $user = User::query()
- ->where('email','=',$email)
- ->first();
- if(!$user){
- return $this->error('邮箱不存在!');
- }
- if($user->status == 0){
- return $this->error('账号已被禁用!');
- }
- $content = '您找回的ID为:'.$user->account.',请妥善保存!';
- $res = EmailController::sendNotice($user->email,'找回ID通知',$content);
- if(!$res){
- return $this->error("找回ID失败!");
- }
- return $this->success('',0,'我们已将ID发至您的电子邮箱!');
- }
- //退出
- public function logout()
- {
- $user = auth('api')->user();
- if($user){
- if(!empty($user->jpush_reg_id)){
- //清空极光别名
- JPushService::updateAlias($user->jpush_reg_id, '');
- }
- $user->online = 0;
- $user->save();
- }
- auth('api')->logout();
- return $this->success('',0,'退出成功!');
- }
- /**
- * @return void
- * 图形验证码
- */
- public function captcha(){
- $captcha = app('captcha')->create('default', true);
- return $this->success($captcha);
- }
- }
|