| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?php
- namespace app\controller\service;
- use app\extra\basic\Base;
- use app\middleware\AuthMiddleware;
- use app\model\saas\SaasChatMsg;
- use app\model\saas\SaasChatStore;
- use app\model\system\SystemUser;
- use app\service\saas\ChatStoreService;
- use app\service\saas\GoodsService;
- use DI\Attribute\Inject;
- use LinFly\Annotation\Attributes\Route\Controller;
- use LinFly\Annotation\Attributes\Route\GetMapping;
- use LinFly\Annotation\Attributes\Route\Middleware;
- use LinFly\Annotation\Attributes\Route\PostMapping;
- use support\Request;
- use support\Response;
- use think\facade\Db;
- use Webman\Push\Api;
- #[Controller("/api/chat"),Middleware(AuthMiddleware::class)]
- class Chat extends Base
- {
- protected array $noNeedLogin = ["testData"];
- #[Inject]
- protected ChatStoreService $service;
- #[Inject]
- protected SaasChatStore $model;
- #[Inject]
- protected GoodsService $goodsService;
- #[GetMapping('user')]
- public function getChatUser(Request $request): Response
- {
- try {
- $param = $request->all();
- $param['poi_id'] = $request->user['store_id'];
- $param['service_id'] = $request->user['id'];
- $param['order'] = "descending";
- $param['field'] = "last_at";
- if ($param['type'] == 1) {
- $param['timeEq'] = date("Y-m-d");
- } else {
- $param['timeLt'] = date("Y-m-d");
- }
- unset($param['type']);
- $data = $this->service->setModel()->getList($param,['user' => function($query){
- $query->field("openid,nickname,avatar,mobile,create_at");
- }],true,['avatar','last','lastTime'],['avatar' => function(){
- return sConf("service.logo");
- },'last' => function ($data,$next) use ($param) {
- $map = ['poi_id' => $next['poi_id'], 'service_id' => $param['service_id'], 'openid' => $next['openid']];
- $unreadSql = Db::table('saas_chat_msg')
- ->where($map)
- ->where('is_read', 0)
- ->field('COUNT(*)')
- ->buildSql();
- return (new SaasChatMsg)->where($map)->field(['LEFT(content, 12) as content', 'create_at', 'type', Db::raw($unreadSql . ' AS num')])->order('create_at',"desc")->find();
- },'lastTime' => function ($data,$next) {
- return formatTime($next['last_at']);
- }]);
- return successTrans(100010,pageFormat($data),200);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping('order')]
- public function getMsgOrder(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "msgId.require" => trans("empty.require")
- ]);
- if (!is_array($param)) return error($param);
- $productType = $this->goodsService->productType();
- $chatStore = $this->model->where("id",$param['msgId'])->with(['orders' => function($query) use($productType){
- $query->field("*")->with(['product' => function($query) use($productType){
- $query->field("product_id,product_name,product_type")->append(['types'])->withAttr(['types' => function($query,$resp) use($productType){
- $productTypeArr = [];
- foreach ($productType as $val) {
- $productTypeArr[$val['key']] = $val['name'];
- }
- return $productTypeArr[$resp['product_type']]??'';
- }]);
- }]);
- }])->findOrEmpty();
- if ($chatStore->isEmpty()) return error("error");
- return success("ok",['order' => $chatStore['orders']]);
- } catch (\Throwable $th) {
- return error($th->getMessage());
- }
- }
- #[GetMapping('msg')]
- public function getMessageData(Request $request): Response
- {
- try {
- $param = $request->all();
- $msg = (new SaasChatMsg)->where(['openid' => $param['openid'],'poi_id' => $request->user['store_id']])->append(['avatar','time','userType','msgType'])->withAttr(['avatar' => function(){
- return sConf("service.logo");
- },'time' => function($resp,$data){
- return strtotime($data['create_at']) * 1000;
- },'userType' => function($resp,$data){
- if ($data['source'] == 1) {
- return "friend";
- } else {
- return "self";
- }
- },'msgType' => function($resp,$data){
- return $data['type'];
- }])->with(['nick' => function ($query) {
- $query->field("id,truename");
- },'user' => function ($query) {
- $query->field("openid,nickname,avatar");
- }])->order("id","desc")->paginate([
- "list_rows" => $param['size'] ?? 10,
- "page" => $param['page'] ?? 1,
- ]);
- return successTrans(100010,pageFormatMsg($msg),200);
- } catch (\Throwable $th) {
- return error($th->getMessage());
- }
- }
- #[PostMapping('text')]
- public function sendTextMsg(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "content.require" => trans("empty.require"),
- "groupId.require" => trans("empty.require"),
- "openid.require" => trans("empty.require"),
- "type.require" => trans("empty.require"),
- ],$request->method());
- if (!is_array($param)) return error($param);
- $str = preg_replace('/\s+/','',$param['content']);
- if (empty($str)) return error("请勿发送空消息");
- (new SaasChatMsg)->insertGetId([
- "source" => 2,
- "openid" => $param['openid'],
- "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
- "type" => $param['type'],
- "msgId" => md5($param['openid'].time()),
- "poi_id" => $param['groupId'],
- "service_id" => $request->user['id'],
- ]);
- (new SaasChatMsg)->where(['openid' => $param['openid'],'service_id' => $request->user['id']])->save(['is_read' => 1]);
- (new SystemUser)->where("id",$request->user['id'])->update(['last_msg_at' => getDateFull()]);
- $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
- $api->trigger("user-".md5($param['openid']),"message",[
- "type" => $param['type'],
- "content" => is_array($param['content'])?json_encode($param['content']):$param['content'],
- "openid" => $param['openid'],
- "nick" => [],
- "user" => [],
- "userType" => "friend",
- "avatar" => sConf("service.logo"),
- "time" => time() * 1000,
- "msgId" => md5($param['openid'].time()),
- ]);
- return successTrans("success.data",['time' => formatTime(time())]);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping('line')]
- public function getUserLine(Request $request): Response
- {
- try {
- $user = (new SystemUser)->where("id",$request->user['id'])->findOrEmpty();
- if ($user->isEmpty()) return error("数据不存在");
- return success("ok",['line' => $user['is_line']]);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[PostMapping('line/set')]
- public function setUserLine(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "status.require" => trans("empty.require"),
- ],$request->method());
- if (!is_array($param)) return error($param);
- $user = (new SystemUser)->where("id",$request->user['id'])->findOrEmpty();
- if ($user->isEmpty()) return error("数据不存在");
- $user->is_line = $param['status'];
- $user->last_active_at = getDateFull();
- $state = $user->save();
- if (!$state) return error('操作失败');
- return success("ok",['line' => $user['is_line']]);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping('search')]
- public function searchChat(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "key.require" => trans("empty.require"),
- "type.require" => trans("empty.require"),
- "page.default" => 1,
- "size.default" => 10
- ],$request->method());
- if (!is_array($param)) return error($param);
- if ($param['type'] == 'user') {
- $data = (new SaasChatStore)->where(['service_id' => $request->user['id']])->where("nickname","like","%{$param['key']}%")->with(['user' => function ($query) {
- $query->field("openid,nickname,avatar");
- }])->paginate([
- "list_rows" => $param['size'],
- "page" => $param['page']
- ]);
- } else {
- $data = (new SaasChatMsg)->where(['service_id' => $request->user['id']])->where("content","like","%{$param['key']}%")->with(['user' => function ($query) {
- $query->field("openid,nickname,avatar");
- }])->paginate([
- "list_rows" => $param['size'],
- "page" => $param['page']
- ]);
- }
- return success('ok',$data->toArray());
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[PostMapping('check')]
- public function searchUserCheck(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "openid.require" => trans("empty.require"),
- "poi_id.require" => trans("empty.require"),
- "service_id.require" => trans("empty.require"),
- ],$request->method());
- if (!is_array($param)) return error($param);
- $data = $this->service->setModel()->getList($param,['user' => function($query){
- $query->field("openid,nickname,avatar,mobile,create_at");
- }],false,['avatar','last','lastTime'],['avatar' => function(){
- return sConf("service.logo");
- },'last' => function ($data,$next) use ($param) {
- $map = ['poi_id' => $next['poi_id'], 'service_id' => $param['service_id'], 'openid' => $next['openid']];
- $unreadSql = Db::table('saas_chat_msg')
- ->where($map)
- ->where('is_read', 0)
- ->field('COUNT(*)')
- ->buildSql();
- return (new SaasChatMsg)->where($map)->field(['LEFT(content, 12) as content', 'create_at', 'type', Db::raw($unreadSql . ' AS num')])->order('create_at',"desc")->find();
- },'lastTime' => function ($data,$next) {
- return formatTime($next['last_at']);
- }]);
- return success('ok',$data);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping('ts')]
- public function testData()
- {
- $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
- $api->trigger("user-".md5("_0001DyNWbn2M-x9ttXGPPZQAydvlcv8-UDd"),"message",[
- "type" => "image",
- "content" => sConf("service.logo"),
- "openid" => "_0001DyNWbn2M-x9ttXGPPZQAydvlcv8-UDd",
- "nick" => [],
- "user" => [],
- "userType" => "friend",
- "avatar" => sConf("service.logo"),
- "time" => time() * 1000,
- "msgId" => time(),
- ]);
- }
- }
|