对接api

This commit is contained in:
2022-05-09 10:03:33 +08:00
parent e7a130533d
commit 4c46c6e22d
16 changed files with 243 additions and 184 deletions
+15 -42
View File
@@ -2,10 +2,9 @@
import Taro from '@tarojs/taro'
import { useRef, useState } from 'react'
import {BASE_URL} from '@/common/constant'
import qs from 'qs';
type Params = {
code: string|null
success: true|false
@@ -24,6 +23,16 @@ type Params = {
pageSize?: number
}
type option = {
url?: string,
method?: 'get'|'post'|'put'|'delete',
type?: string,
data?: any,
page?: number,
pageSize?: number,
pagination?: true|false
}
/**
* 返回状态信息,根据 http 状态错
* @param {Number} status
@@ -80,7 +89,7 @@ const showStatus = (status) => {
* @param {Object} options.data 请求的参数
* @returns {Object} 返回fetch(), loading, error, code, msg
*/
export const useRequest = (options = {
export const useRequest = (options:option = {
url: '/',
method: 'get',
type: 'json',
@@ -90,7 +99,7 @@ export const useRequest = (options = {
pagination: false, // 是否分页
}) => {
options.url = `${BASE_URL}${options.url}`
let params:Params = {
code: null, // 业务码
success: false, // 请求是否成功
@@ -110,43 +119,8 @@ export const useRequest = (options = {
const stateRef = useRef({...params})
const [state, setState] = useState({...stateRef.current})
// // 每页多少条记录
// const handleSizeChange = (val) => {
// state.size = val
// }
// // 当前
// const handleCurrentChange = (val) => {
// state.page = val
// }
// /**
// * 筛选搜索
// * @param {Object} val `{name:'ppx'}` name 为字段名,ppx 为字段内容
// */
// const handleFilterChange = (val) => {
// state.filter = val
// }
// 排序
/**
* 排序字段,排序方式
* @param {String} val 排序字段
* @param {String} sortOrder `desc` / `asc`
*/
// eslint-disable-next-line
const handleSort = (val, orderby = "desc") => {
if (val != null) {
stateRef.current.sort = orderby ? {
sort_key: `${orderby == 'desc' ? '-' + val : val}`
} : null
}
}
// 请求函数
const fetchData = async (sub_options) => {
const fetchData = async (sub_options?:any) => {
stateRef.current.loading = true
setState((e) => ({...e, loading:true}))
stateRef.current.query = {
@@ -164,7 +138,7 @@ export const useRequest = (options = {
...options,
...{
header: {
"Platform": 3,
"Platform": 6,
"Authorization": token || stateRef.current.token,
}
},
@@ -214,7 +188,6 @@ export const useRequest = (options = {
}
return {
handleSort,
fetchData,
state,
}