LoginService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. //免登录接口
  90. public function loginNoPass($username)
  91. {
  92. $user = $this->loginDao->getUserByUserName($username);
  93. if($user['is_send_message'] == "1") {
  94. $check = $this->check_phone($user['phone']);
  95. if(!$check){
  96. $this->throwError('手机号为空或手机号格式错误','0912');
  97. }
  98. $phone = $user['phone'];
  99. $code = rand('1000','9999');
  100. $handle_id = $phone.rand('10000000','99999999');
  101. $this->loginDao->setCache('sendcode_'.$phone,$code,600);
  102. $this->loginDao->setCache('sendcode_handle_id'.$handle_id,$phone,600);
  103. $info = send_message::sendSms2UpDate($phone,$code);
  104. log::record("loginNoPass() set code => {handle_id: $handle_id \t code: $code \t phone: $phone \t}");
  105. $data = ['need_code'=> true, 'phone' => $check, 'handle_id' => $handle_id];
  106. return $data;
  107. }
  108. $token = $this->loginDao->saveCache($user);
  109. // 存储登录信息
  110. $this->loginDao->saveLoginInfo($user['id']);
  111. $institution = $this->loginDao->getInsInfo($user['institution_id']);
  112. $otherIns = $this->loginDao->getOtherRules($user['id']);
  113. $insList = [];
  114. if(!empty($otherIns)) {
  115. $insList = $otherIns;
  116. }
  117. $insList[] = ['id'=>$user['institution_id'],'name'=>$user['institution']];
  118. $data = [
  119. 'token' => $token,
  120. 'realname' => $user['realname'],
  121. 'is_admin' => $user['is_admin'],
  122. 'username' => $user['username'],
  123. 'institution' => $user['institution'],
  124. 'institution_id'=> $user['institution_id'],
  125. 'report_full'=> $user['report_full'],
  126. 'is_new_browser'=> $institution['is_new_browser'],
  127. 'department_name' => $user['department'],
  128. 'role'=> $user['doctor_role'],
  129. 'message_push' => $user['message_push'],
  130. 'need_code'=> false,
  131. 'user_id' =>$user['id'],
  132. 'is_auto_mode' =>$institution['is_auto_mode'],
  133. 'remote' =>empty($institution['parent_institution']) ? '0' : '1',
  134. 'institution_list'=>$insList,
  135. 'message_voice'=>$user['message_voice'] ?? 0
  136. ];
  137. log::record('loginNoPass()当前登陆的医生id为:'.$user['id'].',登陆token为:'.$token);
  138. $arr = ['type'=>2,'doctor_id'=>$user['id'],'doctor_name'=>$user['realname'],'institution_id'=>$user['institution_id']];
  139. $this->loginDao->saveDoctorVisit($arr);
  140. return $data;
  141. }
  142. public function send_message_again($param)
  143. {
  144. // 获取 handle_id
  145. if(!isset($param['handle_id']) || empty($param['handle_id'])) {
  146. $this->throwError('系统错误 not find handle id',2001);
  147. }
  148. $handle_id = $param['handle_id'];
  149. // 获取 phone
  150. if(!$this->loginDao->getCache('sendcode_handle_id'.$handle_id)) {
  151. $this->throwError('操作过时,请重新登录',2002);
  152. }
  153. // 发送验证码并记录
  154. $phone = $this->loginDao->getCache('sendcode_handle_id'.$handle_id);
  155. $this->loginDao->delCache('sendcode_handle_id'.$handle_id);
  156. $code = rand('1000','9999');
  157. $info = send_message::sendSms2UpDate($phone,$code);
  158. $handle_id = $phone.rand('10000000','99999999');
  159. log::record('目前的手机号是'.$phone.'存储的缓存为sendcode_handle_id'.$handle_id);
  160. log::record('目前的验证码是'.$code.'存储的手机号为sendcode_'.$phone);
  161. $this->loginDao->setCache('sendcode_'.$phone, $code, 600);
  162. $this->loginDao->setCache('sendcode_handle_id'.$handle_id, $phone, 600);
  163. log::record("send_message_again() set code => {handle_id: $handle_id \t code: $code \t phone: $phone \t}");
  164. $data =['info'=>$info,'handle_id'=>$handle_id];
  165. return $data;
  166. }
  167. public function check_code($param)
  168. {
  169. if(!isset($param['handle_id']) || !isset($param['code'])) {
  170. $this->throwError('系统错误 not find handle id or code',2003);
  171. }
  172. $handle_id = $param['handle_id'];
  173. $code = $param['code'];
  174. if(empty($handle_id) || empty($code)) {
  175. $this->throwError('handle id or code is null or empty',2004);
  176. }
  177. // 获取 phone
  178. if(!$this->loginDao->getCache('sendcode_handle_id'.$handle_id)) {
  179. $this->throwError('操作过时,请重新登录',2002);
  180. }
  181. $phone = $this->loginDao->getCache('sendcode_handle_id'.$handle_id);
  182. if(!$this->loginDao->getCache('sendcode_'.$phone)) {
  183. $this->throwError('操作过时,请重新登录',2002);
  184. }
  185. $check_code = $this->loginDao->getCache('sendcode_'.$phone);
  186. if(!empty($check_code) && $code != $check_code) {
  187. log::record('当前的手机号是'.$phone.'存储的为sendcode_handle_id'.$handle_id);
  188. log::record('当前的验证码是'.$code);
  189. log::record('缓存的验证码是'.$check_code.'存储的为sendcode_'.$phone);
  190. $this->throwError('错误的验证码','0090');
  191. }
  192. log::record("check_code() params => {handle_id: $handle_id \t code: $code \t phone: $phone \t check_code: $check_code }");
  193. $sessionid = UUIDUtils::uuid();
  194. log::record($sessionid);
  195. $user = $this->loginDao->getDoctorByPhone($phone);
  196. $this->loginDao->setCache($sessionid,$user,43200);
  197. log::record('----登录信息----');
  198. log::record($this->loginDao->getCache($sessionid));
  199. log::record('----登录信息----');
  200. $institution = $this->loginDao->getInsInfo($user['institution_id']);
  201. unset($user['password']);
  202. $data = [
  203. 'token' => $sessionid,
  204. 'realname' => $user['realname'],
  205. 'is_admin' => $user['is_admin'],
  206. 'username' => $user['username'],
  207. 'institution' => $user['institution'],
  208. 'department_name' => $user['department'],
  209. 'role'=> $user['doctor_role'],
  210. 'message_push' => $user['message_push'],
  211. 'user_id' =>$user['id'],
  212. 'is_auto_mode' =>$institution['is_auto_mode'],
  213. 'remote' =>empty($institution['parent_institution']) ? '0' : '1',
  214. ];
  215. log::record('当前登陆的医生id为:'.$user['id'].',登陆token为:'.$sessionid);
  216. return $data;
  217. }
  218. public function check_phone($mobile)
  219. {
  220. if(empty($mobile)){
  221. return false;
  222. }
  223. if(strlen($mobile) != 11){
  224. return false;
  225. }
  226. $preg = preg_match('/^1[34578]\d{9}$/', $mobile);
  227. if(!$preg){
  228. return false;
  229. }
  230. $start = substr($mobile,0,3);
  231. $end = substr($mobile,7,4);
  232. $phone = $start.'****'.$end;
  233. return $phone;
  234. }
  235. public function logout($token)
  236. {
  237. $data = $this->loginDao->logout($token);
  238. return $data;
  239. }
  240. public function out($session)
  241. {
  242. $data = $this->loginDao->out($session);
  243. return $data;
  244. }
  245. }