Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
ucenter
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
65a67c6f
authored
Jan 16, 2024
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
完善登录注册
parent
cfc11a12
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
85 additions
and
63 deletions
app/Exceptions/InvalidRequestException.php
app/Http/Controllers/Api/AuthController.php
app/Http/Controllers/Api/UserController.php
app/Http/Kernel.php
app/Http/Requests/UserLogin.php
app/Http/Requests/UserRegister.php
app/Http/Services/UserService.php
config/database.php
routes/api.php
app/Exceptions/InvalidRequestException.php
View file @
65a67c6f
...
...
@@ -20,20 +20,6 @@ class InvalidRequestException extends \Exception
public
function
render
(
Request
$request
)
{
$request_uri
=
isset
(
$_SERVER
[
'REQUEST_URI'
])
?
$_SERVER
[
'REQUEST_URI'
]
:
''
;
$path_info
=
parse_url
(
$request_uri
);
$err_info
=
[
'domain'
=>
isset
(
$_SERVER
[
'HTTP_HOST'
])
?
$_SERVER
[
'HTTP_HOST'
]
:
''
,
'interface'
=>
isset
(
$path_info
)
?
$path_info
[
'path'
]
:
''
,
'user_agent'
=>
isset
(
$_SERVER
[
'HTTP_USER_AGENT'
])
?
$_SERVER
[
'HTTP_USER_AGENT'
]
:
''
,
'ip'
=>
request
()
->
getClientIp
(),
'time'
=>
time
(),
'other'
=>
''
,
'request_params'
=>
$_REQUEST
,
'msg'
=>
$this
->
getMessage
(),
"code"
=>
$this
->
getCode
(),
];
Log
::
error
(
json_encode
(
$err_info
,
JSON_UNESCAPED_UNICODE
));
return
response
()
->
json
(
json_decode
(
Response
::
setError
(
$this
->
message
),
true
));
}
}
app/Http/Controllers/Api/AuthController.php
0 → 100644
View file @
65a67c6f
<?php
namespace
App\Http\Controllers\Api
;
use
App\Http\Controllers\Controller
;
use
App\Http\Requests\UserLogin
;
use
App\Http\Requests\UserRegister
;
use
App\Http\Services\UserService
;
use
Illuminate\Http\Request
;
class
AuthController
extends
Controller
{
public
function
register
(
UserRegister
$request
)
{
$data
=
$request
->
only
([
'mobile'
,
'email'
,
'password'
,
'salt'
,
]);
$ucId
=
UserService
::
register
(
$data
);
return
$this
->
setSuccessData
([
'uc_id'
=>
$ucId
]);
}
public
function
login
(
UserLogin
$request
)
{
$data
=
$request
->
only
([
'mobile'
,
'email'
,
'password'
,
]);
$ucId
=
UserService
::
login
(
$data
);
if
(
empty
(
$ucId
))
{
$this
->
setError
(
'账号和密码校验失败'
);
}
return
$this
->
setSuccessData
([
'uc_id'
=>
$ucId
]);
}
}
app/Http/Controllers/Api/UserController.php
View file @
65a67c6f
...
...
@@ -3,44 +3,8 @@
namespace
App\Http\Controllers\Api
;
use
App\Http\Controllers\Controller
;
use
App\Http\Requests\UserLogin
;
use
App\Http\Requests\UserRegister
;
use
App\Http\Services\UserService
;
use
Illuminate\Http\Request
;
class
UserController
extends
Controller
{
public
function
register
(
UserRegister
$request
)
{
$data
=
$request
->
only
([
'mobile'
,
'email'
,
'password'
,
'salt'
,
]);
$ucId
=
UserService
::
register
(
$data
);
$this
->
setSuccessData
([
'uc_id'
=>
$ucId
]);
}
public
function
login
(
UserLogin
$request
)
{
$data
=
$request
->
only
([
'mobile'
,
'email'
,
'password'
,
]);
$ucId
=
UserService
::
login
(
$data
);
if
(
empty
(
$ucId
))
{
$this
->
setError
(
'账号和密码校验失败'
);
}
$this
->
setSuccessData
([
'uc_id'
=>
$ucId
]);
}
}
app/Http/Kernel.php
View file @
65a67c6f
...
...
@@ -22,8 +22,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\CheckForMaintenanceMode
::
class
,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize
::
class
,
\App\Http\Middleware\TrimStrings
::
class
,
// \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\Monitor
::
class
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull
::
class
,
];
/**
...
...
@@ -49,7 +48,6 @@ class Kernel extends HttpKernel
\App\Http\Middleware\ApiAuthenticate
::
class
,
\Illuminate\Routing\Middleware\SubstituteBindings
::
class
,
\App\Http\Middleware\TrimStrings
::
class
,
\App\Http\Middleware\Permission
::
class
,
],
'sync'
=>
[
...
...
@@ -74,6 +72,5 @@ class Kernel extends HttpKernel
'signed'
=>
\Illuminate\Routing\Middleware\ValidateSignature
::
class
,
// 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified'
=>
\Illuminate\Auth\Middleware\EnsureEmailIsVerified
::
class
,
'monitor'
=>
\App\Http\Middleware\Monitor
::
class
,
];
}
app/Http/Requests/UserLogin.php
View file @
65a67c6f
...
...
@@ -2,7 +2,10 @@
namespace
App\Http\Requests
;
use
App\Exceptions\InvalidRequestException
;
use
Illuminate\Contracts\Validation\Validator
;
use
Illuminate\Foundation\Http\FormRequest
;
use
Illuminate\Http\Exceptions\HttpResponseException
;
class
UserLogin
extends
FormRequest
{
...
...
@@ -13,7 +16,7 @@ class UserLogin extends FormRequest
*/
public
function
authorize
()
{
return
fals
e
;
return
tru
e
;
}
/**
...
...
@@ -24,7 +27,14 @@ class UserLogin extends FormRequest
public
function
rules
()
{
return
[
//
'password'
=>
'required'
,
'mobile'
=>
'required_without:email'
,
'email'
=>
'required_without:mobile'
];
}
protected
function
failedValidation
(
Validator
$validator
)
{
throw
new
InvalidRequestException
(
$validator
->
errors
()
->
first
());
}
}
app/Http/Requests/UserRegister.php
View file @
65a67c6f
...
...
@@ -2,7 +2,10 @@
namespace
App\Http\Requests
;
use
App\Exceptions\InvalidRequestException
;
use
Illuminate\Contracts\Validation\Validator
;
use
Illuminate\Foundation\Http\FormRequest
;
use
Illuminate\Http\Exceptions\HttpResponseException
;
class
UserRegister
extends
FormRequest
{
...
...
@@ -30,4 +33,9 @@ class UserRegister extends FormRequest
'email'
=>
'required_without:mobile'
];
}
protected
function
failedValidation
(
Validator
$validator
)
{
throw
new
InvalidRequestException
(
$validator
->
errors
()
->
first
());
}
}
app/Http/Services/UserService.php
View file @
65a67c6f
...
...
@@ -2,6 +2,7 @@
namespace
App\Http\Services
;
use
App\Exceptions\InvalidRequestException
;
use
App\Http\Models\UserModel
;
class
UserService
...
...
@@ -14,16 +15,21 @@ class UserService
if
(
!
empty
(
$data
[
'mobile'
]))
{
$ucId
=
UserModel
::
where
(
'mobile'
,
$data
[
'mobile'
])
->
value
(
'uc_id'
);
}
else
{
$ucId
=
UserModel
::
where
(
'email'
,
$data
[
'email'
])
->
value
(
'uc_id'
);
}
if
(
$ucId
)
{
return
$ucId
;
}
else
{
//去新增注册
$data
[
'create_time'
]
=
time
();
$ucId
=
UserModel
::
insertGetId
(
$data
);
}
return
$ucId
;
}
//登录接口
public
function
login
(
$data
)
public
static
function
login
(
$data
)
{
$loginQuery
=
UserModel
::
where
(
'status'
,
UserModel
::
STATUS_ENABLE
);
if
(
!
empty
(
$data
[
'mobile'
]))
{
...
...
@@ -34,6 +40,10 @@ class UserService
$loginQuery
->
where
(
'email'
,
$data
[
'email'
]);
}
if
(
empty
(
$salt
))
{
throw
new
InvalidRequestException
(
'该账号不存在'
);
}
$passwordHash
=
PasswordService
::
passwordHash
(
$data
[
'password'
],
$salt
);
$user
=
$loginQuery
->
where
(
'password'
,
$passwordHash
)
->
first
();
...
...
config/database.php
View file @
65a67c6f
...
...
@@ -37,10 +37,10 @@ return [
// 新魔方
'mysql'
=>
[
'driver'
=>
'mysql'
,
'host'
=>
get_resource_config_section
(
'db'
,
'db_
topic
'
)[
'host'
],
'database'
=>
get_resource_config_section
(
'db'
,
'db_
topic
'
)[
'db'
],
// liexin_crm
'username'
=>
get_resource_config_section
(
'db'
,
'db_
topic
'
)[
'user'
],
'password'
=>
get_resource_config_section
(
'db'
,
'db_
topic
'
)[
'passwd'
],
'host'
=>
get_resource_config_section
(
'db'
,
'db_
ucenter
'
)[
'host'
],
'database'
=>
get_resource_config_section
(
'db'
,
'db_
ucenter
'
)[
'db'
],
// liexin_crm
'username'
=>
get_resource_config_section
(
'db'
,
'db_
ucenter
'
)[
'user'
],
'password'
=>
get_resource_config_section
(
'db'
,
'db_
ucenter
'
)[
'passwd'
],
'port'
=>
3306
,
'charset'
=>
'utf8'
,
'collation'
=>
'utf8_general_ci'
,
...
...
routes/api.php
View file @
65a67c6f
...
...
@@ -4,5 +4,6 @@ use Illuminate\Support\Facades\Route;
//接口相关路由
Route
::
namespace
(
'Api'
)
->
group
(
function
()
{
Route
::
match
([
"get"
,
"post"
],
'/customFormData/exportCustomFormData'
,
'CustomFormDataController@exportCustomFormData'
);
Route
::
match
([
"GET"
,
"POST"
],
'/auth/register'
,
'AuthController@register'
);
Route
::
match
([
"GET"
,
"POST"
],
'/auth/login'
,
'AuthController@login'
);
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment