diff --git a/niucloud/app/api/controller/apiController/Personnel.php b/niucloud/app/api/controller/apiController/Personnel.php new file mode 100644 index 00000000..add513ac --- /dev/null +++ b/niucloud/app/api/controller/apiController/Personnel.php @@ -0,0 +1,155 @@ +$this->member_id, + ]; + $res = (new PersonnelService())->info($where); + if(!$res){ + return fail('账户信息有误'); + } + return success($result); + } + + /** + * 登录 + * @return Response + */ + public function login() + { + $data = $this->request->params([ + ['username', ''], + ['password', ''], + ]); + //校验登录注册配置 + (new ConfigService())->checkLoginConfig(MemberLoginTypeDict::USERNAME); + //参数验证 + //验证码验证 + $result = (new LoginService())->account($data['username'], $data['password']); + if (!$result) { + //账号密码错误, 重置验证码 + return fail('ACCOUNT_OR_PASSWORD_ERROR'); + } + return success($result); + } + + /** + * 登出 + * @return Response + */ + public function logout() + { + (new LoginService)->logout(); + return success('MEMBER_LOGOUT'); + } + + /** + * 创建验证码 + * @return Response + */ + public function captcha() + { + return success((new CaptchaService())->create()); + } + + /** + * 发送手机验证码 + * @param $type + * @return Response + * @throws Exception + */ + public function sendMobileCode($type) + { + $data = $this->request->params([ + ['mobile', ''], + ]); + return success((new LoginService())->sendMobileCode($data['mobile'], $type)); + } + + /** + * 手机号登录 + * @return Response + */ + public function mobile() + { + $data = $this->request->params([ + ['mobile', ''], + ['nickname', ''], + ['headimg', ''], + ['mobile', ''] + ]); + //校验登录注册配置 + (new ConfigService())->checkLoginConfig(MemberLoginTypeDict::MOBILE); + return success((new LoginService())->mobile($data)); + } + + /** + * 重置密码 + * @return Response + */ + public function resetPassword() + { + $data = $this->request->params([ + ['mobile', ''], + ['password', ''] + ]); + //参数验证 + $this->validate($data, 'app\validate\member\Member.reset_password'); + (new LoginService())->resetPassword($data['mobile'], $data['password']); + return success('PASSWORD_RESET_SUCCESS'); + } + + //销售教师人员登陆 + public function personnelLogin() + { + $data = $this->request->params([ + ['phone', ''], + ['password', ''], + ['login_type', ''],//登陆类型|1=教练,2=销售 + ]); + //验证码验证 + $result = (new LoginService())->loginByPersonnel($data); + + if(!$result['user_type']){ + if($data['login_type'] == 1){ + $msg = '暂无教练权限'; + }else{ + $msg = '暂无销售权限'; + } + return fail($msg);//code|0错误 + } + + return success($result);//code|1正确 + } +} diff --git a/niucloud/app/api/controller/login/Login.php b/niucloud/app/api/controller/login/Login.php index a454d941..4a5b2b83 100644 --- a/niucloud/app/api/controller/login/Login.php +++ b/niucloud/app/api/controller/login/Login.php @@ -109,14 +109,26 @@ class Login extends BaseController return success('PASSWORD_RESET_SUCCESS'); } + //销售教师人员登陆 public function personnelLogin() { $data = $this->request->params([ ['phone', ''], ['password', ''], + ['login_type', ''],//登陆类型|1=教练,2=销售 ]); //验证码验证 $result = (new LoginService())->loginByPersonnel($data); - return success($result); + + if(!$result['user_type']){ + if($data['login_type'] == 1){ + $msg = '暂无教练权限'; + }else{ + $msg = '暂无销售权限'; + } + return fail($msg);//code|0错误 + } + + return success($result);//code|1正确 } } diff --git a/niucloud/app/api/middleware/ApiPersonnelCheckToken.php b/niucloud/app/api/middleware/ApiPersonnelCheckToken.php new file mode 100644 index 00000000..dd8ee824 --- /dev/null +++ b/niucloud/app/api/middleware/ApiPersonnelCheckToken.php @@ -0,0 +1,58 @@ +appType(AppTypeDict::PERSONNEL); + // 校验渠道 + ( new AuthService() )->checkChannel($request); + //通过配置来设置系统header参数 + try { + $token = $request->apiToken(); + $token_info = ( new LoginService() )->parsePersonnelToken($token); + if (!empty($token_info)) { + $request->memberId($token_info[ 'member_id' ]); + } + //校验会员和站点 + $a= ( new AuthService() )->checkPersonnelMember($request); + } catch (AuthException $e) { + //是否将登录错误抛出 + if ($is_throw_exception) + return fail($e->getMessage(), [], $e->getCode()); + } + return $next($request); + } +} diff --git a/niucloud/app/api/route/route.php b/niucloud/app/api/route/route.php index 217081ae..c8fcfbde 100644 --- a/niucloud/app/api/route/route.php +++ b/niucloud/app/api/route/route.php @@ -11,6 +11,7 @@ use app\api\middleware\ApiChannel; use app\api\middleware\ApiCheckToken; +use app\api\middleware\ApiPersonnelCheckToken; use app\api\middleware\ApiLog; use app\api\route\dispatch\BindDispatch; use core\dict\DictLoader; @@ -74,8 +75,7 @@ Route::group(function() { //登录 Route::get('login', 'login.Login/login'); - //员工登录 - Route::post('personnelLogin', 'login.Login/personnelLogin'); + //第三方绑定 Route::post('bind', BindDispatch::class); @@ -162,5 +162,63 @@ Route::group(function() { })->middleware(ApiChannel::class) ->middleware(ApiCheckToken::class, true) ->middleware(ApiLog::class); + + + + + + + +//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓-----员工端相关-----↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ +//无需token验证的 +Route::group(function() { + //员工登录 + Route::post('personnelLogin', 'login.Login/personnelLogin'); +})->middleware(ApiChannel::class) + ->middleware(ApiPersonnelCheckToken::class) + ->middleware(ApiLog::class); + + +//需要token验证的 +Route::group(function() { + //员工端详情 + Route::get('personnel/info', 'apiController.Personnel/info'); + +})->middleware(ApiChannel::class) + ->middleware(ApiPersonnelCheckToken::class, true) + ->middleware(ApiLog::class); +//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑-----员工端相关-----↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ + + + + + + +//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓-----用户端相关-----↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ +//无需token验证的 +Route::group(function() { + //员工登录 + Route::post('personnelLogin', 'login.Login/personnelLogin'); +})->middleware(ApiChannel::class) + ->middleware(ApiCheckToken::class) + ->middleware(ApiLog::class); + + +//需要token验证的 +Route::group(function() { + + Route::get('personnel/info', 'apiController.Personnel/info'); + +})->middleware(ApiChannel::class) + ->middleware(ApiCheckToken::class, true) + ->middleware(ApiLog::class); +//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑-----用户端相关-----↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ + + + + + + + //加载插件路由 ( new DictLoader("Route") )->load([ 'app_type' => 'api' ]); diff --git a/niucloud/app/service/api/apiService/PersonnelService.php b/niucloud/app/service/api/apiService/PersonnelService.php new file mode 100644 index 00000000..659cc20f --- /dev/null +++ b/niucloud/app/service/api/apiService/PersonnelService.php @@ -0,0 +1,91 @@ +model = new Personnel(); + } + + //获取员工信息 + public function info(array $where,string $field = '*'){ + $model = $this->model; + if(!empty($where['id'])){ + $model = $model->where('id',$where['id']); + } + $res = $model->field($field)->find();//员工信息 + + //查询部门信息 + $campus_person_role = CampusPersonRole::where('person_id',$where['id'])->select()->toArray(); + $role_id = array_column($campus_person_role, 'role_id'); + $dept_id = array_column($campus_person_role, 'dept_id'); + + + if($res){ + $res = $res->toArray(); + }else{ + $res = []; + return $res; + } + + } + + + /** + * 获取会员的模型对象(todo 慎用!!! 现主要用于登录) + * @param array $data + * @return Member|array|mixed|Model !!! 仔细看,返回值是模型对象 如果想要判断是否为空 请用 $member->isEmpty() + */ + public function findMemberInfo(array $data){ + //会员账号 + if(!empty($data['username'])) + $where[] = ['username', '=', $data['username']]; + //会员手机号 + if(!empty($data['mobile'])) + $where[] = ['mobile', '=', $data['mobile']]; + //会员id + if(!empty($data['id'])) + $where[] = ['id', '=', $data['id']]; + //微信公众号openid + if(!empty($data['wx_openid'])) + $where[] = ['wx_openid', '=', $data['wx_openid']]; + //微信小程序openid + if(!empty($data['weapp_openid'])) + $where[] = ['weapp_openid', '=', $data['weapp_openid']]; + // 微信unionid + if(!empty($data['wx_unionid'])) + $where[] = ['wx_unionid', '=', $data['wx_unionid']]; + if(!empty($data['username|mobile'])) + $where[] = ['username|mobile', '=', $data['username|mobile']]; + if(empty($where)){ + $where[] = ['member_id', '=', -1]; + } + return $this->model->where($where)->findOrEmpty(); + } +} diff --git a/niucloud/app/service/api/login/AuthService.php b/niucloud/app/service/api/login/AuthService.php index 4dca4543..6f66ad4e 100644 --- a/niucloud/app/service/api/login/AuthService.php +++ b/niucloud/app/service/api/login/AuthService.php @@ -14,6 +14,7 @@ namespace app\service\api\login; use app\dict\common\ChannelDict; use app\model\member\Member; use app\Request; +use app\service\api\apiService\PersonnelService; use app\service\api\member\MemberService; use app\service\core\channel\CoreH5Service; use app\service\core\channel\CorePcService; @@ -49,6 +50,20 @@ class AuthService extends BaseApiService return true; } + //检测员工用户是否存在 + public function checkPersonnelMember(Request $request) + { + //如果登录信息非法就报错 + if ($this->member_id > 0) { + $member_service = new PersonnelService(); + $member_info = $member_service->findMemberInfo([ 'id' => $this->member_id ]); + if ($member_info->isEmpty()) { + throw new AuthException('MEMBER_NOT_EXIST', 401); + } + } + return true; + } + /** * 校验渠道 * @param Request $request diff --git a/niucloud/app/service/api/login/LoginService.php b/niucloud/app/service/api/login/LoginService.php index b556f2a0..141f1aca 100644 --- a/niucloud/app/service/api/login/LoginService.php +++ b/niucloud/app/service/api/login/LoginService.php @@ -15,6 +15,7 @@ use app\dict\member\MemberLoginTypeDict; use app\dict\member\MemberRegisterTypeDict; use app\dict\sys\AppTypeDict; use app\dict\sys\SmsDict; +use app\model\campus_person_role\CampusPersonRole; use app\model\member\Member; use app\model\personnel\Personnel; use app\model\sys\SysUser; @@ -163,7 +164,7 @@ class LoginService extends BaseApiService } /** - * 解析token + * 解析客户端token * @param string|null $token * @return array */ @@ -190,6 +191,34 @@ class LoginService extends BaseApiService return $token_info; } + /** + * 解析员工端token + * @param string|null $token + * @return array + */ + public function parsePersonnelToken(?string $token) + { + if (empty($token)) { + //定义专属于授权认证机制的错误响应, 定义专属语言包 + throw new AuthException('MUST_LOGIN', 401); + } + + try { + $token_info = TokenAuth::parseToken($token, AppTypeDict::PERSONNEL); + } catch (Throwable $e) { +// if(env('app_debug', false)){ +// throw new AuthException($e->getMessage(), 401); +// }else{ + throw new AuthException('LOGIN_EXPIRE', 401); +// } + } + if (!$token_info) { + throw new AuthException('MUST_LOGIN', 401); + } + //验证有效次数或过期时间 + return $token_info; + } + /** * 手机发送验证码 * @param $mobile @@ -326,6 +355,7 @@ class LoginService extends BaseApiService if ($member_info->status != 2) throw new ApiException('账号状态异常禁止登录'); $user = (new SysUser())->where('username', $params['phone'])->find(); + //create_password($params['password'])//创建密码 if (!check_password($params['password'], $user->password)) throw new ApiException('账号或密码错误'); $user->login_time = time(); $user->last_ip = $this->request->ip(); @@ -334,11 +364,26 @@ class LoginService extends BaseApiService $user->last_time = time(); $user->save(); $expire_time = env('system.api_token_expire_time') ?? 3600; - $token_info = TokenAuth::createToken($member_info->id, AppTypeDict::PERSONNEL, ['id' => $member_info->id, 'phone' => $member_info->phone], $expire_time);; + //生成token + $token_info = TokenAuth::createToken($member_info->id, AppTypeDict::PERSONNEL, ['id' => $member_info->id, 'member_id'=>$member_info->id, 'phone' => $member_info->phone, 'user_type' => $params['login_type']], $expire_time); event("PersonnelLogin", $member_info); + + $role = CampusPersonRole::where('person_id',$member_info['id'])->column('role_id'); + + //login_type|1=教练,2=销售 + $user_type = '';//1=教练,2=销售 + if(in_array(2,$role) && $params['login_type'] == 2){ + //销售登陆 + $user_type = 2; + }elseif(in_array(3,$role) && $params['login_type'] == 1){ + //教师登陆 + $user_type = 1; + } + return [ - 'token' => $token_info['token'], - 'expires_time' => $token_info['params']['exp'] + 'token' => $token_info['token'],//token + 'expires_time' => $token_info['params']['exp'],//过期时间 + 'user_type' => $user_type//用户类型 ]; } }