Wales scrambled to beat Fiji 39-24 in the Nations Championship in Cardiff and win consecutive matches for the first time in three years.