app.tsx 6.8 KB

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