| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace app\controller\api;
- use app\extra\basic\Base;
- use app\middleware\AuthMiddleware;
- use app\model\saas\SaasChatMsg;
- use app\model\saas\SaasService;
- use app\model\saas\SaasStore;
- 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("/dy/msg"),Middleware(AuthMiddleware::class)]
- class Msg extends Base
- {
- protected int $isUser = 1;
- #[PostMapping("checkIn")]
- public function checkIn(Request $request): Response
- {
- try {
- $param = $request->all();
- $store = (new SaasStore)->where("poi_id",$param['poi'])->findOrEmpty();
- if ($store->isEmpty()) return error("店铺不存在");
- // 获取最新的10条聊天记录
- $userAvatar = $request->user['avatar'];
- $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $store['poi_id'],'is_user' => $this->isUser])->append(['avatar','time','userType'])->withAttr(['avatar' => function($data,$resp) use($userAvatar){
- if ($resp['source'] == 1) { // 用户
- return $userAvatar??'';
- } else {
- return sConf("service.logo");
- }
- },'time' => function($resp,$data){
- return strtotime($data['create_at']) * 1000;
- },'userType' => function($data,$resp) {
- if ($resp['source'] == 1) { // 用户
- return 'self';
- } else {
- return 'friend';
- }
- }])->order("create_at","desc")->paginate([
- "list_rows" => 10,
- "page" => 1
- ]);
- $star = (new SaasService)->where("poi_id",$param['poi'])->with(['quick'])->findOrEmpty();
- return $this->encode("ok",[
- "msg" => pageFormatMsg($msg),
- "avatar" => [
- "self" => $userAvatar,
- "friend" => empty($store['poi_logo']) ? sConf("service.logo") : $store['poi_logo'],
- ],
- "quick" => $star,
- "store" => ['name' => $store['poi_name'],"chat_id" => $store['poi_id']]
- ]);
- } catch (\Throwable $throwable) {
- return error($throwable);
- }
- }
- #[GetMapping("msg")]
- public function getMessageList(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "poi.require" => trans("empty.require"),
- "page.default" => 1,
- "size.default" => 10
- ],$request->method());
- if (!is_array($param)) return error($param);
- $userAvatar = $request->user['avatar'];
- $msg = (new SaasChatMsg)->where(['openid' => $request->user['openid'],'poi_id' => $param['poi'],'is_user' => $this->isUser])->append(['avatar','time','userType'])->withAttr(['avatar' => function($data,$resp) use($userAvatar){
- if ($resp['source'] == 1) {
- return $userAvatar??'';
- } else {
- return sConf("service.logo");
- }
- },'time' => function($resp,$data){
- return strtotime($data['create_at']) * 1000;
- },'userType' => function($data,$resp) {
- if ($resp['source'] == 1) { // 用户
- return 'self';
- } else {
- return 'friend';
- }
- }])->order("create_at","desc")->paginate([
- "list_rows" => $param['size']??10,
- "page" => $param['page']??2,
- ]);
- return $this->encode("ok",pageFormatMsg($msg));
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- }
|