vue跨域请求后端加密接口

vue跨域请求后端加密接口

1、设置跨域代理

首先在config/index.js 里面找到proxyTable :{} ,然后在里面加入
在main.js设置一个基础路径:axios.defaults.baseURL=”/api”,这样调用接口的时候可以不写api,直接写/接口名称即可。
例如:let _url4=”/dictionaryTypes”

“/api”:{
target: ‘http://47.104.218.122:8087’,
changeOrigin: true,
pathRewrite: {
‘^/api’: ‘/’
}
}

请求不到数据时

在axios.get(url,{})参数中加如下

1、emulateJSON :true,关键性作用

axios封装代码

import Vue from "vue";
import axios from 'axios'
import crypto from 'crypto'
import moment from 'moment'
import { token } from './config.js'
import { BASE_URL,ACCOUNT,TOKEN_URL } from './config.js'
export default {
 async ajaxGet( url, data = {}, method = 'get') {
    var c = this;
        var result = c.re( url, data, method);
        return result.then(function(value){
            if( value.success == 'false' && value.error_code == 'error_no_auth') {
            }
            return value;
        });
  },
  re( url, data, method){
    var c = this;
    var token_cookies = c.readToken();
    if( token_cookies) {
        var result = c.getContent( token_cookies, url, data, method);
    }
    return result;
},
  readToken() {
        var token = '';
        return token.toString();
    },
  getContent:function(token, url, data, method){
    var that=this;
    var headers={};
      headers['Authorization']='ACCESS_TOKEN='+token;
      var geheader = that.generatePgrouterHeader( url, method);
      var headers =Object.assign(headers, geheader); 
      return axios.get(url,{
              withCredentials:true,
              dataType: 'json',
              method: method,
              params: data,
              emulateJSON :true,
              headers : headers
          }).then(function(value) {
             console.log(value);
              return value;
          });
   },
  generatePgrouterHeader(api, method) {
        const time = moment().unix() * 1000;
        const key = '';
        const md5 = crypto.createHash('md5');
        const signString = 'app' + method.toUpperCase() + api +time + key;
        const pgsign = 'app:' + md5.update(signString, 'utf8').digest('hex');
        return {pgdate: time, pgsign: pgsign};
    },
   decrypt(data){
        const key= ''
        const iv= ''
        var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv),
        buffer = Buffer.concat([
            decipher.update(Buffer.from(data, 'base64')),
            decipher.final()
        ]);
        string = buffer.toString();

        return string;
    },
}

你可能感兴趣的:(vue,js)