Service.php 9.9 KB

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