User.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\kefu;
  12. use app\Request;
  13. use app\services\system\attachment\SystemAttachmentServices;
  14. use app\services\user\group\UserGroupServices;
  15. use crmeb\services\CacheService;
  16. use crmeb\services\UploadService;
  17. use think\facade\App;
  18. use app\services\kefu\UserServices;
  19. use app\services\user\label\UserLabelCateServices;
  20. use app\services\user\label\UserLabelRelationServices;
  21. use app\services\message\service\StoreServiceRecordServices;
  22. use think\facade\Config;
  23. /**
  24. * Class User
  25. * @package app\kefuapi\controller
  26. */
  27. class User extends AuthController
  28. {
  29. /**
  30. * User constructor.
  31. * @param App $app
  32. * @param StoreServiceRecordServices $services
  33. */
  34. public function __construct(App $app, StoreServiceRecordServices $services)
  35. {
  36. parent::__construct($app);
  37. $this->services = $services;
  38. }
  39. /**
  40. * 获取当前客服和用户的聊天记录
  41. * @return mixed
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function recordList(string $nickname = '', $is_tourist = 0)
  47. {
  48. return $this->success($this->services->getServiceList($this->kefuInfo['uid'], $nickname, (int)$is_tourist));
  49. }
  50. /**
  51. * 获取用户信息
  52. * @param UserServices $services
  53. * @param $uid
  54. * @return mixed
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function userInfo(UserServices $services, $uid)
  60. {
  61. return $this->success($services->getUserInfo((int)$uid));
  62. }
  63. /**
  64. * 标签分类
  65. * @param UserLabelCateServices $services
  66. * @return mixed
  67. */
  68. public function getUserLabel(UserLabelCateServices $services, $uid)
  69. {
  70. return $this->success($services->getUserLabel((int)$uid));
  71. }
  72. /**
  73. * 获取用户分组
  74. * @param UserGroupServices $services
  75. * @return mixed
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getUserGroup(UserGroupServices $services)
  81. {
  82. return $this->success($services->getGroupList());
  83. }
  84. /**
  85. * 设置分组
  86. * @param UserGroupServices $services
  87. * @param UserServices $userServices
  88. * @param $uid
  89. * @param $id
  90. * @return mixed
  91. */
  92. public function setUserGroup(UserGroupServices $services, UserServices $userServices, $uid, $id)
  93. {
  94. if (!$services->count(['id' => $id])) {
  95. return $this->fail('添加的会员标签不存在');
  96. }
  97. if (!($userInfo = $userServices->get($uid))) {
  98. return $this->fail('用户不存在');
  99. }
  100. if ($userInfo->group_id == $id) {
  101. return $this->fail('已拥有此分组');
  102. }
  103. $userInfo->group_id = $id;
  104. if ($userInfo->save()) {
  105. return $this->success('设置成功');
  106. } else {
  107. return $this->fail('设置失败');
  108. }
  109. }
  110. /**
  111. * 设置用户标签
  112. * @param UserLabelRelationServices $services
  113. * @param $uid
  114. * @return mixed
  115. */
  116. public function setUserLabel(UserLabelRelationServices $services, $uid)
  117. {
  118. [$labels, $unLabelIds] = $this->request->postMore([
  119. ['label_ids', []],
  120. ['un_label_ids', []]
  121. ], true);
  122. if (!count($labels) && !count($unLabelIds)) {
  123. return $this->fail('请选择标签');
  124. }
  125. if ($services->setUserLable($uid, $labels) && $services->unUserLabel($uid, $unLabelIds)) {
  126. return $this->success('设置成功');
  127. } else {
  128. return $this->fail('设置失败');
  129. }
  130. }
  131. /**
  132. * 退出登陆
  133. * @return mixed
  134. */
  135. public function logout()
  136. {
  137. $key = trim(ltrim($this->request->header(Config::get('cookie.token_name')), 'Bearer'));
  138. CacheService::redisHandler()->delete(md5($key));
  139. return $this->success();
  140. }
  141. /**
  142. * 图片上传
  143. * @param Request $request
  144. * @return mixed
  145. * @throws \Psr\SimpleCache\InvalidArgumentException
  146. */
  147. public function upload(Request $request, SystemAttachmentServices $services)
  148. {
  149. $data = $request->postMore([
  150. ['filename', 'file'],
  151. ]);
  152. if (!$data['filename']) return $this->fail('参数有误');
  153. if (CacheService::has('start_uploads_' . $request->kefuId()) && CacheService::get('start_uploads_' . $request->kefuId()) >= 100) return $this->fail('非法操作');
  154. $upload = UploadService::init();
  155. $info = $upload->to('store/comment')->validate()->move($data['filename']);
  156. if ($info === false) {
  157. return $this->fail($upload->getError());
  158. }
  159. $res = $upload->getUploadInfo();
  160. $services->attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 1, (int)sys_config('upload_type', 1), $res['time'], 2);
  161. if (CacheService::has('start_uploads_' . $request->kefuId()))
  162. $start_uploads = (int)CacheService::get('start_uploads_' . $request->kefuId());
  163. else
  164. $start_uploads = 0;
  165. $start_uploads++;
  166. CacheService::set('start_uploads_' . $request->kefuId(), $start_uploads, 86400);
  167. $res['dir'] = path_to_url($res['dir']);
  168. if (strpos($res['dir'], 'http') === false) $res['dir'] = $request->domain() . $res['dir'];
  169. return $this->success('图片上传成功!', ['name' => $res['name'], 'url' => $res['dir']]);
  170. }
  171. }