Token.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\common\model\base\user;
  3. /**
  4. * @title:
  5. * @Description:
  6. * @Author: goldenrock 112049337@qq.com
  7. * @Date: 2024-08-24 09:40:45
  8. * @LastEditTime: 2024-12-02 14:49:10
  9. * @LastEditors: goldenrock
  10. * @FilePath: \OA_hbdrwhe:\HBDRWHCODE\JindDongFang_platform\app\common\model\base\user\Token.php
  11. */
  12. /**
  13. * 用户TOKEN模型
  14. */
  15. use app\common\model\base\org\OrgRole;
  16. class Token
  17. {
  18. /**
  19. * @title 检查TOKEN有效性
  20. * @param {string} {token} {} {token字符串}
  21. */
  22. static public function checktoken($token = '')
  23. {
  24. $info = cache('TOKENUSER' . $token);
  25. return !empty($info);
  26. }
  27. /**
  28. * @title 更新TOKEN
  29. * @param{int} {uid} {} {用户ID}
  30. * @param{string} {role_code} {登录角色码}
  31. * @param{int} {org_id} {登录单位}
  32. */
  33. static public function updatetoken($uid, $org_id, $role_code)
  34. {
  35. $timeout = time() + 86400;
  36. $where = [];
  37. $where[] = ['uid', '=', $uid];
  38. $uInfo = User::where('user_id', $uid)->find()->toArray();
  39. $role = OrgRole::where('org_id', $org_id)->where('code', $role_code)->with(['org'])->find();
  40. $role = $role ? $role->toArray() : null;
  41. $uInfo['role'] = $role;
  42. $uInfo['role_id'] = $role ? $role['role_id'] : 0;
  43. $uInfo['role_name'] = $role ? $role['name'] : '';
  44. $uInfo['role_code'] = $role_code;
  45. $uInfo['org_id'] = $org_id;
  46. $uInfo['org_name'] = $role ? $role['org']['name'] : '';
  47. unset($uInfo['org']);
  48. $token = self::createtoken(json_encode($uInfo));
  49. $info = [
  50. 'uid' => $uid,
  51. 'token' => $token,
  52. 'timeout' => $timeout,
  53. 'user' => $uInfo,
  54. 'user_id' => $uid
  55. ];
  56. cache('TOKENUSER' . $token, $info, 86400);
  57. return $token;
  58. }
  59. /**
  60. * token字符串不变的情况下,更新token对应缓存的用户信息
  61. * @desc 用于修改用户信息后,同时更新缓存中的用户信息
  62. */
  63. static public function updateTokenUser(string $token)
  64. {
  65. $oldUser = self::tokenUser($token);
  66. $uid = $oldUser['user_id'];
  67. $role_code = $oldUser['role_code'];
  68. $org_id = $oldUser['org_id'];
  69. $timeout = time() + 86400;
  70. $where = [];
  71. $where[] = ['uid', '=', $uid];
  72. $uInfo = User::where('user_id', $uid)->find()->toArray();
  73. $role = OrgRole::where('org_id', $org_id)->where('code', $role_code)->with(['org'])->find();
  74. $role = $role ? $role->toArray() : null;
  75. $uInfo['role'] = $role;
  76. $uInfo['role_id'] = $role ? $role['role_id'] : 0;
  77. $uInfo['role_name'] = $role ? $role['name'] : '';
  78. $uInfo['role_code'] = $role_code;
  79. $uInfo['org_id'] = $org_id;
  80. $uInfo['org_name'] = $role ? $role['org']['name'] : '';
  81. unset($uInfo['org']);
  82. $info = [
  83. 'uid' => $uid,
  84. 'token' => $token,
  85. 'timeout' => $timeout,
  86. 'user' => $uInfo,
  87. 'user_id' => $uid
  88. ];
  89. cache('TOKENUSER' . $token, $info, 86400);
  90. }
  91. static public function getAdminTokenUser($uid)
  92. {
  93. $timeout = time() + 86400;
  94. $uInfo = User::where('user_id', $uid)->find();
  95. $uInfo = $uInfo ? $uInfo->toArray() : null;
  96. if(!$uInfo) return null;
  97. $token = self::createtoken(json_encode($uInfo));
  98. $info = [
  99. 'uid' => $uInfo['user_id'],
  100. 'token' => $token,
  101. 'timeout' => $timeout,
  102. 'user' => $uInfo,
  103. 'user_id' => $uid
  104. ];
  105. cache('TOKENUSER' . $token, $info, 86400);
  106. return $token;
  107. }
  108. /**
  109. * 使token失效
  110. * @param {string} {token} {token字符串}
  111. * @desc 常用于退出登录时
  112. */
  113. static public function losetoken($token)
  114. {
  115. cache('TOKENUSER' . $token, null);
  116. }
  117. /**
  118. * 生成TOKEN
  119. */
  120. static private function createtoken($string)
  121. {
  122. return md5($string . time() . rand(10000000, 99999999));
  123. }
  124. /**
  125. * 获取token用户
  126. * @param {string} {token} {token字符串}
  127. * 用于获取当前登录用户的信息
  128. */
  129. static public function tokenUser(string $token)
  130. {
  131. try {
  132. $tokenUser = cache('TOKENUSER' . $token);
  133. $user = $tokenUser['user'];
  134. return $user;
  135. } catch (\Exception $e) {
  136. WLog('GETTOKENUSER', $e->getFile() . $e->getLine() . $e->getMessage() . ",TOKEN:$token");
  137. return null;
  138. }
  139. }
  140. }