app.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. //在页面加载完成的生命周期加入这段代码,Taro在componentDidMount生命周期中使用,原生在onLoad中使用
  14. const updateManager = Taro.getUpdateManager()
  15. updateManager.onCheckForUpdate(function (res) {
  16. // 请求完新版本信息的回调
  17. //console.log('请求完新版本信息的回调')
  18. console.log(res.hasUpdate);
  19. })
  20. updateManager.onUpdateReady(function () {
  21. Taro.showModal({
  22. title: '更新提示',
  23. content: '新版本已经准备好,是否重启应用?',
  24. success(res) {
  25. if (res.confirm) {
  26. console.log("go")
  27. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  28. updateManager.applyUpdate()
  29. }
  30. }
  31. })
  32. })
  33. updateManager.onUpdateFailed(function () {
  34. // 新版本下载失败
  35. console.log('新版本下载失败')
  36. // errorReport(ERROREVENTS.VERSION_UPGRADE_FAILED, '新版本下载失败');
  37. })
  38. }
  39. componentDidShow() {
  40. Taro.eventCenter.on('getStorageSync', () => {
  41. let token = Taro.getStorageSync('token');
  42. if (token) {
  43. this.getStorageSync();
  44. }
  45. })
  46. let token = Taro.getStorageSync('token');
  47. if (token) {
  48. this.getStorageSync();
  49. }
  50. this.getWxConfig();
  51. }
  52. componentDidHide() {
  53. Taro.closeSocket();
  54. }
  55. getWxConfig() {
  56. getWxConfig({}).then(v => {
  57. if (v.error.length === 0) {
  58. Taro.setStorageSync("wxConfig", v.data || {});
  59. } else {
  60. Taro.showToast({
  61. title: v.error[0].message,
  62. icon: 'none'
  63. })
  64. }
  65. }).catch((error) => {
  66. console.log(error)
  67. })
  68. }
  69. getStorageSync() {
  70. let token = Taro.getStorageSync('token');
  71. //建立连接
  72. Taro.connectSocket({
  73. header: {
  74. 'token': token,
  75. },
  76. // url: 'wss://bm.jishutao.com/webSocketServer',
  77. url: 'wss://172.16.0.255:8080/webSocketServer'
  78. // url: 'wss://uat.jishutao.com/webSocketServer'
  79. })
  80. //连接成功
  81. Taro.onSocketOpen(function () {
  82. console.log('连接成功')
  83. })
  84. //接收数据
  85. Taro.onSocketMessage(function (data) {
  86. console.log('接收数据', data)
  87. Taro.eventCenter.trigger('GoPuncsshIn', data)
  88. })
  89. //连接失败
  90. Taro.onSocketError(function () {
  91. console.log('websocket连接失败!');
  92. })
  93. //连接关闭
  94. Taro.onSocketClose(function () {
  95. console.log('websocket连接关闭!');
  96. })
  97. }
  98. componentDidCatchError() { }
  99. // 在 App 类中的 render() 函数没有实际作用
  100. // 请勿修改此函数
  101. render() {
  102. return (
  103. <Provider store={store}>
  104. {this.props.children}
  105. </Provider>
  106. )
  107. }
  108. }
  109. export default App