Hydrogels have emerged as promising sensors for detecting heavy metal ions in fluids and have been extensively developed. However, monitoring of multiple target analytes in Chinese herbs remains challenging due to subtle chemical signals and the complex composition of the extracted solutions. To address these challenges, we developed a hydrogel optofluidic sensor to amplify analyte signals through strong light-matter interactions within the hydrogel. This sensing platform integrates a hydrogel film encapsulated in a whispering-gallery-mode (WGM) microcavity for the detection of heavy metal ions, such as Pb2+ and Hg2+. The 3D cross-linked hydrophilic polymer network facilitates ion penetration from analyte solutions, inducing distinct WGM resonance shifts. The red shift in the spectral wavelength serves as a parameter to quantify the content of heavy metal ions. By modification of the hydrogel with aptamers, the optofluidic sensors achieve high sensitivity and selectivity. Finally, the platform's performance was demonstrated using Chinese herbs with varying Pb2+ concentrations, highlighting its practical applicability in real-world scenarios. The proposed hydrogel microcavity exhibit a promising method for development of functional hydrogel sensors and healthcare applications.