Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\controller\kefu;
  3. use app\Request;
  4. use app\services\kefu\KefuServices;
  5. use app\services\kefu\ProductServices;
  6. use app\services\message\service\StoreServiceRecordServices;
  7. use app\services\order\StoreOrderServices;
  8. use app\services\system\attachment\SystemAttachmentServices;
  9. use app\services\user\UserAuthServices;
  10. use crmeb\basic\BaseController;
  11. use app\services\user\UserServices;
  12. use app\services\other\CacheServices;
  13. use app\services\message\service\StoreServiceServices;
  14. use app\validate\api\user\StoreServiceFeedbackValidate;
  15. use app\services\message\service\StoreServiceFeedbackServices;
  16. use crmeb\exceptions\AuthException;
  17. use crmeb\services\UploadService;
  18. use crmeb\utils\Arr;
  19. use crmeb\utils\JwtAuth;
  20. use crmeb\services\CacheService;
  21. class Common extends BaseController
  22. {
  23. protected function initialize()
  24. {
  25. }
  26. /**
  27. * 获取erp开关
  28. * @return mixed
  29. */
  30. public function getErpConfig()
  31. {
  32. return $this->success(['open_erp' => !!sys_config('erp_open')]);
  33. }
  34. /**
  35. * 获取客服页面广告内容
  36. * @return mixed
  37. */
  38. public function getKfAdv()
  39. {
  40. /** @var CacheServices $cache */
  41. $cache = app()->make(CacheServices::class);
  42. $content = $cache->getDbCache('kf_adv', '');
  43. return $this->success(compact('content'));
  44. }
  45. /**
  46. * 游客模式下获取客服
  47. * @param StoreServiceServices $services
  48. * @param UserServices $userServices
  49. * @return mixed
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function getServiceUser(StoreServiceServices $services, UserServices $userServices, StoreServiceRecordServices $recordServices, $token = '')
  55. {
  56. $serviceInfoList = $services->getServiceList(['status' => 1, 'online' => 1]);
  57. if (!count($serviceInfoList['list'])) {
  58. return $this->fail('暂无客服人员在线');
  59. }
  60. $uids = array_column($serviceInfoList['list'], 'uid');
  61. $toUid = $tourist_uid = $uid = 0;
  62. if ($token) {
  63. try {
  64. /** @var UserAuthServices $service */
  65. $service = app()->make(UserAuthServices::class);
  66. $authInfo = $service->parseToken($token);
  67. $uid = $authInfo['user']['uid'];
  68. $toUid = $recordServices->value(['user_id' => $uid], 'to_uid');
  69. if (!in_array($toUid, $uids)) {
  70. $toUid = 0;
  71. }
  72. } catch (AuthException $e) {
  73. }
  74. } else {
  75. mt_srand();
  76. $tourist_uid = rand(100000000, 999999999);
  77. }
  78. if (!$toUid) {
  79. $toUid = Arr::getArrayRandKey($uids);
  80. }
  81. $userInfo = $userServices->get($toUid, ['nickname', 'avatar', 'real_name', 'uid']);
  82. if ($userInfo) {
  83. $infoList = array_column($serviceInfoList['list'], null, 'uid');
  84. if (isset($infoList[$toUid])) {
  85. if ($infoList[$toUid]['wx_name']) {
  86. $userInfo['nickname'] = $infoList[$toUid]['wx_name'];
  87. }
  88. if ($infoList[$toUid]['avatar']) {
  89. $userInfo['avatar'] = $infoList[$toUid]['avatar'];
  90. }
  91. }
  92. $userInfo['tourist_uid'] = $uid ? $uid : $tourist_uid;
  93. $tourist_avatar = sys_config('tourist_avatar');
  94. $avatar = Arr::getArrayRandKey(is_array($tourist_avatar) ? $tourist_avatar : []);
  95. $userInfo['tourist_avatar'] = $uid ? '' : $avatar;
  96. $userInfo['is_tourist'] = $tourist_uid ? true : false;
  97. return $this->success($userInfo->toArray());
  98. } else {
  99. return $this->fail('暂无客服人员');
  100. }
  101. }
  102. /**
  103. * 保存反馈信息
  104. * @param Request $request
  105. * @param StoreServiceFeedbackServices $services
  106. * @return mixed
  107. */
  108. public function saveFeedback(Request $request, StoreServiceFeedbackServices $services)
  109. {
  110. $data = $request->postMore([
  111. ['rela_name', ''],
  112. ['phone', ''],
  113. ['content', ''],
  114. ]);
  115. validate(StoreServiceFeedbackValidate::class)->check($data);
  116. $data['content'] = htmlspecialchars($data['content']);
  117. $data['add_time'] = time();
  118. $services->save($data);
  119. return $this->success('保存成功');
  120. }
  121. /**
  122. * 客服反馈页面头部文字
  123. * @return mixed
  124. */
  125. public function getFeedbackInfo()
  126. {
  127. return $this->success(['feedback' => sys_config('service_feedback')]);
  128. }
  129. /**
  130. * 聊天记录
  131. * @param $uid
  132. * @return mixed
  133. */
  134. public function getChatList(Request $request, KefuServices $services, JwtAuth $auth, $token = '')
  135. {
  136. [$uid, $upperId] = $request->postMore([
  137. ['uid', 0],
  138. ['upperId', 0],
  139. ], true);
  140. if (!$uid) {
  141. return $this->fail('缺少参数');
  142. }
  143. if (!$token) {
  144. return $this->fail('缺少登录token');
  145. }
  146. try {
  147. /** @var UserAuthServices $service */
  148. $service = app()->make(UserAuthServices::class);
  149. $authInfo = $service->parseToken($token);
  150. } catch (AuthException $e) {
  151. return $this->fail('无效的token不能查找到用户聊天记录');
  152. }
  153. return $this->success($services->getChatList($authInfo['user']['uid'], $uid, (int)$upperId));
  154. }
  155. /**
  156. * 商品详情
  157. * @param ProductServices $services
  158. * @param $id
  159. * @return mixed
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function getProductInfo(ProductServices $services, $id)
  165. {
  166. return $this->success($services->getProductInfo((int)$id));
  167. }
  168. /**
  169. * 获取订单信息
  170. * @param StoreOrderServices $services
  171. * @param $token
  172. * @param $order_id
  173. * @return mixed
  174. */
  175. public function getOrderInfo(StoreOrderServices $services, $token, $order_id)
  176. {
  177. try {
  178. /** @var UserAuthServices $service */
  179. $service = app()->make(UserAuthServices::class);
  180. $authInfo = $service->parseToken($token);
  181. if (!isset($authInfo['user']['uid'])) {
  182. return $this->fail('非法操作');
  183. }
  184. } catch (AuthException $e) {
  185. return $this->fail('无效的token不能查找到用户聊天记录');
  186. }
  187. return $this->success($services->tidyOrder($services->getUserOrderDetail($order_id, $authInfo['user']['uid'])->toArray(), true));
  188. }
  189. /**
  190. * 图片上传
  191. * @param Request $request
  192. * @return mixed
  193. * @throws \Psr\SimpleCache\InvalidArgumentException
  194. */
  195. public function upload(Request $request, SystemAttachmentServices $services)
  196. {
  197. $data = $request->postMore([
  198. ['filename', 'file'],
  199. ]);
  200. try {
  201. /** @var UserAuthServices $service */
  202. $service = app()->make(UserAuthServices::class);
  203. $authInfo = $service->parseToken($this->request->post('token'));
  204. if (!isset($authInfo['user']['uid'])) {
  205. return $this->fail('非法操作');
  206. }
  207. } catch (AuthException $e) {
  208. return $this->fail('无效的token不能使用上传图片功能');
  209. }
  210. $uid = $authInfo['user']['uid'];
  211. if (!$data['filename']) return $this->fail('参数有误');
  212. if (CacheService::has('start_uploads_' . $uid) && CacheService::get('start_uploads_' . $uid) >= 100) return $this->fail('非法操作');
  213. $upload = UploadService::init();
  214. $info = $upload->to('store/comment')->validate()->move($data['filename']);
  215. if ($info === false) {
  216. return $this->fail($upload->getError());
  217. }
  218. $res = $upload->getUploadInfo();
  219. $services->attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 1, (int)sys_config('upload_type', 1), $res['time'], 2);
  220. if (CacheService::has('start_uploads_' . $uid))
  221. $start_uploads = (int)CacheService::get('start_uploads_' . $uid);
  222. else
  223. $start_uploads = 0;
  224. $start_uploads++;
  225. CacheService::set('start_uploads_' . $uid, $start_uploads, 86400);
  226. $res['dir'] = path_to_url($res['dir']);
  227. if (strpos($res['dir'], 'http') === false) $res['dir'] = $request->domain() . $res['dir'];
  228. return $this->success('图片上传成功!', ['name' => $res['name'], 'url' => $res['dir']]);
  229. }
  230. }