Common.php 9.7 KB

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