You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
6.9 KiB

  1. <template>
  2. <div class="recharge-form">
  3. <h2 class="form-title">后台充值</h2>
  4. <form @submit.prevent="submitForm">
  5. <!-- 精网号输入组 -->
  6. <div class="form-group">
  7. <label for="jwcode">精网号</label>
  8. <!-- 添加 @blur 事件监听器 -->
  9. <input
  10. type="text"
  11. id="jwcode"
  12. v-model="formData.jwcode"
  13. placeholder="请输入精网号"
  14. required
  15. @blur="checkJwcodeExists"
  16. />
  17. </div>
  18. <!-- 充值金额输入组 -->
  19. <div class="form-group">
  20. <label for="amount">充值金额</label>
  21. <!-- 充值金额输入框使用 v-model.number 绑定到 formData.amount确保输入为数字 -->
  22. <input
  23. type="number"
  24. id="amount"
  25. v-model.number="formData.amount"
  26. placeholder="请输入充值金额"
  27. required
  28. min="0"
  29. />
  30. </div>
  31. <!-- 支付方式选择组 -->
  32. <div class="form-group">
  33. <label for="payment-method">支付方式</label>
  34. <!-- 支付方式选择框使用 v-model 绑定到 formData.paymentMethod -->
  35. <select id="payment-method" v-model="formData.paymentMethod" required>
  36. <!-- 默认选项 -->
  37. <option value="">请选择支付方式</option>
  38. <!-- 微信支付选项 -->
  39. <option value="WECHAT">微信</option>
  40. <!-- 支付宝支付选项 -->
  41. <option value="ALIPAY">支付宝</option>
  42. <!-- 银行卡支付选项 -->
  43. <option value="BANK">银行卡</option>
  44. </select>
  45. </div>
  46. <!-- 备注输入组 -->
  47. <div class="form-group">
  48. <label for="notes">备注</label>
  49. <!-- 修正为绑定到 formData.notes -->
  50. <textarea
  51. id="notes"
  52. v-model="formData.notes"
  53. rows="4"
  54. placeholder="请输入备注(非必填)"
  55. ></textarea>
  56. </div>
  57. <!-- 提交按钮组 -->
  58. <div class="form-group">
  59. <!-- 修改按钮根据 isLoading 状态显示不同文字和禁用状态 -->
  60. <button type="submit" :disabled="isLoading">
  61. {{ isLoading ? '提交中...' : '提交' }}
  62. </button>
  63. </div>
  64. </form>
  65. </div>
  66. </template>
  67. <script>
  68. import axios from 'axios';
  69. export default {
  70. data() {
  71. return {
  72. formData: {
  73. jwcode: '',
  74. amount: null,
  75. paymentMethod: '',
  76. notes: ''
  77. },
  78. isLoading: false,
  79. jwcodeExists: true // 新增变量,用于标记精网号是否存在
  80. };
  81. },
  82. methods: {
  83. /**
  84. * 检查精网号是否存在于数据库
  85. */
  86. async checkJwcodeExists() {
  87. if (!this.formData.jwcode) {
  88. this.jwcodeExists = true;
  89. return;
  90. }
  91. try {
  92. // 修改为 post 请求,将精网号作为请求体传递
  93. const response = await axios.post('http://192.168.8.94:5173/recharges/checkJwcode', {
  94. jwcode: this.formData.jwcode
  95. });
  96. if (response.data && typeof response.data.exists === 'boolean') {
  97. this.jwcodeExists = response.data.exists;
  98. } else {
  99. console.error('后端返回的数据格式不正确,缺少 exists 属性或属性类型错误');
  100. // 可以根据实际情况处理错误,比如设置默认值
  101. this.jwcodeExists = true;
  102. }
  103. if (!this.jwcodeExists) {
  104. alert('用户不存在1,请检查精网号。');
  105. }
  106. } catch (error) {
  107. console.error('检查精网号失败:', error);
  108. alert('检查精网号时出错,请稍后再试。');
  109. // 清空精网号记录
  110. this.formData.jwcode = '';
  111. }
  112. },
  113. /**
  114. * 提交表单的方法
  115. * 发送表单数据到后端并处理响应
  116. */
  117. async submitForm() {
  118. // 提交前检查精网号是否为空
  119. if (!this.formData.jwcode) {
  120. alert("精网号不能为空!");
  121. return;
  122. }
  123. // 检查精网号是否存在
  124. if (!this.jwcodeExists) {
  125. alert('用户不存在2,请检查精网号。');
  126. return;
  127. }
  128. try {
  129. // 开始提交,设置加载状态为 true
  130. this.isLoading = true;
  131. const response = await axios.post('http://192.168.8.94:5173/recharges/add/addRecharges', this.formData);
  132. if (response.data.code === "200") {
  133. console.log('后端响应:', response.data.code === "200");
  134. alert('提交成功!');
  135. } else {
  136. alert('提交失败:' + (response.data.message || '未知错误'));
  137. }
  138. } catch (error) {
  139. console.error('提交表单失败:', error);
  140. alert('提交失败,请稍后再试。');
  141. } finally {
  142. // 无论提交成功与否,都清空表单数据
  143. this.formData = {
  144. jwcode: '',
  145. amount: null,
  146. paymentMethod: '',
  147. notes: ''
  148. };
  149. this.isLoading = false;
  150. }
  151. }
  152. }
  153. };
  154. </script>
  155. <style scoped>
  156. /* 充值表单样式 */
  157. .recharge-form {
  158. /* 假设导航栏宽度为 200px,根据实际情况修改 */
  159. margin-left: 200px;
  160. /* 调整表单宽度,使用百分比适应剩余空间 */
  161. width: calc(100% - 300px);
  162. min-width: auto;
  163. min-height: 890px;
  164. margin-top: 10px;
  165. padding: 20px;
  166. border-radius: 8px;
  167. /* 设置表单完全透明 */
  168. background-color: rgba(255, 255, 255, 0.4);
  169. }
  170. /* 表单标题样式 */
  171. .form-title {
  172. text-align: center;
  173. margin-bottom: 30px;
  174. color: #000;
  175. }
  176. /* 表单组样式 */
  177. .form-group {
  178. margin-bottom: 30px; /* 增加表单组之间的间距 */
  179. }
  180. /* 表单组标签样式,修改文字颜色为深黑色 */
  181. .form-group label {
  182. display: block;
  183. margin-bottom: 10px; /* 增加标签与输入框的间距 */
  184. font-weight: bold;
  185. color: #000;
  186. }
  187. /* 表单组输入框、选择框和文本框样式 */
  188. .form-group input[type="text"],
  189. .form-group input[type="number"],
  190. .form-group select,
  191. .form-group textarea {
  192. width: 98%;
  193. padding: 8px;
  194. border: 1px solid #ccc;
  195. border-radius: 4px;
  196. /* 设置文本框、选择框和文本域一般透明 */
  197. background-color: rgba(255, 255, 255, 0.5);
  198. color: black;
  199. }
  200. /* 表单组文本框样式 */
  201. .form-group textarea {
  202. resize: vertical;
  203. }
  204. /* 表单组按钮样式 */
  205. .form-group button {
  206. width: 100%;
  207. padding: 10px;
  208. background-color: #007bff;
  209. color: white;
  210. border: none;
  211. border-radius: 4px;
  212. cursor: pointer;
  213. }
  214. /* 表单组按钮悬停样式 */
  215. .form-group button:hover {
  216. background-color: #0056b3;
  217. }
  218. </style>