admin 管理员组

文章数量: 1086866

第八课:【歌手列表】

1.在api中新建 api.js

import jsonp from 'common/js/jsonp'
import {commonParams, options} from './config'export function getSingerList() {const url = '.fcg'const data = Object.assign({}, commonParams, {channel: 'singer',page: 'list',key: 'all_all_all',pagesize: 100,pagenum: 1,hostUin: 0,needNewCode: 0,platform: 'yqq'})return jsonp(url, data, options)
}export function getSingerDetail(singerId) {const url = '.fcg'const data = Object.assign({}, commonParams, {hostUin: 0,needNewCode: 0,platform: 'yqq',order: 'listen',begin: 0,num: 80,songstatus: 1,singermid: singerId})return jsonp(url, data, options)
}

2.创建singer.vue
处理json聚合并排序

 list.forEach((item, index) => {if (index < HOT_SINGER_LEN) {map.hot.items.push(new Singer({name: item.Fsinger_name,id: item.Fsinger_mid}))}const key = item.Findexif (!map[key]) {map[key] = {title: key,items: []}}map[key].items.push(new Singer({name: item.Fsinger_name,id: item.Fsinger_mid}))})// 为了得到有序列表,我们需要处理 maplet ret = []let hot = []for (let key in map) {let val = map[key]if (val.title.match(/[a-zA-Z]/)) {ret.push(val)} else if (val.title === HOT_NAME) {hot.push(val)}}ret.sort((a, b) => {return a.title.charCodeAt(0) - b.title.charCodeAt(0)})return hot.concat(ret)},...mapMutations({setSinger: 'SET_SINGER'})},

3mon/js 创建singer.js 构造Singer类简化代码

export default class Singer {constructor({id, name}) {this.id = idthis.name = namethis.avatar = `${id}.jpg?max_age=2592000`}
}

在singer.vue中

export default class Singer {constructor({id, name}) {this.id = idthis.name = namethis.avatar = `${id}.jpg?max_age=2592000`}
}

改进前

            map.hot.items.push({name: item.Fsinger_name,id: item.Fsinger_mid,avatar:${id}.jpg?max_age=2592000})

改进之后

            map.hot.items.push(new Singer({name: item.Fsinger_name,id: item.Fsinger_mid}))

“`

本文标签: 第八课歌手列表