Kaynağa Gözat

0818-0219-h01

Zory 1 ay önce
ebeveyn
işleme
126b7a3d42

+ 2 - 1
app/controller/common/Login.php

@@ -115,7 +115,8 @@ class Login extends Base
                 "super"     => $userData['is_super'],
                 "shop"      => $agent['poi_name']??'',
                 "trade"     => $agent['trade_type']??0,
-                "type"      => $userData['type']
+                "type"      => $userData['type'],
+                "logo"      => $agent['poi_logo']??'-'
             ]);
         } catch (\Throwable $throwable) {
             return error($throwable->getMessage());

+ 84 - 0
app/controller/merchant/GoodsOff.php

@@ -0,0 +1,84 @@
+<?php
+
+namespace app\controller\merchant;
+
+use app\extra\basic\Base;
+use app\middleware\AuthMiddleware;
+use app\model\saas\SaasGoodsOff;
+use app\service\saas\GoodsOffService;
+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;
+
+
+#[Controller("/api/merchant/goodsoff"),Middleware(AuthMiddleware::class)]
+class GoodsOff extends Base
+{
+    #[Inject]
+    protected GoodsOffService $service;
+
+    #[Inject]
+    protected SaasGoodsOff $model;
+
+    #[GetMapping('list')]
+    public function getDataList(Request $request): Response
+    {
+        try {
+            $param = $request->all();
+            $param['poi_id'] = $request->user['store_id'];
+            $param['is_deleted'] = 1;
+            $data = $this->service->setModel()->getList($param,['store' => function($query){
+                $query->field("poi_id,poi_name,poi_address,poi_city");
+            },'goods' => function($query){
+                $query->field("goods_id,image_list,product_name,price,line_price");
+            }]);
+            return successTrans("success.data",pageFormat($data),200);
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+
+
+    #[PostMapping('save')]
+    public function setDataDetail(Request $request): Response
+    {
+        try {
+            [$state,$msg] = $this->checkDeposit($request->user['store_id']);
+            if (!$state) return error($msg);
+            $param = $request->all();
+            $param['poi_id'] = $request->user['store_id'];
+            $state = $this->model->setAutoData($param);
+            if (!$state) return errorTrans("error.data");
+            return successTrans("success.data");
+        } catch (\Throwable $th) {
+            return error($th->getMessage());
+        }
+    }
+
+
+    #[PostMapping("del")]
+    public function delGoods(Request $request): Response
+    {
+        try {
+            $param = $this->_valid([
+                "id.require"            => trans("empty.require"),
+            ],$request->method());
+            if (!is_array($param)) return error($param);
+            $detail = $this->model->where("id",$param["id"])->findOrEmpty();
+            if ($detail->isEmpty()) return errorTrans("empty.data");
+            if ($detail['poi_id'] <> $request->user['store_id']) return error("非法操作");
+            $detail->is_deleted = 0;
+            $state = $detail->save();
+            if (!$state) return errorTrans("error.data");
+            return successTrans("success.data");
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+}

+ 196 - 0
app/controller/service/Chat.php

@@ -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(),
+        ]);
+
+    }
+
+}

+ 82 - 2
app/controller/v2/Chat.php

@@ -3,8 +3,10 @@
 namespace app\controller\v2;
 
 use app\extra\basic\Base;
+use app\extra\tools\UploadExtend;
 use app\middleware\AuthMiddleware;
 use app\model\saas\SaasChatMsg;
+use app\model\saas\SaasChatStore;
 use app\model\saas\SaasService;
 use app\model\saas\SaasStore;
 use app\model\saas\SaasUserOpen;
@@ -16,6 +18,7 @@ use LinFly\Annotation\Attributes\Route\Middleware;
 use LinFly\Annotation\Attributes\Route\PostMapping;
 use support\Request;
 use support\Response;
+use Webman\Push\Api;
 
 
 #[Controller("/v2/chat"),Middleware(AuthMiddleware::class)]
@@ -190,10 +193,39 @@ class Chat extends Base
                 "type"      => $param['type'],
                 "msgId"     => time(),
                 "poi_id"    => $param['groupId'],
-                "service_id" => '', // 客服ID
+                "service_id" => '105', // 客服ID
             ]);
+            $chatStore = (new SaasChatStore)->where(['openid' => $request->user['openid'],"service_id" => 105])->with(['user' => function($query){
+                $query->field("openid,nickname,avatar,mobile,create_at");
+            }])->append(['avatar'])->withAttr(['avatar' => function () {
+                return sConf("service.logo");
+            }])->findOrEmpty();
+            $total = (new SaasChatMsg)->where(["poi_id" => $chatStore['poi_id'],"service_id" => 105,"openid" => $chatStore['openid'],'is_read' => 0])->count();
+            if (!$chatStore->isEmpty()) {
+                $chatStore->last_at = getDateFull();
+                $chatStore->save();
+                $chatStore['lastTime'] = formatTime(getDateFull());
+                $chatStore['last'] = [
+                    "content"   => is_array($param['content'])?json_encode($param['content']):str_cut_ellipsis($param['content']),
+                    "type"      => $param['type'],
+                    "num"       => $total
+                ];
+            }
             // 推送消息
-
+            $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("service-105","message",[
+                "type"      => $param['type'],
+                "content"   => is_array($param['content'])?json_encode($param['content']):$param['content'],
+                "openid"    => $request->user['openid'],
+                "nick"      => [],
+                "user"      => [],
+                "userType"  => "friend",
+                "avatar"    => $request->user['avatar'],
+                "time"      =>  time() * 1000,
+                "msgId"     => md5($request->user['openid'].time()),
+                "item"      => $chatStore->toArray()
+            ]);
+            return successTrans("success.data");
         } catch (\Throwable $throwable) {
             return error($throwable->getMessage());
         }
@@ -209,6 +241,54 @@ class Chat extends Base
     public function sendImageMsg(Request $request): Response
     {
         try {
+            $param = $this->_valid([
+                "groupId.require"   => trans("empty.require"),
+                "type.require"      => trans("empty.require"),
+                "sendId.require"    => trans("empty.require"),
+            ],"post");
+            if (!is_array($param)) return error($param);
+            $resp = UploadExtend::uploadFile();
+            if (!isset($resp[0]['url'])) return error("发送失败");
+            $param['content'] = $resp[0]['url'];
+            (new SaasChatMsg)->insertGetId([
+                "source"    => 1,
+                "openid"    => $request->user['openid'],
+                "content"   => is_array($param['content'])?json_encode($param['content']):$param['content'],
+                "type"      => $param['type'],
+                "msgId"     => time(),
+                "poi_id"    => $param['groupId'],
+                "service_id" => '105', // 客服ID
+            ]);
+            $chatStore = (new SaasChatStore)->where(['openid' => $request->user['openid'],"service_id" => 105])->with(['user' => function($query){
+                $query->field("openid,nickname,avatar,mobile,create_at");
+            }])->append(['avatar'])->withAttr(['avatar' => function () {
+                return sConf("service.logo");
+            }])->findOrEmpty();
+            $total = (new SaasChatMsg)->where(["poi_id" => $chatStore['poi_id'],"service_id" => 105,"openid" => $chatStore['openid'],'is_read' => 0])->count();
+            if (!$chatStore->isEmpty()) {
+                $chatStore->last_at = getDateFull();
+                $chatStore->save();
+                $chatStore['lastTime'] = formatTime(getDateFull());
+                $chatStore['last'] = [
+                    "content"   => is_array($param['content'])?json_encode($param['content']):str_cut_ellipsis($param['content']),
+                    "type"      => $param['type'],
+                    "num"       => $total
+                ];
+            }
+            // 推送消息
+            $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("service-105","message",[
+                "type"      => $param['type'],
+                "content"   => is_array($param['content'])?json_encode($param['content']):$param['content'],
+                "openid"    => $request->user['openid'],
+                "nick"      => [],
+                "user"      => [],
+                "userType"  => "friend",
+                "avatar"    => $request->user['avatar'],
+                "time"      =>  time() * 1000,
+                "msgId"     => md5($request->user['openid'].time()),
+                "item"      => $chatStore->toArray()
+            ]);
 
         } catch (\Throwable $throwable) {
             return error($throwable->getMessage());

+ 1 - 0
app/model/saas/SaasChatStore.php

@@ -4,6 +4,7 @@ namespace app\model\saas;
 
 use app\extra\basic\Model;
 use app\model\system\SystemUser;
+use think\model\relation\HasMany;
 use think\model\relation\HasOne;
 
 

+ 61 - 0
app/model/saas/SaasGoodsOff.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace app\model\saas;
+
+use app\extra\basic\Model;
+use think\model\relation\HasOne;
+
+
+/**
+ * @property integer $id (主键)
+ * @property integer $poi_id 
+ * @property mixed $goods_id 来客商品ID
+ * @property string $image 
+ * @property string $title 
+ * @property integer $status 状态
+ * @property mixed $create_at
+ */
+class SaasGoodsOff extends Model
+{
+    /**
+     * The connection name for the model.
+     *
+     * @var string|null
+     */
+    protected $connection = 'mysql';
+    
+    /**
+     * The table associated with the model.
+     *
+     * @var string
+     */
+    protected string $table = "saas_goods_off";
+    
+    /**
+     * The primary key associated with the table.
+     *
+     * @var string
+     */
+    protected string $primaryKey = "id";
+    
+    /**
+     * Indicates if the model should be timestamped.
+     *
+     * @var bool
+     */
+    public bool $timestamps = false;
+
+
+    public function store(): HasOne
+    {
+        return $this->hasOne(SaasStore::class,"poi_id","poi_id");
+    }
+
+
+    public function goods(): HasOne
+    {
+        return $this->hasOne(SaasGoods::class,"goods_id","goods_id");
+    }
+
+
+}

+ 2 - 0
app/service/saas/ChatStoreService.php

@@ -34,6 +34,8 @@ class ChatStoreService extends Service
         !empty($param['type']) && $filter[] = ["type", '=', $param['type']];
         !empty($param['name']) && $filter[] = ["poi_name", 'like', "%{$param['name']}%"];
         !empty($param['key']) && $filter[] = ["nickname", 'like', "%{$param['key']}%"];
+        !empty($param['timeLt']) && $filter[] = ["last_at", '<', $param['timeLt']];
+        !empty($param['timeEq']) && $filter[] = ["last_at", 'between', [$param['timeEq']." 00:00:00", $param['timeEq']." 23:59:59"]];
         return $filter;
     }
 

+ 39 - 0
app/service/saas/GoodsOffService.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace app\service\saas;
+
+use app\extra\basic\Service;
+use app\model\saas\SaasGoodsOff;
+
+class GoodsOffService extends Service
+{
+
+
+    /**
+     *
+     * @return $this
+     */
+    public function setModel()
+    {
+        $this->mode = (new SaasGoodsOff);
+        return $this;
+    }
+
+
+    /**
+     *
+     * @param array $param
+     * @return array
+     */
+    public function searchFilter(array $param = []): array
+    {
+        $filter = [];
+        !empty($param['status']) && $filter[] = ["status", '=', ($param['status']-1)];
+        !empty($param['poi']) && $filter[] = ["poi_id", '=', $param['poi']];
+        !empty($param['is_deleted']) && $filter[] = ["is_deleted", '=', $param['is_deleted']];
+        !empty($param['poi_id']) && $filter[] = ["poi_id", '=', $param['poi_id']];
+        !empty($param['title']) && $filter[] = ["title", 'like', "%{$param['title']}%"];
+        return $filter;
+    }
+
+}