app.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. if (process.env.TARO_ENV == 'rn') {
  17. LogBox = require("react-native").LogBox
  18. StatusBar = require("react-native").StatusBar
  19. ActionSheetProvider = require('@expo/react-native-action-sheet').ActionSheetProvider
  20. }
  21. const App: React.FC<PropsWithChildren> = ({ children }) => {
  22. // const dispatch = useDispatch();
  23. if (process.env.TARO_ENV == 'rn') {
  24. global.language = 'en'
  25. // alert(Intl.DateTimeFormat().resolvedOptions().timeZone)
  26. }
  27. if (process.env.TARO_ENV == 'weapp') {
  28. loadWXCache()
  29. global.isDebug = Taro.getStorageSync('isDebug')
  30. const updateManager = Taro.getUpdateManager()
  31. updateManager.onUpdateReady(function () {
  32. Taro.showModal({
  33. title: '更新提示',
  34. content: '新版本已经准备好,是否重启应用?',
  35. showCancel: false,
  36. success: function (res) {
  37. if (res.confirm) {
  38. updateManager.applyUpdate();
  39. }
  40. }
  41. });
  42. });
  43. updateManager.onUpdateFailed(function () {
  44. // 新版本下载失败
  45. Taro.showModal({
  46. title: '已经有新版本喽~',
  47. content: '请您删除当前小程序,到微信 “发现-小程序” 页,重新搜索打开哦~',
  48. showCancel: false,
  49. });
  50. });
  51. }
  52. else {
  53. //关闭其中某些yellow警告
  54. loadRNCache()
  55. LogBox.ignoreLogs(['Warning: ...']); // Ignore log notification by message
  56. LogBox.ignoreAllLogs();//Ignore all log notifications
  57. // const test = require('./utils/push').default
  58. // test()
  59. // jgPush()
  60. // const JPush = require('jpush-react-native')
  61. // JPush.init({ appKey: "4fcc3e237eec4c4fb804ad49", channel: "dev", production: false });
  62. // //连接状态
  63. // JPush.addConnectEventListener(result => {
  64. // console.log(result)
  65. // debugger
  66. // })
  67. // console.log('rn jpush init')
  68. // //通知回调
  69. // var notificationListener = result => {
  70. // console.log("notificationListener:" + JSON.stringify(result))
  71. // alert(JSON.stringify(result))
  72. // };
  73. // JPush.addNotificationListener(notificationListener);
  74. // //本地通知回调
  75. // var localNotificationListener = result => {
  76. // console.log("localNotificationListener:" + JSON.stringify(result))
  77. // };
  78. // JPush.addLocalNotificationListener(localNotificationListener);
  79. // //自定义消息回调
  80. // var customMessageListener = result => {
  81. // console.log("customMessageListener:" + JSON.stringify(result))
  82. // };
  83. // JPush.addCustomMessageListener(customMessageListener);
  84. // //应用内消息回调
  85. // JPush.pageEnterTo("HomePage") // 进入首页,当页面退出时请调用 JPush.pageLeave('HomePage')
  86. // var inappMessageListener = result => {
  87. // console.log("inappMessageListener:" + JSON.stringify(result))
  88. // alert(JSON.stringify(result))
  89. // };
  90. // JPush.addInappMessageListener(inappMessageListener);
  91. // //tag alias事件回调
  92. // var tagAliasListener = result => {
  93. // console.log("tagAliasListener:" + JSON.stringify(result))
  94. // };
  95. // JPush.addTagAliasListener(tagAliasListener);
  96. // //手机号码事件回调
  97. // var mobileNumberListener = result => {
  98. // console.log("mobileNumberListener:" + JSON.stringify(result))
  99. // };
  100. // JPush.addMobileNumberListener(mobileNumberListener);
  101. }
  102. function loadWXCache() {
  103. // var showDayRing = Taro.getStorageSync('showDayRing') || false;
  104. // var showNightRing = Taro.getStorageSync('showNightRing') || false;
  105. // dispatch(showDay(showDayRing))
  106. // dispatch(showNight(showNightRing))
  107. // var gps = Taro.getStorageSync('gps')
  108. // if (gps) {
  109. // global.locationDetail = JSON.parse(gps)
  110. // }
  111. var userData = Taro.getStorageSync('userData')
  112. if (userData) {
  113. global.token = JSON.parse(userData).token
  114. // dispatch(getInfoSuccess(JSON.parse(userData)));
  115. }
  116. }
  117. async function loadRNCache() {
  118. // var showDayRing = await getStorage('showDayRing') || false;
  119. // var showNightRing = await getStorage('showNightRing') || false;
  120. // dispatch(showDay(showDayRing))
  121. // dispatch(showNight(showNightRing))
  122. // var gps = await getStorage('gps')
  123. // if (gps) {
  124. // global.locationDetail = JSON.parse(gps)
  125. // }
  126. var userData = await getStorage('userData')
  127. if (userData) {
  128. global.token = JSON.parse(userData).token
  129. // dispatch(getInfoSuccess(JSON.parse(userData)));
  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' />
  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