Chat.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\controller\service;
  3. use app\extra\basic\Base;
  4. use app\middleware\AuthMiddleware;
  5. use app\model\saas\SaasChatMsg;
  6. use app\model\saas\SaasChatStore;
  7. use app\model\system\SystemUser;
  8. use app\service\saas\ChatStoreService;
  9. use app\service\saas\GoodsService;
  10. use DI\Attribute\Inject;
  11. use LinFly\Annotation\Attributes\Route\Controller;
  12. use LinFly\Annotation\Attributes\Route\GetMapping;
  13. use LinFly\Annotation\Attributes\Route\Middleware;
  14. use LinFly\Annotation\Attributes\Route\PostMapping;
  15. use support\Request;
  16. use support\Response;
  17. use think\facade\Db;
  18. use Webman\Push\Api;
  19. #[Controller("/api/chat"),Middleware(AuthMiddleware::class)]
  20. class Chat extends Base
  21. {
  22. protected array $noNeedLogin = ["testData"];
  23. #[Inject]
  24. protected ChatStoreService $service;
  25. #[Inject]
  26. protected SaasChatStore $model;
  27. #[Inject]
  28. protected GoodsService $goodsService;
  29. #[GetMapping('user')]
  30. public function getChatUser(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. if ($param['type'] == 1) {
  39. $param['timeEq'] = date("Y-m-d");
  40. } else {
  41. $param['timeLt'] = date("Y-m-d");
  42. }
  43. unset($param['type']);
  44. $data = $this->service->setModel()->getList($param,['user' => function($query){
  45. $query->field("openid,nickname,avatar,mobile,create_at");
  46. }],true,['avatar','last','lastTime'],['avatar' => function(){
  47. return sConf("service.logo");
  48. },'last' => function ($data,$next) use ($param) {
  49. $map = ['poi_id' => $next['poi_id'], 'service_id' => $param['service_id'], 'openid' => $next['openid']];
  50. $unreadSql = Db::table('saas_chat_msg')
  51. ->where($map)
  52. ->where('is_read', 0)
  53. ->field('COUNT(*)')
  54. ->buildSql();
  55. return (new SaasChatMsg)->where($map)->field(['LEFT(content, 12) as content', 'create_at', 'type', Db::raw($unreadSql . ' AS num')])->order('create_at',"desc")->find();
  56. },'lastTime' => function ($data,$next) {
  57. return formatTime($next['last_at']);
  58. }]);
  59. return successTrans(100010,pageFormat($data),200);
  60. } catch (\Throwable $throwable) {
  61. return error($throwable->getMessage());
  62. }
  63. }
  64. #[GetMapping('order')]
  65. public function getMsgOrder(Request $request): Response
  66. {
  67. try {
  68. $param = $this->_valid([
  69. "msgId.require" => trans("empty.require")
  70. ]);
  71. if (!is_array($param)) return error($param);
  72. $productType = $this->goodsService->productType();
  73. $chatStore = $this->model->where("id",$param['msgId'])->with(['orders' => function($query) use($productType){
  74. $query->field("*")->with(['product' => function($query) use($productType){
  75. $query->field("product_id,product_name,product_type")->append(['types'])->withAttr(['types' => function($query,$resp) use($productType){
  76. $productTypeArr = [];
  77. foreach ($productType as $val) {
  78. $productTypeArr[$val['key']] = $val['name'];
  79. }
  80. return $productTypeArr[$resp['product_type']]??'';
  81. }]);
  82. }]);
  83. }])->findOrEmpty();
  84. if ($chatStore->isEmpty()) return error("error");
  85. return success("ok",['order' => $chatStore['orders']]);
  86. } catch (\Throwable $th) {
  87. return error($th->getMessage());
  88. }
  89. }
  90. #[GetMapping('msg')]
  91. public function getMessageData(Request $request): Response
  92. {
  93. try {
  94. $param = $request->all();
  95. $msg = (new SaasChatMsg)->where(['openid' => $param['openid'],'poi_id' => $request->user['store_id']])->append(['avatar','time','userType','msgType'])->withAttr(['avatar' => function(){
  96. return sConf("service.logo");
  97. },'time' => function($resp,$data){
  98. return strtotime($data['create_at']) * 1000;
  99. },'userType' => function($resp,$data){
  100. if ($data['source'] == 1) {
  101. return "friend";
  102. } else {
  103. return "self";
  104. }
  105. },'msgType' => function($resp,$data){
  106. return $data['type'];
  107. }])->with(['nick' => function ($query) {
  108. $query->field("id,truename");
  109. },'user' => function ($query) {
  110. $query->field("openid,nickname,avatar");
  111. }])->order("id","desc")->paginate([
  112. "list_rows" => $param['size'] ?? 10,
  113. "page" => $param['page'] ?? 1,
  114. ]);
  115. return successTrans(100010,pageFormatMsg($msg),200);
  116. } catch (\Throwable $th) {
  117. return error($th->getMessage());
  118. }
  119. }
  120. #[PostMapping('text')]
  121. public function sendTextMsg(Request $request): Response
  122. {
  123. try {
  124. $param = $this->_valid([
  125. "content.require" => trans("empty.require"),
  126. "groupId.require" => trans("empty.require"),
  127. "openid.require" => trans("empty.require"),
  128. "type.require" => trans("empty.require"),
  129. ],$request->method());
  130. if (!is_array($param)) return error($param);
  131. $str = preg_replace('/\s+/','',$param['content']);
  132. if (empty($str)) return error("请勿发送空消息");
  133. (new SaasChatMsg)->insertGetId([
  134. "source" => 2,
  135. "openid" => $param['openid'],
  136. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  137. "type" => $param['type'],
  138. "msgId" => md5($param['openid'].time()),
  139. "poi_id" => $param['groupId'],
  140. "service_id" => $request->user['id'],
  141. ]);
  142. (new SaasChatMsg)->where(['openid' => $param['openid'],'service_id' => $request->user['id']])->save(['is_read' => 1]);
  143. (new SystemUser)->where("id",$request->user['id'])->update(['last_msg_at' => getDateFull()]);
  144. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  145. $api->trigger("user-".md5($param['openid']),"message",[
  146. "type" => $param['type'],
  147. "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
  148. "openid" => $param['openid'],
  149. "nick" => [],
  150. "user" => [],
  151. "userType" => "friend",
  152. "avatar" => sConf("service.logo"),
  153. "time" => time() * 1000,
  154. "msgId" => md5($param['openid'].time()),
  155. ]);
  156. return successTrans("success.data",['time' => formatTime(time())]);
  157. } catch (\Throwable $throwable) {
  158. return error($throwable->getMessage());
  159. }
  160. }
  161. #[GetMapping('ts')]
  162. public function testData()
  163. {
  164. $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
  165. $api->trigger("user-".md5("_0001DyNWbn2M-x9ttXGPPZQAydvlcv8-UDd"),"message",[
  166. "type" => "image",
  167. "content" => sConf("service.logo"),
  168. "openid" => "_0001DyNWbn2M-x9ttXGPPZQAydvlcv8-UDd",
  169. "nick" => [],
  170. "user" => [],
  171. "userType" => "friend",
  172. "avatar" => sConf("service.logo"),
  173. "time" => time() * 1000,
  174. "msgId" => time(),
  175. ]);
  176. }
  177. }