banner.vue 13.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
<template>
  <div class="banner" v-loading="loading">
    <div class="head clear-both">
      <el-button @click="add" plain type="success" style="float: right" v-if="!$store.state.readonly">新增</el-button>
    </div>
    <el-tabs v-model="searchFrom.type" type="card" style="background: white;padding-top: 10px" @tab-click="getList">
      <el-tab-pane label="磨耳朵" name="2"></el-tab-pane>
      <el-tab-pane label="启蒙小课堂" name="3"></el-tab-pane>
      <el-tab-pane label="明星学员" name="4"></el-tab-pane>
    </el-tabs>
    <el-tabs v-model="searchFrom.status" type="card" style="background: white;padding-top: 10px" @tab-click="getList">
      <el-tab-pane label="全部" name="-1"></el-tab-pane>
      <el-tab-pane label="上架" name="1"></el-tab-pane>
      <el-tab-pane label="下架" name="2"></el-tab-pane>
    </el-tabs>
    <el-table
      :data="bannerList"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题">
      </el-table-column>
      <el-table-column
        prop="url"
        label="图片">
        <template slot-scope="scope">
          <a :href="scope.row.url" target="_blank">
            <img class="short-banner" :src="scope.row.url"/>
          </a>
        </template>
      </el-table-column>
      <el-table-column
        label="状态">
        <template slot-scope="scope">
          {{scope.row.status | status}}
        </template>
      </el-table-column>
      <el-table-column
        prop="link"
        label="链接">
      </el-table-column>
      <el-table-column
        label="操作"
        v-if="!$store.state.readonly"
        width="100">
        <template slot-scope="scope">
          <el-popover
            placement="top"
            width="280">
            <div style="text-align: center">
              <el-button size="mini" plain type="primary" @click="edit(scope.row)">
                编辑
              </el-button>
              <el-button size="mini" type="danger" plain @click="del(scope.row)" v-if="$store.state.deletePermission">
                删除
              </el-button>
              <el-button v-if="scope.row.status == 2" size="mini" type="primary" plain @click="changeStatus(scope.row)">
                上架
              </el-button>
              <el-button v-if="scope.row.status == 1" size="mini" type="primary" plain @click="changeStatus(scope.row)">
                下架
              </el-button>
              <el-button v-if="scope.$index > 0" size="mini" type="primary" plain @click="moveUp(scope.$index)">
                上移
              </el-button>
              <el-button v-if="scope.$index !== bannerList.length - 1" size="mini" type="primary" plain @click="moveDown(scope.$index)">
                下移
              </el-button>
            </div>
            <el-button slot="reference" size="mini" type="text" >操作</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <page :nowPage="nowPage" :total="total" :limit="limit" @pageChange="onPageChange" @sizeChange="onSizeChange"/>
    <el-dialog
      :title="dialog.title"
      center
      append-to-body
      :visible.sync="dialog.show"
      width="30%">
      <el-form ref="form" :rules="dialog.rules" :model="dialog.form" label-width="100px">
        <el-form-item label="名称" prop="title">
          <el-input v-model="dialog.form.title"></el-input>
        </el-form-item>
        <el-form-item label="二级名称" prop="sub_title">
          <el-input v-model="dialog.form.sub_title"></el-input>
        </el-form-item>
        <el-form-item label="链接" prop="link">
          <el-input v-model="dialog.form.link"></el-input>
        </el-form-item>
        <div class="upload-block">
          <el-upload
            action="/api/public/upload/zone"
            :http-request="uploadFile"
            :class="{disabled:!uploadShow}"
            :before-upload="beforeAvatarUpload"
            list-type="picture-card"
            :file-list="imageList"
            :on-success="handleAvatarSuccess"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <p class="size">750*400</p>
        </div>
        <el-upload
          v-if="searchFrom.type==2&&audio.length==0"
          class="upload-demo"
          :http-request="uploadFileAudio2"
          :before-upload="beforeAvatarAudio"
          :file-list="audio"
          :on-exceed="handleExceed"
          :on-remove="removeFileAudio2"
          drag
          action="https://jsonplaceholder.typicode.com/posts/"
          multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处,或<em>点击上传</em>
            <br>
            <span style="color: #888888;font-size: 12px">只能上传MP3文件</span>
          </div>
        </el-upload>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.show = false">取 消</el-button>
        <el-button type="primary" @click="sub">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {getBannerListApi,addBannerApi,editBannerApi,getBannerDetailApi,delBannerApi,moveApi,uploadFileApi,bannerChangeStatusApi} from "../../service/api";
  import page from '../framework/page'
  export default {
    name: "banner",
    components:{
      page
    },
    data(){
      return {
        loading: false,
        bannerList: [],
        searchFrom:{
          status:'1',
          type:'2'
        },
        total: 0,
        nowPage: 1,
        uploadShow: true,
        limit: 10,
        dialog:{
          title:'新增',
          show:false,
          rules: {
            title: [
              {required: true, message: '请填写名称', trigger: 'change'},
            ],
            link: [
              {required: true, message: '请填写链接', trigger: 'change'},
            ],
            sub_title: [
              {required: true, message: '请填写子名称', trigger: 'change'},
            ]
          },
          form:{
            title:'',
            url:'',
            link:'',
            id: '',
            sub_title:''
          }
        },
        imageList: [],
        audio:[]
      }
    },
    created(){
      this.getList()
    },
    filters:{
      status(value){
        if(value === 1){
          return '上架中'
        }else{
          return '已下架'
        }
      }
    },
    methods: {
      removeFileAudio2(file,fileList){
        this.audio = fileList
      },
      handleExceed(){
      },
      // 音频文件限制
      beforeAvatarAudio(file){
        const isJPG = (file.type === 'audio/mp3' );
        if (!isJPG) {
          this.$message.error('上传视频只能是MP3格式!');
        }
        return isJPG;
      },
      // 上传音频2
      uploadFileAudio2(a){
          uploadFileApi({file:a.file,type:'local',obj:a}).then(res=>{
            this.$message({
              type: 'success',
              message: '上传成功!'
            });
            a.onSuccess('上传成功');
            this.dialog.form.link = process.env.IMAGE_URL_HEAD + res.url
            this.audio.push({name:res.name,url:process.env.IMAGE_URL_HEAD + res.url,title:'',tips:'',image:''});
          })
      },
      onPageChange(val){
        this.nowPage = val;
        this.getList()
      },
      changeStatus(data){
        let msg;
        let status;
        if(data.status === 1){
          msg = '此操作将下架,是否继续?';
          status = 2
        }else{
          msg = '此操作将上架,是否继续?';
          status = 1
        }
        this.$confirm(msg, '提示', {
          confirmButtonText: '继续',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let json = {
            status:status
          };
          bannerChangeStatusApi(data.id,json).then(res=>{
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
            this.getList()
          })
        })

      },
      onSizeChange(val){
        this.nowPage = 1;
        this.limit = val;
        this.getList()
      },
      getList(){
        this.loading = true;
        let json = {
          limit: this.limit,
          page: this.nowPage
        };
        if(this.searchFrom.status !== '-1'){
          json.status = this.searchFrom.status
        }
        getBannerListApi(this.searchFrom.type,json).then(res=>{
          if (res) {
            this.bannerList = res.list;
            this.total = res.total;
            this.loading = false;
          }
        })
      },
      edit(data){
        this.dialog.form.id = data.id;
        this.dialog.title = '编辑';
        getBannerDetailApi(data.id).then((res)=>{
          this.dialog.form.title = res.title
          this.dialog.form.sub_title = res.sub_title
          this.dialog.form.link = res.link
          this.imageList = [{name:res.url,url:res.url}]
          this.dialog.show = true;
          this.uploadShow = false;
          this.audio=[];
        })
      },
      add(){
        this.dialog.show = true;
        this.dialog.form.id = '';
        this.dialog.title = '新增';
        this.dialog.form.title = ''
        this.dialog.form.sub_title = ''
        this.dialog.form.link = '';
        this.imageList = [];
        this.uploadShow = true;
        this.audio=[];
      },
      del(data){
        this.$confirm('此操作将删除该?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delBannerApi(data.id).then(res=>{
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList()
          });
        });
      },
      handleAvatarSuccess(res) {
        this.imageList = [{name:res.data.url,url:process.env.IMAGE_URL_HEAD + res.data.url}]
        this.dialog.form.url = process.env.IMAGE_URL_HEAD + res.data.url
      },
      beforeAvatarUpload(){
        this.uploadShow = false
      },
      handleRemove(){
        this.uploadShow = true
      },
      sub(){
        this.$refs['form'].validate((valid) => {
          if(valid){
            let dia = this.dialog;
            if(dia.form.id){
              let json = {
                title:dia.form.title,
                link:dia.form.link,
                sub_title:dia.form.sub_title
              };
              if (this.imageList.length > 0) {
                  json.url = this.imageList[0].url
              }
              this.$confirm('确认修改', '提示', {
                confirmButtonText: '继续',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                editBannerApi(dia.form.id,json).then(()=>{
                  this.$message({
                    type: 'success',
                    message: '修改成功!'
                  });
                  dia.show = false;
                  this.getList()
                })
              })

            }else{
              let json = {
                title:dia.form.title,
                link:dia.form.link,
                sub_title:dia.form.sub_title
              };
              if (this.imageList.length > 0) {
                json.url = this.imageList[0].url
              }
              this.$confirm('确认添加', '提示', {
                confirmButtonText: '继续',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                addBannerApi(this.searchFrom.type,json).then(()=>{
                  this.$message({
                    type: 'success',
                    message: '添加成功!'
                  });
                  dia.show = false;
                  this.getList()
                })
              })

            }
          }
        })
      },
      moveUp(index){
        let list = this.bannerList;
        this.sort(list[index].id,list[index-1].id);
      },
      moveDown(index){
        let list = this.bannerList;
        this.sort(list[index + 1].id,list[index].id);
      },
      sort(upId, downId){
        this.loading = true;
        moveApi(upId,downId).then(()=>{
          this.loading = false;
          this.getList();
        })
      },
      uploadFile(a) {
        this.loading = true;
        this.$store.dispatch('setProgress',{type:'new',id:a.file.uid});
        this.fileUid = a.file.uid;
        uploadFileApi({file:a.file,type:'local'}).then(res=>{
          this.imageList = [{name:res.url,url:process.env.IMAGE_URL_HEAD + res.url}]
          this.loading = false;
          this.$message({
            type: 'success',
            message: '上传成功!'
          });
        }).catch(()=>{
          this.loading = false;
        })
      }
    }
  }
</script>
<style @scoped lang="less">
  .banner{
    .head{
      /*padding: 5px;*/
      margin-bottom: 10px;
    }
    width: 100%;
    padding: 20px 0;
    .page-div{
      text-align: center;
      padding-top: 20px
    }
  }
  .short-banner {
    width: 50px;
  }
  .clear-both{
  &:after{
     content: '';
     display: block;
     clear: both;
   }
  }
  .size {
    color: #666;
    font-size: 14px;
  }
</style>
<style>
  .disabled .el-upload--picture-card {
    display: none !important;
  }
</style>