h5-form.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <div :class="config.classText.join(' ')">
  3. <div class="form-container">
  4. <!-- 第一行:图片 -->
  5. <div v-if="config.showImage" class="form-image-row">
  6. <img v-if="config.formImage" :src="config.formImage" class="form-image" />
  7. </div>
  8. <!-- 第二行:手机号输入框 -->
  9. <div class="form-input-row">
  10. <input
  11. type="tel"
  12. class="form-input phone-input"
  13. placeholder="请输入手机号"
  14. v-model="phoneNumber"
  15. />
  16. </div>
  17. <!-- 第三行:验证码输入框 + 获取验证码按钮 -->
  18. <div class="form-code-row">
  19. <input
  20. type="text"
  21. class="form-input code-input"
  22. placeholder="请输入验证码"
  23. v-model="verifyCode"
  24. />
  25. <button class="get-code-btn" @click="getVerifyCode">
  26. {{ verifyCodeBtnText }}
  27. </button>
  28. </div>
  29. <!-- 第四行:提交按钮 -->
  30. <div v-if="config.showSubmitBtn" class="form-submit-row">
  31. <!-- 文字+背景色样式 -->
  32. <button
  33. v-if="config.submitBtnStyle === 'text'"
  34. class="submit-btn text-style"
  35. :style="{ backgroundColor: config.submitBtnColor }"
  36. >
  37. {{ config.submitBtnText }}
  38. </button>
  39. <!-- 图片样式 -->
  40. <img
  41. v-else-if="config.submitBtnStyle === 'image'"
  42. :src="config.submitBtnImage || require('@/assets/images/link-button.gif')"
  43. class="submit-btn image-style"
  44. />
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. export default {
  51. name: 'h5-form',
  52. props: {
  53. config: {
  54. type: Object,
  55. default: () => {
  56. return {
  57. classText: ['parent-div'],
  58. showImage: true,
  59. formImage: '',
  60. showSubmitBtn: true,
  61. submitBtnStyle: 'text', // 'text' 或 'image'
  62. submitBtnColor: '#409EFF',
  63. submitBtnText: '提交',
  64. submitBtnImage: ''
  65. }
  66. }
  67. }
  68. },
  69. data() {
  70. return {
  71. phoneNumber: '',
  72. verifyCode: '',
  73. countdownTime: 0,
  74. verifyCodeBtnText: '获取验证码'
  75. }
  76. },
  77. methods: {
  78. getVerifyCode() {
  79. if (!this.phoneNumber) {
  80. this.$message.warning('请先输入手机号');
  81. return;
  82. }
  83. // 启动倒计时
  84. this.countdownTime = 60;
  85. this.updateButtonText();
  86. },
  87. updateButtonText() {
  88. if (this.countdownTime > 0) {
  89. this.verifyCodeBtnText = `${this.countdownTime}s`;
  90. this.countdownTime--;
  91. setTimeout(() => {
  92. this.updateButtonText();
  93. }, 1000);
  94. } else {
  95. this.verifyCodeBtnText = '获取验证码';
  96. }
  97. }
  98. }
  99. }
  100. </script>
  101. <style src="./css/base.css"></style>
  102. <style lang="scss" scoped>
  103. .form-container {
  104. width: 100%;
  105. padding: 16px;
  106. display: flex;
  107. flex-direction: column;
  108. gap: 12px;
  109. }
  110. .active {
  111. border-color: #02ff9b;
  112. }
  113. .form-image-row {
  114. width: 100%;
  115. display: flex;
  116. justify-content: center;
  117. margin-bottom: 8px;
  118. }
  119. .form-image {
  120. max-width: 100%;
  121. height: auto;
  122. max-height: 150px;
  123. border-radius: 4px;
  124. }
  125. .form-input-row,
  126. .form-code-row {
  127. width: 100%;
  128. display: flex;
  129. gap: 8px;
  130. }
  131. .form-code-row {
  132. justify-content: space-between;
  133. }
  134. .form-input {
  135. border: 1px solid #DCDFE6;
  136. border-radius: 20px;
  137. padding: 10px 16px;
  138. font-size: 14px;
  139. color: #303133;
  140. &::placeholder {
  141. color: #909399;
  142. }
  143. &:focus {
  144. outline: none;
  145. border-color: #409EFF;
  146. box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  147. }
  148. }
  149. .phone-input {
  150. width: 100%;
  151. }
  152. .code-input {
  153. flex: 1;
  154. min-width: 150px;
  155. }
  156. .get-code-btn {
  157. padding: 10px 16px;
  158. border: 1px solid #409EFF;
  159. background-color: #fff;
  160. color: #409EFF;
  161. border-radius: 20px;
  162. font-size: 14px;
  163. cursor: pointer;
  164. transition: all 0.3s ease;
  165. white-space: nowrap;
  166. &:hover {
  167. background-color: #F0F9FF;
  168. }
  169. &:active {
  170. background-color: #E6F2FF;
  171. }
  172. }
  173. .form-submit-row {
  174. width: 100%;
  175. display: flex;
  176. justify-content: center;
  177. margin-top: 8px;
  178. }
  179. .submit-btn {
  180. border: none;
  181. border-radius: 20px;
  182. font-size: 16px;
  183. cursor: pointer;
  184. transition: all 0.3s ease;
  185. width: calc(100% - 32px);
  186. &.text-style {
  187. padding: 12px 48px;
  188. color: #fff;
  189. font-weight: 600;
  190. &:hover {
  191. opacity: 0.9;
  192. }
  193. &:active {
  194. opacity: 0.8;
  195. }
  196. }
  197. &.image-style {
  198. max-width: 100%;
  199. height: auto;
  200. max-height: 60px;
  201. object-fit: contain;
  202. width: auto;
  203. &:hover {
  204. opacity: 0.9;
  205. }
  206. }
  207. }
  208. </style>