Form.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <view>
  3. <view class="form-card" v-if="viewStatus">
  4. <view class="flex_c_l form-item hr-solid">
  5. <view class="form-lable">规格类型</view>
  6. <up-radio-group v-model="formData.serviceType" class="form-input">
  7. <up-radio v-for="(item, index) in serviceOption" :key="index" :label="item.name" :name="item.value"
  8. activeColor="#07C160" inactiveColor="#C4C4C4">
  9. </up-radio>
  10. </up-radio-group>
  11. </view>
  12. <view class="flex_c_l form-item hr-solid">
  13. <view class="form-lable">规格说明</view>
  14. <up-input v-model="formData.specDescribe" clearable border="none" placeholder="填写本服务内容说明"
  15. class="form-input" placeholder-class="form-placeholder"></up-input>
  16. </view>
  17. <view class="flex_c_l form-item hr-solid">
  18. <view class="form-lable">服务价格</view>
  19. <up-input v-model="formData.price" clearable border="none" placeholder="单次服务的价格" class="form-input"
  20. placeholder-class="form-placeholder" @blur="blurInput('number','price')"></up-input>
  21. </view>
  22. <view class="flex_c_l form-item hr-solid">
  23. <view class="form-lable">购买单位</view>
  24. <up-input v-model="formData.unit" clearable border="none" placeholder="购买单位如次、平方" class="form-input"
  25. placeholder-class="form-placeholder"></up-input>
  26. </view>
  27. <view class="flex_c_l form-item hr-solid">
  28. <view class="form-lable">最少购买</view>
  29. <up-input v-model="formData.minPurchaseQuantity" clearable border="none" placeholder="用户下单最少购买数量"
  30. class="form-input" placeholder-class="form-placeholder" @blur="blurInput('number','minPurchaseQuantity')"></up-input>
  31. </view>
  32. <view class="flex_c_l form-item hr-solid">
  33. <view class="form-lable">服务时长</view>
  34. <up-input v-model="formData.duration" clearable border="none" placeholder="预计单次服务时常" class="form-input"
  35. placeholder-class="form-placeholder" @blur="blurInput('number','duration')"></up-input>
  36. </view>
  37. </view>
  38. <view class="form-card" v-else>
  39. <view class="flex_c_l form-item hr-solid">
  40. <view class="form-lable">规格类型</view>
  41. <view class="form-input">{{ serviceType }}服务</view>
  42. </view>
  43. <view class="flex_c_l form-item hr-solid">
  44. <view class="form-lable">规格说明</view>
  45. <view class="form-input">{{ formData.specDescribe }}</view>
  46. </view>
  47. <view class="flex_c_l form-item hr-solid">
  48. <view class="form-lable">服务价格</view>
  49. <view class="form-input">{{ formData.price }}</view>
  50. </view>
  51. <view class="flex_c_l form-item hr-solid">
  52. <view class="form-lable">购买单位</view>
  53. <view class="form-input">{{ formData.unit }}</view>
  54. </view>
  55. <view class="flex_c_l form-item hr-solid">
  56. <view class="form-lable">最少购买</view>
  57. <view class="form-input">{{ formData.minPurchaseQuantity }}</view>
  58. </view>
  59. <view class="flex_c_l form-item hr-solid">
  60. <view class="form-lable">服务时长</view>
  61. <view class="form-input">{{ formData.duration }}</view>
  62. </view>
  63. </view>
  64. </view>
  65. </template>
  66. <script setup>
  67. import { computed, ref, reactive } from 'vue';
  68. import { provide, inject } from 'vue';
  69. const viewStatus = inject('viewStatus');
  70. const props = defineProps({
  71. form: {
  72. type: Object,
  73. default: () => {
  74. return {
  75. name: '',
  76. }
  77. }
  78. }
  79. })
  80. const serviceOption = [
  81. {
  82. name: '周期',
  83. value: '10'
  84. },
  85. {
  86. name: '单次',
  87. value: '20'
  88. },
  89. {
  90. name: '非时效',
  91. value: '30'
  92. }
  93. ]
  94. const formData = computed(() => {
  95. return props.form;
  96. })
  97. const serviceType = computed(() =>{
  98. const type = serviceOption.find(item => item.value === formData.value.serviceType)
  99. return type.name
  100. })
  101. function isNumber(str) {
  102. return /^[\+\-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/.test(str);
  103. }
  104. //数字校验
  105. const numberVer = (key) => {
  106. return new Promise((resolve, reject) => {
  107. if(!formData.value[key]){
  108. reject('请输入内容');
  109. }
  110. const isNum = isNumber(formData.value[key]);
  111. if (!isNum) {
  112. formData.value[key] = formData.value[key].replace(/[^0-9.]/g, '');// 移除非数字字符
  113. formData.value[key] = Number(formData.value[key]);
  114. reject('请输入正确的数值');
  115. }
  116. const value = Number(formData.value[key]);
  117. if (value < 0) {
  118. reject('数值过小,请重新输入');
  119. }
  120. formData.value[key] = value;
  121. resolve();
  122. })
  123. }
  124. // 校验
  125. const blurInput = async (key,objectKey) => {
  126. try {
  127. const verFun = {
  128. number: numberVer,
  129. }
  130. const res = await verFun[key](objectKey);
  131. console.log("TCL: numberInput -> res", res)
  132. } catch (error) {
  133. uni.showToast({
  134. title: error,
  135. icon: 'none',
  136. duration: 1000
  137. });
  138. console.log("TCL: numberInput -> error", error)
  139. }
  140. }
  141. </script>
  142. <style lang="scss" scoped>
  143. .form-card {
  144. background: #fff;
  145. border-radius: 20rpx;
  146. margin-bottom: 20rpx;
  147. .form-item {
  148. padding: 32rpx 0;
  149. margin-left: 32rpx;
  150. padding-right: 32rpx;
  151. .form-lable {
  152. font-size: 32rpx;
  153. line-height: 44rpx;
  154. color: #1D2129;
  155. margin-right: 48rpx;
  156. }
  157. }
  158. .form-input {
  159. flex: 1;
  160. font-family: PingFang SC;
  161. font-size: 32rpx;
  162. line-height: 44rpx;
  163. color: #130F26;
  164. }
  165. }
  166. .code-btn {
  167. border-radius: 14rpx;
  168. background: linear-gradient(180deg, #FD8F7C -75%, #FE534B 140%);
  169. // margin-right: 32rpx;
  170. font-family: PingFang SC;
  171. font-size: 26rpx;
  172. font-weight: normal;
  173. line-height: 42rpx;
  174. letter-spacing: normal;
  175. color: #FFFFFF;
  176. padding: 13rpx 20rpx;
  177. }
  178. </style>
  179. <style>
  180. .form-placeholder {
  181. color: #C9CDD4;
  182. }
  183. </style>