chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\Api\Controller\V1;
|
||||
|
||||
use app\api\validate\Login as LoginValidate;
|
||||
use think\facade\Event;
|
||||
use think\facade\Validate;
|
||||
use ywxapp\controller\ApiController;
|
||||
use ywxapp\model\MemberUser as UserModel;
|
||||
use ywxapp\service\JwtService;
|
||||
|
||||
|
||||
class User extends ApiController
|
||||
{
|
||||
protected $noNeedLogin = ['*'];
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
public function initialize()
|
||||
{
|
||||
$this->model = new \ywxapp\model\Members();
|
||||
}
|
||||
|
||||
|
||||
public function index()
|
||||
{
|
||||
return json(['message' => 'This is version 1 of the API']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册会员.
|
||||
* @route put /register, method:put
|
||||
* @param string $username 用户名
|
||||
* @param string $password 密码
|
||||
* @param string $email 邮箱
|
||||
* @param string $mobile 手机号
|
||||
* @param string $code 验证码
|
||||
* @return \think\Response
|
||||
*/
|
||||
|
||||
public function register(\think\Request $request)
|
||||
{
|
||||
$data = $request->param();
|
||||
$validate = validate([
|
||||
'account|账户或手机号' => 'require',
|
||||
'password' => 'alphaDash',
|
||||
'code' => 'number|length:4',
|
||||
'captcha' => 'alphaNum',
|
||||
]);
|
||||
if (! $validate->check($data)) {
|
||||
$this->result->error($validate->getError());
|
||||
}
|
||||
|
||||
$account = $request->param('account');
|
||||
$password = $request->has('password') ? $request->param('password') : md5("xixingwl");
|
||||
$code = $request->param('code');
|
||||
if (Validate::is($account, 'email') && $request->has('code')) {
|
||||
$ret = \ywxapp\library\Sms::instence()->check($account, $code, 'register');
|
||||
if (! $ret) {
|
||||
$this->result->error('Code is incorrect');
|
||||
}
|
||||
|
||||
}
|
||||
if (Validate::is($account, 'mobile') && $request->has('code')) {
|
||||
$ret = \ywxapp\library\Sms::instence()->check($account, $code, 'register');
|
||||
if (! $ret) {
|
||||
$this->result->error('Code is incorrect');
|
||||
}
|
||||
|
||||
}
|
||||
$extend = [];
|
||||
if ($request->param('avatar')) {
|
||||
$extend['avatar'] = $request->param('avatar');
|
||||
}
|
||||
|
||||
if ($request->param('nickname')) {
|
||||
$extend['nickname'] = $request->param('nickname');
|
||||
}
|
||||
|
||||
$this->user->create($account, $password, $extend);
|
||||
$this->result->success(['userinfo' => $this->Member->info]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Member Login.
|
||||
*
|
||||
* @param string $account 账号
|
||||
* @param string $password 密码
|
||||
* @return \think\Response
|
||||
*/
|
||||
|
||||
public function login(\think\Request $request)
|
||||
{
|
||||
|
||||
$data = $this->request->param();
|
||||
try {
|
||||
validate(LoginValidate::class)->check($data);
|
||||
$info = UserModel::where('account', $data['username'])
|
||||
->whereOr('mobile', $data['username'])
|
||||
->whereOr('email', $data['username'])
|
||||
->findOrEmpty();
|
||||
if ($info->isEmpty()) {
|
||||
$this->result->error('用户不存在', 4010);
|
||||
}
|
||||
// 检查账户状态
|
||||
if ($info->status == 0) {
|
||||
$this->result->error('账号已被禁用', 403);
|
||||
}
|
||||
// 检查是否被锁定
|
||||
if ($info->isLocked()) {
|
||||
$lockTime = strtotime($info->lock_time) + 1800 - time();
|
||||
$minutes = ceil($lockTime / 60);
|
||||
$this->result->error("账号被锁定,请 {$minutes} 分钟后重试", 403);
|
||||
}
|
||||
$info->resetPassword($data['password']);
|
||||
// 验证密码
|
||||
if (! $info->checkPassword($data['password'])) {
|
||||
$info->recordLoginFail($this->request->ip()); // 记录失败
|
||||
$this->result->error('密码错误', 4011);
|
||||
}
|
||||
Event::trigger('MemberLog', [
|
||||
'uid' => $info->uid,
|
||||
'action' => 'login',
|
||||
'ip' => $this->request->ip(),
|
||||
'remark' => '用户注册',
|
||||
]);
|
||||
$newClaims = [
|
||||
'uid' => $info->uid,
|
||||
'account' => $info->account,
|
||||
];
|
||||
JwtService::instance()->createToken($newClaims);
|
||||
$this->result->success($info);
|
||||
} catch (ValidateException $e) {
|
||||
$this->result->error($e->getMessage(), 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user detail
|
||||
* @route get /detail, method:get
|
||||
* @param int $uid
|
||||
* @return \think\Response
|
||||
*/
|
||||
|
||||
public function detail(\think\Request $request, int $uid = 0)
|
||||
{
|
||||
if (! $uid) {
|
||||
$this->result->error('Invalid parameters', 404);
|
||||
}
|
||||
|
||||
$this->result->success($this->user->info);
|
||||
}
|
||||
|
||||
/**
|
||||
* update user detail
|
||||
* @route put /update, method:get
|
||||
* @param int $uid
|
||||
* @return \think\Response
|
||||
*/
|
||||
|
||||
public function update(\think\Request $request, int $uid = 0)
|
||||
{
|
||||
if (! $uid) {
|
||||
$this->result->error('Invalid parameters', 404);
|
||||
}
|
||||
|
||||
$this->result->success($this->user->info);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE user detail
|
||||
* @route DELETE /update, method:DELETE
|
||||
* @param int $uid
|
||||
* @return \think\Response
|
||||
*/
|
||||
|
||||
public function delete(\think\Request $request, int $uid = 0)
|
||||
{
|
||||
if (! $uid) {
|
||||
$this->result->error('Invalid parameters', 404);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function getEncryptPassword($password, $salt = '')
|
||||
{
|
||||
return md5(md5($password) . $salt);
|
||||
}
|
||||
|
||||
|
||||
public function init()
|
||||
{
|
||||
return json(['message' => 'This is version 1 of the API']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user