Skip to content

Commit

Permalink
fix(SPA): 修复获取数据时数量传出10而判断为15的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mutoe committed Feb 25, 2019
1 parent ba50f4d commit 673080c
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 65 deletions.
2 changes: 1 addition & 1 deletion resources/spa/src/api/news.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function getNewsList (params) {
* @param {number} type [类型: 0: 已发布, 1: 待审核, 2: 已驳回]
* @returns {Promise<NewsObject[]>}
*/
export function getMyNews ({ type = 0, limit = 15, after = 0 }) {
export function getMyNews ({ type = 0, after = 0 }) {
const params = { type, limit, after }
return api.get('/user/news/contributes', {
params,
Expand Down
3 changes: 2 additions & 1 deletion resources/spa/src/api/questions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { limit } from './index'
import api from './api'

/**
Expand Down Expand Up @@ -27,6 +28,6 @@ export function queryList (params = {}) {
* @return {Promise}
* @author Seven Du <[email protected]>
*/
export function list (type, offset = 0, limit = 15) {
export function list (type, offset = 0) {
return queryList({ type, limit, offset })
}
4 changes: 3 additions & 1 deletion resources/spa/src/api/topic.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { limit } from './index'
import api from './api'

/**
Expand All @@ -7,13 +8,14 @@ import api from './api'
* @export
* @param {Object} params
* @param {string} [params.q] 搜索关键字
* @param {number} [params.limit=15]
* @param {number} [params.limit]
* @param {string} [params.direction=desc]
* @param {number} [params.index=0]
* @param {string} [params.only] 是否热门 'hot'
* @returns
*/
export function getTopicList (params) {
if (!params.limit) params.limit = limit
if (params.q === '') return Promise.resolve({ data: [] })
const url = '/feed/topics'
return api.get(url, { params, validateStatus: s => s === 200 })
Expand Down
55 changes: 42 additions & 13 deletions resources/spa/src/api/user.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { limit } from './index'
import api from './api'
import store from '@/stores'
import $Message from '@/plugins/message-box'
import lstore from '@/plugins/lstore/lstore.js'

/**
* 定义用户对象
* @typedef {{id: number, name: string, ...more}} UserObject
* @typedef {Object} UserObject
* @property {number} id
*/

const resArray = { data: [] }
Expand Down Expand Up @@ -89,34 +91,32 @@ export const findUserByType = (type, params) => {
* 查找附近的人
* @author jsonleex <[email protected]>
* @export
* @param {number} options.lng: longitude 经度
* @param {number} options.lat: latitude 纬度
* @param {number} page 当前页
* @param {number} options.lng: longitude 经度
* @param {number} options.lat: latitude 纬度
* @param {number} page 当前页
* @returns {Promise<UserObject[]>}
*/
export const findNearbyUser = ({ lng: longitude, lat: latitude }, page = 0) => {
const params = {
limit: 10,
limit,
longitude,
latitude,
}
page > 0 && (params.page = page)

return api
.get('around-amap', { params })
.then(data => data)
return api.get('around-amap', { params })
.catch(() => resArray)
}

/**
* 获取用户基本信息
* 优先返回本地数据
* 获取用户基本信息 优先返回本地数据
* @author jsonleex <[email protected]>
* @export
* @param {number} id
* @param {number} id
* @param {boolean} [force=false]
* @returns {Promise<UserObject>}
*/
export const getUserInfoById = (id, force = false) => {
export async function getUserInfoById (id, force = false) {
const user = store.state.USERS[`user_${id}`]
if (user && !force) return user

Expand Down Expand Up @@ -156,7 +156,7 @@ export function getUserList (params) {
* @param {number} options.offset
* @returns {Promise<UserObject[]>}
*/
export function getUserFansByType ({ uid, type, limit = 15, offset = 0 }) {
export function getUserFansByType ({ uid, type, offset = 0 }) {
const params = {
limit,
offset,
Expand Down Expand Up @@ -281,3 +281,32 @@ export function reportUser (userId, reason) {
const url = `/report/users/${userId}`
return api.post(url, { reason }, { validateStatus: s => s === 201 })
}

/**
* 获取用户标签
*
* @author mutoe <[email protected]>
* @export
* @param {number} userId
* @returns
*/
export function getUserTags (userId) {
const url = `/users/${userId}/tags`
return api.get(url, { validateStatus: s => s === 200 })
}

/**
* 获取好友列表
*
* @author mutoe <[email protected]>
* @export
* @param {Object} [params]
* @param {number} [params.offset]
* @param {number} [params.limit]
* @param {string} [params.keyword]
* @returns {UserObject[]}
*/
export function getUserFriends (params) {
const url = '/user/follow-mutual'
return api.get(url, { params, validateStatus: s => s === 200 })
}
5 changes: 3 additions & 2 deletions resources/spa/src/page/UserFans.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
</template>

<script>
import UserItem from '@/components/UserItem'
import { limit } from '@/api'
import { getUserFansByType } from '@/api/user.js'
import UserItem from '@/components/UserItem'
const typeMap = ['followers', 'followings']
export default {
Expand All @@ -73,7 +74,7 @@ export default {
},
param () {
return {
limit: 15,
limit,
type: this.type,
uid: this.userId,
}
Expand Down
14 changes: 7 additions & 7 deletions resources/spa/src/page/UserHome.vue
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@

<script>
import _ from 'lodash'
import uploadApi from '@/api/upload'
import FeedCard from '@/components/FeedCard/FeedCard.vue'
import HeadRoom from 'headroom.js'
import wechatShare from '@/util/wechatShare.js'
import { limit } from '@/api'
import uploadApi from '@/api/upload'
import * as api from '@/api/user'
import wechatShare from '@/util/wechatShare'
import { checkImageType } from '@/util/imageCheck'
import { startSingleChat } from '@/vendor/easemob'
import { checkImageType } from '@/util/imageCheck.js'
import * as api from '@/api/user.js'
import FeedCard from '@/components/FeedCard/FeedCard.vue'
export default {
name: 'UserHome',
Expand Down Expand Up @@ -456,7 +456,7 @@ export default {
if (this.fetchFeeding) return
this.fetchFeeding = true
const params = {
limit: 15,
limit,
type: 'users',
user: this.userId,
}
Expand Down
10 changes: 5 additions & 5 deletions resources/spa/src/page/article/ArticleLikes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</template>

<script>
import { limit } from '@/api'
import UserItem from '@/components/UserItem.vue'
export default {
Expand Down Expand Up @@ -73,28 +74,27 @@ export default {
},
onRefresh (callback) {
// 名称 类型 描述
// limit Integer 获取条数,默认 20
// after Integer id 获取之后数据,默认 0
this.$http
.get(this.url, { params: { limit: 15 } })
.get(this.url, { params: { limit } })
.then(({ data = [] }) => {
this.likes = data
data.length > 0 && (this.maxId = data[data.length - 1].id)
this.$refs.loadmore.afterRefresh(data.length < 15)
this.$refs.loadmore.afterRefresh(data.length < limit)
})
},
onLoadMore (callback) {
this.$http
.get(this.url, {
params: {
limit: 15,
limit,
after: this.maxId,
},
})
.then(({ data = [] }) => {
this.likes = [...this.likes, ...data]
data.length > 0 && (this.maxId = data[data.length - 1].id)
this.$refs.loadmore.afterLoadMore(data.length < 15)
this.$refs.loadmore.afterLoadMore(data.length < limit)
})
},
},
Expand Down
10 changes: 6 additions & 4 deletions resources/spa/src/page/article/ArticleRewards.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
</template>

<script>
import { limit } from '@/api'
export default {
name: 'ArticleRewards',
data () {
Expand Down Expand Up @@ -84,21 +86,21 @@ export default {
order_type string 默认 date, amount 打赏金额 date 打赏时间
*/
this.$http
.get(this.url, { params: { limit: 15 } })
.get(this.url, { params: { limit } })
.then(({ data = [] }) => {
this.rewards = data
this.$refs.loadmore.afterRefresh(data.length < 15)
this.$refs.loadmore.afterRefresh(data.length < limit)
})
.catch(() => {
this.$refs.loadmore.afterRefresh(true)
})
},
onLoadMore () {
this.$http
.get(this.url, { params: { limit: 15, offset: this.rewards.length } })
.get(this.url, { params: { limit, offset: this.rewards.length } })
.then(({ data = [] }) => {
this.rewards = [...this.rewards, ...data]
this.$refs.loadmore.afterLoadmore(data.length < 15)
this.$refs.loadmore.afterLoadmore(data.length < limit)
})
.catch(() => {
this.$refs.loadmore.afterLoadmore(true)
Expand Down
7 changes: 4 additions & 3 deletions resources/spa/src/page/feed/FeedList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@
* @typedef {{id: number, user, ...others}} FeedDetail
*/
import FeedCard from '@/components/FeedCard/FeedCard.vue'
import { limit } from '@/api'
import { noop } from '@/util'
import FeedCard from '@/components/FeedCard/FeedCard.vue'
const feedTypesMap = ['new', 'hot', 'follow']
Expand Down Expand Up @@ -123,13 +124,13 @@ export default {
const type = this.feedType.replace(/^\S/, s => s.toUpperCase())
const action = `feed/get${type}Feeds`
const data = await this.$store.dispatch(action, { refresh: true })
this.$refs.loadmore.afterRefresh(data.length < 15)
this.$refs.loadmore.afterRefresh(data.length < limit)
},
async onLoadMore () {
const type = this.feedType.replace(/^\S/, s => s.toUpperCase())
const action = `feed/get${type}Feeds`
const data = await this.$store.dispatch(action, { after: this.after })
this.$refs.loadmore.afterLoadMore(data.length < 15)
this.$refs.loadmore.afterLoadMore(data.length < limit)
},
},
}
Expand Down
5 changes: 3 additions & 2 deletions resources/spa/src/page/find/FindNer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
<script>
import { mapState } from 'vuex'
import { noop } from '@/util'
import UserItem from '@/components/UserItem.vue'
import { limit } from '@/api'
import { findNearbyUser } from '@/api/user.js'
import UserItem from '@/components/UserItem.vue'
export default {
name: 'FindNer',
Expand Down Expand Up @@ -59,7 +60,7 @@ export default {
user && sortedUsers.push(user)
}
this.users = sortedUsers
const more = this.users.length < 15
const more = this.users.length < limit
callback(more)
},
onRefresh (callback) {
Expand Down
8 changes: 5 additions & 3 deletions resources/spa/src/page/find/FindNew.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
</JoLoadMore>
</template>
<script>
import UserItem from '@/components/UserItem.vue'
import { limit } from '@/api'
import { findUserByType } from '@/api/user.js'
import UserItem from '@/components/UserItem.vue'
export default {
name: 'FindPop',
components: {
Expand All @@ -32,15 +34,15 @@ export default {
onRefresh () {
findUserByType('latests').then(({ data: users } = {}) => {
users && (this.users = users)
this.$refs.loadmore.afterRefresh(users.length < 15)
this.$refs.loadmore.afterRefresh(users.length < limit)
})
},
onLoadMore () {
findUserByType('latests', {
offset: this.users.length,
}).then(({ data: users }) => {
this.users = [...this.users, ...users]
this.$refs.loadmore.afterLoadmore(users.length < 15)
this.$refs.loadmore.afterLoadmore(users.length < limit)
})
},
},
Expand Down
9 changes: 6 additions & 3 deletions resources/spa/src/page/find/FindPop.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
/>
</JoLoadMore>
</template>

<script>
import UserItem from '@/components/UserItem.vue'
import { limit } from '@/api'
import { findUserByType } from '@/api/user.js'
import UserItem from '@/components/UserItem.vue'
export default {
name: 'FindPop',
components: {
Expand All @@ -32,15 +35,15 @@ export default {
onRefresh (callback) {
findUserByType('populars').then(({ data: users } = {}) => {
users && (this.users = users)
this.$refs.loadmore.afterRefresh(users.length < 15)
this.$refs.loadmore.afterRefresh(users.length < limit)
})
},
onLoadMore (callback) {
findUserByType('populars', {
offset: this.users.length,
}).then(({ data: users }) => {
this.users = [...this.users, ...users]
this.$refs.loadmore.afterLoadMore(users.length < 15)
this.$refs.loadmore.afterLoadMore(users.length < limit)
})
},
},
Expand Down
Loading

0 comments on commit 673080c

Please sign in to comment.