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.

229 lines
6.7 KiB

1 month ago
  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. const response = await axios.get(`http://192.168.8.94:8080/recharges/checkJwcode/${this.formData.jwcode}`);
  93. if (response.data && typeof response.data.exists === 'boolean') {
  94. this.jwcodeExists = response.data.exists;
  95. } else {
  96. console.error('后端返回的数据格式不正确,缺少 exists 属性或属性类型错误');
  97. // 可以根据实际情况处理错误,比如设置默认值
  98. this.jwcodeExists = true;
  99. }
  100. if (!this.jwcodeExists) {
  101. alert('用户不存在1,请检查精网号。');
  102. }
  103. } catch (error) {
  104. console.error('检查精网号失败:', error);
  105. alert('检查精网号时出错,请稍后再试。');
  106. // 清空精网号记录
  107. this.formData.jwcode = '';
  108. }
  109. },
  110. /**
  111. * 提交表单的方法
  112. * 发送表单数据到后端并处理响应
  113. */
  114. async submitForm() {
  115. // 提交前检查精网号是否为空
  116. if (!this.formData.jwcode) {
  117. alert("精网号不能为空!");
  118. return;
  119. }
  120. // 检查精网号是否存在
  121. if (!this.jwcodeExists) {
  122. alert('用户不存在2,请检查精网号。');
  123. return;
  124. }
  125. try {
  126. // 开始提交,设置加载状态为 true
  127. this.isLoading = true;
  128. const response = await axios.post('http://192.168.8.94:8080/recharges/add/addRecharges', this.formData);
  129. if (response.data.code === "200") {
  130. console.log('后端响应:', response.data.code === "200");
  131. alert('提交成功!');
  132. } else {
  133. alert('提交失败:' + (response.data.message || '未知错误'));
  134. }
  135. } catch (error) {
  136. console.error('提交表单失败:', error);
  137. alert('提交失败,请稍后再试。');
  138. } finally {
  139. // 无论提交成功与否,都清空表单数据
  140. this.formData = {
  141. jwcode: '',
  142. amount: null,
  143. paymentMethod: '',
  144. notes: ''
  145. };
  146. this.isLoading = false;
  147. }
  148. }
  149. }
  150. };
  151. </script>
  152. <style scoped>
  153. /* 充值表单样式 */
  154. .recharge-form {
  155. min-width: 600px;
  156. margin-left: 8px;
  157. margin-top: 2px;
  158. padding: 20px;
  159. /* border: 10px solid #ccc; */
  160. border-radius: 8px;
  161. background-color: #f9f9f9;
  162. }
  163. /* 表单标题样式 */
  164. .form-title {
  165. text-align: center;
  166. margin-bottom: 20px;
  167. }
  168. /* 表单组样式 */
  169. .form-group {
  170. margin-bottom: 15px;
  171. }
  172. /* 表单组标签样式 */
  173. .form-group label {
  174. display: block;
  175. margin-bottom: 5px;
  176. font-weight: bold;
  177. }
  178. /* 表单组输入框、选择框和文本框样式 */
  179. .form-group input[type="text"],
  180. .form-group input[type="number"],
  181. .form-group select,
  182. .form-group textarea {
  183. width: 98%;
  184. padding: 8px;
  185. border: 1px solid #ccc;
  186. border-radius: 4px;
  187. }
  188. /* 表单组文本框样式 */
  189. .form-group textarea {
  190. resize: vertical;
  191. }
  192. /* 表单组按钮样式 */
  193. .form-group button {
  194. width: 100%;
  195. padding: 10px;
  196. background-color: #007bff;
  197. color: white;
  198. border: none;
  199. border-radius: 4px;
  200. cursor: pointer;
  201. }
  202. /* 表单组按钮悬停样式 */
  203. .form-group button:hover {
  204. background-color: #0056b3;
  205. }
  206. </style>