DayLight.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import { useEffect, useState } from 'react'
  2. import './DayLight.scss'
  3. import { View, Text, Switch, Image } from '@tarojs/components'
  4. import Taro from '@tarojs/taro'
  5. import { useDispatch, useSelector } from 'react-redux'
  6. import { updateMember } from '@/store/day'
  7. import { systemLocation } from '@/services/common'
  8. import { clearLocation, getPerm, latestLocation, uploadPerm } from '@/services/user'
  9. import { TimeFormatter } from '@/utils/time_format'
  10. import { ColorType } from '@/context/themes/color'
  11. import { rpxToPx } from '@/utils/tools'
  12. import { useTranslation } from 'react-i18next'
  13. export default function DayLight() {
  14. const [showRing, setShowRing] = useState(false)
  15. const [isTomorrow, setIsTomorrow] = useState(false)
  16. const [isMember, setIsMember] = useState(false)
  17. const [isNight, setIsNight] = useState(false)
  18. const [authInfo, setAuthInfo] = useState(null)
  19. const [sunriseTime, setSunriseTime] = useState('06:00')
  20. const [sunsetTime, setSunsetTime] = useState('18:00')
  21. const [showLocatin, setShowLocation] = useState(true)
  22. const dayNight = useSelector((state: any) => state.dayNight);
  23. const user = useSelector((state: any) => state.user);
  24. const {t} = useTranslation()
  25. const dispatch = useDispatch();
  26. useEffect(() => { getContent() }, [])
  27. useEffect(() => {
  28. setIsMember(user.test_user)
  29. if (authInfo)
  30. dispatch(updateMember({ isMember: user.test_user, gpsInfo: authInfo }))
  31. if (user.test_user && authInfo) {
  32. if ((authInfo as any).daylights.length == 1) {
  33. setSunriseTime((authInfo as any).daylights[0].sunrise)
  34. }
  35. else {
  36. setSunriseTime((authInfo as any).daylights[1].sunrise)
  37. }
  38. setSunsetTime((authInfo as any).daylights[0].sunset)
  39. }
  40. else {
  41. setSunriseTime('06:00')
  42. setSunsetTime('18:00')
  43. }
  44. }, [user.test_user, authInfo])
  45. useEffect(() => {
  46. latestLocation().then(data => {
  47. if ((data as any).lat) {
  48. (data as any).latitude = (data as any).lat;
  49. (data as any).longitude = (data as any).lng;
  50. setAuthInfo(data as any)
  51. if ((data as any).daylights.length == 1) {
  52. setSunriseTime((data as any).daylights[0].sunrise)
  53. }
  54. else {
  55. setSunriseTime((data as any).daylights[1].sunrise)
  56. }
  57. setSunsetTime((data as any).daylights[0].sunset)
  58. Taro.setStorage({
  59. key: 'gps',
  60. data: JSON.stringify(data as any)
  61. })
  62. dispatch(updateMember({ isMember: user.test_user, gpsInfo: (data as any) }))
  63. }
  64. })
  65. if (user.isLogin) {
  66. getPerm({}).then(res => {
  67. setShowRing((res as any).show_night_ring)
  68. })
  69. }
  70. }, [user.isLogin])
  71. async function getStorage(key: string) {
  72. try {
  73. const res = await Taro.getStorage({ key });
  74. return res.data;
  75. } catch {
  76. return '';
  77. }
  78. }
  79. async function getContent() {
  80. const isShow = await getStorage('showLightRing') || false
  81. setShowRing(isShow)
  82. const hideLocation = await getStorage('hideLocation') || false
  83. setShowLocation(!hideLocation)
  84. global.showNightRing = isShow
  85. global.refreshIndex()
  86. setTimeout(()=>{
  87. global.refreshIndex()
  88. },200)
  89. const gpsInfo = await getStorage('gps') || null
  90. if (gpsInfo) {
  91. setAuthInfo(JSON.parse(gpsInfo))
  92. dispatch(updateMember({ isMember: user.test_user, gpsInfo: JSON.parse(gpsInfo) }))
  93. }
  94. else {
  95. setAuthInfo(null)
  96. }
  97. checkData()
  98. }
  99. function checkData() {
  100. var date = new Date()
  101. var minutes = date.getHours() * 60 + date.getMinutes()
  102. var riseMinutes = parseInt(sunriseTime.split(':')[0]) * 60 + parseInt(sunriseTime.split(':')[1])
  103. var setMinutes = parseInt(sunsetTime.split(':')[0]) * 60 + parseInt(sunsetTime.split(':')[1])
  104. if (minutes >= riseMinutes && minutes < setMinutes) {
  105. setIsNight(false)
  106. }
  107. else {
  108. setIsNight(true)
  109. }
  110. if (minutes >= setMinutes) {
  111. setIsTomorrow(true)
  112. }
  113. else {
  114. setIsTomorrow(false)
  115. }
  116. }
  117. function buy() {
  118. Taro.navigateTo({
  119. url: '/pages/account/Member'
  120. })
  121. }
  122. function auth() {
  123. Taro.chooseLocation({
  124. success: function (res) {
  125. console.log(res)
  126. var today = new Date()
  127. var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000)
  128. var strToday = `${today.getFullYear()}-${TimeFormatter.padZero(today.getMonth() + 1)}-${TimeFormatter.padZero(today.getDate())}`
  129. var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
  130. systemLocation({ lat: res.latitude, lng: res.longitude, date_start: strToday, date_end: strTomorrow }).then(data => {
  131. console.log(data);
  132. (data as any).latitude = res.latitude;
  133. (data as any).longitude = res.longitude;
  134. setAuthInfo(data as any)
  135. setSunriseTime((data as any).daylights[1].sunrise)
  136. setSunsetTime((data as any).daylights[0].sunset)
  137. Taro.setStorage({
  138. key: 'gps',
  139. data: JSON.stringify(data as any)
  140. })
  141. dispatch(updateMember({ isMember: user.test_user, gpsInfo: (data as any) }))
  142. })
  143. },
  144. fail(res) {
  145. Taro.showToast({
  146. title: '位置修改失败!\n请重新选择就近位置',
  147. icon: 'none'
  148. })
  149. },
  150. })
  151. }
  152. function getLocation(){
  153. if ((authInfo as any).address.city.length>0){
  154. return (authInfo as any).address.city
  155. }
  156. if ((authInfo as any).address.province.length>0){
  157. return (authInfo as any).address.province
  158. }
  159. if ((authInfo as any).address.country.length>0){
  160. return (authInfo as any).address.country
  161. }
  162. return t('feature.track_time_duration.third_ring.unknown')
  163. }
  164. function clearData() {
  165. Taro.showModal({
  166. title: '提示',
  167. content: '确认清除位置数据?',
  168. success: function (res) {
  169. if (res.confirm) {
  170. clearLocation().then(res => {
  171. Taro.removeStorage({ key: 'gps' })
  172. setAuthInfo(null)
  173. dispatch(updateMember({ isMember: user.test_user, gpsInfo: null }))
  174. })
  175. } else if (res.cancel) {
  176. console.log('用户点击取消')
  177. }
  178. }
  179. })
  180. }
  181. var split = new Date().toString().split(' ');
  182. var timezone = split[split.length - 2];
  183. return <View>
  184. <View className='cell_bg'>
  185. <View className='cell_full'>
  186. <Text className="cell_title">{t('feature.track_time_duration.third_ring.night_ring')}</Text>
  187. <Switch checked={showRing}
  188. color={ColorType.night}
  189. onChange={(e) => {
  190. setShowRing(e.detail.value)
  191. global.showNightRing = e.detail.value
  192. global.refreshIndex()
  193. Taro.setStorage({
  194. key: 'showLightRing',
  195. data: e.detail.value
  196. })
  197. uploadPerm({ show_night_ring: e.detail.value })
  198. }}
  199. />
  200. </View>
  201. {
  202. showRing && <View className="cell_line" style={{ height: 1 }} />
  203. }
  204. {
  205. showRing && <View className="cell_full">
  206. <Text className="cell_title">{t('feature.track_time_duration.third_ring.sunset_today')}</Text>
  207. <Text className="cell_value">{sunsetTime}</Text>
  208. </View>
  209. }
  210. {
  211. showRing && <View className="cell_line" style={{ height: 1 }} />
  212. }
  213. {
  214. showRing && <View className="cell_full">
  215. <Text className="cell_title">{t('feature.track_time_duration.third_ring.sunrise_tomorrow')}</Text>
  216. <Text className="cell_value">{sunriseTime}</Text>
  217. </View>
  218. }
  219. </View>
  220. {
  221. showRing && <Text className='cell_footer' >{
  222. !isMember ? t('feature.track_time_duration.third_ring.member_desc'):
  223. authInfo ? t('feature.track_time_duration.third_ring.base_location_desc'):
  224. t('feature.track_time_duration.third_ring.enter_location_desc')
  225. }</Text>
  226. }
  227. {
  228. showRing && <View style={{marginTop:rpxToPx(40)}}>
  229. {/* <Text className='daylight_note'>{isMember && authInfo ? 'Calculated based on your location.' : 'Sunset and sunrise times are global average. For actual times at your location, join our Pro program.'}</Text> */}
  230. {
  231. isMember ? <View className="cell_bg">
  232. {
  233. showLocatin && <View className="cell_full" onClick={auth}>
  234. <Text className="cell_title">{t('feature.track_time_duration.third_ring.location')}</Text>
  235. <Text className="cell_value">{authInfo ? getLocation() : t('feature.track_time_duration.third_ring.enter')}</Text>
  236. <Image className="cell_arrow" src={require('@/assets/images/arrow3.png')} />
  237. </View>
  238. }
  239. {
  240. authInfo && showLocatin && <View className="cell_line" style={{ height: 1 }} />
  241. }
  242. {
  243. authInfo && showLocatin && <View className="cell_full" >
  244. <Text className="cell_title">{t('feature.track_time_duration.third_ring.latitude')}</Text>
  245. <Text className="cell_value">{(authInfo as any).latitude}</Text>
  246. </View>
  247. }
  248. {
  249. authInfo && showLocatin && <View className="cell_line" style={{ height: 1 }} />
  250. }
  251. {
  252. authInfo && showLocatin && <View className="cell_full">
  253. <Text className="cell_title" >{t('feature.track_time_duration.third_ring.longitude')}</Text>
  254. <Text className="cell_value">{(authInfo as any).longitude}</Text>
  255. </View>
  256. }
  257. {
  258. authInfo && showLocatin && <View className="cell_line" style={{ height: 1 }} />
  259. }
  260. {
  261. authInfo && showLocatin && <View className="cell_full">
  262. <Text className="cell_title">{t('feature.track_time_duration.third_ring.timezone')}</Text>
  263. <Text className="cell_value">{timezone}</Text>
  264. </View>
  265. }
  266. </View> :
  267. <View className='become_vip' onClick={buy}>
  268. <Text style={{ color: '#000' }}>{t('feature.common.become_pro')}</Text>
  269. </View>
  270. }
  271. {
  272. authInfo && <Text className='cell_footer' onClick={() => {
  273. var enable = !showLocatin
  274. setShowLocation(enable)
  275. Taro.setStorage({
  276. key: 'hideLocation',
  277. data: !enable
  278. })
  279. }}>{showLocatin ? 'Hide my location' : 'Show my location'}</Text>
  280. }
  281. {
  282. authInfo && showLocatin && user.test_user && <Text className='cell_footer' onClick={clearData}>清除位置数据</Text>
  283. }
  284. </View>
  285. }
  286. </View>
  287. }