|
|
@@ -0,0 +1,196 @@
|
|
|
+<?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('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(),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|