ExchangeForm.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use client";
  2. import { useState, useEffect } from "react";
  3. import { X } from "lucide-react";
  4. import { fetchApi } from "../utils/fetch";
  5. export default function ExchangeForm({
  6. currentUser,
  7. setCurrentUser,
  8. item,
  9. onClose,
  10. setAlert,
  11. }) {
  12. const [formData, setFormData] = useState({});
  13. const [isSubmitting, setIsSubmitting] = useState(false);
  14. useEffect(() => {
  15. if (currentUser && currentUser.account) {
  16. setFormData((prevData) => ({
  17. ...prevData,
  18. account: currentUser.account,
  19. }));
  20. }
  21. }, []);
  22. const handleInputChange = (e) => {
  23. setFormData({ ...formData, [e.target.name]: e.target.value });
  24. };
  25. const handleSubmit = async (e) => {
  26. e.preventDefault();
  27. setIsSubmitting(true);
  28. try {
  29. const params = {
  30. userId: currentUser.id,
  31. username: currentUser.username,
  32. item,
  33. status: "待兑换未审核",
  34. exchangeInfo: {
  35. account: formData.account || "",
  36. address: formData.address || "",
  37. name: formData.name || "",
  38. phone: formData.phone || "",
  39. },
  40. exchangeTime: new Date(), // 添加兑换时间
  41. // exchangeCount: formData.exchangeCount || 1,
  42. };
  43. // console.log("params", params);
  44. const res = await fetchApi("/api/exchange-history", {
  45. method: "POST",
  46. body: JSON.stringify(params),
  47. });
  48. if (res.success) {
  49. setAlert({
  50. type: "success",
  51. message: "提交成功,正在审核中",
  52. });
  53. onClose();
  54. const updatedUser = {
  55. ...currentUser,
  56. points: currentUser.points - item.points,
  57. account: formData.account || currentUser.account,
  58. };
  59. localStorage.setItem("currentUser", JSON.stringify(updatedUser));
  60. setCurrentUser(updatedUser);
  61. } else {
  62. setAlert({ type: "error", message: res.error });
  63. }
  64. } catch (error) {
  65. console.error("兑换请求失败:", error);
  66. setAlert({ type: "error", message: res.error });
  67. } finally {
  68. setIsSubmitting(false);
  69. }
  70. };
  71. return (
  72. <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
  73. <div className="bg-white text-black rounded-lg max-w-md w-full mx-4">
  74. <div className="flex justify-end items-center p-4">
  75. <button
  76. onClick={onClose}
  77. className="text-gray-600 hover:text-gray-800 transition duration-300"
  78. aria-label="关闭"
  79. >
  80. <X size={24} />
  81. </button>
  82. </div>
  83. <form onSubmit={handleSubmit} className="px-4 pb-4">
  84. {item.type === "彩金" || item.type === "优惠券" ? (
  85. <div className="mb-4">
  86. <label htmlFor="account" className="block mb-2 font-semibold">
  87. 兑换账号
  88. </label>
  89. <input
  90. type="text"
  91. id="account"
  92. name="account"
  93. placeholder="请输入智博1919账号"
  94. required
  95. className="w-full border rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
  96. onChange={handleInputChange}
  97. value={formData.account || ""}
  98. />
  99. </div>
  100. ) : (
  101. <>
  102. <div className="mb-4">
  103. <label htmlFor="name" className="block mb-2 font-semibold">
  104. 姓名
  105. </label>
  106. <input
  107. type="text"
  108. id="name"
  109. name="name"
  110. required
  111. className="w-full border rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
  112. onChange={handleInputChange}
  113. />
  114. </div>
  115. <div className="mb-4">
  116. <label htmlFor="phone" className="block mb-2 font-semibold">
  117. 电话
  118. </label>
  119. <input
  120. type="tel"
  121. id="phone"
  122. name="phone"
  123. required
  124. className="w-full border rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
  125. onChange={handleInputChange}
  126. />
  127. </div>
  128. <div className="mb-4">
  129. <label htmlFor="address" className="block mb-2 font-semibold">
  130. 地址
  131. </label>
  132. <textarea
  133. id="address"
  134. name="address"
  135. required
  136. className="w-full border rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
  137. onChange={handleInputChange}
  138. ></textarea>
  139. </div>
  140. </>
  141. )}
  142. <div className="flex justify-end mt-6">
  143. <button
  144. type="button"
  145. onClick={onClose}
  146. className="mr-4 px-4 py-2 border rounded text-gray-600 hover:bg-gray-100 transition duration-300"
  147. disabled={isSubmitting}
  148. >
  149. 取消
  150. </button>
  151. <button
  152. type="submit"
  153. className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600 transition duration-300"
  154. disabled={isSubmitting}
  155. >
  156. {isSubmitting ? "提交中..." : "确认兑换"}
  157. </button>
  158. </div>
  159. </form>
  160. </div>
  161. </div>
  162. );
  163. }