Service.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\model\system\SystemUser;
  10. use app\service\saas\ChatStoreService;
  11. use app\service\saas\GoodsService;
  12. use DI\Attribute\Inject;
  13. use LinFly\Annotation\Attributes\Route\Controller;
  14. use LinFly\Annotation\Attributes\Route\GetMapping;
  15. use LinFly\Annotation\Attributes\Route\Middleware;
  16. use LinFly\Annotation\Attributes\Route\PostMapping;
  17. use support\Request;
  18. use support\Response;
  19. use Webman\Push\Api;
  20. #[Controller("/api/service/chat"),Middleware(AuthMiddleware::class)]
  21. class Service extends Base
  22. {
  23. #[Inject]
  24. protected ChatStoreService $service;
  25. #[Inject]
  26. protected SaasChatStore $model;
  27. #[Inject]
  28. protected GoodsService $goodsService;
  29. // protected array $noNeedLogin = ["getServiceData"];
  30. #[GetMapping('list')]
  31. public function getServiceData(Request $request): Response
  32. {
  33. try {
  34. $param = $request->all();
  35. $param['poi_id'] = $request->user['store_id'];
  36. $param['service_id'] = $request->user['id'];
  37. $param['order'] = "descending";
  38. $param['field'] = "last_at";
  39. $data = $this->service->setModel()->getList($param,['user' => function($query){
  40. $query->field("openid,nickname,avatar,mobile,create_at");
  41. }],true,['last','avatar'],['last' => function($data,$resp) use($param){
  42. $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();
  43. if ($last->isEmpty()) {
  44. return ['type' => "text","content"=>"-",'num' => 0,'time' => time(),"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
  45. }
  46. $total = (new SaasChatMsg)->where(["poi_id" => $resp['poi_id'],"service_id" => $param['service_id'],"openid" => $resp['openid'],'is_read' => 0])->count();
  47. return ["content"=> str_cut_ellipsis($last['content'],12),'num' => $total,"create_at"=> formatTime($last['create_at']),"time"=> strtotime($last['create_at']),'type' => $last['type']];
  48. },'avatar' => function(){
  49. return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
  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. (new SaasChatMsg)->where(['openid' => $param['openid'],'service_id' => $request->user['id']])->save(['is_read' => 1]);
  127. (new SystemUser)->where("id",$request->user['id'])->update(['last_msg_at' => getDateFull()]);
  128. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  129. $api->trigger("user-{$param['openid']}","message",[
  130. "type" => $param['type'],
  131. "time" => time() * 1000,
  132. "msgId" => md5($param['openid'].time()),
  133. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  134. "source" => 2,
  135. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  136. "poi_id" => $param['groupId'],
  137. "openid" => $param['openid'],
  138. "service_id" => $request->user['id'],
  139. "create_at" => formatTime(time())
  140. ]);
  141. return successTrans("success.data",['time' => formatTime(time())]);
  142. } catch (\Throwable $th) {
  143. return error($th->getMessage());
  144. }
  145. }
  146. #[PostMapping('send/price')]
  147. public function sendPriceData(Request $request): Response
  148. {
  149. try {
  150. $param = $this->_valid([
  151. "name.require" => trans("empty.require"),
  152. "price.require" => trans("empty.require"),
  153. "groupId.require" => trans("empty.require"),
  154. "openid.require" => trans("empty.require"),
  155. "type.require" => trans("empty.require"),
  156. ],"post");
  157. if (!is_array($param)) return error($param);
  158. $orderSn = CodeExtend::uniqidDate(18,"P");
  159. $msgId = md5($param['openid'].time());
  160. $priceState = (new SaasOrderPrice)->insertGetId([
  161. "openid" => $param['openid'],
  162. "poi_id" => $param['groupId'],
  163. "service_id" => $request->user['id'],
  164. "name" => $param['name'],
  165. "price" => $param['price'] * 100,
  166. "image" => "https://jymini.oss-cn-guangzhou.aliyuncs.com/mini/chajia.jpg",
  167. "order_sn" => $orderSn,
  168. "msgId" => $msgId,
  169. ]);
  170. if (!$priceState) return errorTrans("error.data");
  171. $msgData = [
  172. "img" => "https://jymini.oss-cn-guangzhou.aliyuncs.com/mini/chajia.jpg",
  173. "name" => $param['name'],
  174. "order" => $orderSn,
  175. "price" => $param['price'],
  176. "msgId" => $msgId,
  177. "status" => 0
  178. ];
  179. $state = (new SaasChatMsg)->insertGetId([
  180. "source" => 2,
  181. "openid" => $param['openid'],
  182. "content" => json_encode($msgData),
  183. "type" => $param['type'],
  184. "msgId" => $msgId,
  185. "poi_id" => $param['groupId'],
  186. "service_id" => $request->user['id'],
  187. ]);
  188. if (!$state) return errorTrans("error.data");
  189. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  190. $api->trigger("user-{$param['openid']}","message",[
  191. "type" => $param['type'],
  192. "time" => time() * 1000,
  193. "msgId" => $msgId,
  194. "content" => json_encode($msgData),
  195. "source" => 2,
  196. "avatar" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png",
  197. "poi_id" => $param['groupId'],
  198. "openid" => $param['openid'],
  199. "service_id" => $request->user['id'],
  200. "create_at" => formatTime(time()),
  201. ]);
  202. return successTrans("success.data",$msgData);
  203. } catch (\Throwable $throwable) {
  204. return error($throwable->getMessage());
  205. }
  206. }
  207. }