DayNightCard.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import { PageContainer, Switch, Text, View } 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, 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 { updateMember } from '@/store/day_night'
  13. import Modal from '@/components/layout/Modal.weapp'
  14. import { rpxToPx } from '@/utils/tools'
  15. export default function DayNightCard(props: { isNight: boolean, switchChanged: Function }) {
  16. const [expand, setExpand] = useState(false)
  17. const user = useSelector((state: any) => state.user);
  18. const [authInfo, setAuthInfo] = useState(null)
  19. const [count, setCount] = useState(0)
  20. const [sunriseTime, setSunriseTime] = useState('06:00')
  21. const [sunriseTmrTime, setSunriseTmrTime] = useState('06:00')
  22. const [sunsetTime, setSunsetTime] = useState('18:00')
  23. const [showDetailModal, setShowDetailModal] = useState(false)
  24. const dispatch = useDispatch();
  25. const { t } = useTranslation()
  26. useEffect(() => {
  27. if (user.isLogin) {
  28. getPerm({}).then(res => {
  29. setExpand(props.isNight ? (res as any).show_night_ring : (res as any).show_day_ring)
  30. })
  31. }
  32. getContent()
  33. }, [user.isLogin])
  34. useEffect(() => {
  35. setInterval(() => {
  36. setCount((prevCounter) => prevCounter + 1)
  37. }, 1000)
  38. }, [])
  39. useDidShow(() => {
  40. getContent()
  41. })
  42. async function getContent() {
  43. const isShow = await getStorage(props.isNight ? 'showNightRing' : 'showDayRing') || false
  44. setExpand(isShow)
  45. const gpsInfo = await getStorage('gps') || null
  46. if (gpsInfo) {
  47. var data = JSON.parse(gpsInfo)
  48. setAuthInfo(data)
  49. setSunriseTime((data as any).daylights[0].sunrise)
  50. setSunriseTmrTime((data as any).daylights[1].sunrise)
  51. setSunsetTime((data as any).daylights[0].sunset)
  52. dispatch(updateMember({ isMember: user.test_user, gpsInfo: data }))
  53. }
  54. else {
  55. setSunriseTime('06:00')
  56. setSunriseTmrTime('06:00')
  57. setSunsetTime('18:00')
  58. setAuthInfo(null)
  59. }
  60. if (props.isNight) {
  61. global.showNightRing = isShow
  62. global.refreshIndex()
  63. }
  64. else {
  65. props.switchChanged(isShow)
  66. }
  67. }
  68. async function getStorage(key: string) {
  69. try {
  70. const res = await Taro.getStorage({ key });
  71. return res.data;
  72. } catch {
  73. return '';
  74. }
  75. }
  76. function footer() {
  77. return <View className='day_night_card_footer'>
  78. <Text style={{color:'#9E9E9E',fontSize:rpxToPx(20)}}>{
  79. // !isMember ? t('feature.track_time_duration.third_ring.member_desc') :
  80. authInfo ? t('feature.track_time_duration.third_ring.base_location_desc') :
  81. t('feature.track_time_duration.third_ring.enter_location_desc')
  82. }</Text>
  83. </View>
  84. }
  85. function tapCard(e) {
  86. if (!user.isLogin) {
  87. return
  88. }
  89. Taro.showActionSheet({
  90. itemList: [
  91. 'Current location Info',
  92. 'Choose a new location',
  93. 'clear location data'
  94. //t('feature.track_time_duration.action_sheet.switch_scenario'),
  95. //t('feature.track_time_duration.action_sheet.change_schedule')
  96. ]
  97. }).then(res => {
  98. switch (res.tapIndex) {
  99. case 0:
  100. setShowDetailModal(true)
  101. break
  102. case 1:
  103. auth()
  104. break;
  105. case 2:
  106. clearData()
  107. break;
  108. }
  109. })
  110. }
  111. function auth() {
  112. Taro.chooseLocation({
  113. success: function (res) {
  114. console.log(res)
  115. var today = new Date()
  116. var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000)
  117. var strToday = `${today.getFullYear()}-${TimeFormatter.padZero(today.getMonth() + 1)}-${TimeFormatter.padZero(today.getDate())}`
  118. var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
  119. systemLocation({ lat: res.latitude, lng: res.longitude, date_start: strToday, date_end: strTomorrow }).then(data => {
  120. console.log(data);
  121. (data as any).latitude = res.latitude;
  122. (data as any).longitude = res.longitude;
  123. setAuthInfo(data as any)
  124. setSunriseTime((data as any).daylights[0].sunrise)
  125. setSunriseTmrTime((data as any).daylights[1].sunrise)
  126. setSunsetTime((data as any).daylights[0].sunset)
  127. Taro.setStorage({
  128. key: 'gps',
  129. data: JSON.stringify(data as any)
  130. })
  131. dispatch(updateMember({ isMember: user.test_user, gpsInfo: (data as any) }))
  132. })
  133. },
  134. fail(res) {
  135. Taro.showToast({
  136. title: '位置修改失败!\n请重新选择就近位置',
  137. icon: 'none'
  138. })
  139. },
  140. })
  141. }
  142. function getLocation() {
  143. if ((authInfo as any).address.city.length > 0) {
  144. return (authInfo as any).address.city
  145. }
  146. if ((authInfo as any).address.province.length > 0) {
  147. return (authInfo as any).address.province
  148. }
  149. if ((authInfo as any).address.country.length > 0) {
  150. return (authInfo as any).address.country
  151. }
  152. return t('feature.track_time_duration.third_ring.unknown')
  153. }
  154. function timeCount() {
  155. var now = new Date()
  156. if (props.isNight) {
  157. var list = sunsetTime.split(':')
  158. var hour = parseInt(list[0])
  159. var min = parseInt(list[1])
  160. var second = list.length == 3 ? parseInt(list[2]) : 0
  161. now.setHours(hour)
  162. now.setMinutes(min)
  163. now.setSeconds(second)
  164. return TimeFormatter.countdown(now.getTime())
  165. } else {
  166. var list = sunriseTime.split(':')
  167. var hour = parseInt(list[0])
  168. var min = parseInt(list[1])
  169. var second = list.length == 3 ? parseInt(list[2]) : 0
  170. now.setHours(hour)
  171. now.setMinutes(min)
  172. now.setSeconds(second)
  173. return TimeFormatter.countdown(now.getTime())
  174. }
  175. }
  176. function timeDesc() {
  177. var now = new Date()
  178. if (props.isNight) {
  179. var list = sunsetTime.split(':')
  180. var hour = parseInt(list[0])
  181. var min = parseInt(list[1])
  182. var second = list.length == 3 ? parseInt(list[2]) : 0
  183. now.setHours(hour)
  184. now.setMinutes(min)
  185. now.setSeconds(second)
  186. if (now.getTime() < new Date().getTime()) {
  187. return 'Time past Sunset'
  188. }
  189. return 'Time to Sunset'
  190. }
  191. else {
  192. var list = sunriseTime.split(':')
  193. var hour = parseInt(list[0])
  194. var min = parseInt(list[1])
  195. var second = list.length == 3 ? parseInt(list[2]) : 0
  196. now.setHours(hour)
  197. now.setMinutes(min)
  198. now.setSeconds(second)
  199. if (now.getTime() < new Date().getTime()) {
  200. return 'Time past Sunrise'
  201. }
  202. return 'Time to Sunrise'
  203. }
  204. }
  205. function clearData() {
  206. Taro.showModal({
  207. title: '提示',
  208. content: '确认清除位置数据?',
  209. success: function (res) {
  210. if (res.confirm) {
  211. clearLocation().then(res => {
  212. Taro.removeStorage({ key: 'gps' })
  213. setAuthInfo(null)
  214. setSunriseTime('06:00')
  215. setSunriseTmrTime('06:00')
  216. setSunsetTime('18:00')
  217. dispatch(updateMember({ isMember: user.test_user, gpsInfo: null }))
  218. })
  219. } else if (res.cancel) {
  220. console.log('用户点击取消')
  221. }
  222. }
  223. })
  224. }
  225. function detailModalContent() {
  226. var split = new Date().toString().split(' ');
  227. var timezone = split[split.length - 2];
  228. if (!authInfo) {
  229. return <View>
  230. <Text>暂时位置信息</Text>
  231. </View>
  232. }
  233. return <View>
  234. {
  235. <View className="cell_bg">
  236. {
  237. <View className="cell_full">
  238. <Text className="cell_title">{t('feature.track_time_duration.third_ring.location')}</Text>
  239. <Text className="cell_value">{authInfo ? getLocation() : t('feature.track_time_duration.third_ring.enter')}</Text>
  240. </View>
  241. }
  242. <View className="cell_line" style={{ height: 1 }} />
  243. {
  244. <View className="cell_full" >
  245. <Text className="cell_title">{t('feature.track_time_duration.third_ring.latitude')}</Text>
  246. <Text className="cell_value">{(authInfo as any).latitude}</Text>
  247. </View>
  248. }
  249. <View className="cell_line" style={{ height: 1 }} />
  250. <View className="cell_full">
  251. <Text className="cell_title" >{t('feature.track_time_duration.third_ring.longitude')}</Text>
  252. <Text className="cell_value">{(authInfo as any).longitude}</Text>
  253. </View>
  254. <View className="cell_line" style={{ height: 1 }} />
  255. <View className="cell_full">
  256. <Text className="cell_title">{t('feature.track_time_duration.third_ring.timezone')}</Text>
  257. <Text className="cell_value">{timezone}</Text>
  258. </View>
  259. </View>
  260. }
  261. </View>
  262. }
  263. function modalContent() {
  264. if (process.env.TARO_ENV == 'weapp') {
  265. return <Modal
  266. testInfo={null}
  267. dismiss={() => {
  268. setShowDetailModal(false)
  269. }}
  270. confirm={() => { }}>
  271. {
  272. detailModalContent()
  273. }
  274. </Modal>
  275. }
  276. else if (process.env.TARO_ENV == 'rn') {
  277. return <PageContainer style={{ backgroundColor: '#1c1c1c' }}
  278. // overlayStyle='background-color:rgba(0,0,0,0.9)'
  279. // custom-style='background-color:#1c1c1c'
  280. overlayStyle={{ backgroundColor: 'rgba(0,0,0,0.9)' }}
  281. customStyle={{ backgroundColor: '#1c1c1c' }}
  282. closeOnSlideDown={false}
  283. onBeforeEnter={() => {
  284. }}
  285. onBeforeLeave={() => {
  286. }}
  287. onClick={() => { alert('b') }}
  288. onClickOverlay={() => { alert('a') }}
  289. onAfterLeave={() => { setShowDetailModal(false) }}
  290. show={showDetailModal} round={true} overlay={true} position='bottom'
  291. >
  292. {
  293. detailModalContent()
  294. }
  295. </PageContainer>
  296. }
  297. }
  298. return <View style={{ color: '#fff' }}>
  299. <Box onClick={tapCard}>
  300. <View>
  301. <View className='day_night_top'>
  302. <Text className='day_night_title'>{props.isNight ? 'Overnight' : 'Day'}</Text>
  303. <View className='free'>限时免费</View>
  304. <View style={{ flex: 1 }} />
  305. <Switch checked={expand}
  306. color={props.isNight ? ColorType.night : ColorType.day}
  307. onClick={(e) => { e.stopPropagation() }}
  308. onChange={(e) => {
  309. props.switchChanged(e.detail.value)
  310. e.stopPropagation()
  311. setExpand(e.detail.value)
  312. if (props.isNight) {
  313. global.showNightRing = e.detail.value
  314. global.refreshIndex()
  315. }
  316. Taro.setStorage({
  317. key: props.isNight ? 'showNightRing' : 'showDayRing',
  318. data: e.detail.value
  319. })
  320. if (user.isLogin) {
  321. if (props.isNight) {
  322. uploadPerm({ show_night_ring: e.detail.value })
  323. }
  324. else {
  325. uploadPerm({ show_day_ring: e.detail.value })
  326. }
  327. }
  328. }}
  329. />
  330. </View>
  331. <View style={{ display: 'flex', flexDirection: 'column' }}>
  332. <Text style={{ color: props.isNight ? ColorType.night : ColorType.day }}>{props.isNight ? `Today ${sunsetTime} - Tomorrow ${sunriseTmrTime}` : `${sunriseTime} - ${sunsetTime}`}</Text>
  333. <Text>{props.isNight ? 'Sunset to Sunrise' : 'Sunrise to Sunset'}</Text>
  334. <Text style={{ color: props.isNight ? ColorType.night : ColorType.day }}>{timeCount()}</Text>
  335. <Text>{timeDesc()}</Text>
  336. </View>
  337. {
  338. footer()
  339. }
  340. </View>
  341. </Box>
  342. {
  343. showDetailModal && modalContent()
  344. }
  345. </View>
  346. }