Quantum batteries at the verge of a phase transition