DayNightSwiperPopup.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import { PageContainer, Switch, Text, View, Image } from '@tarojs/components'
  2. import Modal from '@/components/layout/Modal.weapp'
  3. import { useEffect, useState } from 'react'
  4. import DayNightDetailPopup from './DayNightDetailPopup'
  5. import { useTranslation } from 'react-i18next'
  6. import Taro from '@tarojs/taro'
  7. import { systemLocation } from '@/services/common'
  8. import showAlert from '@/components/basic/Alert'
  9. import { getTimezone, rpxToPx } from '@/utils/tools'
  10. import { clearLocation } from '@/services/user'
  11. import { TimeFormatter } from '@/utils/time_format'
  12. let Linking;
  13. if (process.env.TARO_ENV == 'rn') {
  14. // useNavigation = require("@react-navigation/native").useNavigation
  15. Linking = require('react-native').Linking;
  16. }
  17. export default function DayNightSwiperPopup(props: { authInfo: any }) {
  18. const [showDetailModal, setShowDetailModal] = useState(false)
  19. const [showDetailPopup, setShowDetailPopup] = useState(false)
  20. const [authInfo, setAuthInfo] = useState<any>(props.authInfo)
  21. const [dayDate, setDayDate] = useState<any>('')
  22. const [nightDate, setNightDate] = useState<any>('')
  23. const [isNight, setIsNight] = useState(false)
  24. useEffect(() => {
  25. setAuthInfo(props.authInfo)
  26. }, [props.authInfo])
  27. const { t } = useTranslation()
  28. function modalContent() {
  29. return <Modal
  30. testInfo={null}
  31. dismiss={() => {
  32. setShowDetailPopup(false)
  33. }}
  34. confirm={() => { }}>
  35. <DayNightDetailPopup
  36. isNight={isNight}
  37. authInfo={authInfo}
  38. nightDate={nightDate}
  39. dayDate={dayDate}
  40. updateLocation={auth}
  41. onClose={() => { setShowDetailPopup(false) }}
  42. />
  43. </Modal>
  44. }
  45. function modalContent2() {
  46. return <Modal
  47. testInfo={null}
  48. dismiss={() => {
  49. setShowDetailModal(false)
  50. }}
  51. confirm={() => { }}>
  52. {
  53. detailModalContent()
  54. }
  55. </Modal>
  56. }
  57. function getLocation() {
  58. if (!authInfo) {
  59. return ''
  60. }
  61. return `${getCity()} | ${Math.abs(parseInt(authInfo.lat))}°${parseInt(authInfo.lat) < 0 ? 'S' : 'N'} ${Math.abs(parseInt(authInfo.lng))}°${parseInt(authInfo.lng) < 0 ? 'W' : 'E'}`
  62. }
  63. function getCity() {
  64. var city = ''
  65. if (!authInfo) {
  66. return ''
  67. }
  68. if ((authInfo as any).address) {
  69. if ((authInfo as any).address.city.length > 0) {
  70. city = (authInfo as any).address.city
  71. }
  72. else if ((authInfo as any).address.province.length > 0) {
  73. city = (authInfo as any).address.province
  74. }
  75. else if ((authInfo as any).address.country.length > 0) {
  76. city = (authInfo as any).address.country
  77. }
  78. else {
  79. city = t('feature.track_time_duration.third_ring.unknown')
  80. }
  81. }
  82. else {
  83. city = t('feature.track_time_duration.third_ring.unknown')
  84. }
  85. return city
  86. }
  87. function detailModalContent() {
  88. var timezone = getTimezone()
  89. if (!authInfo || !(authInfo as any).lat) {
  90. return <View style={{ height: 100, display: 'flex', alignItems: 'center', justifyContent: 'center', width: rpxToPx(750) }}>
  91. <Text>{t('feature.location.no_location')}</Text>
  92. </View>
  93. }
  94. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  95. {
  96. <View className="cell_bg">
  97. {
  98. <View className="cell_full">
  99. <Text className="cell_title">{t('feature.track_time_duration.third_ring.location')}</Text>
  100. <Text className="cell_value">{authInfo ? getLocation() : t('feature.track_time_duration.third_ring.enter')}</Text>
  101. </View>
  102. }
  103. <View className="cell_line" style={{ height: 1 }} />
  104. {
  105. <View className="cell_full" >
  106. <Text className="cell_title">{t('feature.track_time_duration.third_ring.latitude')}</Text>
  107. <Text className="cell_value">{(authInfo as any).lat}</Text>
  108. </View>
  109. }
  110. <View className="cell_line" style={{ height: 1 }} />
  111. <View className="cell_full">
  112. <Text className="cell_title" >{t('feature.track_time_duration.third_ring.longitude')}</Text>
  113. <Text className="cell_value">{(authInfo as any).lng}</Text>
  114. </View>
  115. <View className="cell_line" style={{ height: 1 }} />
  116. <View className="cell_full">
  117. <Text className="cell_title">{t('feature.track_time_duration.third_ring.timezone')}</Text>
  118. <Text className="cell_value">{timezone}</Text>
  119. </View>
  120. </View>
  121. }
  122. <Text onClick={() => {
  123. clearData()
  124. setShowDetailModal(false)
  125. }} style={{ width: '100%', textAlign: 'center', marginBottom: 50, color: '#fff', marginTop: 20 }}>{t('feature.location.clear_info')}</Text>
  126. </View>
  127. }
  128. function clearData() {
  129. showAlert({
  130. title:t('feature.location.clear_alert_title'),
  131. content:t('feature.location.clear_alert_content'),
  132. cancelText:t('feature.location.clear_alert_cancel'),
  133. confirmText:t('feature.location.clear_alert_confirm'),
  134. showCancel:true,
  135. confirm:()=>{
  136. clearLocation().then(res => {
  137. global.swiperDayNightRefresh()
  138. global.refreshNight()
  139. global.refreshDay()
  140. })
  141. }
  142. })
  143. }
  144. function auth(e?: any) {
  145. if (e && process.env.TARO_ENV == 'weapp') {
  146. e.stopPropagation()
  147. }
  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 * 5)
  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. if (process.env.TARO_ENV == 'rn') {
  154. Taro.getLocation({
  155. success(res) {
  156. systemLocation({
  157. lat: res.latitude,
  158. lng: res.longitude,
  159. date_start: strYesterday,
  160. date_end: strTomorrow,
  161. coordinate_system_standard: 'WGS-84'
  162. }).then(data => {
  163. global.swiperDayNightRefresh()
  164. global.refreshNight()
  165. global.refreshDay()
  166. // updateDate(data);
  167. global.locationDetail = data
  168. setAuthInfo(data as any)
  169. Taro.setStorage({
  170. key: 'gps',
  171. data: JSON.stringify(data as any)
  172. })
  173. // dispatch(updateMember({ isMember: isMember, gpsInfo: (data as any) }))
  174. if (Taro.getSystemInfoSync().platform == 'android' && process.env.TARO_ENV == 'rn') {
  175. var ToastAndroid = require('react-native').ToastAndroid;
  176. ToastAndroid.show(t('feature.day_night.location_updated'), ToastAndroid.LONG);
  177. }
  178. else {
  179. showAlert({
  180. title: 'Success',
  181. content: t('feature.day_night.location_updated'),
  182. showCancel: false
  183. })
  184. // Taro.showToast({
  185. // title: t('feature.day_night.location_updated'),
  186. // icon: 'none'
  187. // })
  188. }
  189. })
  190. },
  191. fail(res) {
  192. if (res.errMsg == 'Permissions denied!') {
  193. showAlert({
  194. title: t('feature.auth_sys.location_title'),
  195. content: t('feature.auth_sys.location_desc'),
  196. showCancel: true,
  197. cancelText: t('feature.auth_sys.location_cancel'),
  198. confirmText: t('feature.auth_sys.location_confirm'),
  199. confirm: () => {
  200. Linking.openURL('app-settings:')
  201. }
  202. })
  203. }
  204. else {
  205. if (Taro.getSystemInfoSync().platform == 'android' && process.env.TARO_ENV == 'rn') {
  206. var ToastAndroid = require('react-native').ToastAndroid;
  207. ToastAndroid.show(t('feature.day_night.location_failed'), ToastAndroid.LONG);
  208. }
  209. else {
  210. showAlert({
  211. title: 'Error',
  212. content: t('feature.day_night.location_updated'),
  213. showCancel: false
  214. })
  215. // Taro.showToast({
  216. // title: t('feature.day_night.location_failed'),
  217. // icon: 'none'
  218. // })
  219. }
  220. }
  221. console.log(res)
  222. },
  223. complete(res) {
  224. }
  225. })
  226. return
  227. }
  228. Taro.chooseLocation({
  229. latitude: authInfo && authInfo.lat ? authInfo.lat : undefined,
  230. longitude: authInfo && authInfo.lat ? authInfo.lng : undefined,
  231. success: function (res) {
  232. systemLocation({
  233. lat: res.latitude,
  234. lng: res.longitude,
  235. name: res.name,
  236. address: res.address,
  237. date_start: strYesterday,
  238. date_end: strTomorrow,
  239. coordinate_system_standard: 'GCJ-02'
  240. }).then(data => {
  241. // updateDate(data);
  242. global.locationDetail = data
  243. setAuthInfo(data as any)
  244. Taro.setStorage({
  245. key: 'gps',
  246. data: JSON.stringify(data as any)
  247. })
  248. // dispatch(updateMember({ isMember: isMember, gpsInfo: (data as any) }))
  249. if (global.swiperDayNightRefresh) {
  250. global.swiperDayNightRefresh()
  251. }
  252. global.refreshNight()
  253. global.refreshDay()
  254. })
  255. },
  256. fail(res) {
  257. Taro.showToast({
  258. title: '位置修改失败!\n请重新选择就近位置',
  259. icon: 'none'
  260. })
  261. },
  262. complete(res) {
  263. }
  264. })
  265. }
  266. global.showDayNightSwiperPop = (isNight, strNight, strDay) => {
  267. setNightDate(strNight)
  268. setDayDate(strDay)
  269. setIsNight(isNight)
  270. setShowDetailPopup(true)
  271. }
  272. global.showDayNightSwiperModal = () => {
  273. setShowDetailModal(true)
  274. }
  275. return <View>
  276. {
  277. showDetailPopup && modalContent()
  278. }
  279. {
  280. showDetailModal && modalContent2()
  281. }
  282. </View>
  283. }