KefuLogic.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\kefu;
  20. use app\common\enum\KefuTerminalEnum;
  21. use app\common\logic\BaseLogic;
  22. use app\common\logic\ChatLogic;
  23. use app\common\model\Admin;
  24. use app\common\model\Kefu;
  25. use app\kefuapi\logic\LoginLogic;
  26. use app\kefuapi\service\KefuTokenService;
  27. /**
  28. * 客服逻辑
  29. * Class KefuLogic
  30. * @package app\adminapi\logic\kefu
  31. */
  32. class KefuLogic extends BaseLogic
  33. {
  34. /**
  35. * @notes 添加客服
  36. * @param array $params
  37. * @return Kefu|\think\Model
  38. * @author 段誉
  39. * @date 2022/3/8 17:52
  40. */
  41. public static function add(array $params)
  42. {
  43. return Kefu::create([
  44. 'admin_id' => $params['admin_id'],
  45. 'nickname' => $params['nickname'],
  46. 'disable' => $params['disable'],
  47. 'sort' => empty($params['sort']) ? 1 : $params['sort'],
  48. 'avatar' => $params['avatar']
  49. ]);
  50. }
  51. /**
  52. * @notes 编辑客服
  53. * @param array $params
  54. * @return Kefu
  55. * @author 段誉
  56. * @date 2022/3/8 17:55
  57. */
  58. public static function edit(array $params)
  59. {
  60. return Kefu::update([
  61. 'id' => $params['id'],
  62. 'nickname' => $params['nickname'],
  63. 'sort' => empty($params['sort']) ? 1 : $params['sort'],
  64. 'disable' => $params['disable'],
  65. 'avatar' => $params['avatar'],
  66. ]);
  67. }
  68. /**
  69. * @notes 删除客服
  70. * @param int $id
  71. * @return bool
  72. * @author 段誉
  73. * @date 2022/3/8 17:59
  74. */
  75. public static function del(int $id): bool
  76. {
  77. return Kefu::destroy($id);
  78. }
  79. /**
  80. * @notes 获取客服详情
  81. * @param int $id
  82. * @return array
  83. * @author 段誉
  84. * @date 2022/3/8 18:05
  85. */
  86. public static function detail(int $id)
  87. {
  88. return Kefu::with(['admin' => function ($query) {
  89. $query->withField(['id', 'account', 'name']);
  90. }])->findOrEmpty($id)->toArray();
  91. }
  92. /**
  93. * @notes 设置客服状态
  94. * @param array $params
  95. * @return Kefu
  96. * @author 段誉
  97. * @date 2022/3/8 18:23
  98. */
  99. public static function setStatus(array $params)
  100. {
  101. if ($params['disable'] == 1) {
  102. ChatLogic::setChatDisable($params['id']);
  103. }
  104. return Kefu::update(['id' => $params['id'], 'disable' => $params['disable']]);
  105. }
  106. /**
  107. * @notes 客服token
  108. * @param int $id
  109. * @return false|string
  110. * @author 段誉
  111. * @date 2022/3/18 16:40
  112. */
  113. public static function login(int $id)
  114. {
  115. try{
  116. $kefu = (new Admin())->alias('a')
  117. ->field(['k.id', 'k.disable' => 'kefu_disable', 'a.disable' => 'admin_disable'])
  118. ->join('kefu k', 'a.id = k.admin_id')
  119. ->where(['k.id' => $id])
  120. ->findOrEmpty();
  121. if($kefu->isEmpty()) {
  122. throw new \Exception('该客服信息缺失');
  123. }
  124. if ($kefu['kefu_disable'] || $kefu['admin_disable']) {
  125. throw new \Exception('该客服已被禁用');
  126. }
  127. $token = KefuTokenService::setToken($kefu['id'], KefuTerminalEnum::PC)['token'] ?? '';
  128. return request()->domain() . '/kefu?token='. $token;
  129. } catch(\Exception $e) {
  130. self::$error = $e->getMessage();
  131. return false;
  132. }
  133. }
  134. }