123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <template>
- <div class="my-cover">
- <div class="operateBox" @click="dialogVisible = true">
- <span>选择素材</span>
- <!-- 负责展示父组件传过来的数据 -->
- <img :src="imgUrl" alt="" class="img">
- </div>
- <!-- 对话框 -->
- <el-dialog
- :append-to-body="true"
- :visible.sync="dialogVisible"
- width="720px">
- <el-card class="box-card">
- <div style="padding-bottom: 20px;">
- <el-radio-group v-model="collect" size="mini" @change="collectChange">
- <el-radio-button :label="false">全部</el-radio-button>
- <el-radio-button :label="true">收藏</el-radio-button>
- </el-radio-group>
- </div>
- <el-row :gutter="10">
- <el-col
- v-for="(image) in imageList"
- :key="image.id" class="img_item" :xs="12" :sm="6" :md="6" :lg="4">
- <div class="img_box" @click="selectImage(image)">
- <img :src="image.url" alt="">
- <!-- 当前点击项会展示小对勾
- v-show/v-if
- -->
- <div class="selected" v-show="image.selected">
- <img src="./img/selected.png" alt="">
- </div>
- </div>
- </el-col>
- </el-row>
- <el-pagination
- style="margin-top:10px;"
- background
- layout="sizes, prev, pager, next"
- :page-sizes="[5, 10, 15, 20]"
- :page-size="10"
- @current-change="currentChange"
- @size-change="sizeChange"
- :total="total_cont">
- </el-pagination>
- </el-card>
- <span slot="footer" class="dialog-footer">
- <el-button @click="cancel">取 消</el-button>
- <el-button type="primary" @click="confirm">确 定</el-button>
- </span>
- </el-dialog>
- </div>
- </template>
- <script>
- // 引入图片素材接口
- import { getImageList } from '@/api/image'
- export default {
- props: {
- imgUrl: {
- type: String
- }
- },
- data () {
- return {
- imageList: [], // 素材列表
- total_cont: 0, // 素材总数
- curPage: 1, // 当前要请求第几页数据
- per_page: 10, // 当前每页的条数
- dialogVisible: false, // 弹出框是否显示
- collect: false // 是否为收藏状态
- }
- },
- methods: {
- openDialog () {
- this.dialogVisible = true
- },
- currentChange (pageNum) {
- this.curPage = pageNum
- this.hGetImage()
- },
- // 发起素材请求列表
- hGetImage () {
- getImageList({
- collect: this.collect,
- per_page: this.per_page, // 每页的条数
- page: this.curPage // 当前请求第几页
- }).then(res => {
- // 素材列表
- // 处理源数据 在里面的每一项都添加一个字段 专门用来控制对勾是否显示
- // [{}{}{}] 完成映射关系的数组处理使用map函数
- this.imageList = res.data.data.results.map((item) => {
- return {
- ...item, // 把数组项中之前就有的三个字段展开放过来
- selected: false // false代表不展示 true代表展示
- }
- })
- this.total_cont = res.data.data.total_count
- })
- },
- sizeChange (pageSize) {
- this.per_page = pageSize
- this.hGetImage()
- },
- collectChange (collect) {
- this.hGetImage()
- },
- cancel () {
- this.dialogVisible = false
- },
- confirm () {
- // 遍历this.imageList找到seleted为true的那一项 取到它的url交给父组件
- let url = ''
- this.imageList.forEach((item) => {
- if (item.selected) {
- url = item.url
- }
- })
- // 交给父组件 这里是最关键的一部 不管中间经过什么样的交互
- this.$emit('update:imgUrl', url)
- this.dialogVisible = false
- },
- selectImage (image) {
- // 排它思想 selected
- // 把所有项都设置为false
- // 这个地方的选择并不是最终决定要将哪项抛给父组件 只有点击确定按钮才能决定
- // 点击确定按钮的时候需要去找当前身上有对勾的项然后把它的url拿到 交给父组件
- this.imageList.forEach((item) => {
- item.selected = false
- })
- // 把自己设置为true
- image.selected = true
- }
- },
- mounted () {
- this.hGetImage()
- }
- }
- </script>
- <style lang="less" scoped>
- .my-cover{
- display: inline-block;
- }
- .operateBox{
- display: inline-block;
- width: 100px;
- height: 100px;
- margin-left: 10px;
- border-radius: 4px;
- border:1px solid #ccc;
- font-size:13px;
- color:#666;
- text-align: center;
- line-height: 100px;
- overflow: hidden;
- cursor: pointer;
- position: relative;
- .img{
- position: absolute;
- top:0;
- left:0;
- width: 100%;
- height: 100%;
- }
- }
- .img_item {
- position: relative;
- box-sizing: border-box;
- .img_box{
- height: 120px;
- margin-bottom: 10px;
- position: relative;
- img{
- width: 100%;
- height: 100%;
- }
- .selected{
- position: absolute;
- width: 20%;
- left:0;
- top:0;
- }
- }
- }
- .option {
- position: absolute;
- left: 5px;
- right:5px;
- bottom: 5px;
- height: 30px;
- background: rgba(0, 0, 0, 0.3);
- color: #fff;
- display: flex;
- span {
- flex:1;
- text-align: center;
- cursor: pointer;
- line-height: 30px;
- }
- }
- .avatar-uploader-icon {
- font-size: 28px;
- color: #8c939d;
- width: 178px;
- height: 178px;
- line-height: 178px;
- text-align: center;
- }
- .uploadBox{
- text-align: center;
- img {
- width:100%;
- }
- // scoped代表当前的样式只能影响当前的组件 影响不了子组件的样式
- // 加上 /deep/ 之后可以进行深度样式控制
- // 当试图直接控制子组件内部元素样式的时候就需要添加 /deep/
- /deep/.el-upload {
- border: 1px dashed #d9d9d9;
- border-radius: 6px;
- cursor: pointer;
- position: relative;
- overflow: hidden;
- }
- }
- // 1.要复用 有多个地方都需要用到此组件
- // 2.维护性 .vue单文件代码行数不能太多
- // 组件的抽离必定增加组件之间的通信成本
- </style>
|