• vue+ElementUI 实现管理端照片墙(或广告位)效果


    场景:在管理端配置照片墙或者广告位

    先上效果图: 

    步骤:

    首先布局html结构:

    1. <template>
    2. <div>
    3. <el-form :inline="true" v-loading="upLoading" element-loading-text="拼命上传中" element-loading-spinner="el-icon-loading">
    4. <el-form-item v-for="(item,index) in homePosterList" :key="index" style="width:49%">
    5. <el-image :src="item.src">el-image>
    6. <el-upload
    7. :action="upUrl" :limit="2"
    8. :show-file-list="false"
    9. :on-success="function (res,file) {return handleAvatarSuccess(res,file,index)}"
    10. :before-upload="beforeAvatarUpload" :data="{type:'2'}">
    11. <el-button type="success" plain>重新上传el-button>
    12. el-upload>
    13. <el-button class="deletePoster" @click.prevent="removeDomain(item)">删除el-button>
    14. el-form-item>
    15. <el-form-item style="width:100%;">
    16. <el-button type="primary" @click="submitForm('homePosterList')">提交el-button>
    17. <el-button @click="addDomain">添加广告位el-button>
    18. el-form-item>
    19. el-form>
    20. div>
    21. template>

    我是放在一个表单中的,也可以单独放出来

    v-loading="upLoading"  这是加了个上传图片的时候加载效果

    element-loading-text="拼命上传中"  加载的文字

    element-loading-spinner="el-icon-loading"  加载的图标

    v-for="(item,index) in homePosterList"  先循环从接口得到的一个数组

     js模块:

    1. import { goodsSort,setUp } from "@/api/sysManager";
    2. export default {
    3. data() {
    4. return {
    5. upLoading:false,
    6. upUrl:process.env.BASE_API + '/common/upload', //图片上传地址,
    7. homePosterId:'',
    8. homePosterList:[]
    9. }
    10. },
    11. created(){
    12. this.getAppletHomePoster()
    13. },
    14. methods: {
    15. getAppletHomePoster(){
    16. goodsSort({"conditions":[{"type":"code","value":"ad_user_index"}]}).then(res=>{
    17. if(res.status==200&&res.data.length>0){
    18. this.homePosterId=res.data[0].id
    19. var imgListArr = JSON.parse(res.data[0].value)
    20. imgListArr.images.map(item=>{
    21. let obj={}
    22. obj.src=item
    23. console.log('obj',obj)
    24. this.homePosterList.push(obj)
    25. })
    26. console.log('首页广告位的数据:',this.homePosterList)
    27. }
    28. })
    29. },
    30. beforeAvatarUpload(file){//上传时
    31. const isJPG = (file.type==='image/png' || file.type==='image/jpeg' || file.type==='image/gif')
    32. const isLt2M = file.size / 1024 / 1024 < 2;
    33. if (!isJPG) {
    34. this.$message.error('请上传jpg/png/jif格式的图片')
    35. return
    36. }
    37. if (!isLt2M) {
    38. this.$message.error('请上传小于 2MB 的图片!');
    39. return
    40. }
    41. this.upLoading=true
    42. },
    43. handleAvatarSuccess(res, file, index){//上传成功时
    44. console.log('上传成功res',res)
    45. console.log('上传成功file',file)
    46. console.log('上传成功index',index)
    47. this.homePosterList[index].src=process.env.VUE_APP_IMG+file.response.data[0].path
    48. this.upLoading=false
    49. },
    50. submitForm(formName) {//提交
    51. var imgJoin=[]
    52. this.homePosterList.map(item=>{
    53. imgJoin.push(item.src)
    54. if(item.src==""){
    55. this.$message.error('请先完善新增广告位图片!');
    56. return
    57. }
    58. })
    59. setUp(
    60. {"model":{
    61. "id":this.homePosterId,
    62. "value":JSON.stringify({"images": imgJoin})
    63. }
    64. }).then(res=>{
    65. if(res.status==200){
    66. this.$message.success('添加成功!');
    67. this.homePosterList=[]
    68. this.getAppletHomePoster()
    69. }
    70. })
    71. },
    72. removeDomain(item) {//删除
    73. var index = this.homePosterList.indexOf(item)
    74. if (index !== -1) {
    75. this.homePosterList.splice(index, 1)
    76. }
    77. },
    78. addDomain() {//添加
    79. if(this.homePosterList.length<10){
    80. this.homePosterList.push({src:''});
    81. }else{
    82. this.$message.error('最多只能添加10个广告位');
    83. }
    84. }
    85. }
    86. }

    完整代码: 

    1. <script>
    2. import { goodsSort,setUp } from "@/api/sysManager";
    3. export default {
    4. data() {
    5. return {
    6. upLoading:false,
    7. upUrl:process.env.BASE_API + '/common/upload', //图片上传地址,
    8. homePosterId:'',
    9. homePosterList:[]
    10. }
    11. },
    12. created(){
    13. this.getAppletHomePoster()
    14. },
    15. methods: {
    16. getAppletHomePoster(){
    17. goodsSort({"conditions":[{"type":"code","value":"ad_user_index"}]}).then(res=>{
    18. if(res.status==200&&res.data.length>0){
    19. this.homePosterId=res.data[0].id
    20. var imgListArr = JSON.parse(res.data[0].value)
    21. imgListArr.images.map(item=>{
    22. let obj={}
    23. obj.src=item
    24. console.log('obj',obj)
    25. this.homePosterList.push(obj)
    26. })
    27. console.log('首页广告位的数据:',this.homePosterList)
    28. }
    29. })
    30. },
    31. beforeAvatarUpload(file){//上传时
    32. const isJPG = (file.type==='image/png' || file.type==='image/jpeg' || file.type==='image/gif')
    33. const isLt2M = file.size / 1024 / 1024 < 2;
    34. if (!isJPG) {
    35. this.$message.error('请上传jpg/png/jif格式的图片')
    36. return
    37. }
    38. if (!isLt2M) {
    39. this.$message.error('请上传小于 2MB 的图片!');
    40. return
    41. }
    42. this.upLoading=true
    43. },
    44. handleAvatarSuccess(res, file, index){//上传成功时
    45. console.log('上传成功res',res)
    46. console.log('上传成功file',file)
    47. console.log('上传成功index',index)
    48. this.homePosterList[index].src=process.env.VUE_APP_IMG+file.response.data[0].path
    49. this.upLoading=false
    50. },
    51. submitForm(formName) {//提交
    52. var imgJoin=[]
    53. this.homePosterList.map(item=>{
    54. imgJoin.push(item.src)
    55. if(item.src==""){
    56. this.$message.error('请先完善新增广告位图片!');
    57. return
    58. }
    59. })
    60. setUp(
    61. {"model":{
    62. "id":this.homePosterId,
    63. "value":JSON.stringify({"images": imgJoin})
    64. }
    65. }).then(res=>{
    66. if(res.status==200){
    67. this.$message.success('添加成功!');
    68. this.homePosterList=[]
    69. this.getAppletHomePoster()
    70. }
    71. })
    72. },
    73. removeDomain(item) {//删除
    74. var index = this.homePosterList.indexOf(item)
    75. if (index !== -1) {
    76. this.homePosterList.splice(index, 1)
    77. }
    78. },
    79. addDomain() {//添加
    80. if(this.homePosterList.length<10){
    81. this.homePosterList.push({src:''});
    82. }else{
    83. this.$message.error('最多只能添加10个广告位');
    84. }
    85. }
    86. }
    87. }
    88. script>
    89. <style scoped lang="scss">
    90. ::v-deep .el-form-item__content{display:flex;align-items:center;}
    91. ::v-deep .el-form-item__content .el-image{margin-right:30px;width:300px;height:140px;}
    92. ::v-deep .deletePoster{margin-left:30px;}
    93. style>

    以上只是写了一个小功能,很多不完善的地方,希望各位大神多多指教 

  • 相关阅读:
    信通传媒携各家虚商领袖,走进九牛汇
    生成式人工智能在软件开发过程现代化中的作用
    Redis主从复制的核心原理
    机器学习(一):概述
    Sql server 使用DBCC Shrinkfile 收缩日志文件
    微信小程序 java在线租房-房屋租赁系统springboot
    ARP欺骗攻击
    oracle提高执行更新或新增的SQL效率
    【LLM-RAG】知识库问答 | 检索 | embedding_rag embedding
    JavaScript设计模式(三) 迭代器模式 发布-订阅模式
  • 原文地址:https://blog.csdn.net/qq_17211063/article/details/127634987