App.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, isString} from "js-fast-way";
  7. import {getWssApiUrl} from "@/config/envApi";
  8. export default {
  9. data() {
  10. return {
  11. userInfo: {}, refreshLock: false, isSocket: false
  12. }
  13. },
  14. onLaunch: function() {
  15. //console.log('App Launch')
  16. },
  17. onShow: function() {
  18. //console.log('App Show')
  19. },
  20. onHide: function() {
  21. //console.log('App Hide')
  22. },
  23. mounted() {
  24. const store = useAppStore()
  25. this.userInfo = store.userInfo
  26. this.getRefreshToken()
  27. this.setInitSocket()
  28. },
  29. methods:{
  30. //刷新token
  31. getRefreshToken() {
  32. let _this = this;
  33. setInterval(() => {
  34. const token = getStorage('token', true)
  35. const date = calcDate(token.datetime, new Date().getTime())
  36. if (isNullES(date)) return false;
  37. if (date.seconds >= website.tokenTime && !this.refreshLock) {
  38. this.refreshLock = true
  39. console.log('刷新token')
  40. this.refreshTokenApi().then(() => {
  41. _this.refreshLock = false
  42. }).catch(() => {
  43. _this.refreshLock = false
  44. uni.reLaunch({
  45. url: '/pages/login/login'
  46. });
  47. })
  48. }
  49. }, 10000)
  50. },
  51. async refreshTokenApi() {
  52. const store = useAppStore()
  53. const { dept_id, role_id, tenant_id } = this.userInfo
  54. const refreshToken = getStorage('refreshToken')
  55. const {error, status, res} = await userApi.refreshToken({
  56. token: refreshToken,
  57. tenantId: tenant_id,
  58. deptId: dept_id,
  59. roleId: role_id
  60. });
  61. if (!error && status === 200) {
  62. const data = getObjValue(res)
  63. setStorage('token', data?.access_token)
  64. setStorage('refreshToken', data?.refresh_token)
  65. store.setUserInfo(res)
  66. return Promise.resolve(res)
  67. } else {
  68. return Promise.reject(res)
  69. }
  70. },
  71. //长连接
  72. setInitSocket() {
  73. const _this = this
  74. const timeId = setInterval(() => {
  75. if (_this.isSocket) {
  76. clearInterval(timeId)
  77. } else {
  78. _this.setAppSocket(timeId)
  79. }
  80. }, 3000)
  81. },
  82. setAppSocket() {
  83. const {user_id} = this.userInfo
  84. if (user_id) this.initWebSocket(user_id)
  85. },
  86. initWebSocket(user_id) {
  87. const _this = this
  88. uni.connectSocket({
  89. url: getWssApiUrl() + user_id,
  90. complete: ()=> {
  91. console.log('websocket链接成功')
  92. }
  93. });
  94. uni.onSocketOpen((res) => {
  95. _this.isSocket = true
  96. console.log('WebSocket连接已打开!');
  97. _this.sendSocketMessage();
  98. });
  99. uni.onSocketError((res) => {
  100. _this.isSocket = false
  101. console.log('WebSocket连接打开失败,请检查!');
  102. });
  103. uni.onSocketClose((res) => {
  104. _this.isSocket = false
  105. console.log('WebSocket 已关闭!');
  106. //关闭后在连接
  107. _this.setInitSocket()
  108. });
  109. //收到的消息
  110. uni.onSocketMessage(({data}) => {
  111. const countData = isString(data) ? JSON.parse(data) : {}
  112. console.log(countData)
  113. });
  114. },
  115. sendSocketMessage() {
  116. const store = useAppStore()
  117. uni.sendSocketMessage({
  118. data: store.projectId + ',' + store.contractId
  119. });
  120. },
  121. }
  122. }
  123. </script>
  124. <style lang="scss">
  125. @import "@/colorui/main.css";
  126. @import "@/colorui/icon.css";
  127. @import "@/style/app.scss";
  128. </style>