Unified Enhancement of the Generalization and Robustness of Language Models via Bi-Stage Optimization