App.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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() {
  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. const _this = this
  73. const timeId = setInterval(() => {
  74. if (_this.isSocket) {
  75. clearInterval(timeId)
  76. } else {
  77. _this.setAppSocket(timeId)
  78. }
  79. }, 3000)
  80. },
  81. setAppSocket() {
  82. const {user_id} = this.userInfo
  83. if (user_id) this.initWebSocket(user_id)
  84. },
  85. initWebSocket(user_id) {
  86. const _this = this
  87. const store = useAppStore()
  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. if (!isNullES(countData)) {
  113. console.log(countData)
  114. store.setMsgCountData(countData)
  115. }
  116. });
  117. },
  118. sendSocketMessage() {
  119. const store = useAppStore()
  120. uni.sendSocketMessage({
  121. data: store.projectId + ',' + store.contractId
  122. });
  123. },
  124. }
  125. }
  126. </script>
  127. <style lang="scss">
  128. @import "@/colorui/main.css";
  129. @import "@/colorui/icon.css";
  130. @import "@/style/app.scss";
  131. </style>