Service.php 10 KB

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