DayNightCard.tsx 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. import { PageContainer, Switch, Text, View, Image } from '@tarojs/components'
  2. import './DayNightCard.scss'
  3. import { ColorType } from '@/context/themes/color'
  4. import { useEffect, useState } from 'react'
  5. import Box from '@/components/layout/Box'
  6. import Taro, { useDidShow } from '@tarojs/taro'
  7. import { clearLocation, getPerm, latestLocation, uploadPerm } from '@/services/user'
  8. import { useDispatch, useSelector } from 'react-redux'
  9. import { useTranslation } from 'react-i18next'
  10. import { TimeFormatter } from '@/utils/time_format'
  11. import { systemLocation } from '@/services/common'
  12. import { setDayRingData, showDay } from '@/store/day'
  13. import { setNightRingData, showNight, updateMember } from '@/store/night'
  14. import Modal from '@/components/layout/Modal.weapp'
  15. import { getTimezone, rpxToPx } from '@/utils/tools'
  16. import { jumpPage } from '@/features/trackTimeDuration/hooks/Common'
  17. import DayNightDetailPopup from './DayNightDetailPopup'
  18. import '@/utils/ring_card.scss';
  19. import { IconNight, IconSunrise, IconSunset } from '@/components/basic/Icons'
  20. import DayNightRing from './DayNightRing'
  21. import dayjs from 'dayjs'
  22. import DayNightStatus from './DayNightStatus'
  23. import showAlert from '@/components/basic/Alert'
  24. // import checkLocation from '@/utils/native_permission_check'
  25. let useNavigation;
  26. let Linking;
  27. if (process.env.TARO_ENV == 'rn') {
  28. useNavigation = require("@react-navigation/native").useNavigation
  29. Linking = require('react-native').Linking;
  30. }
  31. let locationDetail;
  32. let sunriseA = new Date()
  33. sunriseA.setHours(6)
  34. sunriseA.setMinutes(0)
  35. sunriseA.setSeconds(0)
  36. sunriseA.setMilliseconds(0)
  37. const sunsetA = new Date()
  38. sunsetA.setHours(18)
  39. sunsetA.setMinutes(0)
  40. sunsetA.setSeconds(0)
  41. sunsetA.setMilliseconds(0)
  42. const utc = require('dayjs/plugin/utc')
  43. const timezone = require('dayjs/plugin/timezone')
  44. var advanced = require("dayjs/plugin/advancedFormat")
  45. dayjs.extend(utc)
  46. dayjs.extend(timezone)
  47. dayjs.extend(advanced)
  48. let noTimestampData = false;
  49. export default function DayNightCard(props: { isNight: boolean, count: number, detailPop?: Function }) {
  50. const user = useSelector((state: any) => state.user);
  51. const dayStore = useSelector((state: any) => state.day);
  52. const nightStore = useSelector((state: any) => state.night);
  53. const [authInfo, setAuthInfo] = useState(global.locationDetail ? global.locationDetail : null)
  54. const [sunriseDate, setSunriseDate] = useState<any>(sunriseA)
  55. const [sunsetDate, setSunsetDate] = useState<any>(sunsetA)
  56. const [showDetailModal, setShowDetailModal] = useState(false)
  57. const [showDetailPopup, setShowDetailPopup] = useState(false)
  58. const [nightDate, setNightDate] = useState(new Date())
  59. const [dayDate, setDayDate] = useState(new Date())
  60. const dispatch = useDispatch();
  61. const { t } = useTranslation()
  62. const isMember = true;
  63. let navigation;
  64. if (useNavigation) {
  65. navigation = useNavigation()
  66. }
  67. useEffect(() => {
  68. if (user.isLogin) {
  69. if (global.locationDetail) {
  70. locationDetail = global.locationDetail
  71. setAuthInfo(locationDetail)
  72. }
  73. getLatestLocation()
  74. if (props.isNight) {
  75. global.refreshNight = () => {
  76. getLatestLocation()
  77. }
  78. }
  79. else {
  80. global.refreshDay = () => {
  81. getLatestLocation()
  82. }
  83. }
  84. } else {
  85. setSunriseDate(sunriseA)
  86. setSunsetDate(sunsetA)
  87. dispatch(setDayRingData({
  88. date: new Date().getTime(),
  89. sunrise: '06:00',
  90. sunset: '18:00'
  91. }))
  92. dispatch(setNightRingData({
  93. date: new Date().getTime(),
  94. sunrise: '06:00',
  95. sunset: '18:00'
  96. }))
  97. setAuthInfo(null)
  98. global.locationDetail = null
  99. updateUnloginData()
  100. }
  101. }, [user.isLogin])
  102. useEffect(() => {
  103. locationDetail = global.locationDetail
  104. setAuthInfo(locationDetail)
  105. }, [global.locationDetail])
  106. useEffect(() => {
  107. if (locationDetail)
  108. updateDate(locationDetail)
  109. }, [])
  110. useEffect(() => {
  111. if (!locationDetail && global.locationDetail){
  112. locationDetail = global.locationDetail
  113. }
  114. if (locationDetail) {
  115. var detail: any = locationDetail
  116. var now = new Date()
  117. if (now.getHours() == 0 && now.getMinutes() == 0 && now.getSeconds() == 0) {
  118. detail.daylights.splice(0, 1)
  119. }
  120. locationDetail = detail
  121. if (detail && (detail as any).daylights) {
  122. (detail as any).daylights.map(item => {
  123. if (noTimestampData) {
  124. item.sunrise_ts = new Date(item.date + 'T' + item.sunrise + ':00').getTime()
  125. item.sunset_ts = new Date(item.date + 'T' + item.sunset + ':00').getTime()
  126. }
  127. })
  128. }
  129. // setLocationDetail(detail)
  130. updateDate(detail)
  131. }
  132. }, [props.count])
  133. useDidShow(() => {
  134. if (!user.isLogin) {
  135. dispatch(showNight(false))
  136. dispatch(showDay(false))
  137. Taro.setStorage({
  138. key: 'showNightRing',
  139. data: false
  140. })
  141. Taro.setStorage({
  142. key: 'showDayRing',
  143. data: false
  144. })
  145. }
  146. })
  147. function getLatestLocation() {
  148. var today = new Date()
  149. var yesterday = new Date(today.getTime() - 24 * 3600 * 1000)
  150. var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000 * 50)
  151. var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}`
  152. var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
  153. latestLocation({
  154. date_start: strYesterday, date_end: strTomorrow
  155. }).then(data => {
  156. // (data as any).timezone = 'GMT-1000'
  157. if (data && (data as any).daylights) {
  158. (data as any).daylights.map(item => {
  159. if (!item.sunrise_ts) {
  160. noTimestampData = true
  161. item.sunrise_ts = new Date(item.date + 'T' + item.sunrise + ':00').getTime()
  162. item.sunset_ts = new Date(item.date + 'T' + item.sunset + ':00').getTime()
  163. }
  164. else {
  165. noTimestampData = false
  166. }
  167. })
  168. }
  169. setAuthInfo(data as any)
  170. updateDate(data)
  171. locationDetail = data
  172. global.locationDetail = locationDetail
  173. Taro.setStorage({
  174. key: 'gps',
  175. data: JSON.stringify(data as any)
  176. })
  177. })
  178. }
  179. function updateDate(data) {
  180. var today = new Date()
  181. if (user.isLogin && isMember && data && data.daylights && data.daylights.length > 0) {
  182. // var sunriseObj,sunsetObj;
  183. var list = data.daylights
  184. for (var i = 0; i < (list.length - 1); i++) {
  185. //夜间
  186. if (list[i].sunset_ts < today.getTime() && list[i + 1].sunrise_ts > today.getTime()) {
  187. if (props.isNight) {
  188. global.nightObj = {
  189. is_polar_night: list[i].is_polar_night,
  190. sunrise: {
  191. timestamp: list[i + 1].sunrise_ts,
  192. time: list[i + 1].sunrise
  193. },
  194. sunset: {
  195. timestamp: list[i].sunset_ts,
  196. time: list[i].sunset
  197. }
  198. }
  199. }
  200. else {
  201. global.dayObj = {
  202. is_polar_day: list[i + 1].is_polar_day,
  203. sunrise: {
  204. timestamp: list[i + 1].sunrise_ts,
  205. time: list[i + 1].sunrise
  206. },
  207. sunset: {
  208. timestamp: list[i + 1].sunset_ts,
  209. time: list[i + 1].sunset
  210. }
  211. }
  212. }
  213. }
  214. //白天
  215. else if (list[i].sunrise_ts < today.getTime() && list[i].sunset_ts > today.getTime()) {
  216. if (props.isNight) {
  217. global.nightObj = {
  218. is_polar_night: list[i].is_polar_night,
  219. sunrise: {
  220. timestamp: list[i + 1].sunrise_ts,
  221. time: list[i + 1].sunrise
  222. },
  223. sunset: {
  224. timestamp: list[i].sunset_ts,
  225. time: list[i].sunset
  226. }
  227. }
  228. }
  229. else {
  230. global.dayObj = {
  231. is_polar_day: list[i].is_polar_day,
  232. sunrise: {
  233. timestamp: list[i].sunrise_ts,
  234. time: list[i].sunrise
  235. },
  236. sunset: {
  237. timestamp: list[i].sunset_ts,
  238. time: list[i].sunset
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. else {
  246. locationDetail = null
  247. setAuthInfo(null)
  248. dispatch(updateMember({ isMember: isMember, gpsInfo: null }))
  249. dispatch(setDayRingData({
  250. date: sunriseDate.getTime(),
  251. sunrise: '06:00',
  252. sunset: '18:00'
  253. }))
  254. dispatch(setNightRingData({
  255. date: sunsetDate.getTime(),
  256. sunrise: '06:00',
  257. sunset: '18:00'
  258. }))
  259. debugger
  260. updateUnloginData()
  261. }
  262. }
  263. function updateUnloginData() {
  264. var now = new Date()
  265. var sunriseT = 0
  266. var sunsetT = 0
  267. var sunriseTDay = 0
  268. var sunsetTDay = 0
  269. if (now.getHours() < 6) {
  270. var temp1 = new Date(now.getTime() - 24 * 3600 * 1000)
  271. temp1.setHours(18)
  272. temp1.setMinutes(0)
  273. temp1.setSeconds(0)
  274. temp1.setMilliseconds(0)
  275. sunsetT = temp1.getTime()
  276. sunriseT = sunsetT + 12 * 3600 * 1000
  277. sunriseTDay = sunriseT
  278. sunsetTDay = sunriseTDay + 12 * 3600 * 1000
  279. }
  280. else if (now.getHours() < 18) {
  281. var temp1 = new Date()
  282. temp1.setHours(6)
  283. temp1.setMinutes(0)
  284. temp1.setSeconds(0)
  285. temp1.setMilliseconds(0)
  286. sunriseTDay = temp1.getTime()
  287. sunsetTDay = sunriseTDay + 12 * 3600 * 1000
  288. sunsetT = sunsetTDay
  289. sunriseT = sunsetT + 12 * 3600 * 1000
  290. }
  291. else {
  292. var temp1 = new Date()
  293. temp1.setHours(18)
  294. temp1.setMinutes(0)
  295. temp1.setSeconds(0)
  296. temp1.setMilliseconds(0)
  297. sunsetT = temp1.getTime()
  298. sunriseT = sunsetT + 12 * 3600 * 1000
  299. sunriseTDay = sunriseT
  300. sunsetTDay = sunriseTDay + 12 * 3600 * 1000
  301. }
  302. global.nightObj = {
  303. sunrise: {
  304. timestamp: sunriseT,
  305. time: '06:00'
  306. },
  307. sunset: {
  308. timestamp: sunsetT,
  309. time: '18:00'
  310. }
  311. }
  312. global.dayObj = {
  313. sunrise: {
  314. timestamp: sunriseTDay,
  315. time: '06:00'
  316. },
  317. sunset: {
  318. timestamp: sunsetTDay,
  319. time: '18:00'
  320. }
  321. }
  322. }
  323. function tapCard(e) {
  324. // if (process.env.TARO_ENV == 'weapp') {
  325. // e.stopPropagation()
  326. // }
  327. if (!user.isLogin) {
  328. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  329. return
  330. }
  331. global.showDayNightSwiperPop(props.isNight, nightDate, dayDate)
  332. // if (!dayStore.showDayRing) {
  333. // return
  334. // }
  335. // setShowDetailPopup(true)
  336. // if (props.detailPop)
  337. // props.detailPop(props.isNight, nightDate, dayDate)
  338. }
  339. function auth(e?: any) {
  340. if (e && process.env.TARO_ENV == 'weapp') {
  341. e.stopPropagation()
  342. }
  343. var today = new Date()
  344. var yesterday = new Date(today.getTime() - 24 * 3600 * 1000)
  345. var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000 * 5)
  346. var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}`
  347. var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
  348. if (process.env.TARO_ENV == 'rn') {
  349. Taro.getLocation({
  350. success(res) {
  351. systemLocation({
  352. lat: res.latitude,
  353. lng: res.longitude,
  354. date_start: strYesterday,
  355. date_end: strTomorrow,
  356. coordinate_system_standard: 'WGS-84'
  357. }).then(data => {
  358. noTimestampData = false
  359. console.log('get Location', data)
  360. updateDate(data);
  361. locationDetail = data;
  362. global.locationDetail = locationDetail
  363. setAuthInfo(data as any)
  364. Taro.setStorage({
  365. key: 'gps',
  366. data: JSON.stringify(data as any)
  367. })
  368. dispatch(updateMember({ isMember: isMember, gpsInfo: (data as any) }))
  369. showRing()
  370. if (Taro.getSystemInfoSync().platform == 'android' && process.env.TARO_ENV == 'rn') {
  371. var ToastAndroid = require('react-native').ToastAndroid;
  372. ToastAndroid.show(t('feature.day_night.location_updated'), ToastAndroid.LONG);
  373. }
  374. else {
  375. showAlert({
  376. title:'Success',
  377. content:t('feature.day_night.location_updated'),
  378. showCancel:false
  379. })
  380. // Taro.showToast({
  381. // title: t('feature.day_night.location_updated'),
  382. // icon: 'none'
  383. // })
  384. }
  385. })
  386. },
  387. fail(res) {
  388. if (res.errMsg == 'Permissions denied!') {
  389. showAlert({
  390. title: t('feature.auth_sys.location_title'),
  391. content: t('feature.auth_sys.location_desc'),
  392. showCancel: true,
  393. cancelText: t('feature.auth_sys.location_cancel'),
  394. confirmText: t('feature.auth_sys.location_confirm'),
  395. confirm: () => {
  396. Linking.openURL('app-settings:')
  397. }
  398. })
  399. }
  400. else {
  401. if (Taro.getSystemInfoSync().platform == 'android' && process.env.TARO_ENV == 'rn') {
  402. var ToastAndroid = require('react-native').ToastAndroid;
  403. ToastAndroid.show(t('feature.day_night.location_failed'), ToastAndroid.LONG);
  404. }
  405. else {
  406. showAlert({
  407. title:'Error',
  408. content:t('feature.day_night.location_failed'),
  409. showCancel:false
  410. })
  411. // Taro.showToast({
  412. // title: t('feature.day_night.location_failed'),
  413. // icon: 'none'
  414. // })
  415. }
  416. }
  417. console.log(res)
  418. showRing()
  419. },
  420. complete(res) {
  421. }
  422. })
  423. return
  424. }
  425. Taro.chooseLocation({
  426. latitude: authInfo && authInfo.lat ? authInfo.lat : undefined,
  427. longitude: authInfo && authInfo.lat ? authInfo.lng : undefined,
  428. success: function (res) {
  429. systemLocation({
  430. lat: res.latitude,
  431. lng: res.longitude,
  432. name: res.name,
  433. address: res.address,
  434. date_start: strYesterday,
  435. date_end: strTomorrow,
  436. coordinate_system_standard: 'GCJ-02'
  437. }).then(data => {
  438. noTimestampData = false;
  439. updateDate(data);
  440. locationDetail = data;
  441. global.locationDetail = locationDetail
  442. setAuthInfo(data as any)
  443. Taro.setStorage({
  444. key: 'gps',
  445. data: JSON.stringify(data as any)
  446. })
  447. showRing()
  448. dispatch(updateMember({ isMember: isMember, gpsInfo: (data as any) }))
  449. if (global.swiperDayNightRefresh) {
  450. global.swiperDayNightRefresh()
  451. }
  452. })
  453. },
  454. fail(res) {
  455. Taro.showToast({
  456. title: '位置修改失败!\n请重新选择就近位置',
  457. icon: 'none'
  458. })
  459. showRing()
  460. },
  461. complete(res) {
  462. }
  463. })
  464. }
  465. function showRing() {
  466. dispatch(showNight(true))
  467. dispatch(showDay(true))
  468. Taro.setStorage({
  469. key: 'showNightRing',
  470. data: true
  471. })
  472. Taro.setStorage({
  473. key: 'showDayRing',
  474. data: true
  475. })
  476. }
  477. function getLocation() {
  478. if (!authInfo) {
  479. return ''
  480. }
  481. return `${getCity()} | ${Math.abs(parseInt(authInfo.lat))}°${parseInt(authInfo.lat) < 0 ? 'S' : 'N'} ${Math.abs(parseInt(authInfo.lng))}°${parseInt(authInfo.lng) < 0 ? 'W' : 'E'}`
  482. }
  483. function getCity() {
  484. var city = ''
  485. if (!authInfo) {
  486. return ''
  487. }
  488. if ((authInfo as any).address) {
  489. if ((authInfo as any).address.city.length > 0) {
  490. city = (authInfo as any).address.city
  491. }
  492. else if ((authInfo as any).address.province.length > 0) {
  493. city = (authInfo as any).address.province
  494. }
  495. else if ((authInfo as any).address.country.length > 0) {
  496. city = (authInfo as any).address.country
  497. }
  498. else {
  499. city = t('feature.track_time_duration.third_ring.unknown')
  500. }
  501. }
  502. else {
  503. city = t('feature.track_time_duration.third_ring.unknown')
  504. }
  505. return city
  506. }
  507. function clearData() {
  508. Taro.showModal({
  509. title: '提示',
  510. content: '确认清除位置数据?',
  511. success: function (res) {
  512. if (res.confirm) {
  513. clearLocation().then(res => {
  514. getLatestLocation()
  515. })
  516. } else if (res.cancel) {
  517. console.log('用户点击取消')
  518. }
  519. }
  520. })
  521. }
  522. function detailModalContent() {
  523. var timezone = getTimezone()
  524. if (!authInfo || !(authInfo as any).lat) {
  525. return <View style={{ height: 100, display: 'flex', alignItems: 'center', justifyContent: 'center', width: rpxToPx(750) }}>
  526. <Text>暂无位置信息</Text>
  527. </View>
  528. }
  529. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  530. {
  531. <View className="cell_bg">
  532. {
  533. <View className="cell_full">
  534. <Text className="cell_title">{t('feature.track_time_duration.third_ring.location')}</Text>
  535. <Text className="cell_value">{authInfo ? getLocation() : t('feature.track_time_duration.third_ring.enter')}</Text>
  536. </View>
  537. }
  538. <View className="cell_line" style={{ height: 1 }} />
  539. {
  540. <View className="cell_full" >
  541. <Text className="cell_title">{t('feature.track_time_duration.third_ring.latitude')}</Text>
  542. <Text className="cell_value">{(authInfo as any).lat}</Text>
  543. </View>
  544. }
  545. <View className="cell_line" style={{ height: 1 }} />
  546. <View className="cell_full">
  547. <Text className="cell_title" >{t('feature.track_time_duration.third_ring.longitude')}</Text>
  548. <Text className="cell_value">{(authInfo as any).lng}</Text>
  549. </View>
  550. <View className="cell_line" style={{ height: 1 }} />
  551. <View className="cell_full">
  552. <Text className="cell_title">{t('feature.track_time_duration.third_ring.timezone')}</Text>
  553. <Text className="cell_value">{timezone}</Text>
  554. </View>
  555. </View>
  556. }
  557. <Text onClick={() => {
  558. clearData()
  559. setShowDetailModal(false)
  560. }} style={{ width: '100%', textAlign: 'center', marginBottom: 50, color: '#fff', marginTop: 20 }}>清除位置</Text>
  561. </View>
  562. }
  563. function modalContent2() {
  564. return <Modal
  565. testInfo={null}
  566. dismiss={() => {
  567. setShowDetailModal(false)
  568. }}
  569. confirm={() => { }}>
  570. {
  571. detailModalContent()
  572. }
  573. </Modal>
  574. }
  575. function modalContent() {
  576. return <Modal
  577. testInfo={null}
  578. dismiss={() => {
  579. setShowDetailPopup(false)
  580. }}
  581. confirm={() => { }}>
  582. <DayNightDetailPopup
  583. isNight={props.isNight}
  584. authInfo={authInfo}
  585. nightDate={nightDate}
  586. dayDate={dayDate}
  587. updateLocation={auth}
  588. onClose={() => { setShowDetailPopup(false) }}
  589. />
  590. </Modal>
  591. }
  592. function longClick() {
  593. if (user.isLogin && user.test_user && authInfo)
  594. global.showDayNightSwiperModal()
  595. // setShowDetailModal(true)
  596. }
  597. function showLocationAlert(e) {
  598. if (process.env.TARO_ENV == 'weapp') {
  599. e.stopPropagation()
  600. }
  601. tapCard(e)
  602. }
  603. function nightRing() {
  604. return <DayNightRing
  605. isNight={true}
  606. isThirdRing={false}
  607. canvasId={'day_night_card_night_ring'}
  608. authInfo={authInfo}
  609. />
  610. }
  611. function localNow(now: Date) {
  612. if (authInfo && authInfo.timezone) {
  613. return new Date(TimeFormatter.transferTimestamp(now.getTime(), authInfo.timezone.gmt))
  614. }
  615. return now
  616. }
  617. function dayRing() {
  618. return <DayNightRing
  619. isNight={false}
  620. isThirdRing={false}
  621. canvasId={'day_night_card_big_day'}
  622. authInfo={authInfo}
  623. />
  624. }
  625. function getSunsetDuration() {
  626. if (authInfo && authInfo.night_completed && new Date().getTime() > authInfo.night_completed.sunrise_ts) {
  627. global.sunsetDuration = TimeFormatter.durationFormate(authInfo.night_completed.sunrise_ts, authInfo.night_completed.sunset_ts, true)
  628. return TimeFormatter.durationFormate(authInfo.night_completed.sunrise_ts, authInfo.night_completed.sunset_ts, true)
  629. }
  630. var now = new Date()
  631. if (authInfo && global.nightObj) {
  632. var nightObj = global.nightObj
  633. if (nightObj.sunset.timestamp < now.getTime() && nightObj.sunrise.timestamp > now.getTime()) {
  634. global.sunsetDuration = TimeFormatter.countdown(nightObj.sunset.timestamp, now.getTime(), true)
  635. return TimeFormatter.countdown(nightObj.sunset.timestamp, now.getTime(), true)
  636. }
  637. global.sunsetDuration = TimeFormatter.calculateTimeDifference(nightObj.sunset.timestamp, nightObj.sunrise.timestamp);
  638. return TimeFormatter.calculateTimeDifference(nightObj.sunset.timestamp, nightObj.sunrise.timestamp);
  639. }
  640. now = localNow(now)
  641. if (props.isNight && sunsetDate) {
  642. if (now.getTime() > sunsetDate.getTime()) {
  643. global.sunsetDuration = TimeFormatter.countdown(sunsetDate.getTime(), now.getTime(), true)
  644. return TimeFormatter.countdown(sunsetDate.getTime(), now.getTime(), true)
  645. }
  646. // return TimeFormatter.countdown(sunsetDate.getTime())
  647. }
  648. var sunRiseObj = nightStore.nightRingSunrise
  649. var sunSetObj = nightStore.nightRingSunset
  650. var sunRise = 24 * 60 + parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  651. var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  652. if (sunSetObj.indexOf('PM') != -1) {
  653. sunSet += 12 * 60
  654. }
  655. var duration = (sunRise - sunSet) * 60 * 1000
  656. global.sunsetDuration = TimeFormatter.calculateTimeDifference(now.getTime(), now.getTime() + duration);
  657. return TimeFormatter.calculateTimeDifference(now.getTime(), now.getTime() + duration);
  658. }
  659. function getSunriseDuration() {
  660. if (authInfo && authInfo.day_completed && new Date().getTime() > authInfo.day_completed.sunset_ts) {
  661. global.sunriseDuration = TimeFormatter.durationFormate(authInfo.day_completed.sunrise_ts, authInfo.day_completed.sunset_ts, true)
  662. return TimeFormatter.durationFormate(authInfo.day_completed.sunrise_ts, authInfo.day_completed.sunset_ts, true)
  663. }
  664. var now = new Date()
  665. var now = new Date()
  666. if (global.dayObj) {
  667. var dayObj = global.dayObj
  668. if (dayObj.sunrise.timestamp < now.getTime() && dayObj.sunset.timestamp > now.getTime()) {
  669. global.sunriseDuration = TimeFormatter.countdown(dayObj.sunrise.timestamp, now.getTime(), true)
  670. return TimeFormatter.countdown(dayObj.sunrise.timestamp, now.getTime(), true)
  671. }
  672. global.sunriseDuration = TimeFormatter.calculateTimeDifference(dayObj.sunrise.timestamp, dayObj.sunset.timestamp, true);
  673. return TimeFormatter.calculateTimeDifference(dayObj.sunrise.timestamp, dayObj.sunset.timestamp, true);
  674. }
  675. return ''
  676. // now = localNow(now)
  677. // if (!props.isNight && sunriseDate) {
  678. // if (now.getTime() > sunriseDate.getTime()) {
  679. // global.sunriseDuration = TimeFormatter.countdown(sunriseDate.getTime())
  680. // return TimeFormatter.countdown(sunriseDate.getTime(), now.getTime())
  681. // }
  682. // }
  683. // var sunRiseObj = dayStore.dayRingSunrise
  684. // var sunSetObj = dayStore.dayRingSunset
  685. // var sunRise = parseInt(sunRiseObj.split(':')[0]) * 60 + parseInt(sunRiseObj.split(':')[1])
  686. // var sunSet = parseInt(sunSetObj.split(':')[0]) * 60 + parseInt(sunSetObj.split(':')[1])
  687. // if (sunSetObj.indexOf('PM') != -1) {
  688. // sunSet += 12 * 60
  689. // }
  690. // var duration = (sunSet - sunRise) * 60 * 1000
  691. // global.sunriseDuration = TimeFormatter.calculateTimeDifference(now.getTime(), now.getTime() + duration);
  692. // return TimeFormatter.calculateTimeDifference(now.getTime(), now.getTime() + duration);
  693. }
  694. function completedDate() {
  695. if (props.isNight) {
  696. if (authInfo && authInfo.night_completed && new Date().getTime() > authInfo.night_completed.sunrise_ts) {
  697. if (localNow(new Date()).getTime() != new Date().getTime()) {
  698. return TimeFormatter.getMonthAndDayByTimestamp(authInfo.night_completed.sunrise_ts, true)
  699. }
  700. return TimeFormatter.dateDescription(authInfo.night_completed.sunrise_ts, true)
  701. }
  702. return ''
  703. }
  704. if (authInfo && authInfo.day_completed && new Date().getTime() > authInfo.day_completed.sunset_ts) {
  705. if (localNow(new Date()).getTime() != new Date().getTime()) {
  706. return TimeFormatter.getMonthAndDayByTimestamp(authInfo.day_completed.sunset_ts, true)
  707. }
  708. return TimeFormatter.dateDescription(authInfo.day_completed.sunset_ts, true)
  709. }
  710. return ''
  711. }
  712. function showArrow() {
  713. if (props.isNight) {
  714. if (authInfo && authInfo.night_completed && new Date().getTime() > authInfo.night_completed.sunrise_ts) {
  715. return true
  716. }
  717. return false
  718. }
  719. if (authInfo && authInfo.day_completed && new Date().getTime() > authInfo.day_completed.sunset_ts) {
  720. return true
  721. }
  722. return false
  723. }
  724. function currentStatus() {
  725. if (showArrow()) {
  726. if (props.isNight) {
  727. if (authInfo.day_completed && new Date().getTime() > authInfo.day_completed.sunset_ts) {
  728. return t('feature.day_night.last_updated')
  729. }
  730. return t('feature.day_night.ended')
  731. }
  732. return t('feature.day_night.last_updated')
  733. }
  734. var strTime = props.isNight ? getSunsetDuration() : getSunriseDuration()
  735. return (strTime.indexOf('小时') != -1 || strTime.indexOf('分钟') != -1) ? t('feature.day_night.upcoming') : t('feature.day_night.in_real_time')
  736. }
  737. function footerDesc() {
  738. var now = new Date()
  739. if (props.isNight) {
  740. if (global.nightObj) {
  741. if (now.getTime() < global.nightObj.sunset.timestamp) {
  742. return TimeFormatter.countdown(global.nightObj.sunset.timestamp, now.getTime(), true)
  743. }
  744. return TimeFormatter.countdown(global.nightObj.sunrise.timestamp, now.getTime(), true)
  745. }
  746. }
  747. else {
  748. if (global.dayObj) {
  749. if (now.getTime() < global.dayObj.sunrise.timestamp) {
  750. return TimeFormatter.countdown(global.dayObj.sunrise.timestamp, now.getTime(), true)
  751. }
  752. return TimeFormatter.countdown(global.dayObj.sunset.timestamp, now.getTime(), true)
  753. }
  754. }
  755. return ''
  756. }
  757. function footerIcon() {
  758. var now = new Date()
  759. if (props.isNight) {
  760. if (now.getTime() < sunsetDate.getTime()) {
  761. return <IconSunset color={ColorType.white + '66'} width={rpxToPx(28)} />
  762. }
  763. return <IconSunrise color={ColorType.white + '66'} width={rpxToPx(28)} />
  764. }
  765. if (now.getTime() > sunriseDate.getTime() && now.getTime() < sunsetDate.getTime()) {
  766. return <IconSunset color={ColorType.white + '66'} width={rpxToPx(28)} />
  767. }
  768. return <IconSunrise color={ColorType.white + '66'} width={rpxToPx(28)} />
  769. }
  770. function currentTime() {
  771. var now = new Date()
  772. if (authInfo && authInfo.timezone) {
  773. var t1 = TimeFormatter.tzLocalTime(now.getTime(), authInfo.timezone.id)////dayjs(now.getTime()).tz(authInfo.timezone.id)
  774. if (now.getHours() == t1.hour() && now.getMinutes() == t1.minute()) {
  775. return <View />
  776. }
  777. }
  778. var now2 = localNow(now)
  779. if (now.getTime() == now2.getTime()) {
  780. return ''
  781. }
  782. let offset = 0;
  783. if (authInfo && authInfo.timezone) {
  784. var current1 = dayjs()
  785. var current = TimeFormatter.tzLocalTime(now.getTime(), authInfo.timezone.id)//dayjs().tz(authInfo.timezone.id)
  786. offset = current.date() * 24 * 60 + current.hour() * 60 + current.minute() - current1.date() * 24 * 60 - current1.hour() * 60 - current1.minute()
  787. }
  788. var hour = Math.floor(Math.abs(offset) / 60)
  789. var minute = Math.abs(offset) % 60
  790. var time = ''
  791. if (global.language == 'en') {
  792. time = `${hour} h`
  793. if (minute > 0) {
  794. time += ` ${minute} m`
  795. }
  796. }
  797. else {
  798. time = `${hour}小时`
  799. if (minute > 0) {
  800. time += `${minute}分钟`
  801. }
  802. }
  803. // const ccc = dayjs().tz('Arctic/Longyearbyen').format('YYYY-MM-DD HH:mm:ss')
  804. // const current = dayjs().utcOffset(offset).format('YYYY-MM-DD HH:mm:ss')
  805. var current;
  806. if (authInfo && authInfo.timezone) {
  807. current = TimeFormatter.tzTimeFormateLocalTime(new Date().getTime(), authInfo.timezone.id, 'HH:mm')//dayjs().tz(authInfo.timezone.id).format('HH:mm')
  808. }
  809. else {
  810. current = dayjs().format('HH:mm')
  811. }
  812. return <View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
  813. <Text className='ring_city' numberOfLines={1}>{getCity()}</Text>
  814. <Text className='ring_clock' style={{ color: props.isNight ? ColorType.night : ColorType.day }}>{current}</Text>
  815. <Text className='ring_timezone'>{
  816. offset > 0 ? t('feature.day_night.ahead_desc', { time: time }) : t('feature.day_night.behind_desc', { time: time })
  817. }</Text>
  818. </View>
  819. }
  820. function cardContent() {
  821. return <View style={{ position: 'relative' }}>
  822. <View style={{ display: 'flex', flexDirection: 'column', zIndex: 1 }} onClick={showLocationAlert} onLongPress={longClick}>
  823. <View className="ring_full_container" style={{ paddingBottom: 0 }}>
  824. <View style={{
  825. display: 'flex',
  826. flexDirection: 'row',
  827. marginTop: 0,
  828. marginBottom: 0,
  829. padding: 0,
  830. alignItems: 'center'
  831. }}>
  832. <View style={{ display: 'flex', position: 'relative' }}>
  833. {
  834. props.isNight ? nightRing() : dayRing()
  835. }
  836. <View style={{ color: '#fff', position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, alignItems: 'center', justifyContent: 'center', display: 'flex' }}>{currentTime()}</View>
  837. </View>
  838. <View style={{ display: 'flex', flexDirection: 'column', flexShrink: 0, flex: 1, marginLeft: rpxToPx(52) }}>
  839. <View style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
  840. <Text className='ring_card_duration_title'>{props.isNight ? t('feature.common.overnight') : t('feature.common.day')}</Text>
  841. {
  842. !props.isNight && global.dayObj && global.dayObj.is_polar_day && <View className='polar_tag'>{t('feature.day_night.polar_day')}</View>
  843. }
  844. {
  845. props.isNight && global.nightObj && global.nightObj.is_polar_night && <View className='polar_tag'>{t('feature.day_night.polar_night')}</View>
  846. }
  847. </View>
  848. <Text className='ring_card_duration_value' style={{ color: props.isNight ? ColorType.night : ColorType.day }}>{props.isNight ? getSunsetDuration() : getSunriseDuration()}</Text>
  849. </View>
  850. {/* {
  851. !showArrow() && <Image className="card_arrow2" src={require('@/assets/images/arrow3.png')} />
  852. } */}
  853. <View className='day_card_header'>
  854. {
  855. props.isNight && global.nightObj && <DayNightStatus
  856. authInfo={authInfo}
  857. isNight={props.isNight}
  858. sunrise_ts={global.nightObj.sunrise.timestamp}
  859. sunset_ts={global.nightObj.sunset.timestamp}
  860. />
  861. }
  862. {
  863. !props.isNight && global.dayObj && <DayNightStatus
  864. authInfo={authInfo}
  865. isNight={props.isNight}
  866. sunrise_ts={global.dayObj.sunrise.timestamp}
  867. sunset_ts={global.dayObj.sunset.timestamp}
  868. />
  869. }
  870. </View>
  871. <Image className="card_arrowaaa"
  872. style={{ backgroundColor: global.isDebug ? 'blue' : 'transparent' }}
  873. src={require('@/assets/images/arrow3.png')} />
  874. {/* <View className='day_night_card_footer1' style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  875. {
  876. !showArrow() && <View className='footer_sun_icon_bg' style={{ backgroundColor: global.isDebug ? 'pink' : 'transparent' }}>
  877. {
  878. footerIcon()
  879. }
  880. </View>
  881. }
  882. <Text className='footer_desc_text' style={{ backgroundColor: global.isDebug ? 'black' : 'transparent' }}>{showArrow() ? completedDate() : footerDesc()}</Text>
  883. <Image className="card_arrowaaa"
  884. style={{ backgroundColor: global.isDebug ? 'blue' : 'transparent' }}
  885. src={require('@/assets/images/arrow3.png')} />
  886. </View> */}
  887. </View>
  888. </View>
  889. </View>
  890. {
  891. showDetailPopup && modalContent()
  892. }
  893. {
  894. showDetailModal && modalContent2()
  895. }
  896. </View>
  897. }
  898. function detail() {
  899. return <View>
  900. {cardContent()}
  901. <View className='day_night_card_footer_btn'
  902. onClick={(e) => {
  903. if (process.env.TARO_ENV == 'weapp') {
  904. e.stopPropagation()
  905. }
  906. if (props.isNight) {
  907. global.tapShowThirdRing(false)
  908. }
  909. else {
  910. global.tapShowThirdRing(true)
  911. }
  912. }}
  913. style={{ backgroundColor: props.isNight ? ColorType.night : ColorType.day }}>
  914. <Text style={{ color: props.isNight ? ColorType.white : ColorType.black, fontWeight: 'bold' }}>{t('feature.day_night.sync')}</Text>
  915. </View>
  916. </View>
  917. }
  918. return process.env.TARO_ENV == 'weapp' ? <Box style={{marginBottom:0}}>{detail()}</Box> :
  919. <View className='rn_card_bg'>{detail()}</View>
  920. }