app.tsx 7.5 KB

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