UserChatReplyForm.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Admin\Forms\User;
  3. use App\Http\Controllers\Api\Repositories\WxContRepositories;
  4. use App\Models\WxChat;
  5. use App\Wen\Utils\GatewayUtils;
  6. use App\Wen\Utils\UserUtils;
  7. use Dcat\Admin\Contracts\LazyRenderable;
  8. use Dcat\Admin\Traits\LazyWidget;
  9. use Dcat\Admin\Widgets\Form;
  10. class UserChatReplyForm extends Form implements LazyRenderable
  11. {
  12. use LazyWidget;
  13. /**
  14. * 处理表单请求.
  15. *
  16. * @param array $input
  17. *
  18. * @return mixed
  19. */
  20. public function handle(array $input)
  21. {
  22. $id = $input['id'];
  23. $content = $input['content'];
  24. $img = $input['img'];
  25. if(_empty_($id)){
  26. return $this->response()
  27. ->error('参数错误');
  28. }
  29. if(_empty_($content) && _empty_($img)){
  30. return $this->response()
  31. ->error('参数错误');
  32. }
  33. $chat = WxChat::find($id);
  34. $oid = $chat->user_id;
  35. $uid = $chat->object_id;
  36. $model = WxContRepositories::add($uid, $oid, $content, $img, '', '', 0);//普通聊天
  37. if($model){
  38. $websocket_id = GatewayUtils::uid2client_id($oid);
  39. if($websocket_id){
  40. GatewayUtils::success($websocket_id, 5, ['chat_content'=>_mini_emoji($content, true), 'chat_image'=>$img,
  41. 'from_user'=>UserUtils::get_cached_user($uid) ]);
  42. }
  43. return $this->response()
  44. ->success('回复成功.')
  45. ->refresh();
  46. }
  47. return $this->response()
  48. ->error('回复失败.');
  49. }
  50. /**
  51. * 构建表单.
  52. */
  53. public function form()
  54. {
  55. $this->html(function (){
  56. return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">以被聊天的身份向发起人聊天</div>';
  57. }, '');
  58. $this->hidden('id')->value($this->payload["id"]);
  59. $this->textarea('content', "内容");
  60. $this->image('img', '图片')->help('不能同时发内容和图片')->url('files/uploads')->uniqueName()->autoUpload();
  61. }
  62. }