User.php 6.2 KB

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