AdminTokenService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\service;
  15. use app\common\cache\AdminTokenCache;
  16. use app\common\model\auth\AdminSession;
  17. use app\common\model\auth\AdminRole;
  18. use think\facade\Config;
  19. /**
  20. * 管理员token
  21. * Class AdminTokenService
  22. * @package app\adminapi\service
  23. */
  24. class AdminTokenService
  25. {
  26. /**
  27. * @notes 设置或更新管理员token
  28. * @param $adminId //管理员id
  29. * @param $terminal //多终端名称
  30. * @param $multipointLogin //是否支持多处登录
  31. * @return false|mixed
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @author 令狐冲
  36. * @date 2021/7/2 20:25
  37. */
  38. public static function setToken($adminId, $terminal, $multipointLogin = 1)
  39. {
  40. $time = time();
  41. $adminSession = AdminSession::where([['admin_id', '=', $adminId], ['terminal', '=', $terminal]])->find();
  42. //获取token延长过期的时间
  43. $expireTime = $time + self::getExpireDuration($adminId);
  44. $adminTokenCache = new AdminTokenCache();
  45. //token处理
  46. if ($adminSession) {
  47. if ($adminSession->expire_time < $time || $multipointLogin === 0) {
  48. //清空缓存
  49. $adminTokenCache->deleteAdminInfo($adminSession->token);
  50. //如果token过期或账号设置不支持多处登录,更新token
  51. $adminSession->token = create_token($adminId);
  52. }
  53. $adminSession->expire_time = $expireTime;
  54. $adminSession->update_time = $time;
  55. $adminSession->save();
  56. } else {
  57. //找不到在该终端的token记录,创建token记录
  58. $adminSession = AdminSession::create([
  59. 'admin_id' => $adminId,
  60. 'terminal' => $terminal,
  61. 'token' => create_token($adminId),
  62. 'expire_time' => $expireTime
  63. ]);
  64. }
  65. return $adminTokenCache->setAdminInfo($adminSession->token);
  66. }
  67. /**
  68. * @notes 延长token过期时间
  69. * @param $token
  70. * @return array|false|mixed
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @author 令狐冲
  75. * @date 2021/7/5 14:25
  76. */
  77. public static function overtimeToken($token)
  78. {
  79. $time = time();
  80. $adminSession = AdminSession::where('token', '=', $token)->findOrEmpty();
  81. if ($adminSession->isEmpty()) {
  82. return false;
  83. }
  84. //延长token过期时间
  85. $adminSession->expire_time = $time + self::getExpireDuration($adminSession->admin_id);
  86. $adminSession->update_time = $time;
  87. $adminSession->save();
  88. return (new AdminTokenCache())->setAdminInfo($adminSession->token);
  89. }
  90. public static function getExpireDuration($adminId)
  91. {
  92. $roolName = AdminRole::alias('a')
  93. ->join('system_role b', 'a.role_id = b.id')
  94. ->where('a.admin_id', $adminId)
  95. ->value('b.name');
  96. if (strpos($roolName, '客服') !== false || strpos($roolName, '派单') !== false) {
  97. return Config::get('project.kefu_admin_token.expire_duration');
  98. } else {
  99. return Config::get('project.admin_token.expire_duration');
  100. }
  101. }
  102. /**
  103. * @notes 设置token为过期
  104. * @param $token
  105. * @return bool
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. * @author 令狐冲
  110. * @date 2021/7/5 14:31
  111. */
  112. public static function expireToken($token)
  113. {
  114. $adminSession = AdminSession::where('token', '=', $token)
  115. ->with('admin')
  116. ->findOrEmpty();
  117. if ($adminSession->isEmpty()) {
  118. return false;
  119. }
  120. //当支持多处登录的时候,服务端不注销
  121. if ($adminSession->admin->multipoint_login === 1) {
  122. return false;
  123. }
  124. $time = time();
  125. $adminSession->expire_time = $time;
  126. $adminSession->update_time = $time;
  127. $adminSession->save();
  128. return (new AdminTokenCache())->deleteAdminInfo($token);
  129. }
  130. }