|
|
@@ -9,29 +9,92 @@ import { RootState } from '@/store'
|
|
|
import classNames from 'classnames'
|
|
|
import A3hotList from '../A3hotList'
|
|
|
import { message } from 'antd'
|
|
|
-import http from '@/utils/http'
|
|
|
// import isMobile from '@/utils/isMobile'
|
|
|
const imgArrTemp = ['like.png', 'likeAc.png']
|
|
|
const imgArr = imgArrTemp.map(item => require(`@/assets/img/${item}`))
|
|
|
|
|
|
+const SCENE_CODE = 'SG-WVqbTpbkApi_96'
|
|
|
+const LIKE_STORAGE_KEY = `count_like_${SCENE_CODE}`
|
|
|
+const VIEW_STORAGE_KEY = `count_view_${SCENE_CODE}`
|
|
|
+const DEFAULT_LIKE_NUM = 100
|
|
|
+const DEFAULT_VIEW_NUM = 300
|
|
|
+
|
|
|
+const getLocalCount = (key: string, fallback: number) => {
|
|
|
+ try {
|
|
|
+ const raw = window.localStorage.getItem(key)
|
|
|
+ if (raw == null) return fallback
|
|
|
+ const n = Number(raw)
|
|
|
+ return Number.isFinite(n) ? n : fallback
|
|
|
+ } catch {
|
|
|
+ return fallback
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const setLocalCount = (key: string, value: number) => {
|
|
|
+ try {
|
|
|
+ window.localStorage.setItem(key, String(value))
|
|
|
+ } catch {
|
|
|
+ // 隐私模式等无法写入时忽略
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const pickCount = (value: unknown) => (typeof value === 'number' && Number.isFinite(value) ? value : null)
|
|
|
+
|
|
|
+const fetchCountApi = async (url: string) => {
|
|
|
+ const response = await fetch(url)
|
|
|
+ if (!response.ok) throw new Error('count api failed')
|
|
|
+ return response.json()
|
|
|
+}
|
|
|
+
|
|
|
+const bumpLocalView = () => {
|
|
|
+ try {
|
|
|
+ const stored = window.localStorage.getItem(VIEW_STORAGE_KEY)
|
|
|
+ const next = stored == null ? DEFAULT_VIEW_NUM : getLocalCount(VIEW_STORAGE_KEY, DEFAULT_VIEW_NUM) + 1
|
|
|
+ setLocalCount(VIEW_STORAGE_KEY, next)
|
|
|
+ return next
|
|
|
+ } catch {
|
|
|
+ return DEFAULT_VIEW_NUM
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
function A2main() {
|
|
|
- const SCENE_CODE = 'SG-WVqbTpbkApi_96'
|
|
|
// 漫游的状态
|
|
|
const { state3d } = useSelector((state: RootState) => state.three)
|
|
|
|
|
|
// 点赞
|
|
|
const [like, setLike] = useState(false)
|
|
|
- const [likeNum, setLikeNum] = useState(1)
|
|
|
- const [viewNum, setViewNum] = useState(1)
|
|
|
+ const [likeNum, setLikeNum] = useState(DEFAULT_LIKE_NUM)
|
|
|
+ const [viewNum, setViewNum] = useState(DEFAULT_VIEW_NUM)
|
|
|
const [isShowSection, setIsShowSection] = useState(false)
|
|
|
|
|
|
useEffect(() => {
|
|
|
- http.get(`https://count.4dage.com/api/count/detail/${SCENE_CODE}`).then(res => {
|
|
|
- setLikeNum(res.data.starSum!)
|
|
|
- })
|
|
|
- http.get(`https://count.4dage.com/api/count/saveVisit/${SCENE_CODE}`).then(res => {
|
|
|
- setViewNum(res.data.visitSum!)
|
|
|
- })
|
|
|
+ fetchCountApi(`https://count.4dage.com/api/count/detail/${SCENE_CODE}`)
|
|
|
+ .then(res => {
|
|
|
+ const num = pickCount(res.data?.starSum)
|
|
|
+ if (num != null) {
|
|
|
+ setLikeNum(num)
|
|
|
+ setLocalCount(LIKE_STORAGE_KEY, num)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ setLikeNum(getLocalCount(LIKE_STORAGE_KEY, DEFAULT_LIKE_NUM))
|
|
|
+ })
|
|
|
+ .catch(() => {
|
|
|
+ setLikeNum(getLocalCount(LIKE_STORAGE_KEY, DEFAULT_LIKE_NUM))
|
|
|
+ })
|
|
|
+
|
|
|
+ fetchCountApi(`https://count.4dage.com/api/count/saveVisit/${SCENE_CODE}`)
|
|
|
+ .then(res => {
|
|
|
+ const num = pickCount(res.data?.visitSum)
|
|
|
+ if (num != null) {
|
|
|
+ setViewNum(num)
|
|
|
+ setLocalCount(VIEW_STORAGE_KEY, num)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ setViewNum(bumpLocalView())
|
|
|
+ })
|
|
|
+ .catch(() => {
|
|
|
+ setViewNum(bumpLocalView())
|
|
|
+ })
|
|
|
}, [])
|
|
|
|
|
|
// 点击点赞
|
|
|
@@ -39,12 +102,28 @@ function A2main() {
|
|
|
if (like) return
|
|
|
setLike(true)
|
|
|
setTimeout(() => {
|
|
|
- http.get(`https://count.4dage.com/api/count/saveStar/${SCENE_CODE}`).then(res => {
|
|
|
- setLikeNum(likeNum + 1)
|
|
|
+ const applyLocalLike = () => {
|
|
|
+ setLikeNum(n => {
|
|
|
+ const next = n + 1
|
|
|
+ setLocalCount(LIKE_STORAGE_KEY, next)
|
|
|
+ return next
|
|
|
+ })
|
|
|
setLike(false)
|
|
|
- })
|
|
|
+ }
|
|
|
+ fetchCountApi(`https://count.4dage.com/api/count/saveStar/${SCENE_CODE}`)
|
|
|
+ .then(res => {
|
|
|
+ if (pickCount(res.data?.starSum) != null) {
|
|
|
+ const num = pickCount(res.data.starSum)!
|
|
|
+ setLikeNum(num)
|
|
|
+ setLocalCount(LIKE_STORAGE_KEY, num)
|
|
|
+ setLike(false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ applyLocalLike()
|
|
|
+ })
|
|
|
+ .catch(applyLocalLike)
|
|
|
}, 2000)
|
|
|
- }, [like, likeNum])
|
|
|
+ }, [like])
|
|
|
|
|
|
// 点击热点列表
|
|
|
const [hotListShow, setHotListShow] = useState(false)
|