|
|
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api;
|
|
|
|
|
|
use App\Models\Api\Personnel\Employee as EmployeeModel;
|
|
|
use App\Http\Requests\Api\Login as Request;
|
|
|
+use App\Facades\Servers\Sms\VerifyCode as Sms;
|
|
|
|
|
|
/**
|
|
|
* API登录控制器
|
|
|
@@ -54,12 +55,11 @@ class Login extends Api
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 登录方法 /manager/login/out
|
|
|
+ * 退出方法 /manager/login/out
|
|
|
* @author 唐远望
|
|
|
* @version 1.0
|
|
|
* @date 2025-12-09
|
|
|
- * @param string username 登录账号
|
|
|
- * @param string password 登录密码
|
|
|
+ * @param string authcode 用户令牌
|
|
|
*
|
|
|
*/
|
|
|
public function out(EmployeeModel $EmployeeModel)
|
|
|
@@ -110,4 +110,111 @@ class Login extends Api
|
|
|
// 表单令牌
|
|
|
return json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送验证码
|
|
|
+ * @author 唐远望
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2026-01-16
|
|
|
+ * @param string phone 手机号码
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public function send_code(Request $Request, EmployeeModel $EmployeeModel)
|
|
|
+ {
|
|
|
+ // 验证规则
|
|
|
+ $Request->scene('send_code')->validate();
|
|
|
+ // 接收数据
|
|
|
+ $mobile = request('phone', '');
|
|
|
+ if (!$mobile) return json_send(['code' => 'error', 'msg' => '请先填写手机号']);
|
|
|
+ // 获取数据
|
|
|
+ $session = session('loginSmsCode');
|
|
|
+ // 如果有数据,并且验证码创建的时间在一分钟之内
|
|
|
+ if ($session && time() - $session['create_time'] < 60) return json_send(['code' => 'error', 'msg' => '请稍后再试']);
|
|
|
+ // 查询用户
|
|
|
+ $admin = $EmployeeModel->query()->where('mobile', $mobile)->first(['status']);
|
|
|
+ if ($admin && $admin['status']) return json_send(['code' => 'error', 'msg' => '用户已被停用']);
|
|
|
+ $code = strval(rand(100000, 999999));
|
|
|
+ $result = Sms::sendCode($mobile, $code);
|
|
|
+ if (isset($result['error'])) return json_send(['code' => 'error', 'msg' => $result['error']]);
|
|
|
+ $session = ['code' => $code, 'mobile' => $mobile, 'create_time' => time()];
|
|
|
+ session(['loginSmsCode' => $session]);
|
|
|
+ return json_send(['code' => 'success', 'msg' => '发送成功', 'data' => $code]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 邮箱登录 /manager/login/email
|
|
|
+ * @author 唐远望
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2026-01-16
|
|
|
+ * @param string email 邮箱号码
|
|
|
+ * @param string password 登录密码
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public function email(Request $Request,EmployeeModel $EmployeeModel)
|
|
|
+ {
|
|
|
+ // 验证规则
|
|
|
+ $Request->scene('email')->validate();
|
|
|
+ // 接收数据
|
|
|
+ $email = $Request->input('email', '');
|
|
|
+ // 接收数据
|
|
|
+ $password = $Request->input('password', '');
|
|
|
+ // 查询用户
|
|
|
+ $admin = $EmployeeModel->where('email', $email)->first(['id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
+ // 用户不存在
|
|
|
+ if (!$admin) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
|
+ // 用户不存在
|
|
|
+ if ($admin['status']) return json_send(['code' => 'error', 'msg' => '该账号已停用']);
|
|
|
+ // 转数组
|
|
|
+ $admin = $admin->toArray();
|
|
|
+ // 比对密码
|
|
|
+ if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
|
|
|
+ // 登录
|
|
|
+ $accessToken = $EmployeeModel->Login($admin['uid'], 'api');
|
|
|
+ // 比对密码
|
|
|
+ if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
|
+ // 获取权限列表
|
|
|
+ $accessToken['username'] = $admin['name'];
|
|
|
+ // 表单令牌
|
|
|
+ return json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 手机验证码登录 /manager/login/mobile_code
|
|
|
+ * @author 唐远望
|
|
|
+ * @version 1.0
|
|
|
+ * @date 2026-01-16
|
|
|
+ * @param string mobile 手机号码
|
|
|
+ * @param string code 验证码
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public function mobile_code(Request $Request,EmployeeModel $EmployeeModel)
|
|
|
+ {
|
|
|
+ // 验证规则
|
|
|
+ $Request->scene('mobile_code')->validate();
|
|
|
+ // 接收数据
|
|
|
+ $phone = $Request->input('phone', '');
|
|
|
+ // 接收数据
|
|
|
+ $code = $Request->input('code', '');
|
|
|
+ // 获取数据
|
|
|
+ $session = session('loginSmsCode');
|
|
|
+ if (!$session) return json_send(['code' => 'error', 'msg' => '请先获取手机号验证码']);
|
|
|
+ if ($session['code'] != $code || $session['phone'] != $phone) return json_send(['code' => 'error', 'msg' => '验证码错误']);
|
|
|
+ // 查询用户
|
|
|
+ $admin = $EmployeeModel->where('mobile', $phone)->first(['id', 'name', 'mobile', 'status', 'password', 'insert_time', 'update_time']);
|
|
|
+ // 用户不存在
|
|
|
+ if (!$admin) return json_send(['code' => 'error', 'msg' => '账号不存在']);
|
|
|
+ // 用户不存在
|
|
|
+ if ($admin['status']) return json_send(['code' => 'error', 'msg' => '该账号已停用']);
|
|
|
+ // 转数组
|
|
|
+ $admin = $admin->toArray();
|
|
|
+ // 登录
|
|
|
+ $accessToken = $EmployeeModel->Login($admin['uid'],'api');
|
|
|
+ // 比对密码
|
|
|
+ if (isset($accessToken['error'])) return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
|
|
|
+ // 获取权限列表
|
|
|
+ $accessToken['username'] = $admin['name'];
|
|
|
+ // 表单令牌
|
|
|
+ return json_send(['code' => 'success', 'msg' => '登录成功', 'data' => $accessToken]);
|
|
|
+ }
|
|
|
}
|