mock

导入数据

  1. 打开swigger => network => swagger.json 下载到本地
  2. 项目设置
  3. 导入数据

webpack proxy 内使用

    '/api/latest/mock': {
      target: "http://127.0.0.1:4523/mock/366182/",
      changeOrigin: true,
       // 用来查找转发后的地址
      onProxyRes: function(proxyRes, req, res) {
        console.log(proxyRes)
      }
    },

测试接口

注册公共脚本

var cryptoJs = require("crypto-js");
const HmacSHA256 = cryptoJs.HmacSHA256;
const Base64 = cryptoJs.enc.Base64;
const idKey = {
    accessKeyId: 'xxx', // 根据项目固定
    secretKey: 'xxx', // 根据项目固定
  }

const globalEncodeURIComponent = encodeURIComponent

const jsonStringify = (val) => (val instanceof Object ? JSON.stringify(val) : val)

const signatureConfig = {
    signatureMethod: 'HMAC-SHA256',
    signatureVersion: '1.0',
}

const encAndRep = (key) => {
    if (key === '' || key === undefined || key === null) {
        return ''
    }
    return globalEncodeURIComponent(key)
        .replace(/\+/g, '%20')
        .replace(/\*/g, '%2A')
        // .replace("%7E", "~")
        .replace(/'/g, '%27')
        .replace(/\(/g, '%28')
        .replace(/!/g, '%21')
        .replace(/\)/g, '%29')
}

/**
 * 签名算法
 */
const signatureIt = (jsonObject, secret, method) => {
    var meth = method
    var str
    var arr = []

    for (var key in jsonObject) {
        if (key === 'access_token' || key === 'parameter') { continue }
        if ({}.hasOwnProperty.call(jsonObject, key)) {
            arr.push(encAndRep(key) + '=' + encAndRep(jsonStringify(jsonObject[key])))
        }
    }
    var str2 = arr.sort().join('&')

    str = meth + '&' + '%2F' + '&' + encAndRep(str2).replace(/\~/g, '%7E')
    str = Base64.stringify(HmacSHA256(str, secret + '&'))
    return str
}

function generate(opt, httpMethod, accessKeyId, accessKeySecret, timestamp = new Date().getTime()) {
    const param = {
        ...signatureConfig,
        accessKeyId,
        timestamp,
    }
    for (let key in opt) {
        if (Object.prototype.hasOwnProperty.call(opt, key)) {
            if (opt[key] !== undefined && opt[key] !== null) {
                param[key] = opt[key]
            }
        }
    }
    param.signKey = JSON.stringify(opt.parameter)
    param.signature = signatureIt(param, accessKeySecret, httpMethod)

    return param
}

function stringify(param) {
    return Object.keys(param)
        .map((key) => {
            return `${key}=${globalEncodeURIComponent(jsonStringify(param[key]))}`
        }).join('&')
}

let body = {
    parameter:JSON.parse(pm.request.body.raw),
    action:pm.request.url.path[2],
    version:'1.0'
}
// 重置raw
pm.request.body.raw = generate(body, 'POST', idKey.accessKeyId, idKey.secretKey)

waker
247 声望9 粉丝