['name'], ]; } private function baseQuery() { $query = QywxExternalContact::where($this->searchWhere); if (!empty($this->params['follow_user'])) { $kw = addcslashes((string) $this->params['follow_user'], '%_\\'); $query->whereLike('follow_users', '%' . $kw . '%'); } return $query; } /** * @notes 获取列表 */ public function lists(): array { $lists = $this->baseQuery() ->order('id', 'desc') ->limit($this->limitOffset, $this->limitLength) ->select() ->toArray(); $wxUserids = []; foreach ($lists as $item) { $raw = json_decode($item['follow_users'] ?? '[]', true); if (!is_array($raw)) { continue; } foreach ($raw as $fu) { if (!is_array($fu)) { continue; } $wx = trim((string) ($fu['userid'] ?? '')); if ($wx !== '') { $wxUserids[$wx] = true; } } } $wxUserids = array_keys($wxUserids); $adminNameByWx = []; if ($wxUserids !== []) { $adminNameByWx = Admin::whereIn('work_wechat_userid', $wxUserids)->column('name', 'work_wechat_userid'); } foreach ($lists as &$item) { $followUsers = json_decode($item['follow_users'] ?? '[]', true); $followUsers = is_array($followUsers) ? $followUsers : []; foreach ($followUsers as &$fu) { if (!is_array($fu)) { continue; } $wx = trim((string) ($fu['userid'] ?? '')); if ($wx !== '' && isset($adminNameByWx[$wx]) && $adminNameByWx[$wx] !== '') { $fu['admin_name'] = $adminNameByWx[$wx]; } } unset($fu); $item['follow_users'] = $followUsers; $followAdminIds = json_decode($item['follow_admin_ids'] ?? '[]', true); $item['follow_admin_ids'] = is_array($followAdminIds) ? $followAdminIds : []; $fromDb = (int) ($item['external_first_add_time'] ?? 0); $fromJson = CustomerLogic::minFollowCreatetime($followUsers); $item['external_first_add_time'] = $fromDb > 0 ? $fromDb : $fromJson; } unset($item); return $lists; } /** * @notes 获取数量 */ public function count(): int { return $this->baseQuery()->count(); } }