播放器

轮播

//Recommend/index.js
import React from 'react'
import Slider from '../../components/slider'

function Recommend() {
  const bannerList = [1, 2, 3, 4].map(() => {
    return {
      imageUrl: 'http://p1.music.126.net/ZYLJ2oZn74yUz5x8NBGkVA==/109951164331219056.jpg'
    }
  })
  return (
    <div>
      <Slider bannerList={bannerList}/>
    </div>
  )
}

export default Recommend
//components/slider/index.js
import React, { useState, useEffect } from 'react'
import { SliderContainer } from './style'
import 'swiper/css/swiper.css'
import Swiper from 'swiper'

function Slider(props) {
  const { bannerList } = props
  const [sliderSwiper, setSliderSwiper] = useState(null)

  useEffect(() => {
    if (bannerList.length && !sliderSwiper) {
      const sliderSwiper = new Swiper('.swiper-container', {
        loop: true,
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        pagination: {
          el: '.swiper-pagination'
        }
      })
      setSliderSwiper(sliderSwiper)
    }
  }, [bannerList.length, sliderSwiper])
  
  return (
    <SliderContainer>
      <div className='before'></div>
      <div className="swiper-container">
        <div className="swiper-wrapper">
          {
            bannerList.map((banner, index) => {
              return (
                <div className="swiper-slide" key={index}>
                  <div className='swiper-nav'>
                    <img src={banner.imageUrl} width='100%' height='100%' alt='推荐'/>
                  </div>
                </div>
              )
            })
          }
        </div>
        <div className="swiper-pagination"></div>
      </div>
    </SliderContainer>
  )
}

export default Slider
//components/scroll/style.js
import styled from "styled-components"
import style from '../../assets/global-style'

export const SliderContainer = styled.div`
  position: relative;
  width: 100%;
  height: 100%;
  .before {
    position: absolute;
    top: 0;
    height: 60%;
    width: 100%;
    background: ${style['theme-color']}
  }
  .swiper-container {
    position: relative;
    width: 98%;
    height: 160px;
    overflow: hidden;
    margin: auto;
    border-radius: 6px;
    .swiper-nav {
      position: absolute;
      width: 100%;
      height: 100%;
    }
    --swiper-theme-color: ${style['theme-color']};
  }
`

推荐列表-mock

//Recommend/index.js
import React from 'react'
import CommonList from '../../components/list';

function Recommend() {
  const recommendList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].map(() => {
    return {
      id: 1,
      picUrl: 'https://p1.music.126.net/fhmefjUfMD-8qtj3JKeHbA==/18999560928537533.jpg',
      playCount: 17171122,
      name: '朴树、许巍、李健、郑钧、老狼、赵雷'
    }
  })

  return (
    <div>
      <CommonList list={recommendList}/>
    </div>
  )
}

export default Recommend
//components/list/index.js
import React from 'react'
import { ListWrapper, List, ListItem } from './style'
import { getCount } from '../../api/utils'

function CommonList(props){
  const { list } = props 
  
  return (
    <ListWrapper>
      <h1 className='title'>推荐歌单</h1>
      <List>
        {
          list.map((item, index) => {
            return (
              <ListItem key={item.id + index}>
                <div className='image-wrapper'>
                  <div className='decorate'></div>
                  <img src={item.picUrl + '?param=300x300'} width='100%' height='100%' alt='歌单'/>
                  <div className='play-count'>
                    <span className='iconfont play'>&#xe885;</span>
                    <span className='count'>{getCount(item.playCount)}</span>
                  </div>
                </div>
                <div className='desc'>{item.name}</div>
              </ListItem>
            )
          })
        }
      </List>
    </ListWrapper>
  )
}

export default CommonList
//components/list/style.js
import styled from "styled-components"
import style from '../../assets/global-style'

export const ListWrapper = styled.div`
  max-width: 100%;
  .title {
    font-weight: 700;
    padding-left: 6px;
    font-size: 14px;
    line-height: 60px;
  }
`

export const List = styled.div`
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-around;
`

export const ListItem = styled.div`
  position: relative;
  width: 32%;
  .image-wrapper {
    .decorate {
      position: absolute;
      top: 0;
      width: 100%;
      height: 35px;
      border-radius: 3px;
      background: linear-gradient(hsla(0, 0%, 43%, 0.4), hsla(0, 0%, 100%, 0));
    }
    position: relative;
    height: 0;
    padding-bottom: 100%;
    img {
      position: absolute;
      width: 100%;
      height: 100%;
      border-radius: 3px;
    }
    .play-count {
      position: absolute;
      right: 2px;
      top: 2px;
      line-height: 15px;
      color: ${style['font-color-light']};
      font-size: ${style['font-size-s']};
      .play {
        vertical-align: top;
      }
    }
  }
  .desc {
    overflow: hidden;
    margin-top: 2px;
    padding: 0 2px;
    height: 50px;
    text-align: left;
    font-size: ${style['font-size-s']};
    line-height: 1.4;
    color: ${style['font-color-desc']};
  }
`
//api/utils.js
export const getCount = (count) => {
  if (count < 0) return
  if (count < 10000) {
    return count
  } else if (Math.floor(count/10000) < 10000) {
    return Math.floor(count/1000)/10 + '万'
  } else {
    return Math.floor(count/10000000)/10 + '亿'
  }
}

better-scroll 的初步应用

//baseUI/scroll/index.js
import React, { forwardRef, useState,useEffect, useRef, useImperativeHandle } from 'react'
import PropTypes from 'prop-types'
import BScroll from 'better-scroll'
import styled from 'styled-components'

const ScrollContainer = styled.div`
  width: 100%;
  height: 100%;
  overflow: hidden;
`
const Scroll = forwardRef((props, ref) => {
  const [bScroll, setBScroll] = useState();

  const scrollContaninerRef = useRef();

  const { direction, click, refresh,  bounceTop, bounceBottom } = props;

  const { pullUp, pullDown, onScroll } = props;

  useEffect(() => {
    const scroll = new BScroll(scrollContaninerRef.current, {
      scrollX: direction === "horizental",
      scrollY: direction === "vertical",
      probeType: 3,
      click: click,
      bounce:{
        top: bounceTop,
        bottom: bounceBottom
      }
    });
    setBScroll(scroll);
    return () => {
      setBScroll(null);
    }
    // eslint-disable-next-line
  }, []);

  useEffect(() => {
    if(!bScroll || !onScroll) return;
    bScroll.on('scroll', (scroll) => {
      onScroll(scroll);
    })
    return () => {
      bScroll.off('scroll');
    }
  }, [onScroll, bScroll]);

  useEffect(() => {
    if(!bScroll || !pullUp) return;
    bScroll.on('scrollEnd', () => {
      //判断是否滑动到了底部
      if(bScroll.y <= bScroll.maxScrollY + 100){
        pullUp();
      }
    });
    return () => {
      bScroll.off('scrollEnd');
    }
  }, [pullUp, bScroll]);

  useEffect(() => {
    if(!bScroll || !pullDown) return;
    bScroll.on('touchEnd', (pos) => {
      //判断用户的下拉动作
      if(pos.y > 50) {
        pullDown();
      }
    });
    return () => {
      bScroll.off('touchEnd');
    }
  }, [pullDown, bScroll]);


  useEffect(() => {
    if(refresh && bScroll){
      bScroll.refresh();
    }
  });

  useImperativeHandle(ref, () => ({
    refresh() {
      if(bScroll) {
        bScroll.refresh();
        bScroll.scrollTo(0, 0);
      }
    },
    getBScroll() {
      if(bScroll) {
        return bScroll;
      }
    }
  }));


  return (
    <ScrollContainer ref={scrollContaninerRef}>
      {props.children}
    </ScrollContainer>
  );
})

Scroll.defaultProps = {
  direction: "vertical",
  click: true,
  refresh: true,
  onScroll:null,
  pullUpLoading: false,
  pullDownLoading: false,
  pullUp: null,
  pullDown: null,
  bounceTop: true,
  bounceBottom: true
}

Scroll.propTypes = {
  direction: PropTypes.oneOf(['vertical', 'horizental']),
  refresh: PropTypes.bool,
  onScroll: PropTypes.func,
  pullUp: PropTypes.func,
  pullDown: PropTypes.func,
  pullUpLoading: PropTypes.bool,
  pullDownLoading: PropTypes.bool,
  bounceTop: PropTypes.bool,//是否支持向上吸顶
  bounceBottom: PropTypes.bool//是否支持向上吸顶
}

export default Scroll
// Recommend/index.js
import { Content } from './style'
import Scroll from '../../baseUI/scroll'
<Content>
  <Scroll className="list">
    <div>
      <Slider list={bannerList}></Slider>
      <RecommendList list={recommendList}></RecommendList>
    </div>
  </Scroll>
</Content> 

// Recommend/style.js
import styled from'styled-components';

export const Content = styled.div`
  position: fixed;
  top: 90px;
  bottom: 0;
  width: 100%;
`
//slider/style.js
.before {
  position: absolute;
  top: -300px;
  height: 400px;
  width: 100%;
  background: ${style ["theme-color"]};
}

数据层

1.

//api/config.js
import axios from 'axios'

export const baseUrl = 'http://localhost:8000'

//axios 的实例及拦截器配置
const axiosInstance = axios.create ({
  baseURL: baseUrl
})

axiosInstance.interceptors.response.use (
  res => res.data,
  err => {
    console.log (err, "网络错误")
  }
);

export {
  axiosInstance
}

2.

//api/request.js
import { axiosInstance } from "./config"

export const getBannerListRequest = () => {
  return axiosInstance.get('/banner')
}

export const getRecommendListRequest = () => {
  return axiosInstance.get('/personalized')
}

3.

//Recommond/store/reducer.js
import * as actionTypes from './actionTypes'
import { fromJS } from 'immutable'

const defaultState = fromJS ({
  bannerList: [],
  recommendList: [],
})

4.

//Recommond/store/actionTypes.js
export const CHANGE_BANNER_LIST = 'recommend/CHANGE_BANNER_LIST'
export const CHANGE_RECOMMEND_LIST = 'recommend/CHANGE_RECOMMEND_LIST'

5.

//Recommond/store/reducer.js
export default (state = defaultState, action) => {
  switch (action.type) {
    case actionTypes.CHANGE_BANNER_LIST:
      return state.set('bannerList', action.data)
    case actionTypes.CHANGE_RECOMMEND_LIST:
      return state.set('recommendList', action.data)
    default:
      return state
  }
}

6.

//Recommond/store/actionCreators.js
import * as actionTypes from './actionTypes'
import { fromJS } from 'immutable'
import { getBannerListRequest, getRecommendListRequest } from '../../../api/request'

export const changeBannerList = (data) => ({
  type: actionTypes.CHANGE_BANNER_LIST,
  data: fromJS(data)
})

export const changeRecommendList = (data) => ({
  type: actionTypes.CHANGE_RECOMMEND_LIST,
  data: fromJS(data)
})

export const getBannerList = () => {
  return (dispatch) => {
    getBannerListRequest().then (data => {
      dispatch(changeBannerList(data.banners))
    }).catch (() => {
      console.log("轮播图数据传输错误")
    }) 
  }
}

export const getRecommendList = () => {
  return (dispatch) => {
    getRecommendListRequest().then (data => {
      dispatch (changeRecommendList(data.result))
    }).catch (() => {
      console.log ("推荐歌单数据传输错误");
    })
  }
}

7.\color{red}{导出!!!}

//Recommond/store/index.js
import reducer from './reducer'
import * as actionCreators from './actionCreators'

export { reducer, actionCreators }

8.\color{red}{注册!!!}

//src/store/reducer.js
import { combineReducers } from 'redux-immutable'
import { reducer as recommendReducer } from '../application/Recommend/store'

export default combineReducers ({
  recommend: recommendReducer,
})

9.

//Recommond/index.js
import React, { useEffect, memo } from 'react'
import { Content } from './style'
import Slider from '../../components/slider'
import CommonList from '../../components/list'
import Scroll from '../../baseUI/scroll'
import { connect } from "react-redux"
import * as actionCreators from './store/actionCreators'

function Recommend(props) {
  const { bannerList, recommendList } = props
  const { getBannerListDataDispatch, getRecommendListDataDispatch } = props

  useEffect (() => {
    getBannerListDataDispatch()
    getRecommendListDataDispatch()
    //eslint-disable-next-line
  }, [])

  const bannerListJS = bannerList ? bannerList.toJS() : []
  const recommendListJS = recommendList ? recommendList.toJS() : []

  return (
    <Content>
      <Scroll className='list'>
        <div>
          <Slider list={bannerListJS}/>
          <CommonList list={recommendListJS}/>
        </div>
      </Scroll>
    </Content>
  )
}

const mapStateToProps = (state) => ({
  bannerList: state.getIn(['recommend', 'bannerList']),
  recommendList: state.getIn(['recommend', 'recommendList']),
})

const mapDispatchToProps = (dispatch) => {
  return {
    getBannerListDataDispatch() {
      dispatch(actionCreators.getBannerList())
    },
    getRecommendListDataDispatch () {
      dispatch(actionCreators.getRecommendList())
    }
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(memo(Recommend))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,098评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,213评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,960评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,519评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,512评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,533评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,914评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,804评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,563评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,644评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,350评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,933评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,908评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,146评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,847评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,361评论 2 342