app.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { Component, PropsWithChildren } from 'react'
  2. import './app.scss'
  3. import '@/context/locales/index'
  4. import { Provider, useDispatch } from 'react-redux'
  5. import store from './store/store'
  6. import { View } from '@tarojs/components'
  7. import GlobalModal from './components/layout/GlobalModal'
  8. import Taro from '@tarojs/taro'
  9. import { getInfoSuccess } from './store/user'
  10. // import { StatusBar } from 'react-native'
  11. // import jgPush from './utils/push.rn'
  12. // import 'taro-ui/dist/style/index.scss'
  13. let LogBox;
  14. let StatusBar;
  15. let ActionSheetProvider;
  16. let messaging;
  17. if (process.env.TARO_ENV == 'rn') {
  18. LogBox = require("react-native").LogBox
  19. StatusBar = require("react-native").StatusBar
  20. ActionSheetProvider = require('@expo/react-native-action-sheet').ActionSheetProvider
  21. messaging = require('@react-native-firebase/messaging').default
  22. }
  23. const App: React.FC<PropsWithChildren> = ({ children }) => {
  24. // const dispatch = useDispatch();
  25. if (process.env.TARO_ENV == 'rn') {
  26. global.language = 'en'
  27. // messaging().setBackgroundMessageHandler(async remoteMessage => {
  28. // console.log('Message is handled in the background!', remoteMessage);
  29. // });
  30. // alert(Intl.DateTimeFormat().resolvedOptions().timeZone)
  31. }
  32. if (process.env.TARO_ENV == 'weapp') {
  33. loadWXCache()
  34. global.isDebug = Taro.getStorageSync('isDebug')
  35. const updateManager = Taro.getUpdateManager()
  36. updateManager.onUpdateReady(function () {
  37. Taro.showModal({
  38. title: '更新提示',
  39. content: '新版本已经准备好,是否重启应用?',
  40. showCancel: false,
  41. success: function (res) {
  42. if (res.confirm) {
  43. updateManager.applyUpdate();
  44. }
  45. }
  46. });
  47. });
  48. updateManager.onUpdateFailed(function () {
  49. // 新版本下载失败
  50. Taro.showModal({
  51. title: '已经有新版本喽~',
  52. content: '请您删除当前小程序,到微信 “发现-小程序” 页,重新搜索打开哦~',
  53. showCancel: false,
  54. });
  55. });
  56. }
  57. else {
  58. //关闭其中某些yellow警告
  59. loadRNCache()
  60. LogBox.ignoreLogs(['Warning: ...']); // Ignore log notification by message
  61. LogBox.ignoreAllLogs();//Ignore all log notifications
  62. // const test = require('./utils/push').default
  63. // test()
  64. // jgPush()
  65. // const JPush = require('jpush-react-native')
  66. // JPush.init({ appKey: "4fcc3e237eec4c4fb804ad49", channel: "dev", production: false });
  67. // //连接状态
  68. // JPush.addConnectEventListener(result => {
  69. // console.log(result)
  70. // debugger
  71. // })
  72. // console.log('rn jpush init')
  73. // //通知回调
  74. // var notificationListener = result => {
  75. // console.log("notificationListener:" + JSON.stringify(result))
  76. // alert(JSON.stringify(result))
  77. // };
  78. // JPush.addNotificationListener(notificationListener);
  79. // //本地通知回调
  80. // var localNotificationListener = result => {
  81. // console.log("localNotificationListener:" + JSON.stringify(result))
  82. // };
  83. // JPush.addLocalNotificationListener(localNotificationListener);
  84. // //自定义消息回调
  85. // var customMessageListener = result => {
  86. // console.log("customMessageListener:" + JSON.stringify(result))
  87. // };
  88. // JPush.addCustomMessageListener(customMessageListener);
  89. // //应用内消息回调
  90. // JPush.pageEnterTo("HomePage") // 进入首页,当页面退出时请调用 JPush.pageLeave('HomePage')
  91. // var inappMessageListener = result => {
  92. // console.log("inappMessageListener:" + JSON.stringify(result))
  93. // alert(JSON.stringify(result))
  94. // };
  95. // JPush.addInappMessageListener(inappMessageListener);
  96. // //tag alias事件回调
  97. // var tagAliasListener = result => {
  98. // console.log("tagAliasListener:" + JSON.stringify(result))
  99. // };
  100. // JPush.addTagAliasListener(tagAliasListener);
  101. // //手机号码事件回调
  102. // var mobileNumberListener = result => {
  103. // console.log("mobileNumberListener:" + JSON.stringify(result))
  104. // };
  105. // JPush.addMobileNumberListener(mobileNumberListener);
  106. }
  107. function loadWXCache() {
  108. // var showDayRing = Taro.getStorageSync('showDayRing') || false;
  109. // var showNightRing = Taro.getStorageSync('showNightRing') || false;
  110. // dispatch(showDay(showDayRing))
  111. // dispatch(showNight(showNightRing))
  112. // var gps = Taro.getStorageSync('gps')
  113. // if (gps) {
  114. // global.locationDetail = JSON.parse(gps)
  115. // }
  116. var userData = Taro.getStorageSync('userData')
  117. if (userData) {
  118. global.token = JSON.parse(userData).token
  119. // dispatch(getInfoSuccess(JSON.parse(userData)));
  120. }
  121. }
  122. async function loadRNCache() {
  123. var userData = await getStorage('userData')
  124. if (userData) {
  125. global.token = JSON.parse(userData).token
  126. }
  127. // else {
  128. // //测试直接登录
  129. // global.token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJhY2NvdW50Iiwic3ViIjoiMTE0Nzg0NGE5NDIyODk1MmE5Zjc2OTdjYWJjYmU3Y2UiLCJpc3MiOiJmYXN0Iiwibmlja25hbWUiOiJGYXN0ZXIiLCJ0eXAiOiJCZWFyZXIiLCJzZXNzaW9uX3N0YXRlIjoiZWQxMGY5Yzk1YjcxMjdhZThlZjU5ZDI4MTFlNWI1MWUiLCJleHAiOjE3NDY2NzIyMDYsImlhdCI6MTcxNTEzNjIwNn0.fbVPua1K9eDteWqrDBjaB8l8MrchwCp_JiuwQ222wU0'
  130. // }
  131. }
  132. async function getStorage(key: string) {
  133. try {
  134. const res = await Taro.getStorage({ key });
  135. return res.data;
  136. } catch {
  137. return '';
  138. }
  139. }
  140. if (process.env.TARO_ENV == 'rn') {
  141. return <ActionSheetProvider>
  142. <Provider store={store}>
  143. <StatusBar barStyle='light-content' backgroundColor="#000" />
  144. {children}
  145. </Provider>
  146. </ActionSheetProvider>
  147. }
  148. return <Provider store={store}>
  149. {children}
  150. </Provider>
  151. }
  152. // class App extends Component<PropsWithChildren> {
  153. // componentDidMount () {}
  154. // componentDidShow () {}
  155. // componentDidHide () {}
  156. // // this.props.children 是将要会渲染的页面
  157. // render () {
  158. // return this.props.children
  159. // }
  160. // }
  161. export default App