Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: user logout #91

Merged
merged 8 commits into from
May 8, 2022
Merged

feat: user logout #91

merged 8 commits into from
May 8, 2022

Conversation

likui628
Copy link
Collaborator

@likui628 likui628 commented May 3, 2022

Description

  1. 用户登出清空token和用户信息
  2. 用户名和用户头像使用接口返回的数据
  3. 本地缓存value封装在对象{value},避免value值为undefined时JSON.parse报错

close #53

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).

@netlify
Copy link

netlify bot commented May 3, 2022

Deploy Preview for vue-hbs-admin ready!

Name Link
🔨 Latest commit fc01f70
🔍 Latest deploy log https://app.netlify.com/sites/vue-hbs-admin/deploys/62732336ad020f0008926a33
😎 Deploy Preview https://deploy-preview-91--vue-hbs-admin.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

src/layouts/default/header/components/UserDropdown.vue Outdated Show resolved Hide resolved
src/utils/cache.ts Outdated Show resolved Hide resolved
@likui628 likui628 requested a review from Hongbusi May 4, 2022 03:52
@alexzhang1030
Copy link
Collaborator

可以在退出成功后随便给一个什么反馈都可以,例如一个 message.success,增强一下交互感

@likui628
Copy link
Collaborator Author

likui628 commented May 4, 2022

退出之后直接会返回登录界面,本身就是交互了,你觉得呢

@Hongbusi
Copy link
Member

Hongbusi commented May 4, 2022

可以在退出成功后随便给一个什么反馈都可以,例如一个 message.success,增强一下交互感

这个可以有

@likui628
Copy link
Collaborator Author

likui628 commented May 4, 2022

可以在退出成功后随便给一个什么反馈都可以,例如一个 message.success,增强一下交互感

这个可以有

现在有封装了message组件吗,要不要先提个issue

@Hongbusi
Copy link
Member

Hongbusi commented May 4, 2022

没有,先直接用 antdv 的 message 吧

Copy link
Member

@Hongbusi Hongbusi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setCache 和 getCache 对 value 的操作要优化一下

src/utils/cache.ts Outdated Show resolved Hide resolved
@Hongbusi
Copy link
Member

Hongbusi commented May 4, 2022

 setCache(key: string, value: any) {
    if (value) {
      window.localStorage.setItem(key, JSON.stringify(value))
    }
    else {
      window.localStorage.removeItem(key)
    }
  }

在 utils 中通过 value 决定是 setItem 还是 removeItem,是不合适的,更建议在你需要移除 cache 的地方调用 removeCache 来实现删除。

@likui628
Copy link
Collaborator Author

likui628 commented May 4, 2022

 setCache(key: string, value: any) {
    if (value) {
      window.localStorage.setItem(key, JSON.stringify(value))
    }
    else {
      window.localStorage.removeItem(key)
    }
  }

在 utils 中通过 value 决定是 setItem 还是 removeItem,是不合适的,更建议在你需要移除 cache 的地方调用 removeCache 来实现删除。

我之前使用{value:xxx},以及现在判断undefined是进行removeItem删除主要是防止以下问题:

  1. setCache调用者如果没有判断value值是否为undefined就会在localStorage存储key:'undefined'
  2. getCache时进行JSON.pase时就会有异常(value值为‘undefined’字符串)

改成下面这种吗?我不知道我理解的对不对

 setCache(key: string, value: any) {
    if (value) {
      window.localStorage.setItem(key, JSON.stringify(value))
    }
    else {
      this.removeCache(key)
    }
  }

@Hongbusi
Copy link
Member

Hongbusi commented May 4, 2022

LocalCache 类中的 4 个方法,是为了统一对保存复杂类型数据 localStorage 操作进行 JSON 处理。setCache 只关注 setItem 操作,getCache 只关注 getItem 操作...

你可以在退出登录的时候,直接调用 localCache.removeCache(token),来实现删除操作,更合理一些,而不是修改 LocalCache 类的方法来实现。

不知道这样说,你可以理解不

@likui628
Copy link
Collaborator Author

likui628 commented May 4, 2022

LocalCache 类中的 4 个方法,是为了统一对保存复杂类型数据 localStorage 操作进行 JSON 处理。setCache 只关注 setItem 操作,getCache 只关注 getItem 操作...

你可以在退出登录的时候,直接调用 localCache.removeCache(token),来实现删除操作,更合理一些,而不是修改 LocalCache 类的方法来实现。

不知道这样说,你可以理解不

我懂你的意思了,我上面说了,如果setCache进去了undefined值,getCache里调用JSON.parse会有异常,这种边界状态你觉得如何处理比较好。

@Hongbusi
Copy link
Member

Hongbusi commented May 4, 2022

setCache 的时候有一个 if 判断,如果是 undefined 就不会保存了

@likui628
Copy link
Collaborator Author

likui628 commented May 5, 2022

setCache 的时候有一个 if 判断,如果是 undefined 就不会保存了

不保存是可以,如果之前有值就不能替换了。所以还对{value:xx}方式合适

@Hongbusi
Copy link
Member

Hongbusi commented May 5, 2022

当你需要把保存的值变成空的时候,就调用 removeCache。

为什么不用 { value: xxx } 的形式?

如果你在控制台查看存储在本地的数据,外层都包裹一个 { value: xxx },你会不会很烦躁?

@Hongbusi Hongbusi merged commit 884668b into developer-plus:master May 8, 2022
@likui628 likui628 deleted the logout branch May 8, 2022 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

退出登录
3 participants