123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- import buildURL from '../helpers/buildURL'
- import buildFullPath from './buildFullPath'
- import { isBoolean } from '../utils'
- import {ACCESS_TOKEN} from '@/common/util/constants.js'
- export default class Request {
- config = {
- baseUrl: '',
- header: {},
- method: 'GET',
- dataType: 'json',
-
- responseType: 'text',
-
- custom: {},
-
- timeout: 30000,
-
-
- sslVerify: true,
-
-
- withCredentials: false
-
- }
-
- interceptor = {
-
- request: (cb) => {
- if (cb) {
- this.requestBeforeFun = cb
- }
- },
-
- response: (cb, ecb) => {
- if (cb) {
- this.requestComFun = cb
- }
- if (ecb) {
- this.requestComFail = ecb
- }
- }
- }
- requestBeforeFun = (config) => {
- return config
- }
- requestComFun = (response) => {
- return response
- }
- requestComFail = (response) => {
- return response
- }
-
- validateStatus(statusCode) {
- return statusCode === 200
- }
-
- setConfig(f) {
- this.config = f(this.config)
- }
-
- async request(options = {}) {
- return new Promise((resolve, reject) => {
- options.baseUrl = this.config.baseUrl
- options.dataType = options.dataType || this.config.dataType
-
- options.responseType = options.responseType || this.config.responseType
-
-
- options.timeout = options.timeout || this.config.timeout
-
-
- options.withCredentials = isBoolean(options.withCredentials) ? options.withCredentials : this.config.withCredentials
-
- options.url = options.url || ''
- options.data = options.data || {}
- options.params = options.params || {}
- options.header = {...this.config.header, ...(options.header || {})}
- options.method = options.method || this.config.method
- options.custom = {...this.config.custom,...(options.custom || {})}
-
- options.sslVerify = options.sslVerify === undefined ? this.config.sslVerify : options.sslVerify
-
- options.getTask = options.getTask || this.config.getTask
- let next = true
- const cancel = (t = 'handle cancel', config = options) => {
- const err = {
- errMsg: t,
- config: config
- }
- reject(err)
- next = false
- }
- const handleRe = {...this.requestBeforeFun(options, cancel)}
- const _config = {...handleRe}
- if (!next) return
- const requestTask = uni.request({
- url: buildURL(buildFullPath(_config.baseUrl, _config.url), _config.params),
- data: _config.data,
- header: _config.header,
- method: _config.method,
-
- timeout: _config.timeout,
-
- dataType: _config.dataType,
-
- responseType: _config.responseType,
-
-
- sslVerify: _config.sslVerify,
-
-
- withCredentials: _config.withCredentials,
-
- complete: (response) => {
- response.config = handleRe
- if (this.validateStatus(response.statusCode)) {
- response = this.requestComFun(response)
- resolve(response)
- } else {
- response = this.requestComFail(response)
- reject(response)
- }
- }
- })
- if (handleRe.getTask) {
- handleRe.getTask(requestTask, handleRe)
- }
- })
- }
- get(url, options = {}) {
- return this.request({
- url,
- method: 'GET',
- ...options
- })
- }
- post(url, data, options = {}) {
- return this.request({
- url,
- data,
- method: 'POST',
- ...options
- })
- }
-
- put(url, data, options = {}) {
- return this.request({
- url,
- data,
- method: 'PUT',
- ...options
- })
- }
-
-
- delete(url, data, options = {}) {
- return this.request({
- url,
- data,
- method: 'DELETE',
- ...options
- })
- }
-
-
- connect(url, data, options = {}) {
- return this.request({
- url,
- data,
- method: 'CONNECT',
- ...options
- })
- }
-
-
- head(url, data, options = {}) {
- return this.request({
- url,
- data,
- method: 'HEAD',
- ...options
- })
- }
-
-
- options(url, data, options = {}) {
- return this.request({
- url,
- data,
- method: 'OPTIONS',
- ...options
- })
- }
-
-
- trace(url, data, options = {}) {
- return this.request({
- url,
- data,
- method: 'TRACE',
- ...options
- })
- }
-
- upload(url, {
-
- files,
-
-
- fileType,
-
- filePath,
- name,
-
- file,
-
- header = {},
- formData = {},
- custom = {},
- params = {},
- getTask
- }) {
- return new Promise((resolve, reject) => {
- let next = true
- const globalHeader = {...this.config.header}
- delete globalHeader['content-type']
- delete globalHeader['Content-Type']
- const pubConfig = {
- baseUrl: this.config.baseUrl,
- url,
-
- fileType,
-
- filePath,
- method: 'UPLOAD',
- name,
- header: {...globalHeader, ...header},
- formData,
- params,
- custom: {...this.config.custom, ...custom},
- getTask: getTask || this.config.getTask
- }
-
- if (files) {
- pubConfig.files = files
- }
-
-
- if (file) {
- pubConfig.file = file
- }
-
- const cancel = (t = 'handle cancel', config = pubConfig) => {
- const err = {
- errMsg: t,
- config: config
- }
- reject(err)
- next = false
- }
- const handleRe = {...this.requestBeforeFun(pubConfig, cancel)}
- const _config = {
- url: buildURL(buildFullPath(handleRe.baseUrl, handleRe.url), handleRe.params),
-
- fileType: handleRe.fileType,
-
- filePath: handleRe.filePath,
- name: handleRe.name,
- header: handleRe.header,
- formData: handleRe.formData,
- complete: (response) => {
- response.config = handleRe
- try {
-
- if (typeof response.data === 'string') {
- response.data = JSON.parse(response.data)
- }
-
- } catch (e) {
- }
- if (this.validateStatus(response.statusCode)) {
- response = this.requestComFun(response)
- resolve(response)
- } else {
- response = this.requestComFail(response)
- reject(response)
- }
- }
- }
-
- if (handleRe.files) {
- _config.files = handleRe.files
- }
-
-
- if (handleRe.file) {
- _config.file = handleRe.file
- }
-
- if (!next) return
- const requestTask = uni.uploadFile(_config)
- if (handleRe.getTask) {
- handleRe.getTask(requestTask, handleRe)
- }
- })
- }
- download(url, options = {}) {
- return new Promise((resolve, reject) => {
- let next = true
- const pubConfig = {
- baseUrl: this.config.baseUrl,
- url,
- method: 'DOWNLOAD',
- header: {...this.config.header, ...(options.header || {})},
- params: options.params || {},
- custom: {...this.config.custom, ...(options.custom || {})},
- getTask: options.getTask || this.config.getTask
- }
- const cancel = (t = 'handle cancel', config = pubConfig) => {
- const err = {
- errMsg: t,
- config: config
- }
- reject(err)
- next = false
- }
- const handleRe = {...this.requestBeforeFun(pubConfig, cancel)}
- if (!next) return
- const requestTask = uni.downloadFile({
- url: buildURL(buildFullPath(handleRe.baseUrl, handleRe.url), handleRe.params),
- header: handleRe.header,
- complete: (response) => {
- response.config = handleRe
- if (this.validateStatus(response.statusCode)) {
- response = this.requestComFun(response)
- resolve(response)
- } else {
- response = this.requestComFail(response)
- reject(response)
- }
- }
- })
- if (handleRe.getTask) {
- handleRe.getTask(requestTask, handleRe)
- }
- })
- }
- }
|