App.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <script>
  2. export default {
  3. onLaunch: function () {
  4. console.warn('当前组件仅支持 uni_modules 目录结构 ,请升级 HBuilderX 到 3.1.0 版本以上!')
  5. console.log('App Launch')
  6. },
  7. onShow: function () {
  8. console.log('App Show')
  9. console.log(this.$fileBasUrl, 'appppp$fileBasUrl')
  10. this.connectSocket()
  11. this.timer = setInterval(() => {
  12. wx.request({
  13. url: 'http://192.168.0.109:3000/heartbeat?userId=' + wx.getStorageSync('userId'),
  14. })
  15. }, 5000)
  16. // uni.hideTabBar()
  17. },
  18. onHide: function () {
  19. console.log('App Hide')
  20. // close socket
  21. wx.closeSocket()
  22. clearInterval(this.timer)
  23. },
  24. globalData: {
  25. fileBasUrl: 'https://mall.rongtongh.cn/storage/img',
  26. hasLogin: false,
  27. inviteId: null,
  28. orderConfirmIds: [],
  29. selectAddrss: false,
  30. selectCoupon: false,
  31. },
  32. methods: {
  33. connectSocket() {
  34. function connect() {
  35. wx.connectSocket({
  36. url: 'ws://192.168.0.109:3000',
  37. })
  38. wx.onSocketOpen(function (res) {
  39. console.log('WebSocket连接已打开!')
  40. let user = {
  41. name: 'user123',
  42. }
  43. wx.sendSocketMessage({
  44. data: JSON.stringify(user),
  45. })
  46. })
  47. wx.onSocketClose(function (res) {
  48. console.log('WebSocket连接已关闭!')
  49. setTimeout(() => {
  50. console.log('re connect')
  51. // connect()
  52. }, 2000)
  53. })
  54. }
  55. connect()
  56. },
  57. },
  58. }
  59. </script>
  60. <style lang="scss">
  61. @import './uni_modules/jc-tailwind/index.scss';
  62. /*每个页面公共css */
  63. @import '@/uni_modules/uni-scss/index.scss';
  64. /* #ifndef APP-NVUE */
  65. @import '@/static/customicons.css';
  66. @import '@/static/style.scss';
  67. // 设置整个项目的背景色
  68. page {
  69. background-color: #f5f5f5;
  70. }
  71. /* #endif */
  72. .example-info {
  73. font-size: 14px;
  74. color: #333;
  75. padding: 10px;
  76. }
  77. </style>