Service.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\controller\service;
  3. use app\extra\basic\Base;
  4. use app\middleware\AuthMiddleware;
  5. use app\model\saas\SaasChatMsg;
  6. use app\model\saas\SaasChatStore;
  7. use app\service\saas\ChatStoreService;
  8. use DI\Attribute\Inject;
  9. use LinFly\Annotation\Attributes\Route\Controller;
  10. use LinFly\Annotation\Attributes\Route\GetMapping;
  11. use LinFly\Annotation\Attributes\Route\Middleware;
  12. use LinFly\Annotation\Attributes\Route\PostMapping;
  13. use support\Request;
  14. use support\Response;
  15. use Webman\Push\Api;
  16. #[Controller("/api/service/chat"),Middleware(AuthMiddleware::class)]
  17. class Service extends Base
  18. {
  19. #[Inject]
  20. protected ChatStoreService $service;
  21. #[Inject]
  22. protected SaasChatStore $model;
  23. #[GetMapping('list')]
  24. public function getServiceData(Request $request): Response
  25. {
  26. try {
  27. $param = $request->all();
  28. $param['poi_id'] = $request->user['store_id'];
  29. $param['service_id'] = $request->user['id'];
  30. $data = $this->service->setModel()->getList($param,['user' => function($query){
  31. $query->field("openid,nickname,mobile,create_at");
  32. }],true,['last','avatar'],['last' => function($data,$resp) use($param){
  33. $last = (new SaasChatMsg)->where(["poi_id" => $resp['poi_id'],"service_id" => $param['service_id'],"openid" => $resp['openid']])->order("create_at desc")->field("content,create_at,type")->findOrEmpty();
  34. if ($last->isEmpty()) {
  35. return ['type' => "text","content"=>"无",'num' => 0,'time' => time(),"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
  36. }
  37. return ["content"=> $last['content'],'num' => 0,"create_at"=> formatTime($last['create_at']),"time"=> strtotime($last['create_at']),'type' => $last['type']];
  38. },'avatar' => function(){
  39. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  40. }]);
  41. return successTrans(100010,pageFormat($data),200);
  42. } catch (\Throwable $th) {
  43. return error($th->getMessage());
  44. }
  45. }
  46. #[GetMapping('msg')]
  47. public function getMessageData(Request $request): Response
  48. {
  49. try {
  50. $param = $request->all();
  51. $msg = (new SaasChatMsg)->where(['openid' => $param['openid'],'poi_id' => $request->user['store_id']])->append(['avatar','time'])->withAttr(['avatar' => function(){
  52. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  53. },'time' => function($resp,$data){
  54. return strtotime($data['create_at']) * 1000;
  55. }])->order("id","desc")->paginate([
  56. "list_rows" => $param['size'] ?? 10,
  57. "page" => $param['page'] ?? 1,
  58. ]);
  59. return successTrans(100010,pageFormatMsg($msg),200);
  60. } catch (\Throwable $th) {
  61. return error($th->getMessage());
  62. }
  63. }
  64. #[PostMapping('send')]
  65. public function sendMessageData(Request $request): Response
  66. {
  67. try {
  68. $param = $this->_valid([
  69. "content.require" => trans("empty.require"),
  70. "groupId.require" => trans("empty.require"),
  71. "openid.require" => trans("empty.require"),
  72. "type.require" => trans("empty.require"),
  73. ],"post");
  74. if (!is_array($param)) return error($param);
  75. $str = preg_replace('/\s+/','',$param['content']);
  76. if (empty($str)) return error("请勿发送空消息");
  77. $state = (new SaasChatMsg)->insertGetId([
  78. "source" => 2,
  79. "openid" => $param['openid'],
  80. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  81. "type" => $param['type'],
  82. "msgId" => time(),
  83. "poi_id" => $param['groupId'],
  84. "service_id" => $request->user['id'],
  85. ]);
  86. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  87. $api->trigger("user-{$param['openid']}","message",[
  88. "type" => $param['type'],
  89. "time" => time() * 1000,
  90. "msgId" => time(),
  91. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  92. "source" => 2,
  93. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  94. "poi_id" => $param['groupId'],
  95. "openid" => $param['openid'],
  96. "service_id" => $request->user['id'],
  97. "create_at" => formatTime(time()),
  98. ]);
  99. return successTrans("success.data",['time' => formatTime(time())]);
  100. } catch (\Throwable $th) {
  101. return error($th->getMessage());
  102. }
  103. }
  104. }