Skip to content

Commit

Permalink
request的实现优化,request的h5版本复用taro-h5的实现,同时通过接口参数,业务可以灵活选择实现机制
Browse files Browse the repository at this point in the history
  • Loading branch information
58liuyang committed Apr 28, 2024
1 parent 8c8345e commit 7b7ab65
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 288 deletions.
22 changes: 11 additions & 11 deletions packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import osChannelApi from './osChannelApi'
import { RequestTask } from './request'
// import { RequestTask } from './request'

// @ts-ignore
const syncAndRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: true }) || (target => target)
Expand Down Expand Up @@ -880,24 +880,24 @@ class AsyncToSyncProxy {
}

class HybridProxy {
private readonly useAxios: boolean
// private readonly useAxios: boolean
private readonly useOsChannel: boolean
private readonly cacheProxy: any
private readonly requestApi = 'request'
// private readonly requestApi = 'request'

constructor (useAxios: boolean, useOsChannel: boolean, nativeApi: NativeApi) {
this.useAxios = useAxios
constructor (useOsChannel: boolean, nativeApi: NativeApi) {
// this.useAxios = useAxios
this.useOsChannel = useOsChannel
this.cacheProxy = new Proxy(nativeApi, new CacheStorageProxy(nativeApi))
}

get (_target: any, prop: string) {
return (...args: any) => {
if (this.useAxios && prop === this.requestApi) {
judgeUseAxios = this.useAxios
// @ts-ignore
return new RequestTask(...args)
}
// if (this.useAxios && prop === this.requestApi) {
// judgeUseAxios = this.useAxios
// // @ts-ignore
// return new RequestTask(...args)
// }
if (this.useOsChannel && osChannelApi.hasOwnProperty(prop)) {
return osChannelApi[prop](...args)
}
Expand All @@ -907,5 +907,5 @@ class HybridProxy {
}

const nativeApi = new NativeApi()
const native = new Proxy(nativeApi, new HybridProxy(false, false, nativeApi)) // 第一个false是默认走jsb,true是走纯js, 第二个false是不走osChannel
const native = new Proxy(nativeApi, new HybridProxy(false, nativeApi)) // 第一个false是默认走jsb,true是走纯js, 第二个false是不走osChannel
export default native
Original file line number Diff line number Diff line change
@@ -1,98 +1,14 @@
import Taro from '@tarojs/api'
import { isFunction } from '@tarojs/shared'
import { request as h5Request } from '@tarojs/taro-h5'
import { request as nativeReuqest } from './nativeRequest'

import { NativeRequest } from '../../interface/NativeRequest'
import native, { judgeUseAxios } from '../../NativeApi'
import { getParameterError, shouldBeObject } from '../../utils'

export const _request = (options) => {
const name = 'request'

const isObject = shouldBeObject(options)
if (!isObject.flag) {
const res = { errMsg: `${name}:fail ${isObject.msg}` }
return Promise.reject(res)
}

const { url, success, fail, complete, method, ...otherOptions } = options as Exclude<typeof options, undefined>
if (typeof url !== 'string') {
const res = {
errMsg: getParameterError({
para: 'url',
correct: 'string',
wrong: url,
}),
}
isFunction(fail) && fail(res)
isFunction(complete) && complete(res)
return Promise.reject(res)
}

let task!: Taro.RequestTask<any>
const result: ReturnType<typeof Taro.request> = new Promise((resolve, reject) => {
const upperMethod = method ? method.toUpperCase() : method
const taskID = native.request({
url,
method: upperMethod,
...otherOptions,
success: (res: any) => {
isFunction(success) && success(res)
isFunction(complete) && complete(res)
resolve(res)
},
fail: (res: any) => {
isFunction(fail) && fail(res)
isFunction(complete) && complete(res)
reject(res)
},
})
task = judgeUseAxios ? taskID : NativeRequest.getRequestTask(taskID)
}) as any

result.onHeadersReceived = task.onHeadersReceived.bind(task)
result.offHeadersReceived = task.offHeadersReceived.bind(task)
result.abort = task.abort.bind(task)
return result
}

function taroInterceptor (chain) {
return _request(chain.requestParams)
}

// @ts-ignore
const { Link } = Taro
const link = new Link(taroInterceptor)

/**
* 发起 HTTPS 网络请求
*
* @canUse request
* @__object [url, data, header, timeout, method[OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE, TRACE, CONNECT], dataType[json, text, base64, arraybuffer], responseType[text, arraybuffer], enableCache]
* @__success [data, header, statusCode, cookies]
* 封装请求方法
* @param options 请求选项
* @param useNativeRequest 默认使用true
*/
export function request (options) {
const result = link.request.bind(link)(options)
result.catch(() => {})
return result
export function request(options: any, useNativeRequest: boolean = true){
return useNativeRequest ? nativeReuqest(options) : h5Request(options);
}

/**
* 网络请求任务对象
*
* @canUse RequestTask
* @__class [abort, onHeadersReceived, offHeadersReceived]
*/

/**
* 使用拦截器
*
* @canNotUse addInterceptor
*/
export const addInterceptor = link.addInterceptor.bind(link)

/**
* 清除所有拦截器
*
* @canNotUse cleanInterceptors
*/
export const cleanInterceptors = link.cleanInterceptors.bind(link)
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import Taro from '@tarojs/api'
import { isFunction } from '@tarojs/shared'

import { NativeRequest } from '../../interface/NativeRequest'
import native, { judgeUseAxios } from '../../NativeApi'
import { getParameterError, shouldBeObject } from '../../utils'

export const _request = (options) => {
const name = 'request'

const isObject = shouldBeObject(options)
if (!isObject.flag) {
const res = { errMsg: `${name}:fail ${isObject.msg}` }
return Promise.reject(res)
}

const { url, success, fail, complete, method, ...otherOptions } = options as Exclude<typeof options, undefined>
if (typeof url !== 'string') {
const res = {
errMsg: getParameterError({
para: 'url',
correct: 'string',
wrong: url,
}),
}
isFunction(fail) && fail(res)
isFunction(complete) && complete(res)
return Promise.reject(res)
}

let task!: Taro.RequestTask<any>
const result: ReturnType<typeof Taro.request> = new Promise((resolve, reject) => {
const upperMethod = method ? method.toUpperCase() : method
const taskID = native.request({
url,
method: upperMethod,
...otherOptions,
success: (res: any) => {
isFunction(success) && success(res)
isFunction(complete) && complete(res)
resolve(res)
},
fail: (res: any) => {
isFunction(fail) && fail(res)
isFunction(complete) && complete(res)
reject(res)
},
})
task = judgeUseAxios ? taskID : NativeRequest.getRequestTask(taskID)
}) as any

result.onHeadersReceived = task.onHeadersReceived.bind(task)
result.offHeadersReceived = task.offHeadersReceived.bind(task)
result.abort = task.abort.bind(task)
return result
}

function taroInterceptor (chain) {
return _request(chain.requestParams)
}

// @ts-ignore
const { Link } = Taro
const link = new Link(taroInterceptor)

/**
* 发起 HTTPS 网络请求
*
* @canUse request
* @__object [url, data, header, timeout, method[OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE, TRACE, CONNECT], dataType[json, text, base64, arraybuffer], responseType[text, arraybuffer], enableCache]
* @__success [data, header, statusCode, cookies]
*/
export function request (options) {
const result = link.request.bind(link)(options)
result.catch(() => {})
return result
}

/**
* 网络请求任务对象
*
* @canUse RequestTask
* @__class [abort, onHeadersReceived, offHeadersReceived]
*/

/**
* 使用拦截器
*
* @canNotUse addInterceptor
*/
export const addInterceptor = link.addInterceptor.bind(link)

/**
* 清除所有拦截器
*
* @canNotUse cleanInterceptors
*/
export const cleanInterceptors = link.cleanInterceptors.bind(link)
Loading

0 comments on commit 7b7ab65

Please sign in to comment.