远山行小程序后端管理心得总结

简介: 远山行小程序后端管理心得总结

1.跨域


image.png

配置文件 重要的是devServer节点配置


module.exports = {
  pages: {
    index: {
      //入口
      entry: 'src/main.js',
    },
  },
  lintOnSave:false, //关闭语法检查
  //开启代理服务器(方式一)
  /* devServer: {
    proxy: 'http://localhost:5000'
  }, */
  //开启代理服务器(方式二)
  devServer: {
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:7001',
        // pathRewrite:{'^/atguigu':''},
        // ws: true, //用于支持websocket
        // changeOrigin: true //用于控制请求头中的host值
      },
      '/demo': {
        target: 'http://localhost:5001',
        pathRewrite:{'^/demo':''},
        // ws: true, //用于支持websocket
        // changeOrigin: true //用于控制请求头中的host值
      }
    }
  }
}
这里有两个坑target: 'http://127.0.0.1:7001', 这个地址是药请求的
如果想跨域的话就自己请求自己的地址
axios.post('http://localhost:8080/api/admin/login', {
          'username': '19568269510',
          'password': '123456'
        })
大坑 如果请求自己的地址的话一定要看一下 自己的路由地址是localhost 还是127.0.0.1


2.设置登陆以后返回的token

     

window.sessionStorage.setItem('token', res.data.data.token)


3.设置请求的时候的请求头字段


// 配置请求拦截器 增加token
axios.interceptors.request.use((config) => {
  console.log(config);
  config.headers.Authorization=window.sessionStorage.getItem('token')
  return config
})


4.导航路由跳转判断卡顿逻辑函数


router.beforeEach(function (to, from, next) {
  // console.log(to);
  console.log(from);
  if (window.sessionStorage.getItem('token')) {
    next()
  } else {
    if(to.name=='login'){
      next()
    }else{
      next('/login')
    }
  }
})

导航菜单全部展开

图片跟表单信息一块上传

https://blog.csdn.net/qq_41875271/article/details/120179374?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_antiscanv2&utm_relevant_index=2


7.多文件上传获取文件的数组

handleRemove(file, fileList) {
                this.$message.error("删除成功!");
                this.ruleForm2.schoolLicence = []
                fileList.map(x => {
                    this.ruleForm2.schoolLicence.push(x.raw)
                })
            },


文件上传和删除 都写成这样 快速直接


!!!上传的是得到的文件的raw


:on-change//上传
:auto-upload="false"//关闭自动上传
:on-remove//删除


8.上传图片同表单


<el-form-item label="上传学校证件" prop="schoolLicence">
            <div class="shousuo">
                <el-upload action="uploadAction" list-type="picture-card" :on-preview="handlePictureCardPreview"
                    :on-remove="handleRemove" :show-file-list="true" name="img" ref="upload" :data="form"
                    accept="image/png,image/gif,image/jpg,image/jpeg" :headers="headers" :on-exceed="handleExceed"
                    :auto-upload="false" :on-error="uploadError" :before-upload="handleBeforeUpload"
                    :on-change="changeFile">
                    <i class="el-icon-plus"></i>
                </el-upload>
            </div>
        </el-form-item>

  data 定义

schoolLicence: [], //学校认证


表单规则给上 不让他为空(这个是自定义的)


handleBeforeUpload(file) {
                console.log('按钮', this.titleMap)
                if (!(file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/jpg' || file
                        .type ===
                        'image/jpeg')) {
                    this.$notify.warning({
                        title: '警告',
                        message: '请上传格式为image/png, image/gif, image/jpg, image/jpeg的图片'
                    })
                }
                let size = file.size / 1024 / 1024 / 2
                if (size > 2) {
                    this.$notify.warning({
                        title: '警告',
                        message: '图片大小必须小于2M'
                    })
                }
            },
            handleBeforeUpload(file) {
                console.log('按钮', this.titleMap)
                if (!(file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/jpg' || file
                        .type ===
                        'image/jpeg')) {
                    this.$notify.warning({
                        title: '警告',
                        message: '请上传格式为image/png, image/gif, image/jpg, image/jpeg的图片'
                    })
                }
                let size = file.size / 1024 / 1024 / 2
                if (size > 2) {
                    this.$notify.warning({
                        title: '警告',
                        message: '图片大小必须小于2M'
                    })
                }
            },
            //图片上传超过数量限制
            handleExceed(files, fileList) {
                this.$message.error("上传图片不能超过1张!");
            },
            // 删除上传学校证件的函数
            handleRemove(file, fileList) {
                this.$message.error("删除成功!");
                this.ruleForm2.schoolLicence = []
                fileList.map(x => {
                    this.ruleForm2.schoolLicence.push(x.raw)
                })
            },
            // 删除上传学校环境的函数
            handleRemoveschoolPic(file, fileList) {
                this.$message.error("删除成功!");
                this.ruleForm2.schoolPic = []
                fileList.map(x => {
                    this.ruleForm2.schoolPic.push(x.raw)
                })
            },
            // 图片上传失败时
            uploadError() {
                this.$message.error("图片上传失败!");
            },
            //预览图片
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            //文件改变时 on-change方法 ,fileList[0].raw 就是传给后端的值
            //filelist这个对象里面有很多属性,我们上传文件时,实际上传的是filelist列表中每一项的raw,只有raw可以正常上传, 获取到文件后,需要定义变量保存文件。所以需要获取filelist中的raw进行保存。
            //这里我用的formdata上传多文件,console打印formdata,文件在控制台中显示的格式为binary。
            changeFile(file, fileList) {
                this.ruleForm2.schoolLicence = []
                fileList.map(x => {
                    this.ruleForm2.schoolLicence.push(x.raw)
                })
            },


上传


uploadFileFun(formName) {
                console.log(this.uploadFiles);
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let fd = new FormData();
                        fd.append('rname', this.ruleForm2.rname);
                        fd.append('rphone', this.ruleForm2.rphone);
                        fd.append('password', this.ruleForm2.password);
                        fd.append('bname', this.ruleForm2.bname);
                        fd.append('place', this.ruleForm2.place);
                        fd.append('around', this.ruleForm2.around);
                        fd.append('resource', this.ruleForm2.resource);
                        fd.append('representor', this.ruleForm2.representor);
                        fd.append('loginLicence', this.ruleForm2.uploadFiles[0]);
                        fd.append('licence', this.ruleForm2.uploadFiles[1]);
                        fd.append('function', this.ruleForm2.function);
                        let config = {
                            headers: {
                                'Content-Type': 'multipart/form-data'
                            }
                        }
                        // 根据goodsID判断是编辑提交还是新增提交,主要针对新增编辑使用同一个弹框
                        this.$http.post("/api/admin/bussiness/add", fd, config).then(res => {
                            console.log(res);
                            if (res.data.data.msg === "添加成功") {
                                this.dialogFormVisible = false
                                this.$message({
                                    message: '添加企业会员成功',
                                    type: 'success'
                                });
                                //新增完清空表单内容
                                setTimeout(() => {
                                    this.resetForm('ruleForm2')
                                }, 200)
                                this.reload()
                            } else {
                                this.$message.error('数据添加失败请联系管理员');
                            }
                        }).catch(res => {
                            console.log(res)
                        })
                    }
                })
            },

9.表单上传


10.JS 文件流和base64互转


// base64转文件流
base64toFile (dataurl, filename = 'file') {
    let arr = dataurl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let suffix = mime.split('/')[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], `${filename}.${suffix}`, {
        type: mime
    })
},
// 文件流转base64
filetoBase64 (file) {
   let reader = new FileReader(); //实例化文件读取对象
    reader.readAsDataURL(file); //将文件读取为 DataURL,也就是base64编码
    reader.onload = (e)=> { //文件读取成功完成时触发
        let dataURL = e.target.result; //获得文件读取成功后的DataURL,也就是base64编码
        // console.log(dataURL);
    }
},
目录
相关文章
|
1月前
|
存储 小程序 JavaScript
电商小程序09活动管理
电商小程序09活动管理
|
3月前
|
人工智能 小程序 前端开发
小程序模版|报名管理服务源码
小程序模版|报名管理服务源码
|
1月前
|
弹性计算 前端开发 小程序
微信小程序上传文件至阿里云OSS直传(java后端签名+前端直传)
当前的通用文件上传方式是通过前端上传到服务器,再由服务器转存至对象存储。这种方式在处理小文件时效率尚可,但大文件上传因受限于服务器带宽,速度较慢。例如,一个100MB的文件在5Mbps带宽的阿里云ECS上上传至服务器需160秒。为解决此问题,可以采用后端签名的方式,使微信小程序直接上传文件到阿里云OSS,绕过服务器中转。具体操作包括在JAVA后端引入相关依赖,生成签名,并在微信小程序前端使用这个签名进行文件上传,注意设置正确的请求头和formData参数。这样能提高大文件上传的速度。
|
5月前
|
小程序 JavaScript
搞定 Mobx 小程序状态管理
搞定 Mobx 小程序状态管理
156 0
|
5天前
|
小程序 开发者
体验版小程序为何无法访问云端服务器后端接口(请求失败...(已完美解决附加图片))?
体验版小程序为何无法访问云端服务器后端接口(请求失败...(已完美解决附加图片))?
12 0
|
12天前
|
小程序 前端开发 JavaScript
小程序全栈开发:前端与后端的完美结合
【4月更文挑战第12天】本文介绍了小程序全栈开发,涵盖前端和后端的关键点。前端使用WXML和WXSS进行页面结构和样式设计,JavaScript处理逻辑及组件使用;后端采用Node.js等语言处理业务逻辑、数据库设计和API接口开发。前端与后端通过数据交互实现结合,采用前后端分离模式,支持跨平台运行。调试测试后,提交微信审核并上线运营。掌握前端后端结合是小程序成功的关键。
|
2月前
|
小程序 Java 数据挖掘
Java校园智慧管理云平台源码 小程序+android电子班牌系统
智慧校园技术架构 ❀后端:Java ❀框架:springboot ❀前端页面:vue +element-ui ❀小程序:小程序原生开发 ❀电子班牌:Java Android
36 0
|
3月前
|
小程序 Java 大数据
智慧校园管理平台源码(含教师端、家长端、学生端小程序)
智慧校园以互联网为基础,“大数据+云服务+云计算”为核心,融合校园教学、管理、生活软硬件平台,定义智慧校园新生活。智慧校园管理平台管理者、教师、学生、家长提供一站式智慧校园解决方案,实现校园管理智能、.校园生活一体化、校园设施数字化、课堂教学生动化、家校沟通无缝化。集成智能硬件及第三方服务,面向学校、教师、家长、学生,将校内外管理、教学等信息资源进行整合,利用微信端的交互系统实现家校互联。
102 1
|
3月前
|
存储 人工智能 小程序
Java智慧校园小程序+电子班牌+人脸识别管理系统源码
移动教师端、家长端,可完成校级信息向家长端推送,家长阅读通知、通知等实时显示,解决学校与家长之间的信息沟通问题,实现家校互通。
28 1
|
3月前
|
JSON 小程序 前端开发
史上最详细微信小程序授权登录与后端SprIngBoot交互操作说明!!!附源代码
史上最详细微信小程序授权登录与后端SprIngBoot交互操作说明!!!附源代码
379 2

热门文章

最新文章