Skip to content

Commit

Permalink
🐞 fix(leancloud): 未配置 leancloud 不能正常访问的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wallleap committed Apr 1, 2024
1 parent b1d4f0f commit b93d029
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/api/leancloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ export async function increaseHot(post) {
// 查询点赞数
export async function queryLike(type) {
return new Promise((resolve) => {
if (isDev)
return resolve(0)
const query = new AV.Query('Counter')
const Counter = AV.Object.extend('Counter')
query.equalTo('title', 'site')
Expand All @@ -75,11 +73,13 @@ export async function queryLike(type) {
resolve(res.get('time'))
}
else {
res
.increment('time', 1)
.save(null, { fetchWhenSave: true })
.then(counter => resolve(counter.get('time')))
.catch(console.error)
if (!isDev) {
res
.increment('time', 1)
.save(null, { fetchWhenSave: true })
.then(counter => resolve(counter.get('time')))
.catch(console.error)
}
}
}
else {
Expand Down
3 changes: 2 additions & 1 deletion src/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export default {
},
},
created() {
this.visitorStatisticsFn()
if (localStorage.getItem('configLeancloud') === 'yes')
this.visitorStatisticsFn()
},
methods: {
visitorStatisticsFn() {
Expand Down
42 changes: 34 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,46 @@ Vue.prototype.$message = Message
Vue.component('SvgIcon', SvgIcon)
Vue.directive('loading', loadingDirective)

const appId = import.meta.env.VITE_LEANCLOUD_ID
const appKey = import.meta.env.VITE_LEANCLOUD_KEY
const serverURLs = import.meta.env.VITE_LEANCLOUD_SERVER
const appId = import.meta.env.VITE_LEANCLOUD_ID || ''
const appKey = import.meta.env.VITE_LEANCLOUD_KEY || ''
const serverURLs = import.meta.env.VITE_LEANCLOUD_SERVER || ''
const clarity = config.clarity

AV.init({
appId,
appKey,
serverURLs,
})
if (appId && appKey && serverURLs){
localStorage.setItem('configLeancloud', 'yes')
AV.init({
appId,
appKey,
serverURLs,
})
} else {
localStorage.setItem('configLeancloud', 'no')
console.warn('LeanCloud 相关配置未正确设置,请检查环境变量')
}

setTheme()
handleError(config.errorImg)

function isDomainInWhiteList(domain) {
return config.whiteList.some((item) => {
const url = new URL(`https://${item}`);
const itemDomain = url.hostname;
return domain.endsWith(itemDomain);
})
}
document.addEventListener('click', (event) => {
if (event.target.tagName === 'A' && !event.target.href.startsWith(window.location.origin)) {
event.preventDefault()
const url = new URL(event.target.href)
const domain = url.hostname
if (isDomainInWhiteList(domain) === true) {
window.open(event.target.href)
return
}
router.push({ path: 'go', query: {target: event.target.href}})
}
});

if (clarity) {
const script = document.createElement('script')
script.type = 'text/javascript'
Expand Down
10 changes: 9 additions & 1 deletion src/views/about/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export default {
this.getAboutFn()
},
mounted() {
this.queryLikeFn()
if (localStorage.getItem('configLeancloud') === 'yes')
this.queryLikeFn()
},
methods: {
async getAboutFn() {
Expand Down Expand Up @@ -63,6 +64,13 @@ export default {
this.likeTimes = res
},
async likeClick() {
if (localStorage.getItem('configLeancloud') === 'no'){
this.$message({
content: '博主还没有设置 Leancloud,点赞无效',
type: 'error',
})
return
}
if (this.isLiked === 'isLiked') {
this.$message({
content: '您已经点过赞了哦~',
Expand Down
26 changes: 17 additions & 9 deletions src/views/category/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,28 @@ export default {
})
}
const ids = res.map(post => post.id)
const hot = await this.queryHotAction({ ids }).catch((err) => {
this.$message({
content: '获取文章热度失败',
type: 'error',
if (localStorage.getItem('configLeancloud') === 'yes'){
const hot = await this.queryHotAction({ ids }).catch((err) => {
this.$message({
content: '获取文章热度失败',
type: 'error',
})
throw new Error(err)
})
throw new Error(err)
})
if (hot) {
if (hot) {
this.posts = res.map((post) => {
const hotNum = hot[post.id] || 1
post.hot = hotNum
return post
})
}
} else {
this.posts = res.map((post) => {
const hotNum = hot[post.id] || 1
post.hot = hotNum
post.hot = 1
return post
})
}
this.$store.commit('github/setAllPosts', res)
},
},
Expand Down

0 comments on commit b93d029

Please sign in to comment.