1、Promise 返回类型
import store from '@/store';
import { useGetItem } from '@/hooks//useStorage';
export const useSign = (params: {
message: string;
timestamp?: number;
walletAddress?: string;
}): Promise<{
message: string;
sign: string;
timestamp: number;
walletAddress?: string;
}> => {
params.timestamp = new Date().getTime();
params.walletAddress = store.getState().user.walletAddress || useGetItem('walletconnect').accounts[0] || '';
const { message, timestamp, walletAddress } = params;
return new Promise((r, j) => {
let web3 = store.getState().web3.web3 || '';
if(!web3) {
j('provider is not exist');
}
let sign = web3.eth.sign
sign(web3.utils.utf8ToHex(message), walletAddress, (error: any, sign: any) => {
if (sign) r({ message, sign, timestamp, walletAddress })
else j(error)
})
})
}
2、获取对象类型
const params: {
sign: string;
message: string;
cellId: string;
sort?: number;
videoUrl?: string;
imageUrl?: string;
imageFile?: any
} = {
sign: res.sign,
message: res.message,
cellId: props.cellId,
sort: props.sort || 0,
imageFile: file.current || undefined,
imageUrl: imageUrl.current || undefined,
videoUrl: videoUrl.current || undefined
}
const formData = new FormData()
Object.keys(params).forEach((key: string) => {
formData.append(key, params[key as keyof typeof params])
})