Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
projecttwo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangtao
projecttwo
Commits
69bc4795
Commit
69bc4795
authored
Apr 24, 2025
by
wangzhengwen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
user
parent
49507ac1
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
369 additions
and
0 deletions
+369
-0
User.php
app/api/controller/User.php
+145
-0
Sms.php
app/api/service/Sms.php
+15
-0
TokenService.php
app/api/service/TokenService.php
+107
-0
BaseValidate.php
app/api/validate/BaseValidate.php
+56
-0
UserValidate.php
app/api/validate/UserValidate.php
+34
-0
User.php
app/model/project/User.php
+12
-0
No files found.
app/api/controller/User.php
0 → 100644
View file @
69bc4795
<?php
namespace
app\api\controller
;
use
app\api\service\TokenService
;
use
app\api\validate\UserValidate
;
use
app\BaseController
;
use
app\Request
;
use
app\model\project\User
as
userModel
;
class
User
extends
BaseController
{
public
function
login
(
Request
$request
)
{
$vo
=
(
new
UserValidate
())
->
goCheck
([
'mobile'
,
'password'
]);
if
(
$vo
!==
true
)
{
return
$vo
;
}
$data
=
$request
->
param
();
$user
=
userModel
::
where
([
'mobile'
=>
$data
[
'mobile'
]])
->
find
();
if
(
!
$user
)
{
return
$this
->
returnMsg
(
'用户不存在'
,
0
);
}
$data
[
'password'
]
=
md5
(
$data
[
'password'
]
.
$user
[
'salt'
]);
if
(
$data
[
'password'
]
!=
$user
[
'password'
])
{
return
$this
->
returnMsg
(
'密码不正确'
,
0
);
}
$token
=
TokenService
::
generateToken
(
$user
[
'id'
]);
$update
[
'token'
]
=
$token
;
$update
[
'last_login_time'
]
=
time
();
userModel
::
where
([
'id'
=>
$user
[
'id'
]])
->
update
(
$update
);
return
$this
->
returnMsg
(
'操作成功'
,
1
,[
'token'
=>
$token
,
'expires_in'
=>
TokenService
::
EXPIRE
]);
}
public
function
getUserInfo
(
Request
$request
)
{
$vo
=
(
new
UserValidate
())
->
goCheck
([
'token'
]);
if
(
$vo
!==
true
)
{
return
$vo
;
}
$data
=
$request
->
header
();
$is_expires
=
TokenService
::
verifyToken
(
$data
[
'token'
]);
if
(
$is_expires
===
false
)
{
return
$this
->
returnMsg
(
'token无效'
,
0
);
}
$user
=
userModel
::
where
([
'token'
=>
$data
[
'token'
]])
->
find
();
if
(
!
$user
)
{
return
$this
->
returnMsg
(
'token无效'
,
0
);
}
return
$this
->
returnMsg
(
'操作成功'
,
1
,
$user
);
}
public
function
register
(
Request
$request
)
{
$vo
=
(
new
UserValidate
())
->
goCheck
();
if
(
$vo
!==
true
)
{
return
$vo
;
}
$data
=
$request
->
param
();
$is_exit
=
userModel
::
where
([
'mobile'
=>
$data
[
'mobile'
]])
->
whereOr
([
'username'
=>
$data
[
'name'
]])
->
find
();
if
(
$is_exit
)
{
return
$this
->
returnMsg
(
'用户已存在'
,
0
);
}
$user
[
'username'
]
=
$data
[
'name'
];
$user
[
'mobile'
]
=
$data
[
'mobile'
];
$user
[
'salt'
]
=
random
(
4
);
$user
[
'password'
]
=
md5
(
$data
[
'password'
]
.
$user
[
'salt'
]);
$user
[
'reg_time'
]
=
time
();
$res
=
Usermodel
::
insert
(
$user
);
if
(
!
$res
)
{
return
$this
->
returnMsg
(
'注册失败'
,
0
);
}
return
$this
->
returnMsg
(
'注册成功'
,
1
);
}
public
function
restUserPasswordStep1
(
Request
$request
)
{
$vo
=
(
new
UserValidate
())
->
goCheck
([
'str'
,
'code'
]);
if
(
$vo
!==
true
)
{
return
$vo
;
}
$data
=
$request
->
param
();
$user
=
userModel
::
where
([
'mobile'
=>
$data
[
'str'
]])
->
whereOr
([
'username'
=>
$data
[
'str'
]])
->
find
();
if
(
!
$user
)
{
return
$this
->
returnMsg
(
'用户不存在'
,
0
);
}
//sms
return
$this
->
returnMsg
(
'success'
,
1
);
}
public
function
restUserPasswordStep2
(
Request
$request
)
{
$vo
=
(
new
UserValidate
())
->
goCheck
([
'str'
,
'confirm_password'
,
'password'
]);
if
(
$vo
!==
true
)
{
return
$vo
;
}
$data
=
$request
->
param
();
$user
=
userModel
::
where
([
'mobile'
=>
$data
[
'str'
]])
->
whereOr
([
'username'
=>
$data
[
'str'
]])
->
find
();
if
(
!
$user
)
{
return
$this
->
returnMsg
(
'用户不存在'
,
0
);
}
$update
[
'password'
]
=
md5
(
$data
[
'password'
]
.
$user
[
'salt'
]);
userModel
::
where
([
'id'
=>
$user
[
'id'
]])
->
update
(
$update
);
//销毁token
TokenService
::
deleteToken
(
$user
[
'token'
]);
return
$this
->
returnMsg
(
'success'
,
1
);
}
}
\ No newline at end of file
app/api/service/Sms.php
0 → 100644
View file @
69bc4795
<?php
namespace
app\api\service
;
use
app\BaseController
;
class
Sms
extends
BaseController
{
public
function
sendSms
()
{
return
true
;
}
}
\ No newline at end of file
app/api/service/TokenService.php
0 → 100644
View file @
69bc4795
<?php
namespace
app\api\service
;
use
think\facade\Cache
;
use
think\facade\Config
;
class
TokenService
{
// Token前缀
const
TOKEN_PREFIX
=
'user_token:'
;
// 过期时间(秒)
const
EXPIRE
=
7200
;
// 2小时
/**
* 生成用户Token
* @param int $userId 用户ID
* @return string Token字符串
*/
public
static
function
generateToken
(
$userId
)
{
// 生成随机Token
$token
=
self
::
createToken
();
// 存储Token信息
$cacheKey
=
self
::
TOKEN_PREFIX
.
$token
;
$tokenData
=
[
'user_id'
=>
$userId
,
'create_time'
=>
time
(),
'expire_time'
=>
time
()
+
self
::
EXPIRE
];
// 存入缓存
Cache
::
set
(
$cacheKey
,
$tokenData
,
self
::
EXPIRE
);
return
$token
;
}
/**
* 验证Token是否有效
* @param string $token Token字符串
* @return array|bool 成功返回Token数据,失败返回false
*/
public
static
function
verifyToken
(
$token
)
{
if
(
empty
(
$token
))
{
return
false
;
}
$cacheKey
=
self
::
TOKEN_PREFIX
.
$token
;
$tokenData
=
Cache
::
get
(
$cacheKey
);
if
(
empty
(
$tokenData
))
{
return
false
;
}
// 检查是否过期
if
(
$tokenData
[
'expire_time'
]
<
time
())
{
// 自动清除过期Token
Cache
::
delete
(
$cacheKey
);
return
false
;
}
return
$tokenData
;
}
/**
* 刷新Token过期时间
* @param string $token Token字符串
* @return bool 是否刷新成功
*/
public
static
function
refreshToken
(
$token
)
{
$cacheKey
=
self
::
TOKEN_PREFIX
.
$token
;
$tokenData
=
Cache
::
get
(
$cacheKey
);
if
(
empty
(
$tokenData
))
{
return
false
;
}
// 更新过期时间
$tokenData
[
'expire_time'
]
=
time
()
+
self
::
EXPIRE
;
Cache
::
set
(
$cacheKey
,
$tokenData
,
self
::
EXPIRE
);
return
true
;
}
/**
* 删除Token
* @param string $token Token字符串
* @return bool
*/
public
static
function
deleteToken
(
$token
)
{
$cacheKey
=
self
::
TOKEN_PREFIX
.
$token
;
return
Cache
::
delete
(
$cacheKey
);
}
/**
* 生成随机Token
* @return string
*/
protected
static
function
createToken
()
{
return
md5
(
uniqid
(
mt_rand
(),
true
));
}
}
\ No newline at end of file
app/api/validate/BaseValidate.php
0 → 100644
View file @
69bc4795
<?php
namespace
app\api\validate
;
use
think\exception\ValidateException
;
use
think\Validate
;
class
BaseValidate
extends
Validate
{
/**
* 验证数据
*/
public
function
goCheck
(
$fields
=
[],
$data
=
null
)
{
$data
=
$data
?:
request
()
->
param
();
$header
=
request
()
->
header
();
$data
=
array_merge
(
$data
,
$header
);
// 如果指定了字段,只验证这些字段
if
(
!
empty
(
$fields
))
{
$this
->
only
(
$fields
);
}
if
(
!
$this
->
check
(
$data
))
{
return
json
([
'msg'
=>
$this
->
getError
(),
'code'
=>
0
,
'data'
=>
[
'errors'
=>
$this
->
getError
()
]
]);
}
return
true
;
}
/**
* 验证ID是否为正整数
*/
protected
function
isPositiveInteger
(
$value
)
{
return
is_numeric
(
$value
)
&&
is_int
(
$value
+
0
)
&&
(
$value
+
0
)
>
0
;
}
/**
* 验证手机号
*/
protected
function
isMobile
(
$value
)
{
return
preg_match
(
'/^1[3-9]\d{9}$/'
,
$value
);
}
}
\ No newline at end of file
app/api/validate/UserValidate.php
0 → 100644
View file @
69bc4795
<?php
namespace
app\api\validate
;
use
think\Validate
;
class
UserValidate
extends
BaseValidate
{
protected
$rule
=
[
'name'
=>
'require'
,
'mobile'
=>
'require|mobile'
,
'password'
=>
'require'
,
'code'
=>
'require'
,
'token'
=>
'require'
,
'str'
=>
'require'
,
'confirm_password'
=>
'require|checkPasswordEqual'
,
];
protected
$message
=
[
'name.require'
=>
'名称必须'
,
'mobile.require'
=>
'手机号不能为空'
,
'mobile.mobile'
=>
'手机号格式不正确'
,
'password.require'
=>
'密码必须'
,
'code.require'
=>
'验证码必须'
,
'token.require'
=>
'token必须'
,
'str.require'
=>
'必填项不能为空'
,
'confirm_password.checkPasswordEqual'
=>
'两次输入的密码不一致'
];
protected
function
checkPasswordEqual
(
$value
,
$rule
,
$data
)
{
return
$value
===
$data
[
'password'
];
}
}
\ No newline at end of file
app/model/project/User.php
0 → 100644
View file @
69bc4795
<?php
namespace
app\model\project
;
use
think\Model
;
class
User
extends
Model
{
}
\ No newline at end of file
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