ExchangeForm.jsx 5.4 KB

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