LoginService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $otherIns = $this->loginDao->getOtherRules($user['id']);
  60. $insList = [];
  61. if(!empty($otherIns)) {
  62. $insList = $otherIns;
  63. }
  64. $insList[] = ['id'=>$user['institution_id'],'name'=>$user['institution']];
  65. $data = [
  66. 'token' => $token,
  67. 'realname' => $user['realname'],
  68. 'is_admin' => $user['is_admin'],
  69. 'username' => $user['username'],
  70. 'institution' => $user['institution'],
  71. 'institution_id'=> $user['institution_id'],
  72. 'report_full'=> $user['report_full'],
  73. 'is_new_browser'=> $institution['is_new_browser'],
  74. 'department_name' => $user['department'],
  75. 'role'=> $user['doctor_role'],
  76. 'message_push' => $user['message_push'],
  77. 'need_code'=> false,
  78. 'user_id' =>$user['id'],
  79. 'is_auto_mode' =>$institution['is_auto_mode'],
  80. 'remote' =>empty($institution['parent_institution']) ? '0' : '1',
  81. 'institution_list'=>$insList,
  82. 'message_voice'=>$user['message_voice'] ?? 0
  83. ];
  84. log::record('当前登陆的医生id为:'.$user['id'].',登陆token为:'.$token);
  85. $arr = ['type'=>2,'doctor_id'=>$user['id'],'doctor_name'=>$user['realname'],'institution_id'=>$user['institution_id']];
  86. $this->loginDao->saveDoctorVisit($arr);
  87. return $data;
  88. }
  89. public function send_message_again($param)
  90. {
  91. // 获取 handle_id
  92. if(!isset($param['handle_id']) || empty($param['handle_id'])) {
  93. $this->throwError('系统错误 not find handle id',2001);
  94. }
  95. $handle_id = $param['handle_id'];
  96. // 获取 phone
  97. if(!$this->loginDao->getCache('sendcode_handle_id'.$handle_id)) {
  98. $this->throwError('操作过时,请重新登录',2002);
  99. }
  100. // 发送验证码并记录
  101. $phone = $this->loginDao->getCache('sendcode_handle_id'.$handle_id);
  102. $this->loginDao->delCache('sendcode_handle_id'.$handle_id);
  103. $code = rand('1000','9999');
  104. $info = send_message::sendSms2UpDate($phone,$code);
  105. $handle_id = $phone.rand('10000000','99999999');
  106. log::record('目前的手机号是'.$phone.'存储的缓存为sendcode_handle_id'.$handle_id);
  107. log::record('目前的验证码是'.$code.'存储的手机号为sendcode_'.$phone);
  108. $this->loginDao->setCache('sendcode_'.$phone, $code, 600);
  109. $this->loginDao->setCache('sendcode_handle_id'.$handle_id, $phone, 600);
  110. log::record("send_message_again() set code => {handle_id: $handle_id \t code: $code \t phone: $phone \t}");
  111. $data =['info'=>$info,'handle_id'=>$handle_id];
  112. return $data;
  113. }
  114. public function check_code($param)
  115. {
  116. if(!isset($param['handle_id']) || !isset($param['code'])) {
  117. $this->throwError('系统错误 not find handle id or code',2003);
  118. }
  119. $handle_id = $param['handle_id'];
  120. $code = $param['code'];
  121. if(empty($handle_id) || empty($code)) {
  122. $this->throwError('handle id or code is null or empty',2004);
  123. }
  124. // 获取 phone
  125. if(!$this->loginDao->getCache('sendcode_handle_id'.$handle_id)) {
  126. $this->throwError('操作过时,请重新登录',2002);
  127. }
  128. $phone = $this->loginDao->getCache('sendcode_handle_id'.$handle_id);
  129. if(!$this->loginDao->getCache('sendcode_'.$phone)) {
  130. $this->throwError('操作过时,请重新登录',2002);
  131. }
  132. $check_code = $this->loginDao->getCache('sendcode_'.$phone);
  133. if(!empty($check_code) && $code != $check_code) {
  134. log::record('当前的手机号是'.$phone.'存储的为sendcode_handle_id'.$handle_id);
  135. log::record('当前的验证码是'.$code);
  136. log::record('缓存的验证码是'.$check_code.'存储的为sendcode_'.$phone);
  137. $this->throwError('错误的验证码','0090');
  138. }
  139. log::record("check_code() params => {handle_id: $handle_id \t code: $code \t phone: $phone \t check_code: $check_code }");
  140. $sessionid = UUIDUtils::uuid();
  141. log::record($sessionid);
  142. $user = $this->loginDao->getDoctorByPhone($phone);
  143. $this->loginDao->setCache($sessionid,$user,43200);
  144. log::record('----登录信息----');
  145. log::record($this->loginDao->getCache($sessionid));
  146. log::record('----登录信息----');
  147. $institution = $this->loginDao->getInsInfo($user['institution_id']);
  148. unset($user['password']);
  149. $data = [
  150. 'token' => $sessionid,
  151. 'realname' => $user['realname'],
  152. 'is_admin' => $user['is_admin'],
  153. 'username' => $user['username'],
  154. 'institution' => $user['institution'],
  155. 'department_name' => $user['department'],
  156. 'role'=> $user['doctor_role'],
  157. 'message_push' => $user['message_push'],
  158. 'user_id' =>$user['id'],
  159. 'is_auto_mode' =>$institution['is_auto_mode'],
  160. 'remote' =>empty($institution['parent_institution']) ? '0' : '1',
  161. ];
  162. log::record('当前登陆的医生id为:'.$user['id'].',登陆token为:'.$sessionid);
  163. return $data;
  164. }
  165. public function check_phone($mobile)
  166. {
  167. if(empty($mobile)){
  168. return false;
  169. }
  170. if(strlen($mobile) != 11){
  171. return false;
  172. }
  173. $preg = preg_match('/^1[34578]\d{9}$/', $mobile);
  174. if(!$preg){
  175. return false;
  176. }
  177. $start = substr($mobile,0,3);
  178. $end = substr($mobile,7,4);
  179. $phone = $start.'****'.$end;
  180. return $phone;
  181. }
  182. public function logout($token)
  183. {
  184. $data = $this->loginDao->logout($token);
  185. return $data;
  186. }
  187. public function out($session)
  188. {
  189. $data = $this->loginDao->out($session);
  190. return $data;
  191. }
  192. }