import { SERVER_PROTOCOL , SERVER_API_ROOT_API , DEF_REQUEST_CONFIG } from '../config' ; import responseHandler from '@/utils/responseHandler' ; import { IReqData , IResError } from '@/interfaces/common' ; import { request } from '@tarojs/taro' ; import { isObservable , toJS } from 'mobx' ; returns export default function requestData < T = void > ( { api , params = { } , method = 'GET' } ) { return new Promise < T > ( ( resolve , reject ) => { const completeApi = SERVER_PROTOCOL + SERVER_API_ROOT_API + api ; const mergeData = Object . assign ( { } , DEF_REQUEST_CONFIG , params ) ; const requestParams = { } ; for ( let key in mergeData ) { if ( isObservable ( mergeData [ key ] ) ) { requestParams [ key ] = toJS ( mergeData [ key ] ) ; } else if ( typeof mergeData [ key ] === 'string' ) { requestParams [ key ] = mergeData [ key ] . trim ( ) ; } else { requestParams [ key ] = mergeData [ key ] ; } } const options = { url : completeApi , method , header : { 'Content-Type' : 'application/x-www-form-urlencoded' } , data : requestParams } ; request ( options ) . then ( responseHandler ) . then ( resolve ) . catch ( err => { console . log ( err ) ; reject ( err ) ; } ) ; } ) ; }