123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace app\api\servies\login;
- use app\api\response\ZskkErrorResponse;
- use app\api\servies\ZskkDefaultService;
- use app\api\utils\UUIDUtils;
- use app\api\validate\login\LoginValidate;
- use app\api\dao\login\LoginDao;
- use app\api\servies\common\CommonService;
- use think\facade\Log;
- use app\common\library\send_message;
- /**
- * 后台控制器基类
- * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
- */
- class LoginService extends ZskkDefaultService {
- protected $logName = "LoginService";
- private $loginDao = null;
- private $commonService = null;
- // protected function zskkInit(TestDao $testDao) {
- // $this->testDao;
- // }
- public function __construct(LoginDao $loginDao) {
- parent::__construct();
- $this->loginDao = $loginDao;
- }
- public function login($params)
- {
- $params['username'] = base64_decode($params['username']);
- if($params['username'] == '123')
- {
- if(!(isset($_SERVER['HTTP_ZSKK_INSTITUTION']) && $_SERVER['HTTP_ZSKK_INSTITUTION'] == '06300006'))
- {
- $this->throwError('账号或密码错误',1);
- }
- }
- // $params['password'] = base64_decode($params['password']);
- $key = 'zskk'.date('Ymd').'zskk';
- $password = openssl_decrypt($params['password'], 'AES-128-ECB', $key);
- $user = $this->loginDao->checkIsSet($params,$password);
- if($user['is_send_message'] == "1") {
- $check = $this->check_phone($user['phone']);
- if(!$check){
- $this->throwError('手机号为空或手机号格式错误','0912');
- }
- $phone = $user['phone'];
- $code = rand('1000','9999');
- $handle_id = $phone.rand('10000000','99999999');
- $this->loginDao->setCache('sendcode_'.$phone,$code,600);
- $this->loginDao->setCache('sendcode_handle_id'.$handle_id,$phone,600);
- $info = send_message::sendSms2UpDate($phone,$code);
- log::record("login() set code => {handle_id: $handle_id \t code: $code \t phone: $phone \t}");
- $data = ['need_code'=> true, 'phone' => $check, 'handle_id' => $handle_id];
- return $data;
- }
- $token = $this->loginDao->saveCache($user);
- // 存储登录信息
- $this->loginDao->saveLoginInfo($user['id']);
- $institution = $this->loginDao->getInsInfo($user['institution_id']);
- $otherIns = $this->loginDao->getOtherRules($user['id']);
- $insList = [];
- if(!empty($otherIns)) {
- $insList = $otherIns;
- }
- $insList[] = ['id'=>$user['institution_id'],'name'=>$user['institution']];
- $data = [
- 'token' => $token,
- 'realname' => $user['realname'],
- 'is_admin' => $user['is_admin'],
- 'username' => $user['username'],
- 'institution' => $user['institution'],
- 'institution_id'=> $user['institution_id'],
- 'report_full'=> $user['report_full'],
- 'is_new_browser'=> $institution['is_new_browser'],
- 'department_name' => $user['department'],
- 'role'=> $user['doctor_role'],
- 'message_push' => $user['message_push'],
- 'need_code'=> false,
- 'user_id' =>$user['id'],
- 'is_auto_mode' =>$institution['is_auto_mode'],
- 'remote' =>empty($institution['parent_institution']) ? '0' : '1',
- 'institution_list'=>$insList,
- 'message_voice'=>$user['message_voice'] ?? 0
- ];
- log::record('当前登陆的医生id为:'.$user['id'].',登陆token为:'.$token);
- $arr = ['type'=>2,'doctor_id'=>$user['id'],'doctor_name'=>$user['realname'],'institution_id'=>$user['institution_id']];
- $this->loginDao->saveDoctorVisit($arr);
- return $data;
- }
- public function send_message_again($param)
- {
- // 获取 handle_id
- if(!isset($param['handle_id']) || empty($param['handle_id'])) {
- $this->throwError('系统错误 not find handle id',2001);
- }
- $handle_id = $param['handle_id'];
- // 获取 phone
- if(!$this->loginDao->getCache('sendcode_handle_id'.$handle_id)) {
- $this->throwError('操作过时,请重新登录',2002);
- }
- // 发送验证码并记录
- $phone = $this->loginDao->getCache('sendcode_handle_id'.$handle_id);
- $this->loginDao->delCache('sendcode_handle_id'.$handle_id);
- $code = rand('1000','9999');
- $info = send_message::sendSms2UpDate($phone,$code);
- $handle_id = $phone.rand('10000000','99999999');
- log::record('目前的手机号是'.$phone.'存储的缓存为sendcode_handle_id'.$handle_id);
- log::record('目前的验证码是'.$code.'存储的手机号为sendcode_'.$phone);
- $this->loginDao->setCache('sendcode_'.$phone, $code, 600);
- $this->loginDao->setCache('sendcode_handle_id'.$handle_id, $phone, 600);
- log::record("send_message_again() set code => {handle_id: $handle_id \t code: $code \t phone: $phone \t}");
- $data =['info'=>$info,'handle_id'=>$handle_id];
- return $data;
- }
- public function check_code($param)
- {
- if(!isset($param['handle_id']) || !isset($param['code'])) {
- $this->throwError('系统错误 not find handle id or code',2003);
- }
- $handle_id = $param['handle_id'];
- $code = $param['code'];
- if(empty($handle_id) || empty($code)) {
- $this->throwError('handle id or code is null or empty',2004);
- }
- // 获取 phone
- if(!$this->loginDao->getCache('sendcode_handle_id'.$handle_id)) {
- $this->throwError('操作过时,请重新登录',2002);
- }
- $phone = $this->loginDao->getCache('sendcode_handle_id'.$handle_id);
- if(!$this->loginDao->getCache('sendcode_'.$phone)) {
- $this->throwError('操作过时,请重新登录',2002);
- }
- $check_code = $this->loginDao->getCache('sendcode_'.$phone);
- if(!empty($check_code) && $code != $check_code) {
- log::record('当前的手机号是'.$phone.'存储的为sendcode_handle_id'.$handle_id);
- log::record('当前的验证码是'.$code);
- log::record('缓存的验证码是'.$check_code.'存储的为sendcode_'.$phone);
- $this->throwError('错误的验证码','0090');
- }
- log::record("check_code() params => {handle_id: $handle_id \t code: $code \t phone: $phone \t check_code: $check_code }");
- $sessionid = UUIDUtils::uuid();
- log::record($sessionid);
- $user = $this->loginDao->getDoctorByPhone($phone);
- $this->loginDao->setCache($sessionid,$user,43200);
- log::record('----登录信息----');
- log::record($this->loginDao->getCache($sessionid));
- log::record('----登录信息----');
- $institution = $this->loginDao->getInsInfo($user['institution_id']);
- unset($user['password']);
- $data = [
- 'token' => $sessionid,
- 'realname' => $user['realname'],
- 'is_admin' => $user['is_admin'],
- 'username' => $user['username'],
- 'institution' => $user['institution'],
- 'department_name' => $user['department'],
- 'role'=> $user['doctor_role'],
- 'message_push' => $user['message_push'],
- 'user_id' =>$user['id'],
- 'is_auto_mode' =>$institution['is_auto_mode'],
- 'remote' =>empty($institution['parent_institution']) ? '0' : '1',
- ];
- log::record('当前登陆的医生id为:'.$user['id'].',登陆token为:'.$sessionid);
- return $data;
- }
- public function check_phone($mobile)
- {
- if(empty($mobile)){
- return false;
- }
- if(strlen($mobile) != 11){
- return false;
- }
- $preg = preg_match('/^1[34578]\d{9}$/', $mobile);
- if(!$preg){
- return false;
- }
- $start = substr($mobile,0,3);
- $end = substr($mobile,7,4);
- $phone = $start.'****'.$end;
- return $phone;
- }
- public function logout($token)
- {
- $data = $this->loginDao->logout($token);
- return $data;
- }
- public function out($session)
- {
- $data = $this->loginDao->out($session);
- return $data;
- }
- }
|