index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!--test.vue-->
  2. <template>
  3. <div>
  4. <van-form
  5. class="source-form"
  6. @submit="onSubmit"
  7. :style="{
  8. 'background-color': sourceFormConfig.background
  9. }"
  10. >
  11. <van-field
  12. v-if="sourceFormConfig.company"
  13. v-model.trim="form.company"
  14. required
  15. name="公司名称"
  16. label="公司名称"
  17. placeholder="请输入准确的公司名称"
  18. />
  19. <van-field
  20. v-if="sourceFormConfig.name"
  21. v-model.trim="form.name"
  22. required
  23. name="姓名"
  24. label="姓名"
  25. placeholder="请输入姓名"
  26. />
  27. <van-field
  28. v-if="sourceFormConfig.phone"
  29. v-model.trim="form.phone"
  30. required
  31. name="手机号"
  32. label="手机号"
  33. placeholder="请输入准确的手机号"
  34. />
  35. <van-field name="radio" required label="你希望获得哪个行业的报告" v-if="sourceFormConfig.source_desc">
  36. <template #input>
  37. <div class="custom-radio-type">
  38. <van-radio-group
  39. v-model="form.source_desc"
  40. >
  41. <van-radio
  42. v-for="item in sourceFormConfig.sourceOptions"
  43. :name="item"
  44. :key="item"
  45. >
  46. <div class="other-input-box" v-if="form.source_desc === '其他' && item === '其他'">
  47. <span>{{item}}</span>
  48. <van-field
  49. v-model.trim="form.source_desc_temp"
  50. placeholder="请输入行业"
  51. />
  52. </div>
  53. <span v-else>{{item}}</span>
  54. </van-radio>
  55. </van-radio-group>
  56. </div>
  57. </template>
  58. </van-field>
  59. <div style="margin: 16px;">
  60. <van-button round block :type="btnType" :size="size" native-type="submit">{{text}}</van-button>
  61. </div>
  62. </van-form>
  63. </div>
  64. </template>
  65. <script>
  66. import { Form, Field, Button, Radio, RadioGroup } from 'vant';
  67. import $axios from "@/service/httpServer";
  68. export default {
  69. name: 'DsSourceForm',
  70. components: {
  71. [Form.name]: Form,
  72. [Field.name]: Field,
  73. [Radio.name]: Radio,
  74. [RadioGroup.name]: RadioGroup,
  75. [Button.name]: Button
  76. },
  77. data() {
  78. return {
  79. form: {
  80. name: '',
  81. phone: '',
  82. company: '',
  83. source_desc: '',
  84. source_desc_temp: ''
  85. }
  86. }
  87. },
  88. props: {
  89. api: {
  90. type: String,
  91. default: '/salesLeads/collectInfo'
  92. },
  93. btnType:{
  94. type:String,
  95. default:'info'
  96. },
  97. text: {
  98. type: String,
  99. default: '提 交'
  100. },
  101. size:{
  102. type:String,
  103. default:'large'
  104. },
  105. sourceFormConfig: {
  106. type:Object,
  107. default: ()=>({
  108. background: 'transparent',
  109. source: 'default-source-h5',
  110. name: true,
  111. phone: true,
  112. company: true,
  113. source_desc: true,
  114. sourceOptions: [
  115. '建筑工程',
  116. '信息技术',
  117. '弱电安防',
  118. '医疗卫生',
  119. '服务采购',
  120. '其他'
  121. ]
  122. })
  123. }
  124. },
  125. methods: {
  126. onSubmit (values) {
  127. console.log(values)
  128. this.$toast.loading({
  129. duration: 0,
  130. forbidClick: true
  131. });
  132. const params = Object.assign({
  133. source: this.form.source_desc === '其他' ? this.form.source_desc_temp : this.form.source_desc,
  134. origin: location.href
  135. }, this.form)
  136. console.log('source-params', params)
  137. $axios.post(this.api, JSON.stringify(params), {
  138. headers: {
  139. 'Content-Type': 'application/json;charset=utf-8'
  140. }
  141. }).then(res => {
  142. console.log('res', res)
  143. this.$toast.clear()
  144. this.$toast('提交成功,我们会尽快联系您~')
  145. }).catch(() => {
  146. this.$toast.clear()
  147. this.$toast('太火爆了,请稍后重试')
  148. })
  149. }
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. .other-input-box {
  155. display: flex;
  156. flex-direction: row;
  157. align-items: center;
  158. span {
  159. flex-shrink: 0;
  160. }
  161. }
  162. .custom-radio-type {
  163. display: flex;
  164. flex-direction: column;
  165. }
  166. .source-form {
  167. width: 100%;
  168. height: 100%;
  169. ::v-deep {
  170. .van-cell {
  171. flex-direction: column;
  172. background-color: inherit;
  173. }
  174. .van-field__label {
  175. width: 100%;
  176. }
  177. .van-radio {
  178. height: 44px;
  179. }
  180. }
  181. }
  182. </style>