65 lines
1.3 KiB
TypeScript
65 lines
1.3 KiB
TypeScript
import * as Mock from 'mockjs'
|
|
|
|
export function resultSuccess<T>(data: T, message?: string) {
|
|
return Mock.mock({
|
|
code: 0,
|
|
data,
|
|
message: message ? message : 'OK'
|
|
})
|
|
}
|
|
|
|
export function resultPageSuccess<T>(result: T, message?: string) {
|
|
return resultSuccess(result, message)
|
|
}
|
|
|
|
export function resultError(
|
|
code = -1,
|
|
message = 'Request failed',
|
|
data = null
|
|
) {
|
|
return Mock.mock({
|
|
code,
|
|
message,
|
|
data
|
|
})
|
|
}
|
|
|
|
export function pagination<T = any>(
|
|
pageNo: number,
|
|
limit: number,
|
|
array: T[]
|
|
): T[] {
|
|
const offset = (pageNo - 1) * Number(limit)
|
|
return offset + Number(limit) >= array.length
|
|
? array.slice(offset, array.length)
|
|
: array.slice(offset, offset + Number(limit))
|
|
}
|
|
|
|
/**
|
|
* @param {Number} times 回调函数需要执行的次数
|
|
* @param {Function} callback 回调函数
|
|
*/
|
|
export function doCustomTimes(times: number, callback: any) {
|
|
let i = -1
|
|
while (++i < times) {
|
|
callback(i)
|
|
}
|
|
}
|
|
|
|
export interface requestParams {
|
|
method: string
|
|
body: any
|
|
headers?: { token?: string }
|
|
query: any
|
|
}
|
|
|
|
/**
|
|
* @description 本函数用于从request数据中获取token,请根据项目的实际情况修改
|
|
*
|
|
*/
|
|
export function getRequestToken({
|
|
headers
|
|
}: requestParams): string | undefined {
|
|
return headers?.token
|
|
}
|