App.vue 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <script>
  2. import {useAppStore} from "@/store";
  3. import website from "@/config/index";
  4. import userApi from '~api/user/index';
  5. import {getStorage, setStorage} from "@/utils/storage";
  6. import {calcDate, getObjValue, isNullES} from "js-fast-way";
  7. import HcSocket from "@/httpApi/request/socket";
  8. export default {
  9. data() {
  10. return {
  11. userInfo: {}, refreshLock: false, isSocket: false, timeId: null,
  12. }
  13. },
  14. onLaunch() {
  15. HcSocket.initSocket()
  16. },
  17. onShow() {
  18. //console.log('App Show')
  19. },
  20. onHide() {
  21. //console.log('App Hide')
  22. },
  23. mounted() {
  24. const store = useAppStore()
  25. this.userInfo = store.userInfo
  26. this.getRefreshToken()
  27. },
  28. methods:{
  29. //长链接发送消息
  30. socketSendMsg(val) {
  31. if (val) {
  32. HcSocket.sendSocketMsg(val)
  33. }
  34. },
  35. //刷新token
  36. getRefreshToken() {
  37. let _this = this;
  38. setInterval(() => {
  39. const token = getStorage('token', true)
  40. const date = calcDate(token.datetime, new Date().getTime())
  41. if (isNullES(date)) return false;
  42. if (date.seconds >= website.tokenTime && !this.refreshLock) {
  43. this.refreshLock = true
  44. console.log('刷新token')
  45. this.refreshTokenApi().then(() => {
  46. _this.refreshLock = false
  47. }).catch(() => {
  48. _this.refreshLock = false
  49. uni.reLaunch({
  50. url: '/pages/login/login'
  51. });
  52. })
  53. }
  54. }, 10000)
  55. },
  56. async refreshTokenApi() {
  57. const store = useAppStore()
  58. const { dept_id, role_id, tenant_id } = this.userInfo
  59. const refreshToken = getStorage('refreshToken')
  60. const {error, status, res} = await userApi.refreshToken({
  61. token: refreshToken,
  62. tenantId: tenant_id,
  63. deptId: dept_id,
  64. roleId: role_id
  65. });
  66. if (!error && status === 200) {
  67. const data = getObjValue(res)
  68. setStorage('token', data?.access_token)
  69. setStorage('refreshToken', data?.refresh_token)
  70. store.setUserInfo(res)
  71. return Promise.resolve(res)
  72. } else {
  73. return Promise.reject(res)
  74. }
  75. },
  76. }
  77. }
  78. </script>
  79. <style lang="scss">
  80. @import "@/colorui/main.css";
  81. @import "@/colorui/icon.css";
  82. @import "@/style/app.scss";
  83. </style>