ChatController.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\Repositories\WxContRepositories;
  4. use App\Jobs\System\ChatExpandTypeCheckJob;
  5. use App\Jobs\UserInputSafeCheckJob;
  6. use App\Models\Circle\WxCircle;
  7. use App\Models\Pets\WxPetsAdoption;
  8. use App\Models\Pets\WxPetsAdoptionApply;
  9. use App\Models\Posts\WxComment;
  10. use App\Models\Posts\WxPost;
  11. use App\Models\Shop\WxShopGoods;
  12. use App\Models\Shop\WxShopOrderGoods;
  13. use App\Models\Shop\WxShopService;
  14. use App\Models\Used\WxUsedGood;
  15. use App\Models\User\WxUserContactApply;
  16. use App\Models\User\WxUserStudentCertify;
  17. use App\Models\Voter\WxVoter;
  18. use App\Models\Voter\WxVoterPlayer;
  19. use App\Models\WxAuthentication;
  20. use App\Models\WxChat;
  21. use App\Models\User\WxUser;
  22. use App\Wen\Utils\CircleUtils;
  23. use App\Wen\Utils\CommentUtils;
  24. use App\Wen\Utils\FieldUtils;
  25. use App\Wen\Utils\GatewayUtils;
  26. use App\Wen\Utils\PetUtils;
  27. use App\Wen\Utils\PostUtils;
  28. use App\Wen\Utils\Settings;
  29. use App\Wen\Utils\ShopUtils;
  30. use App\Wen\Utils\UserUtils;
  31. use App\Wen\Utils\Utils;
  32. use Illuminate\Http\Request;
  33. use Illuminate\Support\Facades\DB;
  34. class ChatController extends BaseController
  35. {
  36. public function massages_process(Request $request) {
  37. $type = _empty_default_($request->type, '');
  38. if($type == 'review_object'){
  39. return $this->review_object($request);
  40. }
  41. }
  42. private function review_object(Request &$request){
  43. $chat_id = _empty_default_($request->chat_id, 0);
  44. if(_empty_($chat_id)){
  45. return $this->fail(200001);
  46. }
  47. $chat = WxChat::find($chat_id);
  48. if(!$chat){
  49. return $this->fail(200003);
  50. }
  51. // 101: 笔记审核 102:评论审核 103:组局审核 104:选票审核 105:选手审核 106:闲置审核 107:圈子审核 108:学生认证审核 109:身份认证审核 110 俱乐部审核
  52. if(!in_array($chat->expand_type, [101, 102, 103, 104, 105, 106, 107, 108, 109, 110])){
  53. return $this->fail(200004);
  54. }
  55. global $__MINI_GLOBAL_IS_ADMIN_SUPER__,$__MINI_GLOBAL_CURRENT_USER_ID__;
  56. if(!$__MINI_GLOBAL_IS_ADMIN_SUPER__){
  57. if($chat->expand_type == 105){
  58. $voter_id = WxVoterPlayer::where('id', $chat->expand_id)->value('voter_id');
  59. if(_empty_($voter_id)){
  60. return $this->fail(200000);
  61. }
  62. if(WxVoter::where('id', $voter_id)->value('user_id') != $__MINI_GLOBAL_CURRENT_USER_ID__){
  63. return $this->fail(200000);
  64. }
  65. }else{
  66. return $this->fail(200000);
  67. }
  68. }
  69. $flag = _empty_default_($request->flag, 0);
  70. if(!in_array($flag, [1, 2])){
  71. return $this->fail(200004);
  72. }
  73. if($chat->expand_type == 101){
  74. //expand_id
  75. $the_object = WxPost::where('is_examine', 0)->where('id', $chat->expand_id)->first();
  76. if($the_object){
  77. PostUtils::post_pass($the_object, $flag);
  78. return $this->success();
  79. }else{
  80. WxChat::where('id', $chat->id)->forceDelete();
  81. return $this->fail(200003);
  82. }
  83. }else if($chat->expand_type == 102){
  84. $the_object = WxComment::where('comment_state', 0)->where('id', $chat->expand_id)->first();
  85. if($the_object){
  86. CommentUtils::comment_pass($the_object, $flag);
  87. return $this->success();
  88. }else{
  89. WxChat::where('id', $chat->id)->forceDelete();
  90. return $this->fail(200003);
  91. }
  92. }else if($chat->expand_type == 104){
  93. $the_object = WxVoter::find($chat->expand_id);
  94. if($the_object){
  95. if($flag == 1){
  96. if($the_object->status == 0){
  97. $the_object->status = 1;
  98. $the_object->save();
  99. UserUtils::add_user_experience($the_object->user_id, 16, 20);
  100. }
  101. }else{
  102. if($the_object->status == 1){
  103. $the_object->status = 0;
  104. $the_object->save();
  105. }
  106. }
  107. return $this->success();
  108. }else{
  109. WxChat::where('id', $chat->id)->forceDelete();
  110. return $this->fail(200003);
  111. }
  112. }else if($chat->expand_type == 105){
  113. $the_object = WxVoterPlayer::find($chat->expand_id);
  114. if($the_object){
  115. if($flag == 1){
  116. if($the_object->status == 0){
  117. $the_object->status = 1;
  118. $the_object->save();
  119. UserUtils::add_user_experience($the_object->user_id, 19, 20);
  120. }
  121. }else{
  122. if($the_object->status == 1){
  123. $the_object->status = 0;
  124. $the_object->save();
  125. }
  126. }
  127. return $this->success();
  128. }else{
  129. WxChat::where('id', $chat->id)->forceDelete();
  130. return $this->fail(200003);
  131. }
  132. }else if($chat->expand_type == 106){
  133. $the_object = WxUsedGood::find($chat->expand_id);
  134. if($the_object){
  135. if($flag == 1){
  136. if($the_object->status == 0){
  137. $the_object->status = 1;
  138. $the_object->save();
  139. UserUtils::add_user_experience($the_object->user_id, 106, 30);
  140. }
  141. }else{
  142. if($the_object->status == 1){
  143. $the_object->status = 99;
  144. $the_object->save();
  145. }
  146. }
  147. return $this->success();
  148. }else{
  149. WxChat::where('id', $chat->id)->forceDelete();
  150. return $this->fail(200003);
  151. }
  152. }else if($chat->expand_type == 107){
  153. $the_object = WxCircle::where('circle_state', 0)->where('id', $chat->expand_id)->first();
  154. if($the_object){
  155. if($flag == 1){
  156. CircleUtils::circle_pass($the_object, 1, '');
  157. }else{
  158. CircleUtils::circle_pass($the_object, 2, '');
  159. }
  160. return $this->success();
  161. }else{
  162. WxChat::where('id', $chat->id)->forceDelete();
  163. return $this->fail(200003);
  164. }
  165. }else if($chat->expand_type == 108){
  166. $the_object = WxUserStudentCertify::find($chat->expand_id);
  167. if($the_object){
  168. if($flag == 1){
  169. if($the_object->status == 0){
  170. $the_object->status = 1;
  171. $the_object->save();
  172. UserUtils::add_user_experience($the_object->user_id, 18, 30);
  173. }
  174. }else{
  175. if($the_object->status == 1){
  176. $the_object->status = 0;
  177. $the_object->save();
  178. }
  179. }
  180. return $this->success();
  181. }else{
  182. WxChat::where('id', $chat->id)->forceDelete();
  183. return $this->fail(200003);
  184. }
  185. }else if($chat->expand_type == 109){
  186. $the_object = WxAuthentication::where('authentication_state', 0)->where('id', $chat->expand_id)->first();
  187. if($the_object){
  188. if($flag == 1){
  189. UserUtils::authenticate_pass($the_object, 1, '');
  190. }else{
  191. UserUtils::authenticate_pass($the_object, 2, '');
  192. }
  193. return $this->success();
  194. }else{
  195. WxChat::where('id', $chat->id)->forceDelete();
  196. return $this->fail(200003);
  197. }
  198. }else if($chat->expand_type == 110){
  199. $the_object = WxClub::find($chat->expand_id);
  200. if($the_object){
  201. if($flag == 1){
  202. if($the_object->status == 0){
  203. $the_object->status = 1;
  204. $the_object->save();
  205. }
  206. }else{
  207. if($the_object->status == 1){
  208. $the_object->status = 0;
  209. $the_object->save();
  210. }
  211. }
  212. return $this->success();
  213. }else{
  214. WxChat::where('id', $chat->id)->forceDelete();
  215. return $this->fail(200003);
  216. }
  217. }
  218. }
  219. private function after_sale_message(Request &$request){
  220. }
  221. /**
  222. * 用户发起聊天
  223. * @param Request $request
  224. * @return \Illuminate\Http\JsonResponse
  225. */
  226. public function addChat(Request $request)
  227. {
  228. $uid = $request->uid;
  229. $oid = $request->oid;
  230. if($uid == $oid){
  231. return $this->fail(200040);
  232. }
  233. _limit_user('chat', $uid, 30);
  234. if(!UserUtils::is_user_can_speak($uid)){
  235. return $this->fail(200043, [
  236. 'title' => '禁言中,无法言论',
  237. 'content' => '是否前往解除限制',
  238. 'confirmText' => '去解除',
  239. 'target_type' => 6,
  240. 'target_id' => '/pagesA/mine/unlock/unlock?user_id='.$uid
  241. ], '您当前处于禁言期间,无法言论');
  242. }
  243. $chat_content = $request->input('chat_content', '');
  244. $chat_image = $request->input('chat_image', '');
  245. $chat_audio_url = $request->input('chat_audio_url', '');
  246. $chat_audio_length = $request->input('chat_audio_length', '');
  247. $refer_id = $request->input('refer_id', 0);
  248. $shop_good_id = $request->input('shop_good_id', 0);
  249. $voter_id = _abs(_empty_default_($request->voter_id, 0));
  250. $ask_order_id = _abs(_empty_default_($request->ask_order_id, 0));
  251. if(_empty_($uid) || _empty_($oid)){
  252. return $this->fail(200001);
  253. }
  254. if(_empty_($shop_good_id)){
  255. $shop_good_id = 0;
  256. }else{
  257. $shop_good_id = _abs($shop_good_id);
  258. }
  259. if($shop_good_id > 0){
  260. // 比较特殊,需要转换身份,由商家发送
  261. $tmp_ = $oid;
  262. $oid = $uid;
  263. $uid = $tmp_;
  264. $last_chat = WxChat::where('user_id', $uid)->where('object_id', $oid)->orderByDesc('id')->first();
  265. if($last_chat && $last_chat->expand_type == 4 && $last_chat->expand_id == $shop_good_id){
  266. return $this->success();
  267. }
  268. }
  269. if(_empty_($refer_id)){
  270. $refer_id = 0;
  271. }else{
  272. $refer_id = _abs($refer_id);
  273. }
  274. if(_empty_($refer_id) && _empty_($shop_good_id) && _empty_($voter_id)){
  275. if(_empty_($chat_content) && _empty_($chat_image) && (_empty_($chat_audio_url) || _empty_($chat_audio_length))){
  276. return $this->fail(200001);
  277. }
  278. }
  279. if(!UserUtils::is_user_can_chat($uid)){
  280. return $this->fail(200000, [], '抱歉,您没有聊天权限');
  281. }
  282. if($ask_order_id){
  283. $ask_order = DB::table('ask_order')->where('status',10)->where('id',$ask_order_id)->first();
  284. if($ask_order && $ask_order->blogger_user_id == $request->uid){
  285. //提示绑定手机号
  286. $userinfo = DB::table('wx_user')->where('id',$request->uid)->first();
  287. if(empty($userinfo->phone)){
  288. return $this->fail(200016,[],'请先绑定手机号');
  289. }
  290. }
  291. }
  292. $model = WxContRepositories::add($uid, $oid, $chat_content, $chat_image, $chat_audio_url, $chat_audio_length, $refer_id, 0, 0, $shop_good_id, null, 0, $voter_id,$ask_order_id); //通用聊天
  293. //付费咨询的逻辑
  294. if($model){
  295. if($ask_order_id){
  296. //如果是答主的第一句话,倒计时开始
  297. // $ask_order = DB::table('ask_order')->where('status',10)->where('id',$ask_order_id)->first();
  298. if($ask_order){
  299. $blogger_chat = DB::table('wx_chat')->where([
  300. ['user_id','=',$ask_order->blogger_user_id],
  301. ['object_id','=',$ask_order->user_id],
  302. ['expand_type','=',9],
  303. ['expand_id','=',$ask_order_id],
  304. ])->count();
  305. if($blogger_chat == 1){
  306. $countdown_time = $ask_order->ask_minute * 60 + time(); //倒计时时间
  307. DB::table('ask_order')->where('status',10)->where('id',$ask_order_id)->update([
  308. 'status' => 15, //已回复
  309. 'countdown_time' => $countdown_time, //倒计时
  310. ]);
  311. //给提问者发短信
  312. $ask_userinfo = DB::table('wx_user')->where('id',$ask_order->user_id)->first();
  313. if($ask_userinfo && $ask_userinfo->phone && $ask_userinfo->country_code){
  314. $idd = $ask_userinfo->country_code;
  315. $phone = $ask_userinfo->phone;
  316. //
  317. $res = Utils::sendSMSnotice($idd,$phone);
  318. }
  319. }
  320. }
  321. }
  322. }
  323. if($model){
  324. $websocket_id = GatewayUtils::uid2client_id($oid);
  325. if($websocket_id){
  326. $mode = 5;
  327. $socket_content = ['chat_content'=>_mini_emoji($chat_content, true), 'chat_image'=>$chat_image,
  328. 'from_user'=>UserUtils::get_cached_user($uid) ];
  329. if($ask_order_id){
  330. $mode = 55;
  331. $socket_content['ask_order_id'] = $ask_order_id;
  332. }
  333. GatewayUtils::success($websocket_id, $mode, $socket_content);
  334. }
  335. $model->type = 'user';
  336. $imgList = array();
  337. array_push($imgList, ['img_url' => $model->chat_image]);
  338. $model->imgList = $imgList;
  339. $model->user = WxUser::where('id', $model->user_id)->first(FieldUtils::userInfoColums());
  340. $model->datetime = format_datetime($model->created_at);
  341. if($model->chat_audio_url && $model->chat_audio_length){
  342. $model->mode = 'audio';
  343. }else if($model->chat_image){
  344. $model->mode = 'img';
  345. }else if($model->refer_id){
  346. $model->mode = 'refer';
  347. }else{
  348. $model->mode = 'text';
  349. }
  350. if(!_empty_($model->chat_audio_length)){
  351. $min = (int)($model->chat_audio_length / 60);
  352. if($min < 10){
  353. $min = '0'.$min;
  354. }
  355. $sec = $model->chat_audio_length % 60;
  356. if($sec < 10){
  357. $sec = '0'.$sec;
  358. }
  359. $model->chat_audio_length = $min.':'.$sec;
  360. }
  361. }
  362. UserInputSafeCheckJob::dispatch($uid, _mini_emoji($chat_content, true));
  363. if($chat_image || $chat_audio_url){
  364. Utils::image_state_change([$chat_image, $chat_audio_url], 1);
  365. }
  366. return $this->success($model);
  367. }
  368. /**
  369. * 查询用户聊天记录
  370. * @param Request $request
  371. * @return \Illuminate\Http\JsonResponse
  372. */
  373. public function getUserChat(Request $request, $limit = 6)
  374. {
  375. $uid = $request->uid;
  376. $oid = $request->oid;
  377. if(_empty_($uid) || _empty_($oid)){
  378. return $this->fail(200001);
  379. }
  380. $data = WxChat::where(function($query) use ($uid, $oid) {
  381. $query->where(function ($query) use ($uid, $oid) {
  382. $query->where('user_id', $uid)
  383. ->where('object_id', $oid);
  384. })
  385. ->orWhere(function ($query) use ($uid, $oid) {
  386. $query->where('user_id', $oid)
  387. ->where('object_id', $uid);
  388. });
  389. })
  390. ->where('expand_type','!=',9) //排除问答聊天,因为要独立
  391. ->orderByRaw('id DESC')
  392. ->simplePaginate($limit);
  393. $data->map(function ($v) use ($uid){
  394. $v->type = 'user';
  395. $imgList = array();
  396. array_push($imgList, ['img_url' => $v->chat_image]);
  397. $v->imgList = $imgList;
  398. $v->user = WxUser::where('id', $v->user_id)->first(FieldUtils::userInfoColums());
  399. $v->datetime = format_datetime($v->created_at);
  400. if($v->chat_audio_url && $v->chat_audio_length){
  401. $v->mode = 'audio';
  402. }else if($v->chat_image){
  403. $v->mode = 'img';
  404. }else if($v->refer_id){
  405. $v->mode = 'refer';
  406. if($v->expand_type != 1 || $v->expand_id != $v->refer_id){
  407. ChatExpandTypeCheckJob::dispatch();
  408. }
  409. }else if($v->adoption_apply_id){
  410. $v->mode = 'adoption_reply';
  411. if($v->expand_type != 3 || $v->expand_id != $v->adoption_apply_id){
  412. ChatExpandTypeCheckJob::dispatch();
  413. }
  414. }else if($v->contact_apply_id){
  415. $v->mode = 'contact_apply';
  416. if($v->expand_type != 2 || $v->expand_id != $v->contact_apply_id){
  417. ChatExpandTypeCheckJob::dispatch();
  418. }
  419. }else{
  420. if($v->expand_type > 0){
  421. if($v->expand_type == 1){
  422. $v->mode = 'refer';
  423. $v->refer_id = $v->expand_id;
  424. }else if($v->expand_type == 2){
  425. $v->mode = 'contact_apply';
  426. $v->contact_apply_id = $v->expand_id;
  427. }else if($v->expand_type == 3){
  428. $v->mode = 'adoption_reply';
  429. $v->adoption_apply_id = $v->expand_id;
  430. }else if($v->expand_type == 4){
  431. $v->mode = 'shop_good';
  432. $v->shop_good_id = $v->expand_id;
  433. }else if($v->expand_type == 5){
  434. $v->mode = 'used_good';
  435. $v->used_good_id = $v->expand_id;
  436. }else if($v->expand_type == 8){
  437. $v->mode = 'voter';
  438. $v->voter_id = $v->expand_id;
  439. }else if($v->expand_type == 9){
  440. //仅占用
  441. $v->mode = 'ask_order';
  442. $v->ask_order_id = $v->expand_id;
  443. }else if(in_array($v->expand_type, [101, 102, 103, 104, 105, 106, 107, 108, 109, 110])){
  444. $v->mode = 'review';
  445. }else{
  446. // 101: 笔记审核 102:评论审核 103:组局审核 104:选票审核 105:选手审核 106:闲置审核
  447. return $this->fail(200006, [], '数据库expand_type不合法');
  448. }
  449. }else{
  450. $v->mode = 'text';
  451. }
  452. }
  453. if($v->mode == 'review'){
  454. if($v->expand_type == 101){
  455. $the_post = WxPost::find($v->expand_id);
  456. if($the_post){
  457. $posts_content = $the_post->posts_content;
  458. if($posts_content){
  459. $s_content = preg_replace("/<(img|video).*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", '', $posts_content);
  460. $s_content =trim_all_space(strip_tags(html_entity_decode($s_content)), true);
  461. }
  462. $v->chat_content = $the_post->posts_title ?: PostUtils::get_the_title($s_content, $the_post->id);
  463. PostUtils::mediaProcess($the_post, $uid, 90003, 0, true);
  464. $v->chat_image = Utils::imgWithStyle($the_post->img, 0, false, true);
  465. $v->review = [
  466. 'target_type' => 1,
  467. 'target_id' => $v->expand_id,
  468. 'state' => $the_post->is_examine,
  469. 'title_tip' => '笔记',
  470. 'state_tip' => [0=>'待审核', 1=>'审核通过', 2=>'驳回'][$the_post->is_examine],
  471. 'can_refuse' => 1,
  472. 'user' => UserUtils::get_cached_user($the_post->user_id),
  473. ];
  474. }else{
  475. WxChat::where('id', $v->id)->forceDelete();
  476. }
  477. }else if($v->expand_type == 102){
  478. $the_comment = WxComment::find($v->expand_id);
  479. if($the_comment){
  480. $v->chat_image = $the_comment->comment_img_url;
  481. $v->chat_content = $the_comment->comment_content;
  482. $v->review = [
  483. 'target_type' => 25,
  484. 'target_id' => '评论不可跳转',
  485. 'state' => $the_comment->comment_state,
  486. 'title_tip' => '笔记评论',
  487. 'state_tip' => [0=>'待审核', 1=>'审核通过', 2=>'驳回'][$the_comment->comment_state],
  488. 'can_refuse' => 1,
  489. 'user' => UserUtils::get_cached_user($the_comment->user_id),
  490. ];
  491. }else{
  492. WxChat::where('id', $v->id)->forceDelete();
  493. }
  494. }else if($v->expand_type == 104){
  495. $the_voter = WxVoter::find($v->expand_id);
  496. if($the_voter){
  497. $v->chat_content = $the_voter->title;
  498. if($the_voter->banners){
  499. $v->chat_image = _array_key($the_voter->banners, 0, '');
  500. }
  501. $v->review = [
  502. 'target_type' => 33,
  503. 'target_id' => $v->expand_id,
  504. 'state' => $the_voter->status,
  505. 'title_tip' => '选票',
  506. 'state_tip' => [0=>'审核中', 1=>'推送中', 2=>'驳回'][$the_voter->status],
  507. 'can_refuse' => 0,
  508. 'user' => UserUtils::get_cached_user($the_voter->user_id),
  509. ];
  510. }else{
  511. WxChat::where('id', $v->id)->forceDelete();
  512. }
  513. }else if($v->expand_type == 105){
  514. $player = WxVoterPlayer::find($v->expand_id);
  515. if($player){
  516. $v->chat_content = $player->show_name;
  517. if($player->img_urls){
  518. $v->chat_image = _array_key($player->img_urls, 0, '');
  519. }
  520. $v->review = [
  521. 'target_type' => 6,
  522. 'target_id' => '/pagesV/voter/detail/player?id='.$v->expand_id,
  523. 'state' => $player->status,
  524. 'title_tip' => '选票选手',
  525. 'state_tip' => [0=>'审核中', 1=>'推送中', 2=>'驳回'][$player->status],
  526. 'can_refuse' => 0,
  527. 'user' => UserUtils::get_cached_user($player->user_id),
  528. ];
  529. }else{
  530. WxChat::where('id', $v->id)->forceDelete();
  531. }
  532. }else if($v->expand_type == 106){
  533. $the_used = WxUsedGood::find($v->expand_id);
  534. if($the_used){
  535. $v->chat_content = $the_used->title;
  536. if($the_used->image_urls){
  537. $v->chat_image = _array_key($the_used->image_urls[0], 'url', '');
  538. }
  539. $v->review = [
  540. 'target_type' => 28,
  541. 'target_id' => $v->expand_id,
  542. 'state' => $the_used->status == 99 ? 2 : ($the_used->status === 0 ? 0 : 1),
  543. 'title_tip' => '闲置商品',
  544. 'state_tip' => FieldUtils::usedGoodState()[$the_used->status],
  545. 'can_refuse' => 1,
  546. 'user' => UserUtils::get_cached_user($the_used->user_id),
  547. ];
  548. }else{
  549. WxChat::where('id', $v->id)->forceDelete();
  550. }
  551. }else if($v->expand_type == 107){
  552. $the_circle = WxCircle::find($v->expand_id);
  553. if($the_circle){
  554. $v->chat_content = $the_circle->circle_name;
  555. $v->chat_image = $the_circle->head_portrait;
  556. $v->review = [
  557. 'target_type' => 2,
  558. 'target_id' => $v->expand_id,
  559. 'state' => $the_circle->circle_state,
  560. 'title_tip' => '新建圈子',
  561. 'state_tip' => [0=>'审核中', 1=>'推送中', 2=>'驳回'][$the_circle->circle_state],
  562. 'can_refuse' => 1,
  563. 'user' => UserUtils::get_cached_user($the_circle->user_id),
  564. ];
  565. }else{
  566. WxChat::where('id', $v->id)->forceDelete();
  567. }
  568. }else if($v->expand_type == 108){
  569. $student = WxUserStudentCertify::where('user_id', $v->expand_id)->first();
  570. if($student){
  571. $v->chat_content = $student->school;
  572. $v->chat_image = $student->picture;
  573. $v->review = [
  574. 'target_type' => 25,
  575. 'target_id' => '学生认证暂不可跳转',
  576. 'state' => $student->status,
  577. 'title_tip' => '学生认证',
  578. 'state_tip' => [0=>'待审核', 1=>'已通过', 2=>'驳回'][$student->status],
  579. 'can_refuse' => 0,
  580. 'user' => UserUtils::get_cached_user($student->user_id),
  581. ];
  582. }else{
  583. WxChat::where('id', $v->id)->forceDelete();
  584. }
  585. }else if($v->expand_type == 109){
  586. $authentication = WxAuthentication::find($v->expand_id);
  587. if($authentication){
  588. $v->chat_content = $authentication->name;
  589. $v->chat_image = $authentication->identity_picture;
  590. $v->review = [
  591. 'target_type' => 25,
  592. 'target_id' => '身份认证暂不可跳转',
  593. 'state' => $authentication->authentication_state,
  594. 'title_tip' => '身份认证',
  595. 'state_tip' => [0=>'审核中', 1=>'已通过', 2=>'驳回'][$authentication->authentication_state],
  596. 'can_refuse' => 0,
  597. 'user' => UserUtils::get_cached_user($authentication->user_id),
  598. ];
  599. }else{
  600. WxChat::where('id', $v->id)->forceDelete();
  601. }
  602. }else if($v->expand_type == 110){
  603. $club = WxClub::where('id', $v->expand_id)->first();
  604. if($club){
  605. $v->chat_content = _empty_default_($club->name_tmp, $club->name);
  606. $v->chat_image = _empty_default_($club->avatar_tmp, $club->avatar);
  607. $v->review = [
  608. 'target_type' => 35,
  609. 'target_id' => $v->expand_id,
  610. 'state' => $club->status,
  611. 'title_tip' => '俱乐部审核',
  612. 'state_tip' => [0=>'待审核', 1=>'已通过', 2=>'驳回'][$club->status],
  613. 'can_refuse' => 0,
  614. 'user' => UserUtils::get_cached_user($club->user_id),
  615. ];
  616. }else{
  617. WxChat::where('id', $v->id)->forceDelete();
  618. }
  619. }
  620. if(_empty_($v->chat_image)){
  621. $v->chat_image = Settings::get('img_default', '').'?imageMogr2/thumbnail/200x200/crop/200x200/gravity/center/format/jpg/interlace/0/quality/100';
  622. }else{
  623. $v->chat_image = $v->chat_image.'?imageMogr2/thumbnail/200x200/crop/200x200/gravity/center/format/jpg/interlace/0/quality/100';
  624. }
  625. }
  626. if(!_empty_($v->chat_audio_length)){
  627. $min = (int)($v->chat_audio_length / 60);
  628. if($min < 10){
  629. $min = '0'.$min;
  630. }
  631. $sec = $v->chat_audio_length % 60;
  632. if($sec < 10){
  633. $sec = '0'.$sec;
  634. }
  635. $v->chat_audio_length = $min.':'.$sec;
  636. }
  637. if(!_empty_($v->refer_id)){
  638. if($v->refer_id && $v->refer_id > 0){
  639. $refer_post = WxPost::find($v->refer_id);
  640. if($refer_post){
  641. PostUtils::mediaProcess($refer_post, $uid, 15, 0, true);
  642. if($refer_post->posts_content_raw){
  643. $refer_post->posts_content_raw = strip_tags($refer_post->posts_content_raw);
  644. }
  645. $v->refer = $refer_post;
  646. }else{
  647. WxChat::where('id', $v->id)->delete();
  648. }
  649. }else{
  650. $v->refer = null;
  651. }
  652. }
  653. if(!_empty_($v->adoption_apply_id)){
  654. if($v->adoption_apply_id && $v->adoption_apply_id > 0){
  655. $adoption_reply = WxPetsAdoptionApply::find($v->adoption_apply_id);
  656. if($adoption_reply){
  657. $v->adoption_reply = PetUtils::adoption_apply_process($adoption_reply, $uid);
  658. $v->adoption_reply->adoption = PetUtils::adoption_process(WxPetsAdoption::find($adoption_reply->pet_adoption_id));
  659. }else{
  660. WxChat::where('id', $v->id)->delete();
  661. }
  662. }else{
  663. $v->adoption_reply = null;
  664. }
  665. }
  666. if(!_empty_($v->used_good_id)){
  667. $user_good = WxUsedGood::where('id', $v->used_good_id)->first();
  668. if($user_good){
  669. ShopUtils::used_process($user_good);
  670. if($user_good->pic){
  671. $user_good->pic = $user_good->pic.'?imageMogr2/quality/100/format/webp/interlace/0/crop/200x200/gravity/center/thumbnail/200x';
  672. }
  673. $user_good->servicer_word = '亲,想要可以直接拍';
  674. $v->used_good = $user_good;
  675. }else{
  676. WxChat::where('id', $v->id)->delete();
  677. }
  678. }
  679. if(!_empty_($v->voter_id)){
  680. $voter = WxVoter::where('id', $v->voter_id)->first();
  681. if($voter){
  682. if($voter->banners){
  683. $voter->pic = $voter->banners[0];
  684. }
  685. $v->voter = $voter;
  686. }else{
  687. WxChat::where('id', $v->id)->delete();
  688. }
  689. }
  690. if(!_empty_($v->ask_order_id)){
  691. //仅占用
  692. }
  693. if(!_empty_($v->shop_good_id)){
  694. $data = WxShopGoods::where('id', $v->shop_good_id)->first()->toArray();
  695. if($data) {
  696. // 服务
  697. if (isset($data['service_id'])) {
  698. $service_tip = '';
  699. $data['service'] = WxShopService::whereIn('id', json_decode($data['service_id'], true))->get(['name', 'intro'])->map(function ($s) use (&$service_tip){
  700. if(_empty_($service_tip)){
  701. $service_tip .= '"'.$s->name.'"';
  702. }else{
  703. $service_tip .= ',' . '"'.$s->name.'"';
  704. }
  705. return $s;
  706. });
  707. if($service_tip){
  708. $service_tip = '亲,我们这款商品的服务为' . $service_tip . '~';
  709. }
  710. $data['service_tip'] = $service_tip;
  711. if($data['pic'] && is_array($data['pic']) && count($data['pic']) > 0){
  712. $data['pic'][0] = $data['pic'][0].'?imageMogr2/quality/100/format/webp/interlace/0/crop/200x200/gravity/center/thumbnail/200x';
  713. }
  714. } else {
  715. $data['service'] = null;
  716. $data['service_tip'] = '';
  717. }
  718. $data['user_purchase'] = ShopUtils::user_buy_goods_times($uid, $data['id']);
  719. $v->shop_good = $data;
  720. }
  721. }
  722. if(!_empty_($v->contact_apply_id)){
  723. if($v->contact_apply_id && $v->contact_apply_id > 0){
  724. $contact_apply = WxUserContactApply::find($v->contact_apply_id);
  725. if($contact_apply){
  726. if($contact_apply->user_id == $uid){
  727. // 己方发送
  728. $title = '申请查看对方的';
  729. if($contact_apply->is_phone == 1){
  730. $title .= '手机号';
  731. }
  732. if($contact_apply->is_wechat_account == 1){
  733. if($contact_apply->is_phone == 1){
  734. $title .= '、微信号';
  735. }else{
  736. $title .= '微信号';
  737. }
  738. }
  739. $contact_apply->title = $title;
  740. }else{
  741. $title = '我申请查看你的';
  742. if($contact_apply->is_phone == 1){
  743. $title .= '手机号';
  744. }
  745. if($contact_apply->is_wechat_account == 1){
  746. if($contact_apply->is_phone == 1){
  747. $title .= '、微信号';
  748. }else{
  749. $title .= '微信号';
  750. }
  751. }
  752. $contact_apply->title = $title;
  753. }
  754. $contact_apply->status_tip = '申请中';
  755. if($contact_apply->status == 1){
  756. $contact_apply->status_tip = '已同意';
  757. }else if($contact_apply->status == 2){
  758. $contact_apply->status_tip = '已过期';
  759. }
  760. $v->contact_apply = $contact_apply;
  761. }else{
  762. $v->contact_apply = null;
  763. }
  764. }else{
  765. $v->contact_apply = null;
  766. }
  767. }
  768. $allowable[] = [
  769. 'text' => '复制',
  770. 'type' => 'copy',
  771. 'id' => $v->id,
  772. 'raw' => strip_tags($v->chat_content),
  773. 'icon' => 'mini-icon mini-fuzhi'
  774. ];
  775. $v->allowable = $allowable;
  776. return $v;
  777. });
  778. return $this->success($data);
  779. }
  780. /**
  781. * 查询用户问答订单聊天记录
  782. * @param Request $request
  783. * @return \Illuminate\Http\JsonResponse
  784. */
  785. public function getUserAskOrderChat(Request $request, $limit = 6)
  786. {
  787. $uid = $request->uid;
  788. $oid = $request->oid;
  789. $ask_order_id = $request->ask_order_id;
  790. /*if(_empty_($uid) || _empty_($oid)){
  791. return $this->fail(200001);
  792. }
  793. $data = WxChat::where(function($query) use ($uid, $oid) {
  794. $query->where(function ($query) use ($uid, $oid) {
  795. $query->where('user_id', $uid)
  796. ->where('object_id', $oid);
  797. })
  798. ->orWhere(function ($query) use ($uid, $oid) {
  799. $query->where('user_id', $oid)
  800. ->where('object_id', $uid);
  801. });
  802. })*/
  803. $data = WxChat::where('expand_type',9) //只要问答订单的
  804. ->where('expand_id',$ask_order_id) //只要问答订单的
  805. ->orderByRaw('id DESC')
  806. ->simplePaginate($limit);
  807. //问答订单详情
  808. $ask_order = Db::table('ask_order')->where('id',$ask_order_id)->first();
  809. $data->map(function ($v) use ($uid,$ask_order){
  810. //区分用户和答主
  811. $v->ask_order_role = 'user';
  812. if($ask_order){
  813. if($ask_order->user_id == $v->user_id){
  814. $v->ask_order_role = 'user';
  815. }elseif($ask_order->blogger_user_id == $v->user_id){
  816. $v->ask_order_role = 'blogger';
  817. }
  818. }
  819. $v->type = 'user';
  820. $imgList = array();
  821. array_push($imgList, ['img_url' => $v->chat_image]);
  822. $v->imgList = $imgList;
  823. $v->user = WxUser::where('id', $v->user_id)->first(FieldUtils::userInfoColums());
  824. $v->datetime = format_datetime($v->created_at);
  825. if($v->chat_audio_url && $v->chat_audio_length){
  826. $v->mode = 'audio';
  827. }else if($v->chat_image){
  828. $v->mode = 'img';
  829. }else{
  830. $v->mode = 'text';
  831. }
  832. if(!_empty_($v->chat_audio_length)){
  833. $min = (int)($v->chat_audio_length / 60);
  834. if($min < 10){
  835. $min = '0'.$min;
  836. }
  837. $sec = $v->chat_audio_length % 60;
  838. if($sec < 10){
  839. $sec = '0'.$sec;
  840. }
  841. $v->chat_audio_length = $min.':'.$sec;
  842. }
  843. $allowable[] = [
  844. 'text' => '复制',
  845. 'type' => 'copy',
  846. 'id' => $v->id,
  847. 'raw' => strip_tags($v->chat_content),
  848. 'icon' => 'mini-icon mini-fuzhi'
  849. ];
  850. $v->allowable = $allowable;
  851. return $v;
  852. });
  853. return $this->success($data);
  854. }
  855. /**
  856. * 已读对应用户信息
  857. * @param Request $request
  858. * @return \Illuminate\Http\JsonResponse
  859. */
  860. public function readUserChat(Request $request)
  861. {
  862. $uid = $request->uid;
  863. $oid = $request->oid;
  864. if(_empty_($oid)){
  865. return $this->fail(200001);
  866. }
  867. $data = WxChat::where([['user_id','=', $oid],['object_id', '=', $uid],['expand_type', '!=', 9]])->update(['is_read' => 1]);
  868. return $this->success($data);
  869. }
  870. /**
  871. * 已读对应用户咨询信息
  872. * @param Request $request
  873. * @return \Illuminate\Http\JsonResponse
  874. */
  875. public function readAskOrderChat(Request $request)
  876. {
  877. $uid = $request->uid;
  878. $oid = $request->oid;
  879. $ask_order_id = $request->ask_order_id;
  880. if(_empty_($oid) || _empty_($ask_order_id)){
  881. return $this->fail(200001);
  882. }
  883. $data = WxChat::where([['user_id','=', $oid],['object_id', '=', $uid],['expand_type', '=', 9],['expand_id', '=', $ask_order_id]])->update(['is_read' => 1]);
  884. return $this->success($data);
  885. }
  886. /**
  887. * 用户删除聊天记录
  888. */
  889. public function userDelMessage(Request $request)
  890. {
  891. $uid = $request->uid;
  892. $userid = $request->userid;
  893. $data = WxChat::where(function ($query) use ($uid, $userid) {
  894. $query->where('user_id', $uid)
  895. ->where('object_id', $userid);
  896. })
  897. ->orWhere(function ($query) use ($uid, $userid) {
  898. $query->where('user_id', $userid)
  899. ->where('object_id', $uid);
  900. })->update(['chat_state' => 1]);
  901. return $this->success($data);
  902. }
  903. }