Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵增煜
/
tzt-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8812d3c6
authored
Nov 18, 2024
by
赵增煜
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增医师药店端端短信发送、登录接口
parent
bc527fab
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
115 additions
and
0 deletions
+115
-0
app/Api/Controllers/DoctorController.php
+8
-0
app/Api/Controllers/PharmacyController.php
+9
-0
app/Api/Controllers/UserController.php
+93
-0
routes/api.php
+5
-0
No files found.
app/Api/Controllers/DoctorController.php
View file @
8812d3c6
...
@@ -125,6 +125,14 @@ public function correction(Request $request)
...
@@ -125,6 +125,14 @@ public function correction(Request $request)
public
function
logout
()
public
function
logout
()
{
{
$authInfo
=
auth
(
'api'
)
->
user
();
$authInfo
=
auth
(
'api'
)
->
user
();
// 医师表user_id改为0
$doctor
=
DoctorModel
::
where
(
'user_id'
,
$authInfo
->
id
)
->
first
();
if
(
!
$doctor
)
{
return
$this
->
failed
(
'医师信息不存在'
);
}
$doctor
->
user_id
=
0
;
$doctor
->
save
();
// user表中last_login_type字段改为0
$user
=
User
::
where
(
'id'
,
$authInfo
->
id
)
->
where
(
'last_login_type'
,
User
::
LOGIN_TYPE_DOCTOR
)
->
first
();
$user
=
User
::
where
(
'id'
,
$authInfo
->
id
)
->
where
(
'last_login_type'
,
User
::
LOGIN_TYPE_DOCTOR
)
->
first
();
if
(
!
$user
)
{
if
(
!
$user
)
{
return
$this
->
failed
(
'用户不存在'
);
return
$this
->
failed
(
'用户不存在'
);
...
...
app/Api/Controllers/PharmacyController.php
View file @
8812d3c6
...
@@ -151,6 +151,15 @@ public function open(Request $request)
...
@@ -151,6 +151,15 @@ public function open(Request $request)
public
function
logout
()
public
function
logout
()
{
{
$authInfo
=
auth
(
'api'
)
->
user
();
$authInfo
=
auth
(
'api'
)
->
user
();
// 药店表user_id改为0
$pharmacy
=
PharmacyModel
::
where
(
'user_id'
,
$authInfo
->
id
)
->
first
();
if
(
!
$pharmacy
)
{
return
$this
->
failed
(
'药店信息不存在'
);
}
$pharmacy
->
user_id
=
0
;
$pharmacy
->
save
();
// user表last_login_type改为0
$user
=
User
::
where
(
'id'
,
$authInfo
->
id
)
->
where
(
'last_login_type'
,
User
::
LOGIN_TYPE_PHARMACY
)
->
first
();
$user
=
User
::
where
(
'id'
,
$authInfo
->
id
)
->
where
(
'last_login_type'
,
User
::
LOGIN_TYPE_PHARMACY
)
->
first
();
if
(
!
$user
)
{
if
(
!
$user
)
{
return
$this
->
failed
(
'用户不存在'
);
return
$this
->
failed
(
'用户不存在'
);
...
...
app/Api/Controllers/UserController.php
View file @
8812d3c6
...
@@ -3,7 +3,10 @@
...
@@ -3,7 +3,10 @@
namespace
App\Api\Controllers
;
namespace
App\Api\Controllers
;
use
App\Http\Controllers\BaseApiController
;
use
App\Http\Controllers\BaseApiController
;
use
App\Models\DoctorModel
;
use
App\Models\PharmacyModel
;
use
App\Models\User
;
use
App\Models\User
;
use
App\Services\SmsService
;
use
Illuminate\Http\Request
;
use
Illuminate\Http\Request
;
class
UserController
extends
BaseApiController
class
UserController
extends
BaseApiController
...
@@ -53,6 +56,96 @@ public function logout()
...
@@ -53,6 +56,96 @@ public function logout()
}
}
/**
/**
* 发送短信接口
*/
public
function
smsCode
(
Request
$request
)
{
// 验证手机号是否存在
$mobile
=
$request
->
input
(
'mobile'
);
$login_type
=
$request
->
input
(
'login_type'
);
if
(
!
$mobile
)
{
return
response
()
->
json
([
'error'
=>
'手机号不能为空'
]);
}
// 验证手机号格式
if
(
!
preg_match
(
'/^1[3-9]\d{9}$/'
,
$mobile
))
{
return
response
()
->
json
([
'error'
=>
'手机号格式不正确'
]);
}
// 检查手机号在医师或者药店表中是否存在
if
(
$login_type
==
User
::
LOGIN_TYPE_PHARMACY
)
{
$pharmacy
=
PharmacyModel
::
query
()
->
where
(
'mobile'
,
$mobile
)
->
first
();
if
(
!
$pharmacy
)
{
return
$this
->
failed
(
'手机号不存在'
);
}
}
elseif
(
$login_type
==
User
::
LOGIN_TYPE_DOCTOR
)
{
$doctor
=
DoctorModel
::
query
()
->
where
(
'mobile'
,
$mobile
)
->
first
();
if
(
!
$doctor
)
{
return
$this
->
failed
(
'手机号不存在'
);
}
}
else
{
return
$this
->
failed
(
'登录类型错误'
);
}
// 生成短信验证码
$verificationCode
=
rand
(
100000
,
999999
);
// 生成6位随机验证码
// 存储验证码和有效期(10分钟)
cache
()
->
put
(
"sms_verification_code_
{
$login_type
}
_
{
$mobile
}
"
,
$verificationCode
,
600
);
// 600秒 = 10分钟
$templateName
=
'verification_code'
;
$templateData
=
[
'code'
=>
$verificationCode
];
$smsService
=
new
SmsService
();
$response
=
$smsService
->
sendSms
(
$mobile
,
$templateName
,
$templateData
);
return
$this
->
success
(
'验证码已发送'
);
}
// 角色绑定
public
function
bindRole
(
Request
$request
)
{
$authInfo
=
auth
(
'api'
)
->
user
();
$mobile
=
$request
->
input
(
'mobile'
);
$login_type
=
$request
->
input
(
'login_type'
);
$code
=
$request
->
input
(
'code'
);
if
(
!
$mobile
||
!
$login_type
||
!
$code
)
{
return
$this
->
failed
(
'参数错误'
);
}
$verificationCode
=
cache
()
->
get
(
"sms_verification_code_
{
$login_type
}
_
{
$mobile
}
"
);
if
(
$verificationCode
!=
$code
)
{
return
$this
->
failed
(
'验证码错误'
);
}
// 验证手机号是否存在
if
(
$login_type
==
User
::
LOGIN_TYPE_PHARMACY
)
{
$pharmacy
=
PharmacyModel
::
query
()
->
where
(
'mobile'
,
$mobile
)
->
first
();
if
(
!
$pharmacy
)
{
return
$this
->
failed
(
'手机号不存在'
);
}
$pharmacy
->
user_id
=
$authInfo
->
id
;
$pharmacy
->
save
();
}
elseif
(
$login_type
==
User
::
LOGIN_TYPE_DOCTOR
)
{
$doctor
=
DoctorModel
::
query
()
->
where
(
'mobile'
,
$mobile
)
->
first
();
if
(
!
$doctor
)
{
return
$this
->
failed
(
'手机号不存在'
);
}
$doctor
->
user_id
=
$authInfo
->
id
;
$doctor
->
save
();
}
else
{
return
$this
->
failed
(
'登录类型错误'
);
}
// 绑定角色
$user
=
User
::
query
()
->
find
(
$authInfo
->
id
);
$user
->
last_login_type
=
$login_type
;
if
(
$user
->
save
())
{
return
$this
->
success
(
'绑定成功'
);
}
else
{
return
$this
->
failed
(
'绑定失败'
);
}
}
/**
* Refresh a token.
* Refresh a token.
* 刷新token,如果开启黑名单,以前的token便会失效。
* 刷新token,如果开启黑名单,以前的token便会失效。
* 值得注意的是用上面的getToken再获取一次Token并不算做刷新,两次获得的Token是并行的,即两个都可用。
* 值得注意的是用上面的getToken再获取一次Token并不算做刷新,两次获得的Token是并行的,即两个都可用。
...
...
routes/api.php
View file @
8812d3c6
...
@@ -34,6 +34,11 @@
...
@@ -34,6 +34,11 @@
Route
::
post
(
'logout'
,
'App\Api\Controllers\UserController@logout'
);
Route
::
post
(
'logout'
,
'App\Api\Controllers\UserController@logout'
);
// 刷新token
// 刷新token
Route
::
post
(
'refresh'
,
'App\Api\Controllers\UserController@refresh'
);
Route
::
post
(
'refresh'
,
'App\Api\Controllers\UserController@refresh'
);
# 获取发送验证码
Route
::
post
(
'/smscode'
,
'App\Api\Controllers\UserController@smsCode'
);
# 角色绑定
Route
::
post
(
'/role-bind'
,
'App\Api\Controllers\UserController@bindRole'
);
# 获取药品列表
# 获取药品列表
Route
::
get
(
'/drugs'
,
'App\Api\Controllers\DrugController@drugList'
);
Route
::
get
(
'/drugs'
,
'App\Api\Controllers\DrugController@drugList'
);
# Route::get('/test', 'App\Api\Controllers\DrugController@test');
# Route::get('/test', 'App\Api\Controllers\DrugController@test');
...
...
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