LoginService.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\api\servies\login;
  3. use app\api\response\ZskkErrorResponse;
  4. use app\api\servies\ZskkDefaultService;
  5. use app\api\utils\UUIDUtils;
  6. use app\api\validate\login\LoginValidate;
  7. use app\api\dao\login\LoginDao;
  8. use app\api\servies\common\CommonService;
  9. use think\facade\Log;
  10. use app\common\library\send_message;
  11. /**
  12. * 后台控制器基类
  13. * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
  14. */
  15. class LoginService extends ZskkDefaultService {
  16. protected $logName = "LoginService";
  17. private $loginDao = null;
  18. private $commonService = null;
  19. // protected function zskkInit(TestDao $testDao) {
  20. // $this->testDao;
  21. // }
  22. public function __construct(LoginDao $loginDao) {
  23. parent::__construct();
  24. $this->loginDao = $loginDao;
  25. }
  26. public function login($params)
  27. {
  28. $params['username'] = base64_decode($params['username']);
  29. if($params['username'] == '123')
  30. {
  31. if(!(isset($_SERVER['HTTP_ZSKK_INSTITUTION']) && $_SERVER['HTTP_ZSKK_INSTITUTION'] == '06300006'))
  32. {
  33. $this->throwError('账号或密码错误',1);
  34. }
  35. }
  36. // $params['password'] = base64_decode($params['password']);
  37. $key = 'zskk'.date('Ymd').'zskk';
  38. $password = openssl_decrypt($params['password'], 'AES-128-ECB', $key);
  39. $user = $this->loginDao->checkIsSet($params,$password);
  40. if($user['is_send_message'] == "1") {
  41. $check = $this->check_phone($user['phone']);
  42. if(!$check){
  43. $this->throwError('手机号为空或手机号格式错误','0912');
  44. }
  45. $phone = $user['phone'];
  46. $code = rand('1000','9999');
  47. $handle_id = $phone.rand('10000000','99999999');
  48. $this->loginDao->setCache('sendcode_'.$phone,$code,600);
  49. $this->loginDao->setCache('sendcode_handle_id'.$handle_id,$phone,600);
  50. $info = send_message::sendSms2UpDate($phone,$code);
  51. log::record("login() set code => {handle_id: $handle_id \t code: $code \t phone: $phone \t}");
  52. $data = ['need_code'=> true, 'phone' => $check, 'handle_id' => $handle_id];
  53. return $data;
  54. }
  55. $token = $this->loginDao->saveCache($user);
  56. // 存储登录信息
  57. // $this->loginDao->saveLoginInfo($user['id']);
  58. $institution = $this->loginDao->getInsInfo($user['institution_id']);
  59. $data = [
  60. 'token' => $token,
  61. 'realname' => $user['realname'],
  62. 'is_admin' => $user['is_admin'],
  63. 'username' => $user['username'],
  64. 'institution' => $user['institution'],
  65. 'institution_id'=> $user['institution_id'],
  66. 'report_full'=> $user['report_full'],
  67. 'is_new_browser'=> $institution['is_new_browser'],
  68. 'department_name' => $user['department'],
  69. 'role'=> $user['doctor_role'],
  70. 'message_push' => $user['message_push'],
  71. 'need_code'=> false,
  72. 'user_id' =>$user['id'],
  73. 'is_auto_mode' =>$institution['is_auto_mode'],
  74. 'remote' =>empty($institution['parent_institution']) ? '0' : '1',
  75. ];
  76. log::record('当前登陆的医生id为:'.$user['id'].',登陆token为:'.$token);
  77. return $data;
  78. }
  79. public function send_message_again($param)
  80. {
  81. // 获取 handle_id
  82. if(!isset($param['handle_id']) || empty($param['handle_id'])) {
  83. $this->throwError('系统错误 not find handle id',2001);
  84. }
  85. $handle_id = $param['handle_id'];
  86. // 获取 phone
  87. if(!$this->loginDao->getCache('sendcode_handle_id'.$handle_id)) {
  88. $this->throwError('操作过时,请重新登录',2002);
  89. }
  90. // 发送验证码并记录
  91. $phone = $this->loginDao->getCache('sendcode_handle_id'.$handle_id);
  92. $this->loginDao->delCache('sendcode_handle_id'.$handle_id);
  93. $code = rand('1000','9999');
  94. $info = send_message::sendSms2UpDate($phone,$code);
  95. $handle_id = $phone.rand('10000000','99999999');
  96. log::record('目前的手机号是'.$phone.'存储的缓存为sendcode_handle_id'.$handle_id);
  97. log::record('目前的验证码是'.$code.'存储的手机号为sendcode_'.$phone);
  98. $this->loginDao->setCache('sendcode_'.$phone, $code, 600);
  99. $this->loginDao->setCache('sendcode_handle_id'.$handle_id, $phone, 600);
  100. log::record("send_message_again() set code => {handle_id: $handle_id \t code: $code \t phone: $phone \t}");
  101. $data =['info'=>$info,'handle_id'=>$handle_id];
  102. return $data;
  103. }
  104. public function check_code($param)
  105. {
  106. if(!isset($param['handle_id']) || !isset($param['code'])) {
  107. $this->throwError('系统错误 not find handle id or code',2003);
  108. }
  109. $handle_id = $param['handle_id'];
  110. $code = $param['code'];
  111. if(empty($handle_id) || empty($code)) {
  112. $this->throwError('handle id or code is null or empty',2004);
  113. }
  114. // 获取 phone
  115. if(!$this->loginDao->getCache('sendcode_handle_id'.$handle_id)) {
  116. $this->throwError('操作过时,请重新登录',2002);
  117. }
  118. $phone = $this->loginDao->getCache('sendcode_handle_id'.$handle_id);
  119. if(!$this->loginDao->getCache('sendcode_'.$phone)) {
  120. $this->throwError('操作过时,请重新登录',2002);
  121. }
  122. $check_code = $this->loginDao->getCache('sendcode_'.$phone);
  123. if(!empty($check_code) && $code != $check_code) {
  124. log::record('当前的手机号是'.$phone.'存储的为sendcode_handle_id'.$handle_id);
  125. log::record('当前的验证码是'.$code);
  126. log::record('缓存的验证码是'.$check_code.'存储的为sendcode_'.$phone);
  127. $this->throwError('错误的验证码','0090');
  128. }
  129. log::record("check_code() params => {handle_id: $handle_id \t code: $code \t phone: $phone \t check_code: $check_code }");
  130. $sessionid = UUIDUtils::uuid();
  131. log::record($sessionid);
  132. $user = $this->loginDao->getDoctorByPhone($phone);
  133. $this->loginDao->setCache($sessionid,$user,43200);
  134. log::record('----登录信息----');
  135. log::record($this->loginDao->getCache($sessionid));
  136. log::record('----登录信息----');
  137. $institution = $this->loginDao->getInsInfo($user['institution_id']);
  138. unset($user['password']);
  139. $data = [
  140. 'token' => $sessionid,
  141. 'realname' => $user['realname'],
  142. 'is_admin' => $user['is_admin'],
  143. 'username' => $user['username'],
  144. 'institution' => $user['institution'],
  145. 'department_name' => $user['department'],
  146. 'role'=> $user['doctor_role'],
  147. 'message_push' => $user['message_push'],
  148. 'user_id' =>$user['id'],
  149. 'is_auto_mode' =>$institution['is_auto_mode'],
  150. 'remote' =>empty($institution['parent_institution']) ? '0' : '1',
  151. ];
  152. log::record('当前登陆的医生id为:'.$user['id'].',登陆token为:'.$sessionid);
  153. return $data;
  154. }
  155. public function check_phone($mobile)
  156. {
  157. if(empty($mobile)){
  158. return false;
  159. }
  160. if(strlen($mobile) != 11){
  161. return false;
  162. }
  163. $preg = preg_match('/^1[34578]\d{9}$/', $mobile);
  164. if(!$preg){
  165. return false;
  166. }
  167. $start = substr($mobile,0,3);
  168. $end = substr($mobile,7,4);
  169. $phone = $start.'****'.$end;
  170. return $phone;
  171. }
  172. public function logout($token)
  173. {
  174. $data = $this->loginDao->logout($token);
  175. return $data;
  176. }
  177. public function out($session)
  178. {
  179. $data = $this->loginDao->out($session);
  180. return $data;
  181. }
  182. }