Measurements of the transverse-momentum-dependent cross sections of $J/\psi$ production at mid-rapidity in proton+proton collisions at $\sqrt{s} =$ 510 and 500 GeV with the STAR detector