uniapp,h5

12345678910111213141516171819202122
  1. // utils/storage.js
  2. // 存储 JWT 信息到本地存储
  3. export function storeJwtInfo2User(jwt) {
  4. if (jwt) {
  5. const jwtParts = jwt.split('.');
  6. if (jwtParts.length === 3) {
  7. const decodedJwt = JSON.parse(atob(jwtParts[1]));
  8. const user = {
  9. userId: decodedJwt.sub,
  10. username: decodedJwt.username,
  11. email: decodedJwt.email,
  12. avatar: decodedJwt.avatar_url,
  13. bio: decodedJwt.bio
  14. };
  15. uni.setStorageSync('userInfo', user);
  16. }
  17. } else {
  18. uni.setStorageSync('userInfo', {});
  19. }
  20. }