Almost sure invariance principles for sums of $B$-valued random variables with applications to random Fourier series and the empirical characteristic process