Common.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\kefu\KefuServices;
  14. use app\services\kefu\ProductServices;
  15. use app\services\kefu\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\kefu\service\StoreServiceServices;
  23. use app\api\validate\user\StoreServiceFeedbackValidate;
  24. use app\services\kefu\service\StoreServiceFeedbackServices;
  25. use crmeb\exceptions\AuthException;
  26. use app\services\other\UploadService;
  27. use crmeb\services\CacheService;
  28. use crmeb\utils\Arr;
  29. use crmeb\utils\JwtAuth;
  30. class Common extends BaseController
  31. {
  32. protected function initialize()
  33. {
  34. }
  35. /**
  36. * 获取客服页面广告内容
  37. * @return mixed
  38. */
  39. public function getKfAdv()
  40. {
  41. /** @var CacheServices $cache */
  42. $cache = app()->make(CacheServices::class);
  43. $content = $cache->getDbCache('kf_adv', '');
  44. return app('json')->success(compact('content'));
  45. }
  46. /**
  47. * 游客模式下获取客服
  48. * @param StoreServiceServices $services
  49. * @param UserServices $userServices
  50. * @return mixed
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getServiceUser(StoreServiceServices $services, UserServices $userServices, StoreServiceRecordServices $recordServices, $token = '')
  56. {
  57. $serviceInfoList = $services->getServiceList(['status' => 1, 'online' => 1]);
  58. if (!count($serviceInfoList['list'])) {
  59. return app('json')->fail(410136);
  60. }
  61. $uids = array_column($serviceInfoList['list'], 'uid');
  62. $toUid = $tourist_uid = $uid = 0;
  63. if ($token) {
  64. try {
  65. /** @var UserAuthServices $service */
  66. $service = app()->make(UserAuthServices::class);
  67. $authInfo = $service->parseToken($token);
  68. $uid = $authInfo['user']['uid'];
  69. $toUid = $recordServices->value(['user_id' => $uid], 'to_uid');
  70. if (!in_array($toUid, $uids)) {
  71. $toUid = 0;
  72. }
  73. } catch (AuthException $e) {
  74. }
  75. } else {
  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 ?: $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'] = (bool)$tourist_uid;
  97. return app('json')->success($userInfo->toArray());
  98. } else {
  99. return app('json')->fail(410136);
  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 app('json')->success(100000);
  120. }
  121. /**
  122. * 客服反馈页面头部文字
  123. * @return mixed
  124. */
  125. public function getFeedbackInfo()
  126. {
  127. return app('json')->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 app('json')->fail(100100);
  142. }
  143. if (!$token) {
  144. return app('json')->fail(410038);
  145. }
  146. try {
  147. /** @var UserAuthServices $service */
  148. $service = app()->make(UserAuthServices::class);
  149. $authInfo = $service->parseToken($token);
  150. } catch (AuthException $e) {
  151. return app('json')->fail(410090);
  152. }
  153. return app('json')->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 app('json')->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 app('json')->fail(100101);
  183. }
  184. } catch (AuthException $e) {
  185. return app('json')->fail(410090);
  186. }
  187. return app('json')->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 app('json')->fail(100101);
  206. }
  207. } catch (AuthException $e) {
  208. return app('json')->fail(410090);
  209. }
  210. $uid = $authInfo['user']['uid'];
  211. if (!$data['filename']) return app('json')->fail(100100);
  212. if (CacheService::has('start_uploads_' . $uid) && CacheService::get('start_uploads_' . $uid) >= 100) return app('json')->fail('非法操作');
  213. $upload = UploadService::init();
  214. $info = $upload->to('store/comment')->validate()->move($data['filename']);
  215. if ($info === false) {
  216. return app('json')->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 app('json')->success(410091, ['name' => $res['name'], 'url' => $res['dir']]);
  229. }
  230. }