You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

User.php 5.6KB

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