Service.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\controller\service;
  3. use app\extra\basic\Base;
  4. use app\extra\tools\CodeExtend;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\saas\SaasChatMsg;
  7. use app\model\saas\SaasChatStore;
  8. use app\model\saas\SaasOrderPrice;
  9. use app\service\saas\ChatStoreService;
  10. use DI\Attribute\Inject;
  11. use LinFly\Annotation\Attributes\Route\Controller;
  12. use LinFly\Annotation\Attributes\Route\GetMapping;
  13. use LinFly\Annotation\Attributes\Route\Middleware;
  14. use LinFly\Annotation\Attributes\Route\PostMapping;
  15. use support\Request;
  16. use support\Response;
  17. use Webman\Push\Api;
  18. #[Controller("/api/service/chat"),Middleware(AuthMiddleware::class)]
  19. class Service extends Base
  20. {
  21. #[Inject]
  22. protected ChatStoreService $service;
  23. #[Inject]
  24. protected SaasChatStore $model;
  25. #[GetMapping('list')]
  26. public function getServiceData(Request $request): Response
  27. {
  28. try {
  29. $param = $request->all();
  30. $param['poi_id'] = $request->user['store_id'];
  31. $param['service_id'] = $request->user['id'];
  32. $data = $this->service->setModel()->getList($param,['user' => function($query){
  33. $query->field("openid,nickname,mobile,create_at");
  34. }],true,['last','avatar'],['last' => function($data,$resp) use($param){
  35. $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();
  36. if ($last->isEmpty()) {
  37. return ['type' => "text","content"=>"无",'num' => 0,'time' => time(),"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
  38. }
  39. return ["content"=> $last['content'],'num' => 0,"create_at"=> formatTime($last['create_at']),"time"=> strtotime($last['create_at']),'type' => $last['type']];
  40. },'avatar' => function(){
  41. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  42. }]);
  43. return successTrans(100010,pageFormat($data),200);
  44. } catch (\Throwable $th) {
  45. return error($th->getMessage());
  46. }
  47. }
  48. #[GetMapping('msg')]
  49. public function getMessageData(Request $request): Response
  50. {
  51. try {
  52. $param = $request->all();
  53. $msg = (new SaasChatMsg)->where(['openid' => $param['openid'],'poi_id' => $request->user['store_id']])->append(['avatar','time'])->withAttr(['avatar' => function(){
  54. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  55. },'time' => function($resp,$data){
  56. return strtotime($data['create_at']) * 1000;
  57. }])->order("id","desc")->paginate([
  58. "list_rows" => $param['size'] ?? 10,
  59. "page" => $param['page'] ?? 1,
  60. ]);
  61. return successTrans(100010,pageFormatMsg($msg),200);
  62. } catch (\Throwable $th) {
  63. return error($th->getMessage());
  64. }
  65. }
  66. #[PostMapping('send')]
  67. public function sendMessageData(Request $request): Response
  68. {
  69. try {
  70. $param = $this->_valid([
  71. "content.require" => trans("empty.require"),
  72. "groupId.require" => trans("empty.require"),
  73. "openid.require" => trans("empty.require"),
  74. "type.require" => trans("empty.require"),
  75. ],"post");
  76. if (!is_array($param)) return error($param);
  77. $str = preg_replace('/\s+/','',$param['content']);
  78. if (empty($str)) return error("请勿发送空消息");
  79. $state = (new SaasChatMsg)->insertGetId([
  80. "source" => 2,
  81. "openid" => $param['openid'],
  82. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  83. "type" => $param['type'],
  84. "msgId" => time(),
  85. "poi_id" => $param['groupId'],
  86. "service_id" => $request->user['id'],
  87. ]);
  88. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  89. $api->trigger("user-{$param['openid']}","message",[
  90. "type" => $param['type'],
  91. "time" => time() * 1000,
  92. "msgId" => time(),
  93. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  94. "source" => 2,
  95. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  96. "poi_id" => $param['groupId'],
  97. "openid" => $param['openid'],
  98. "service_id" => $request->user['id'],
  99. "create_at" => formatTime(time()),
  100. ]);
  101. return successTrans("success.data",['time' => formatTime(time())]);
  102. } catch (\Throwable $th) {
  103. return error($th->getMessage());
  104. }
  105. }
  106. #[PostMapping('send/price')]
  107. public function sendPriceData(Request $request)
  108. {
  109. try {
  110. $param = $this->_valid([
  111. "name.require" => trans("empty.require"),
  112. "price.require" => trans("empty.require"),
  113. "groupId.require" => trans("empty.require"),
  114. "openid.require" => trans("empty.require"),
  115. "type.require" => trans("empty.require"),
  116. ],"post");
  117. if (!is_array($param)) return error($param);
  118. $orderSn = CodeExtend::uniqidDate(18,"P");
  119. $msgId = time();
  120. $priceState = (new SaasOrderPrice)->insertGetId([
  121. "openid" => $param['openid'],
  122. "poi_id" => $param['groupId'],
  123. "service_id" => $request->user['id'],
  124. "name" => $param['name'],
  125. "price" => $param['price'] * 100,
  126. "image" => "https://jymini.oss-cn-guangzhou.aliyuncs.com/mini/chajia.jpg",
  127. "order_sn" => $orderSn,
  128. "msgId" => $msgId,
  129. ]);
  130. if (!$priceState) return errorTrans("error.data");
  131. $msgData = [
  132. "img" => "https://jymini.oss-cn-guangzhou.aliyuncs.com/mini/chajia.jpg",
  133. "name" => $param['name'],
  134. "order" => $orderSn,
  135. "price" => $param['price'],
  136. "msgId" => $msgId,
  137. "status" => 0
  138. ];
  139. $state = (new SaasChatMsg)->insertGetId([
  140. "source" => 2,
  141. "openid" => $param['openid'],
  142. "content" => json_encode($msgData),
  143. "type" => $param['type'],
  144. "msgId" => $msgId,
  145. "poi_id" => $param['groupId'],
  146. "service_id" => $request->user['id'],
  147. ]);
  148. if (!$state) return errorTrans("error.data");
  149. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  150. $api->trigger("user-{$param['openid']}","message",[
  151. "type" => $param['type'],
  152. "time" => time() * 1000,
  153. "msgId" => time(),
  154. "content" => json_encode($msgData),
  155. "source" => 2,
  156. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  157. "poi_id" => $param['groupId'],
  158. "openid" => $param['openid'],
  159. "service_id" => $request->user['id'],
  160. "create_at" => formatTime(time()),
  161. ]);
  162. return successTrans("success.data",$msgData);
  163. } catch (\Throwable $throwable) {
  164. return error($throwable->getMessage());
  165. }
  166. }
  167. }