App.vue 5.0 KB

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