import React, { useState, useEffect } from "react"; import { View, Text, Button, Input, Image } from "@tarojs/components"; import Taro, { useDidShow } from "@tarojs/taro"; import dayjs from "dayjs"; import "./index.scss"; import { DialogCalendarCard } from "@/components/index"; // import { CalendarUI } from "@/components"; import { CommonPopup, GeneralNavbar } from "@/components"; import httpService from "@/services/httpService"; import img from "@/config/images"; export enum TransactionSubType { All = "", GameActivity = "game_activity", Withdrawal = "withdrawal", Refund = "refund", Compensation = "compensation", } export enum TransactionType { All = "", Income = "income", Expense = "expense", } interface Option { label: string; value: T; } interface TransactionLoadParams { transaction_sub_type: TransactionSubType; date_range?: string[]; } const DownloadBill: React.FC = () => { // 获取当前页面的配置 const currentPage = Taro.getCurrentInstance(); const pageConfig = currentPage.page?.config; const pageTitle = pageConfig?.navigationBarTitleText; const [dateRange, setDateRange] = useState({ start: "", end: "" }); const [transactionSubType, setTransactionSubType] = useState(TransactionSubType.All); const [dateType, setDateType] = useState("week"); const [visible, setVisible] = useState(false); const [show_download_popup, set_show_download_popup] = useState(false); const [isFocus, setIsFocus] = useState(false); const [password, setPassword] = useState(new Array(6).fill("")); useEffect(() => { culculateDateRange(dateType); }, []); const [showFilterPopup, setShowFilterPopup] = useState(false); const culculateDateRange = (dateType: string) => { const today = new Date(); const year = today.getFullYear(); const month = today.getMonth(); const day = today.getDate(); if (dateType === "week") { today.setDate(day - 6); } else if (dateType === "month") { today.setMonth(month - 1); today.setDate(day + 1); } const startYear = today.getFullYear(); const startMonth = today.getMonth(); const startDay = today.getDate(); setDateRange({ start: `${startYear}-${String(startMonth + 1).padStart(2, "0")}-${String( startDay ).padStart(2, "0")}`, end: `${year}-${String(month + 1).padStart(2, "0")}-${String( day ).padStart(2, "0")}`, }); }; const selectDateRange = (range: string) => { if (dateType === range) { if (range === "custom") { setVisible(true); return; } return; } switch (range) { case "week": setCurrentTimeValue(new Date()); setDateType("week"); culculateDateRange("week"); break; case "month": setCurrentTimeValue(new Date()); setDateType("month"); culculateDateRange("month"); break; case "custom": setDateType("custom"); setDateRange({ start: "", end: "" }); break; } }; const [currentTimeValue, setCurrentTimeValue] = useState( new Date() ); const handleConfirm = (val: Date[]) => { setCurrentTimeValue(val); const [start, end] = val; setDateRange({ start: dayjs(start).format("YYYY-MM-DD"), end: dayjs(end).format("YYYY-MM-DD"), }); }; const handlePasswordInput = (e: any) => { const value = e.detail.value; const [one = "", two = "", three = "", four = "", five = "", six = ""] = value.split(""); setPassword([one, two, three, four, five, six]); if (value.length === 6) { // const timer = setTimeout(() => { // // TODO 校验密码 // if (false) { // set_show_download_popup(false); // Taro.showModal({ // content: "支付密码错误,请重试", // cancelText: "忘记密码", // confirmText: "重试", // cancelColor: "#000", // confirmColor: "#fff", // }).then((res) => { // if (res.confirm) { // set_show_download_popup(true); // } else if (res.cancel) { // Taro.navigateTo({ // url: "/user_pages/validPhone/index" // }); // } // }).finally(() => { // clearTimeout(timer); // }); // } else { // // TODO 下载账单 // } // }, 100); } }; const transaction_type_options: Option[] = [ { label: "全部", value: TransactionSubType.All, }, { label: "组织活动", value: TransactionSubType.GameActivity, }, { label: "提现", value: TransactionSubType.Withdrawal, }, { label: "退款", value: TransactionSubType.Refund, }, { label: "企业赔付", value: TransactionSubType.Compensation, }, ]; const [load_transactions_params, set_load_transactions_params] = useState({ transaction_sub_type: TransactionSubType.All, date_range: [], }); const handleClose = () => { setTransactionSubType(load_transactions_params.transaction_sub_type); setShowFilterPopup(false); }; const handleTypeConfirm = () => { set_load_transactions_params((prev) => { return { ...prev, transaction_sub_type: transactionSubType }; }); setShowFilterPopup(false); }; const downloadExample = async () => { const { start, end } = dateRange; if (!start || !end) return; try { const res = await httpService.post("/parameter/many_key", { keys: "bill_example", }); const { bill_example, fileName } = res.data; // 调用下载文件接口 wx.downloadFile({ url: bill_example, // 文件路径 success: function (res) { // 只有200状态码表示下载成功 if (res.statusCode === 200) { // 下载成功后可以使用res.tempFilePath访问临时文件路径 console.log("文件下载成功,临时路径为:", res.tempFilePath); // 保存文件到本地 wx.openDocument({ filePath: res.tempFilePath, showMenu: true, // 显示保存菜单 }); } }, fail: function (err) { console.error("文件下载失败:", err); }, }); } catch (error) { console.error(error); } }; const handleDownloadBill = async () => { const { start, end } = dateRange; if (!start || !end) return; try { const { transaction_sub_type } = load_transactions_params; const date_range = [start, end]; const res = await httpService.post("/wallet/download_bill", { transaction_sub_type, date_range, }); const { fileUrl, fileName } = res.data; // 调用下载文件接口 wx.downloadFile({ url: fileUrl, // 文件路径 success: function (res) { // 只有200状态码表示下载成功 if (res.statusCode === 200) { // 下载成功后可以使用res.tempFilePath访问临时文件路径 console.log("文件下载成功,临时路径为:", res.tempFilePath); // 保存文件到本地 wx.openDocument({ filePath: res.tempFilePath, showMenu: true, // 显示保存菜单 }); } }, fail: function (err) { console.error("文件下载失败:", err); }, }); } catch (error) { console.error(error); } }; return ( {/* 导航栏 */} {/* { Taro.navigateBack(); }} > {pageTitle} */} {/* 顶部导航栏 */} { Taro.navigateBack(); }} /> 最长可导出三个月的账单 示例文件 {/* 示例文件 */} {/* 接收方式 小程序消息 */} { setShowFilterPopup(true); }} > 交易类型 { transaction_type_options.find( (item) => item.value === load_transactions_params.transaction_sub_type )?.label } 账单时间 { selectDateRange("week"); }} > 近一周 { selectDateRange("month"); }} > 近一月 { selectDateRange("custom"); }} > 自定义时间 {dateRange.start && dateRange.end && dateType !== "custom" && ( {dateRange.start}{dateRange.end} )} {dateType === "custom" && ( { setVisible(true); }} > 时间范围 {dateRange.start && dateRange.end ? `${dateRange.start} 至 ${dateRange.end}` : "请选择账单时间"} )} Taro.navigateTo({ url: "/user_pages/downloadBillRecords/index" }) } > 下载记录 {visible && ( setVisible(false)} /> )} {/* 下载账单输入密码弹窗 */} set_show_download_popup(false)} title="提现" className="withdraw_popup" hideFooter={true} > {`支付账单流水文件(文件名).xlsx`} {`文件大小:7KB`} {`请输入交易密码`} {password.map((item, index) => ( {item} ))} item !== "").join("")} maxlength={6} onInput={handlePasswordInput} /> {/* 筛选账单弹窗 */} 交易类型 {transaction_type_options.map( (option: Option) => ( { setTransactionSubType(option.value); }} > {option.label} ) )} ); }; export default DownloadBill;