ChatController.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. if($ask_order_id){
  328. $mode = 55;
  329. }
  330. GatewayUtils::success($websocket_id, $mode, ['chat_content'=>_mini_emoji($chat_content, true), 'chat_image'=>$chat_image,
  331. 'from_user'=>UserUtils::get_cached_user($uid) ]);
  332. }
  333. $model->type = 'user';
  334. $imgList = array();
  335. array_push($imgList, ['img_url' => $model->chat_image]);
  336. $model->imgList = $imgList;
  337. $model->user = WxUser::where('id', $model->user_id)->first(FieldUtils::userInfoColums());
  338. $model->datetime = format_datetime($model->created_at);
  339. if($model->chat_audio_url && $model->chat_audio_length){
  340. $model->mode = 'audio';
  341. }else if($model->chat_image){
  342. $model->mode = 'img';
  343. }else if($model->refer_id){
  344. $model->mode = 'refer';
  345. }else{
  346. $model->mode = 'text';
  347. }
  348. if(!_empty_($model->chat_audio_length)){
  349. $min = (int)($model->chat_audio_length / 60);
  350. if($min < 10){
  351. $min = '0'.$min;
  352. }
  353. $sec = $model->chat_audio_length % 60;
  354. if($sec < 10){
  355. $sec = '0'.$sec;
  356. }
  357. $model->chat_audio_length = $min.':'.$sec;
  358. }
  359. }
  360. UserInputSafeCheckJob::dispatch($uid, _mini_emoji($chat_content, true));
  361. if($chat_image || $chat_audio_url){
  362. Utils::image_state_change([$chat_image, $chat_audio_url], 1);
  363. }
  364. return $this->success($model);
  365. }
  366. /**
  367. * 查询用户聊天记录
  368. * @param Request $request
  369. * @return \Illuminate\Http\JsonResponse
  370. */
  371. public function getUserChat(Request $request, $limit = 6)
  372. {
  373. $uid = $request->uid;
  374. $oid = $request->oid;
  375. if(_empty_($uid) || _empty_($oid)){
  376. return $this->fail(200001);
  377. }
  378. $data = WxChat::where(function($query) use ($uid, $oid) {
  379. $query->where(function ($query) use ($uid, $oid) {
  380. $query->where('user_id', $uid)
  381. ->where('object_id', $oid);
  382. })
  383. ->orWhere(function ($query) use ($uid, $oid) {
  384. $query->where('user_id', $oid)
  385. ->where('object_id', $uid);
  386. });
  387. })
  388. ->where('expand_type','!=',9) //排除问答聊天,因为要独立
  389. ->orderByRaw('id DESC')
  390. ->simplePaginate($limit);
  391. $data->map(function ($v) use ($uid){
  392. $v->type = 'user';
  393. $imgList = array();
  394. array_push($imgList, ['img_url' => $v->chat_image]);
  395. $v->imgList = $imgList;
  396. $v->user = WxUser::where('id', $v->user_id)->first(FieldUtils::userInfoColums());
  397. $v->datetime = format_datetime($v->created_at);
  398. if($v->chat_audio_url && $v->chat_audio_length){
  399. $v->mode = 'audio';
  400. }else if($v->chat_image){
  401. $v->mode = 'img';
  402. }else if($v->refer_id){
  403. $v->mode = 'refer';
  404. if($v->expand_type != 1 || $v->expand_id != $v->refer_id){
  405. ChatExpandTypeCheckJob::dispatch();
  406. }
  407. }else if($v->adoption_apply_id){
  408. $v->mode = 'adoption_reply';
  409. if($v->expand_type != 3 || $v->expand_id != $v->adoption_apply_id){
  410. ChatExpandTypeCheckJob::dispatch();
  411. }
  412. }else if($v->contact_apply_id){
  413. $v->mode = 'contact_apply';
  414. if($v->expand_type != 2 || $v->expand_id != $v->contact_apply_id){
  415. ChatExpandTypeCheckJob::dispatch();
  416. }
  417. }else{
  418. if($v->expand_type > 0){
  419. if($v->expand_type == 1){
  420. $v->mode = 'refer';
  421. $v->refer_id = $v->expand_id;
  422. }else if($v->expand_type == 2){
  423. $v->mode = 'contact_apply';
  424. $v->contact_apply_id = $v->expand_id;
  425. }else if($v->expand_type == 3){
  426. $v->mode = 'adoption_reply';
  427. $v->adoption_apply_id = $v->expand_id;
  428. }else if($v->expand_type == 4){
  429. $v->mode = 'shop_good';
  430. $v->shop_good_id = $v->expand_id;
  431. }else if($v->expand_type == 5){
  432. $v->mode = 'used_good';
  433. $v->used_good_id = $v->expand_id;
  434. }else if($v->expand_type == 8){
  435. $v->mode = 'voter';
  436. $v->voter_id = $v->expand_id;
  437. }else if($v->expand_type == 9){
  438. //仅占用
  439. $v->mode = 'ask_order';
  440. $v->ask_order_id = $v->expand_id;
  441. }else if(in_array($v->expand_type, [101, 102, 103, 104, 105, 106, 107, 108, 109, 110])){
  442. $v->mode = 'review';
  443. }else{
  444. // 101: 笔记审核 102:评论审核 103:组局审核 104:选票审核 105:选手审核 106:闲置审核
  445. return $this->fail(200006, [], '数据库expand_type不合法');
  446. }
  447. }else{
  448. $v->mode = 'text';
  449. }
  450. }
  451. if($v->mode == 'review'){
  452. if($v->expand_type == 101){
  453. $the_post = WxPost::find($v->expand_id);
  454. if($the_post){
  455. $posts_content = $the_post->posts_content;
  456. if($posts_content){
  457. $s_content = preg_replace("/<(img|video).*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", '', $posts_content);
  458. $s_content =trim_all_space(strip_tags(html_entity_decode($s_content)), true);
  459. }
  460. $v->chat_content = $the_post->posts_title ?: PostUtils::get_the_title($s_content, $the_post->id);
  461. PostUtils::mediaProcess($the_post, $uid, 90003, 0, true);
  462. $v->chat_image = Utils::imgWithStyle($the_post->img, 0, false, true);
  463. $v->review = [
  464. 'target_type' => 1,
  465. 'target_id' => $v->expand_id,
  466. 'state' => $the_post->is_examine,
  467. 'title_tip' => '笔记',
  468. 'state_tip' => [0=>'待审核', 1=>'审核通过', 2=>'驳回'][$the_post->is_examine],
  469. 'can_refuse' => 1,
  470. 'user' => UserUtils::get_cached_user($the_post->user_id),
  471. ];
  472. }else{
  473. WxChat::where('id', $v->id)->forceDelete();
  474. }
  475. }else if($v->expand_type == 102){
  476. $the_comment = WxComment::find($v->expand_id);
  477. if($the_comment){
  478. $v->chat_image = $the_comment->comment_img_url;
  479. $v->chat_content = $the_comment->comment_content;
  480. $v->review = [
  481. 'target_type' => 25,
  482. 'target_id' => '评论不可跳转',
  483. 'state' => $the_comment->comment_state,
  484. 'title_tip' => '笔记评论',
  485. 'state_tip' => [0=>'待审核', 1=>'审核通过', 2=>'驳回'][$the_comment->comment_state],
  486. 'can_refuse' => 1,
  487. 'user' => UserUtils::get_cached_user($the_comment->user_id),
  488. ];
  489. }else{
  490. WxChat::where('id', $v->id)->forceDelete();
  491. }
  492. }else if($v->expand_type == 104){
  493. $the_voter = WxVoter::find($v->expand_id);
  494. if($the_voter){
  495. $v->chat_content = $the_voter->title;
  496. if($the_voter->banners){
  497. $v->chat_image = _array_key($the_voter->banners, 0, '');
  498. }
  499. $v->review = [
  500. 'target_type' => 33,
  501. 'target_id' => $v->expand_id,
  502. 'state' => $the_voter->status,
  503. 'title_tip' => '选票',
  504. 'state_tip' => [0=>'审核中', 1=>'推送中', 2=>'驳回'][$the_voter->status],
  505. 'can_refuse' => 0,
  506. 'user' => UserUtils::get_cached_user($the_voter->user_id),
  507. ];
  508. }else{
  509. WxChat::where('id', $v->id)->forceDelete();
  510. }
  511. }else if($v->expand_type == 105){
  512. $player = WxVoterPlayer::find($v->expand_id);
  513. if($player){
  514. $v->chat_content = $player->show_name;
  515. if($player->img_urls){
  516. $v->chat_image = _array_key($player->img_urls, 0, '');
  517. }
  518. $v->review = [
  519. 'target_type' => 6,
  520. 'target_id' => '/pagesV/voter/detail/player?id='.$v->expand_id,
  521. 'state' => $player->status,
  522. 'title_tip' => '选票选手',
  523. 'state_tip' => [0=>'审核中', 1=>'推送中', 2=>'驳回'][$player->status],
  524. 'can_refuse' => 0,
  525. 'user' => UserUtils::get_cached_user($player->user_id),
  526. ];
  527. }else{
  528. WxChat::where('id', $v->id)->forceDelete();
  529. }
  530. }else if($v->expand_type == 106){
  531. $the_used = WxUsedGood::find($v->expand_id);
  532. if($the_used){
  533. $v->chat_content = $the_used->title;
  534. if($the_used->image_urls){
  535. $v->chat_image = _array_key($the_used->image_urls[0], 'url', '');
  536. }
  537. $v->review = [
  538. 'target_type' => 28,
  539. 'target_id' => $v->expand_id,
  540. 'state' => $the_used->status == 99 ? 2 : ($the_used->status === 0 ? 0 : 1),
  541. 'title_tip' => '闲置商品',
  542. 'state_tip' => FieldUtils::usedGoodState()[$the_used->status],
  543. 'can_refuse' => 1,
  544. 'user' => UserUtils::get_cached_user($the_used->user_id),
  545. ];
  546. }else{
  547. WxChat::where('id', $v->id)->forceDelete();
  548. }
  549. }else if($v->expand_type == 107){
  550. $the_circle = WxCircle::find($v->expand_id);
  551. if($the_circle){
  552. $v->chat_content = $the_circle->circle_name;
  553. $v->chat_image = $the_circle->head_portrait;
  554. $v->review = [
  555. 'target_type' => 2,
  556. 'target_id' => $v->expand_id,
  557. 'state' => $the_circle->circle_state,
  558. 'title_tip' => '新建圈子',
  559. 'state_tip' => [0=>'审核中', 1=>'推送中', 2=>'驳回'][$the_circle->circle_state],
  560. 'can_refuse' => 1,
  561. 'user' => UserUtils::get_cached_user($the_circle->user_id),
  562. ];
  563. }else{
  564. WxChat::where('id', $v->id)->forceDelete();
  565. }
  566. }else if($v->expand_type == 108){
  567. $student = WxUserStudentCertify::where('user_id', $v->expand_id)->first();
  568. if($student){
  569. $v->chat_content = $student->school;
  570. $v->chat_image = $student->picture;
  571. $v->review = [
  572. 'target_type' => 25,
  573. 'target_id' => '学生认证暂不可跳转',
  574. 'state' => $student->status,
  575. 'title_tip' => '学生认证',
  576. 'state_tip' => [0=>'待审核', 1=>'已通过', 2=>'驳回'][$student->status],
  577. 'can_refuse' => 0,
  578. 'user' => UserUtils::get_cached_user($student->user_id),
  579. ];
  580. }else{
  581. WxChat::where('id', $v->id)->forceDelete();
  582. }
  583. }else if($v->expand_type == 109){
  584. $authentication = WxAuthentication::find($v->expand_id);
  585. if($authentication){
  586. $v->chat_content = $authentication->name;
  587. $v->chat_image = $authentication->identity_picture;
  588. $v->review = [
  589. 'target_type' => 25,
  590. 'target_id' => '身份认证暂不可跳转',
  591. 'state' => $authentication->authentication_state,
  592. 'title_tip' => '身份认证',
  593. 'state_tip' => [0=>'审核中', 1=>'已通过', 2=>'驳回'][$authentication->authentication_state],
  594. 'can_refuse' => 0,
  595. 'user' => UserUtils::get_cached_user($authentication->user_id),
  596. ];
  597. }else{
  598. WxChat::where('id', $v->id)->forceDelete();
  599. }
  600. }else if($v->expand_type == 110){
  601. $club = WxClub::where('id', $v->expand_id)->first();
  602. if($club){
  603. $v->chat_content = _empty_default_($club->name_tmp, $club->name);
  604. $v->chat_image = _empty_default_($club->avatar_tmp, $club->avatar);
  605. $v->review = [
  606. 'target_type' => 35,
  607. 'target_id' => $v->expand_id,
  608. 'state' => $club->status,
  609. 'title_tip' => '俱乐部审核',
  610. 'state_tip' => [0=>'待审核', 1=>'已通过', 2=>'驳回'][$club->status],
  611. 'can_refuse' => 0,
  612. 'user' => UserUtils::get_cached_user($club->user_id),
  613. ];
  614. }else{
  615. WxChat::where('id', $v->id)->forceDelete();
  616. }
  617. }
  618. if(_empty_($v->chat_image)){
  619. $v->chat_image = Settings::get('img_default', '').'?imageMogr2/thumbnail/200x200/crop/200x200/gravity/center/format/jpg/interlace/0/quality/100';
  620. }else{
  621. $v->chat_image = $v->chat_image.'?imageMogr2/thumbnail/200x200/crop/200x200/gravity/center/format/jpg/interlace/0/quality/100';
  622. }
  623. }
  624. if(!_empty_($v->chat_audio_length)){
  625. $min = (int)($v->chat_audio_length / 60);
  626. if($min < 10){
  627. $min = '0'.$min;
  628. }
  629. $sec = $v->chat_audio_length % 60;
  630. if($sec < 10){
  631. $sec = '0'.$sec;
  632. }
  633. $v->chat_audio_length = $min.':'.$sec;
  634. }
  635. if(!_empty_($v->refer_id)){
  636. if($v->refer_id && $v->refer_id > 0){
  637. $refer_post = WxPost::find($v->refer_id);
  638. if($refer_post){
  639. PostUtils::mediaProcess($refer_post, $uid, 15, 0, true);
  640. if($refer_post->posts_content_raw){
  641. $refer_post->posts_content_raw = strip_tags($refer_post->posts_content_raw);
  642. }
  643. $v->refer = $refer_post;
  644. }else{
  645. WxChat::where('id', $v->id)->delete();
  646. }
  647. }else{
  648. $v->refer = null;
  649. }
  650. }
  651. if(!_empty_($v->adoption_apply_id)){
  652. if($v->adoption_apply_id && $v->adoption_apply_id > 0){
  653. $adoption_reply = WxPetsAdoptionApply::find($v->adoption_apply_id);
  654. if($adoption_reply){
  655. $v->adoption_reply = PetUtils::adoption_apply_process($adoption_reply, $uid);
  656. $v->adoption_reply->adoption = PetUtils::adoption_process(WxPetsAdoption::find($adoption_reply->pet_adoption_id));
  657. }else{
  658. WxChat::where('id', $v->id)->delete();
  659. }
  660. }else{
  661. $v->adoption_reply = null;
  662. }
  663. }
  664. if(!_empty_($v->used_good_id)){
  665. $user_good = WxUsedGood::where('id', $v->used_good_id)->first();
  666. if($user_good){
  667. ShopUtils::used_process($user_good);
  668. if($user_good->pic){
  669. $user_good->pic = $user_good->pic.'?imageMogr2/quality/100/format/webp/interlace/0/crop/200x200/gravity/center/thumbnail/200x';
  670. }
  671. $user_good->servicer_word = '亲,想要可以直接拍';
  672. $v->used_good = $user_good;
  673. }else{
  674. WxChat::where('id', $v->id)->delete();
  675. }
  676. }
  677. if(!_empty_($v->voter_id)){
  678. $voter = WxVoter::where('id', $v->voter_id)->first();
  679. if($voter){
  680. if($voter->banners){
  681. $voter->pic = $voter->banners[0];
  682. }
  683. $v->voter = $voter;
  684. }else{
  685. WxChat::where('id', $v->id)->delete();
  686. }
  687. }
  688. if(!_empty_($v->ask_order_id)){
  689. //仅占用
  690. }
  691. if(!_empty_($v->shop_good_id)){
  692. $data = WxShopGoods::where('id', $v->shop_good_id)->first()->toArray();
  693. if($data) {
  694. // 服务
  695. if (isset($data['service_id'])) {
  696. $service_tip = '';
  697. $data['service'] = WxShopService::whereIn('id', json_decode($data['service_id'], true))->get(['name', 'intro'])->map(function ($s) use (&$service_tip){
  698. if(_empty_($service_tip)){
  699. $service_tip .= '"'.$s->name.'"';
  700. }else{
  701. $service_tip .= ',' . '"'.$s->name.'"';
  702. }
  703. return $s;
  704. });
  705. if($service_tip){
  706. $service_tip = '亲,我们这款商品的服务为' . $service_tip . '~';
  707. }
  708. $data['service_tip'] = $service_tip;
  709. if($data['pic'] && is_array($data['pic']) && count($data['pic']) > 0){
  710. $data['pic'][0] = $data['pic'][0].'?imageMogr2/quality/100/format/webp/interlace/0/crop/200x200/gravity/center/thumbnail/200x';
  711. }
  712. } else {
  713. $data['service'] = null;
  714. $data['service_tip'] = '';
  715. }
  716. $data['user_purchase'] = ShopUtils::user_buy_goods_times($uid, $data['id']);
  717. $v->shop_good = $data;
  718. }
  719. }
  720. if(!_empty_($v->contact_apply_id)){
  721. if($v->contact_apply_id && $v->contact_apply_id > 0){
  722. $contact_apply = WxUserContactApply::find($v->contact_apply_id);
  723. if($contact_apply){
  724. if($contact_apply->user_id == $uid){
  725. // 己方发送
  726. $title = '申请查看对方的';
  727. if($contact_apply->is_phone == 1){
  728. $title .= '手机号';
  729. }
  730. if($contact_apply->is_wechat_account == 1){
  731. if($contact_apply->is_phone == 1){
  732. $title .= '、微信号';
  733. }else{
  734. $title .= '微信号';
  735. }
  736. }
  737. $contact_apply->title = $title;
  738. }else{
  739. $title = '我申请查看你的';
  740. if($contact_apply->is_phone == 1){
  741. $title .= '手机号';
  742. }
  743. if($contact_apply->is_wechat_account == 1){
  744. if($contact_apply->is_phone == 1){
  745. $title .= '、微信号';
  746. }else{
  747. $title .= '微信号';
  748. }
  749. }
  750. $contact_apply->title = $title;
  751. }
  752. $contact_apply->status_tip = '申请中';
  753. if($contact_apply->status == 1){
  754. $contact_apply->status_tip = '已同意';
  755. }else if($contact_apply->status == 2){
  756. $contact_apply->status_tip = '已过期';
  757. }
  758. $v->contact_apply = $contact_apply;
  759. }else{
  760. $v->contact_apply = null;
  761. }
  762. }else{
  763. $v->contact_apply = null;
  764. }
  765. }
  766. $allowable[] = [
  767. 'text' => '复制',
  768. 'type' => 'copy',
  769. 'id' => $v->id,
  770. 'raw' => strip_tags($v->chat_content),
  771. 'icon' => 'mini-icon mini-fuzhi'
  772. ];
  773. $v->allowable = $allowable;
  774. return $v;
  775. });
  776. return $this->success($data);
  777. }
  778. /**
  779. * 查询用户问答订单聊天记录
  780. * @param Request $request
  781. * @return \Illuminate\Http\JsonResponse
  782. */
  783. public function getUserAskOrderChat(Request $request, $limit = 6)
  784. {
  785. $uid = $request->uid;
  786. $oid = $request->oid;
  787. $ask_order_id = $request->ask_order_id;
  788. /*if(_empty_($uid) || _empty_($oid)){
  789. return $this->fail(200001);
  790. }
  791. $data = WxChat::where(function($query) use ($uid, $oid) {
  792. $query->where(function ($query) use ($uid, $oid) {
  793. $query->where('user_id', $uid)
  794. ->where('object_id', $oid);
  795. })
  796. ->orWhere(function ($query) use ($uid, $oid) {
  797. $query->where('user_id', $oid)
  798. ->where('object_id', $uid);
  799. });
  800. })*/
  801. $data = WxChat::where('expand_type',9) //只要问答订单的
  802. ->where('expand_id',$ask_order_id) //只要问答订单的
  803. ->orderByRaw('id DESC')
  804. ->simplePaginate($limit);
  805. //问答订单详情
  806. $ask_order = Db::table('ask_order')->where('id',$ask_order_id)->first();
  807. $data->map(function ($v) use ($uid,$ask_order){
  808. //区分用户和答主
  809. $v->ask_order_role = 'user';
  810. if($ask_order){
  811. if($ask_order->user_id == $v->user_id){
  812. $v->ask_order_role = 'user';
  813. }elseif($ask_order->blogger_user_id == $v->user_id){
  814. $v->ask_order_role = 'blogger';
  815. }
  816. }
  817. $v->type = 'user';
  818. $imgList = array();
  819. array_push($imgList, ['img_url' => $v->chat_image]);
  820. $v->imgList = $imgList;
  821. $v->user = WxUser::where('id', $v->user_id)->first(FieldUtils::userInfoColums());
  822. $v->datetime = format_datetime($v->created_at);
  823. if($v->chat_audio_url && $v->chat_audio_length){
  824. $v->mode = 'audio';
  825. }else if($v->chat_image){
  826. $v->mode = 'img';
  827. }else{
  828. $v->mode = 'text';
  829. }
  830. if(!_empty_($v->chat_audio_length)){
  831. $min = (int)($v->chat_audio_length / 60);
  832. if($min < 10){
  833. $min = '0'.$min;
  834. }
  835. $sec = $v->chat_audio_length % 60;
  836. if($sec < 10){
  837. $sec = '0'.$sec;
  838. }
  839. $v->chat_audio_length = $min.':'.$sec;
  840. }
  841. $allowable[] = [
  842. 'text' => '复制',
  843. 'type' => 'copy',
  844. 'id' => $v->id,
  845. 'raw' => strip_tags($v->chat_content),
  846. 'icon' => 'mini-icon mini-fuzhi'
  847. ];
  848. $v->allowable = $allowable;
  849. return $v;
  850. });
  851. return $this->success($data);
  852. }
  853. /**
  854. * 已读对应用户信息
  855. * @param Request $request
  856. * @return \Illuminate\Http\JsonResponse
  857. */
  858. public function readUserChat(Request $request)
  859. {
  860. $uid = $request->uid;
  861. $oid = $request->oid;
  862. if(_empty_($oid)){
  863. return $this->fail(200001);
  864. }
  865. $data = WxChat::where([['user_id','=', $oid],['object_id', '=', $uid],['expand_type', '!=', 9]])->update(['is_read' => 1]);
  866. return $this->success($data);
  867. }
  868. /**
  869. * 已读对应用户咨询信息
  870. * @param Request $request
  871. * @return \Illuminate\Http\JsonResponse
  872. */
  873. public function readAskOrderChat(Request $request)
  874. {
  875. $uid = $request->uid;
  876. $oid = $request->oid;
  877. $ask_order_id = $request->ask_order_id;
  878. if(_empty_($oid) || _empty_($ask_order_id)){
  879. return $this->fail(200001);
  880. }
  881. $data = WxChat::where([['user_id','=', $oid],['object_id', '=', $uid],['expand_type', '=', 9],['expand_id', '=', $ask_order_id]])->update(['is_read' => 1]);
  882. return $this->success($data);
  883. }
  884. /**
  885. * 用户删除聊天记录
  886. */
  887. public function userDelMessage(Request $request)
  888. {
  889. $uid = $request->uid;
  890. $userid = $request->userid;
  891. $data = WxChat::where(function ($query) use ($uid, $userid) {
  892. $query->where('user_id', $uid)
  893. ->where('object_id', $userid);
  894. })
  895. ->orWhere(function ($query) use ($uid, $userid) {
  896. $query->where('user_id', $userid)
  897. ->where('object_id', $uid);
  898. })->update(['chat_state' => 1]);
  899. return $this->success($data);
  900. }
  901. }