import { Component } from 'react'
import { Provider } from 'react-redux'

import configStore from './store'

import './app.less'
import 'taro-skeleton/dist/index.css'
import Taro from "@tarojs/taro";

import { getWxConfig } from './utils/servers/servers';

import 'taro-ui/dist/style/components/drawer.scss';
import 'taro-ui/dist/style/components/list.scss';

const store = configStore()

class App extends Component {
  componentDidMount() { }

  componentDidShow() {
    Taro.eventCenter.on('getStorageSync', () => {
      let token = Taro.getStorageSync('token');
      if (token) {
        this.getStorageSync();
      }
    })
    let token = Taro.getStorageSync('token');
    if (token) {
      this.getStorageSync();
    }
    this.getWxConfig();
  }

  componentDidHide() {
    Taro.closeSocket();
  }

  getWxConfig() {
    getWxConfig({}).then(v => {
      if (v.error.length === 0) {
        Taro.setStorageSync("wxConfig", v.data || {});
      } else {
        Taro.showToast({
          title: v.error[0].message,
          icon: 'none'
        })
      }
    }).catch((error) => {
      console.log(error)
    })
  }

  getStorageSync() {
    let token = Taro.getStorageSync('token');
    //建立连接
    Taro.connectSocket({
      header: {
        'token': token,
      },
      url: "wss://bm.jishutao.com/webSocketServer",
      // url: 'wss://172.16.0.255:8080/webSocketServer'
      // url: 'wss://uat.jishutao.com/webSocketServer'
    })

    //连接成功
    Taro.onSocketOpen(function () {
      console.log('连接成功')
    })

    //接收数据
    Taro.onSocketMessage(function (data) {
      console.log(data, '接收数据')
      Taro.eventCenter.trigger('GoPuncsshIn', data)
    })

    //连接失败
    Taro.onSocketError(function () {
      console.log('websocket连接失败！');
    })

    //连接关闭
    Taro.onSocketClose(function () {
      console.log('websocket连接关闭！');
    })
  }

  componentDidCatchError() { }

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render() {
    return (
      <Provider store={store}>
        {this.props.children}
      </Provider>
    )
  }
}

export default App
