index.js 1.76 KB
/*
 * @Descripttion: 定义一个Request类,用于封装请求高德地图API的方法
 * @version: 1.0.0
 * @Author: limin
 * @Date: 2023-07-04 17:25:31
 * @LastEditors: limin
 * @LastEditTime: 2023-08-24 09:56:28
 * @FilePath: \glsx-common-sdk\src\request\index.js
 */
import { curl } from './helper'
import RequestMethods from './requestMethods'
import { response as res, sign as defaultSign } from './default'
import { isMiniprogram } from '../helper/index'
class Request {
  // 构造函数,接收key和secret作为参数,如果没有传入,则使用默认值
  constructor({ response = {}, request, headers = {}, alert, isMP = isMiniprogram, logoutCallback } = {}) {
    this.response = Object.assign({}, res, response)
    this.request = request
    this.headers = headers
    this.alert = alert
    this.isMiniprogram = +isMP
    this.logoutCallback = logoutCallback
  }
  /**封装的GET方法 */
  get(data, noSignAttr, returnAll) {
    // 调用request方法,传入请求方式为GET,请求路径为method,请求数据为data
    return this.fetch({ method: RequestMethods.GET, data, noSignAttr, returnAll })
  }
  post(data, noSignAttr, returnAll) {
    // 调用request方法,传入请求方式为GET,请求路径为method,请求数据为data
    return this.fetch({ method: RequestMethods.POST, data, noSignAttr, returnAll })
  }
  /**封装的请求方法 */
  fetch({ data, noSignAttr = [], method = RequestMethods.POST, returnAll = false }) {
    // return curl({ data, method, request: this.request, headers: this.headers, noSignAttr, returnAll, logoutCallback: this.logoutCallback, isMiniprogram: this.isMiniprogram, alert: this.alert, initResponse: this.response })
    return curl.call(this, data, method, noSignAttr, returnAll)
  }
}
export default Request