import Taro , { Component } from '@tarojs/taro' ; import { ISMOCK , MAINHOST } from '../config' ; import { commonParame , requestConfig } from '../config/requestConfig' ; import Tips from './tips' ; declare type Methods = "GET" | "OPTIONS" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT" ; declare type Headers = { [ key ] : string ; } ; declare type Datas = { method : Methods ; [ key ] : any ; } ; interface Options { url : string ; host ? : string ; method ? : Methods ; data ? : Datas ; header ? : Headers ; } export class Request { static loginReadyPromise : Promise < any > = Promise . resolve ( ) ; static isLogining : boolean = false ; static apiLists : { [ key ] : ( ) => any ; } = { } ; static token : string = '' ; static param { Options | string } opts param { Datas } data returns { Options } memberof static conbineOptions ( opts , data , method ) { typeof opts === 'string' && ( opts = { url : opts } ) ; return { data : { ... commonParame , ... opts . data , ... data } , method : opts . method || data . method || method || 'GET' , url : `${ opts . host || MAINHOST }${ opts . url }` } ; } static getToken ( ) { ! this . token && ( this . token = Taro . getStorageSync ( 'token' ) ) ; return this . token ; } static param { Options } opts static async request ( opts ) { const res = await Taro . request ( opts ) ; if ( ISMOCK ) { return res . data ; } if ( res . data . code === 99999 ) { await this . login ( ) ; return this . request ( opts ) ; } if ( res . data ) { return res . data ; } const d = { ... res . data , err : ( res . data && res . data . msg ) || `网络错误～` } ; Tips . toast ( d . err ) ; throw new Error ( d . err ) ; } static returns memberof static login ( ) { if ( ! this . isLogining ) { this . loginReadyPromise = this . onLogining ( ) ; } return this . loginReadyPromise ; } static returns memberof static onLogining ( ) { this . isLogining = true ; return new Promise ( async ( resolve , reject ) => { const { code } = await Taro . login ( ) ; const { data } = await Taro . request ( { url : `${ MAINHOST }${ requestConfig . loginUrl }` , data : { code : code } } ) ; if ( data . code !== 0 || ! data . data || ! data . data . token ) { reject ( ) ; return ; } Taro . setStorageSync ( 'token' , data . data . token ) ; this . isLogining = false ; resolve ( ) ; } ) ; } static param { ( Options | string ) } opts returns memberof static creatRequests ( opts ) { console . log ( opts ) ; return async ( data = { } , method = "GET" ) => { const _opts = this . conbineOptions ( opts , data , method ) ; const res = await this . request ( _opts ) ; return res ; } ; } static returns memberof static getApiList ( requestConfig ) { if ( ! Object . keys ( requestConfig ) . length ) return { } ; Object . keys ( requestConfig ) . forEach ( ( key ) => { this . apiLists [ key ] = this . creatRequests ( requestConfig [ key ] ) ; } ) ; return this . apiLists ; } } const Api = Request . getApiList ( requestConfig ) ; Component . prototype . $api = Api ; export default Api as any ;