getUserInfo('companyId')) ->column('id'); } /** * 非主操盘手仅可查看分配到的设备 * * @return array */ protected function getUserDevicesId(): array { return DeviceUserModel::where([ 'userId' => $this->getUserInfo('id'), 'companyId' => $this->getUserInfo('companyId'), ])->column('deviceId'); } /** * 获取当前用户可访问的设备ID * * @return array */ protected function getDevicesId(): array { return ($this->getUserInfo('isAdmin') == UserModel::ADMIN_STP) ? $this->getCompanyDevicesId() : $this->getUserDevicesId(); } /** * 获取用户可访问的微信ID集合 * * @return array * @throws \Exception */ protected function getAccessibleWechatIds(): array { $deviceIds = $this->getDevicesId(); if (empty($deviceIds)) { throw new \Exception('暂无可用设备', 200); } return DeviceWechatLoginModel::distinct(true) ->where('companyId', $this->getUserInfo('companyId')) ->whereIn('deviceId', $deviceIds) ->column('wechatId'); } /** * 查看朋友圈列表 * * @return \think\response\Json */ public function index() { try { $wechatId = $this->request->param('wechatId/s', ''); if (empty($wechatId)) { return ResponseHelper::error('wechatId不能为空'); } // 权限校验:只能查看当前账号可访问的微信 $accessibleWechatIds = $this->getAccessibleWechatIds(); if (!in_array($wechatId, $accessibleWechatIds, true)) { return ResponseHelper::error('无权查看该微信的朋友圈', 403); } // 获取对应的微信账号ID $accountId = Db::table('s2_wechat_account') ->where('wechatId', $wechatId) ->value('id'); if (empty($accountId)) { return ResponseHelper::error('微信账号不存在或尚未同步', 404); } $query = Db::table('s2_wechat_moments') ->where('wechatAccountId', $accountId); // 关键词搜索 if ($keyword = trim((string)$this->request->param('keyword', ''))) { $query->whereLike('content', '%' . $keyword . '%'); } // 类型筛选 $type = $this->request->param('type', ''); if ($type !== '' && $type !== null) { $query->where('type', (int)$type); } // 时间筛选 $startTime = $this->request->param('startTime', ''); $endTime = $this->request->param('endTime', ''); if ($startTime || $endTime) { $start = $startTime ? strtotime($startTime) : 0; $end = $endTime ? strtotime($endTime) : time(); if ($start && $end && $end < $start) { return ResponseHelper::error('结束时间不能早于开始时间'); } $query->whereBetween('createTime', [$start ?: 0, $end ?: time()]); } $page = (int)$this->request->param('page', 1); $limit = (int)$this->request->param('limit', 10); $paginator = $query->order('createTime', 'desc') ->paginate($limit, false, ['page' => $page]); $list = array_map(function ($item) { return $this->formatMomentRow($item); }, $paginator->items()); return ResponseHelper::success([ 'list' => $list, 'total' => $paginator->total(), 'page' => $page, 'limit' => $limit, ]); } catch (\Exception $e) { return ResponseHelper::error($e->getMessage(), $e->getCode() ?: 500); } } /** * 格式化朋友圈数据 * * @param array $row * @return array */ protected function formatMomentRow(array $row): array { $formatTime = function ($timestamp) { if (empty($timestamp)) { return ''; } return is_numeric($timestamp) ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d H:i:s', strtotime($timestamp)); }; return [ 'id' => (int)$row['id'], 'snsId' => $row['snsId'] ?? '', 'type' => (int)($row['type'] ?? 0), 'content' => $row['content'] ?? '', 'commentList' => $this->decodeJson($row['commentList'] ?? null), 'likeList' => $this->decodeJson($row['likeList'] ?? null), 'resUrls' => $this->decodeJson($row['resUrls'] ?? null), 'createTime' => $formatTime($row['createTime'] ?? null), 'momentEntity' => [ 'lat' => $row['lat'] ?? 0, 'lng' => $row['lng'] ?? 0, 'location' => $row['location'] ?? '', 'picSize' => $row['picSize'] ?? 0, 'userName' => $row['userName'] ?? '', ], ]; } /** * JSON字段解析 * * @param mixed $value * @return array */ protected function decodeJson($value): array { if (empty($value)) { return []; } if (is_array($value)) { return $value; } $decoded = json_decode($value, true); return $decoded ?: []; } }