LoginLogic.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\logic;
  20. use app\common\basics\Logic;
  21. use app\common\model\shop\ShopAdmin;
  22. use app\common\model\ShopSession;
  23. use app\common\model\ShopSession as SessionModel;
  24. use app\common\server\UrlServer;
  25. use think\facade\Config;
  26. use think\facade\Cache;
  27. /**
  28. * 商家移动端管理员登录逻辑
  29. * Class LoginLogic
  30. * @package app\shopapi\logic
  31. */
  32. class LoginLogic extends Logic
  33. {
  34. /**
  35. * @notes 账号密码登录
  36. * @param $params
  37. * @return mixed
  38. * @throws \think\Exception
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @author 段誉
  42. * @date 2021/11/9 16:37
  43. */
  44. public static function accountLogin($params)
  45. {
  46. $adminModel = new ShopAdmin();
  47. $admin = $adminModel->alias('a')
  48. ->join('shop s', 's.id = a.shop_id')
  49. ->field(['a.id', 'a.account', 'a.name', 'role_id', 'shop_id', 's.name' => 'shop_name', 's.logo' => 'shop_logo'])
  50. ->where(['a.account' => $params['account'], 'a.del' => 0])
  51. ->findOrEmpty()->toArray();
  52. $admin['shop_logo'] = UrlServer::getFileUrl($admin['shop_logo']);
  53. $admin['token'] = self::createSession($admin['id'], $params['client']);
  54. //登录信息更新
  55. $adminModel->where(['account' => $params['account']])->update([
  56. 'login_ip' => request()->ip(),
  57. 'login_time' => time()
  58. ]);
  59. return $admin;
  60. }
  61. /**
  62. * @notes 退出登录
  63. * @param $user_id
  64. * @param $client
  65. * @return SessionModel
  66. * @author 段誉
  67. * @date 2021/11/9 16:37
  68. */
  69. public static function logout($user_id, $client)
  70. {
  71. $time = time();
  72. $token = (new ShopSession())
  73. ->where(['admin_id' => $user_id, 'client' => $client])
  74. ->value('token');
  75. Cache::delete($token);
  76. return (new ShopSession())
  77. ->where(['admin_id' => $user_id, 'client' => $client])
  78. ->update(['update_time' => $time, 'expire_time' => $time]);
  79. }
  80. /**
  81. * @notes 创建会话
  82. * @param $admin_id
  83. * @param $client
  84. * @return string
  85. * @author 段誉
  86. * @date 2021/11/9 16:38
  87. */
  88. public static function createSession($admin_id, $client)
  89. {
  90. //清除之前缓存
  91. $token = SessionModel::where(['admin_id' => $admin_id, 'client' => $client])
  92. ->value('token');
  93. if ($token) {
  94. Cache::delete($token);
  95. }
  96. $result = SessionModel::where(['admin_id' => $admin_id, 'client' => $client])
  97. ->findOrEmpty();
  98. $time = time();
  99. $expire_time = $time + Config::get('project.token_expire_time');
  100. // 新token
  101. $token = md5($admin_id . $client . $time);
  102. $shop_amdin = ShopAdmin::where(['id' => $admin_id])->findOrEmpty();
  103. $data = [
  104. 'shop_id' => $shop_amdin['shop_id'],
  105. 'admin_id' => $admin_id,
  106. 'token' => $token,
  107. 'client' => $client,
  108. 'update_time' => $time,
  109. 'expire_time' => $expire_time,
  110. ];
  111. if ($result->isEmpty()) {
  112. SessionModel::create($data);
  113. } else {
  114. SessionModel::where(['admin_id' => $admin_id, 'client' => $client])
  115. ->update($data);
  116. }
  117. //更新登录信息
  118. $login_ip = request()->ip();
  119. ShopAdmin::where(['id' => $admin_id])
  120. ->update(['login_time' => $time, 'login_ip' => $login_ip]);
  121. // 获取最新的用户信息
  122. $admin_info = ShopAdmin::alias('a')
  123. ->join('shop_session s', 'a.id=s.admin_id')
  124. ->where(['s.token' => $token])
  125. ->field('a.*,s.token,s.client')
  126. ->find();
  127. $admin_info = $admin_info ? $admin_info->toArray() : [];
  128. //创建新的缓存
  129. $ttl = 0 + Config::get('project.token_expire_time');
  130. Cache::set($token, $admin_info, $ttl);
  131. return $token;
  132. }
  133. }