/* * @Author: Kane * @Date: 2023-09-11 09:59:00 * @LastEditors: Kane * @FilePath: /task_schedule/src/utils/reward.ts * @Description: * * Copyright (c) ${2022} by Kane, All Rights Reserved. */ import { type AxiosResponse } from "axios"; import { service as instance } from "./api/request.js"; import { API_URL } from "./api/config.js"; interface RewardProject { rewardCode: number; rewardName: string; } interface RewardProjectResponse { success: boolean; message: string; rewardList: RewardProject[]; } interface RewardGainer { acquiredDate: string; callerName: string; callerCode: string; rewardProjectCode: string; rewardProjectName: string; } /** * 请求奖项清单 * @param handler 用于处理请求数据的回调函数; */ function requestRewardPorjectsList( handler: any ): void { if ( handler === undefined || handler === null ) { return; } instance.request({ url: API_URL.URL_RWARD_PROJECTS, method: "post", }) // 请求成功,检查服务器返回结果 .then(( response: AxiosResponse ): void => { const data = response.data ?? {}; const success = data.success ?? false; const message = data.message ?? ""; const rewardProjectList = checkRewardProjects( data.rewardList ?? []); }) .catch(( error: any ): void => { console.log( error ); handler( null, error ); }); } // **功能函数 **********************************/ function checkRewardProjects( data: any[]): RewardProject[] | null { const rewardList: RewardProject[] = []; // 检查一下参数的类型,如果不是数组返回 null if ( !( data instanceof Array )) { return null; } data.forEach(( item: any ) => { const reward = { rewardCode: item.rewardCode ?? "", rewardName: item.rewardName ?? "", }; rewardList.push( reward ); }); return rewardList; } export { type RewardProject, type RewardGainer, type RewardProjectResponse, requestRewardPorjectsList };