Service.php 9.8 KB

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