LoginLogic.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\kefuapi\logic;
  20. use app\common\basics\Logic;
  21. use app\common\enum\KefuEnum;
  22. use app\common\model\Admin;
  23. use app\common\model\kefu\KefuSession;
  24. use app\common\model\shop\ShopAdmin;
  25. use app\common\server\UrlServer;
  26. use think\facade\Config;
  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. if (KefuEnum::TYPE_SHOP == $params['type']) {
  47. $kefu = (new ShopAdmin())->alias('a')
  48. ->field(['k.id', 'k.nickname', 'k.avatar', 'k.shop_id', 'a.account'])
  49. ->join('kefu k', 'a.id = k.admin_id and a.shop_id = k.shop_id')
  50. ->where(['a.account' => $params['account'], 'k.del' => 0])
  51. ->findOrEmpty()->toArray();
  52. } else {
  53. $kefu = (new Admin())->alias('a')
  54. ->field(['k.id', 'k.nickname', 'k.avatar', 'k.shop_id', 'a.account'])
  55. ->join('kefu k', 'a.id = k.admin_id')
  56. ->where(['a.account' => $params['account'], 'k.shop_id' => 0, 'k.del' => 0])
  57. ->findOrEmpty()->toArray();
  58. }
  59. $kefu['avatar'] = !empty($kefu['avatar']) ? UrlServer::getFileUrl($kefu['avatar']) : "";
  60. $kefu['token'] = self::createSession($kefu['id'], $kefu['shop_id'], $params['client']);
  61. return $kefu;
  62. }
  63. /**
  64. * @notes 退出登录
  65. * @param $user_id
  66. * @param $client
  67. * @return KefuSession
  68. * @author 段誉
  69. * @date 2021/11/23 17:14
  70. */
  71. public static function logout($user_id, $client)
  72. {
  73. return (new KefuSession())
  74. ->where(['kefu_id' => $user_id, 'client' => $client])
  75. ->update(['update_time' => time(), 'expire_time' => time()]);
  76. }
  77. /**
  78. * @notes 创建会话
  79. * @param $admin_id
  80. * @param $client
  81. * @return string
  82. * @author 段誉
  83. * @date 2021/11/9 16:38
  84. */
  85. public static function createSession($kefu_id, $shop_id, $client)
  86. {
  87. $result = KefuSession::where(['kefu_id' => $kefu_id, 'client' => $client])->findOrEmpty();
  88. $time = time();
  89. $expire_time = $time + Config::get('project.token_expire_time');
  90. // 新token
  91. $token = md5($kefu_id . $client . $time);
  92. $data = [
  93. 'shop_id' => $shop_id,
  94. 'kefu_id' => $kefu_id,
  95. 'token' => $token,
  96. 'client' => $client,
  97. 'update_time' => $time,
  98. 'expire_time' => $expire_time,
  99. ];
  100. if ($result->isEmpty()) {
  101. KefuSession::create($data);
  102. } else {
  103. KefuSession::where([
  104. 'kefu_id' => $kefu_id,
  105. 'shop_id' => $shop_id,
  106. 'client' => $client,
  107. ])->update($data);
  108. }
  109. return $token;
  110. }
  111. }