app.jsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Component } from 'react'
  2. import { Provider } from 'react-redux'
  3. import configStore from './store'
  4. import './app.less'
  5. import 'taro-skeleton/dist/index.css'
  6. import Taro from "@tarojs/taro";
  7. import { getWxConfig } from './utils/servers/servers';
  8. import 'taro-ui/dist/style/components/drawer.scss';
  9. import 'taro-ui/dist/style/components/list.scss';
  10. const store = configStore()
  11. class App extends Component {
  12. componentDidMount() { }
  13. componentDidShow() {
  14. Taro.eventCenter.on('getStorageSync', () => {
  15. let token = Taro.getStorageSync('token');
  16. if (token) {
  17. this.getStorageSync();
  18. }
  19. })
  20. let token = Taro.getStorageSync('token');
  21. if (token) {
  22. this.getStorageSync();
  23. }
  24. this.getWxConfig();
  25. }
  26. componentDidHide() {
  27. Taro.closeSocket();
  28. }
  29. getWxConfig() {
  30. getWxConfig({}).then(v => {
  31. if (v.error.length === 0) {
  32. Taro.setStorageSync("wxConfig", v.data || {});
  33. } else {
  34. Taro.showToast({
  35. title: v.error[0].message,
  36. icon: 'none'
  37. })
  38. }
  39. }).catch((error) => {
  40. console.log(error)
  41. })
  42. }
  43. getStorageSync() {
  44. let token = Taro.getStorageSync('token');
  45. //建立连接
  46. Taro.connectSocket({
  47. header: {
  48. 'token': token,
  49. },
  50. url: 'wss://bm.jishutao.com/webSocketServer',
  51. // url: 'wss://172.16.0.255:8080/webSocketServer'
  52. // url: 'wss://uat.jishutao.com/webSocketServer'
  53. })
  54. //连接成功
  55. Taro.onSocketOpen(function () {
  56. console.log('连接成功')
  57. })
  58. //接收数据
  59. Taro.onSocketMessage(function (data) {
  60. console.log('接收数据', data)
  61. Taro.eventCenter.trigger('GoPuncsshIn', data)
  62. })
  63. //连接失败
  64. Taro.onSocketError(function () {
  65. console.log('websocket连接失败!');
  66. })
  67. //连接关闭
  68. Taro.onSocketClose(function () {
  69. console.log('websocket连接关闭!');
  70. })
  71. }
  72. componentDidCatchError() { }
  73. // 在 App 类中的 render() 函数没有实际作用
  74. // 请勿修改此函数
  75. render() {
  76. return (
  77. <Provider store={store}>
  78. {this.props.children}
  79. </Provider>
  80. )
  81. }
  82. }
  83. export default App