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_time'],['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"=> str_cut_ellipsis($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. },'last_time' => function($data,$resp){
  49. return $resp['last_at']?date('Y-m-d',strtotime($resp['last_at'])):'-';
  50. }]);
  51. return successTrans(100010,pageFormat($data),200);
  52. } catch (\Throwable $th) {
  53. return error($th->getMessage());
  54. }
  55. }
  56. #[GetMapping('msg')]
  57. public function getMessageData(Request $request): Response
  58. {
  59. try {
  60. $param = $request->all();
  61. $msg = (new SaasChatMsg)->where(['openid' => $param['openid'],'poi_id' => $request->user['store_id']])->append(['avatar','time'])->withAttr(['avatar' => function(){
  62. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  63. },'time' => function($resp,$data){
  64. return strtotime($data['create_at']) * 1000;
  65. }])->with(['nick' => function ($query) {
  66. $query->field("id,truename");
  67. },'user' => function ($query) {
  68. $query->field("openid,nickname,avatar");
  69. }])->order("id","desc")->paginate([
  70. "list_rows" => $param['size'] ?? 10,
  71. "page" => $param['page'] ?? 1,
  72. ]);
  73. return successTrans(100010,pageFormatMsg($msg),200);
  74. } catch (\Throwable $th) {
  75. return error($th->getMessage());
  76. }
  77. }
  78. #[GetMapping('order')]
  79. public function getMsgOrder(Request $request)
  80. {
  81. try {
  82. $param = $this->_valid([
  83. "msgId.require" => trans("empty.require")
  84. ]);
  85. if (!is_array($param)) return error($param);
  86. $productType = $this->goodsService->productType();
  87. $chatStore = $this->model->where("id",$param['msgId'])->with(['orders' => function($query) use($productType){
  88. $query->field("*")->with(['product' => function($query) use($productType){
  89. $query->field("product_id,product_name,product_type")->append(['types'])->withAttr(['types' => function($query,$resp) use($productType){
  90. $productTypeArr = [];
  91. foreach ($productType as $val) {
  92. $productTypeArr[$val['key']] = $val['name'];
  93. }
  94. return $productTypeArr[$resp['product_type']]??'';
  95. }]);
  96. }]);
  97. }])->findOrEmpty();
  98. if ($chatStore->isEmpty()) return error("error");
  99. return success("ok",['order' => $chatStore['orders']]);
  100. } catch (\Throwable $th) {
  101. return error($th->getMessage());
  102. }
  103. }
  104. #[PostMapping('send')]
  105. public function sendMessageData(Request $request): Response
  106. {
  107. try {
  108. $param = $this->_valid([
  109. "content.require" => trans("empty.require"),
  110. "groupId.require" => trans("empty.require"),
  111. "openid.require" => trans("empty.require"),
  112. "type.require" => trans("empty.require"),
  113. ],"post");
  114. if (!is_array($param)) return error($param);
  115. $str = preg_replace('/\s+/','',$param['content']);
  116. if (empty($str)) return error("请勿发送空消息");
  117. $state = (new SaasChatMsg)->insertGetId([
  118. "source" => 2,
  119. "openid" => $param['openid'],
  120. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  121. "type" => $param['type'],
  122. "msgId" => md5($param['openid'].time()),
  123. "poi_id" => $param['groupId'],
  124. "service_id" => $request->user['id'],
  125. ]);
  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. }