quotationsForm.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view class="example">
  3. <!-- 自定义表单校验 -->
  4. <uni-forms ref="customForm" :rules="rules" :modelValue="customFormData" label-align="center">
  5. <uni-forms-item label="产品名称" required name="productName" label-width="85" label-align="center">
  6. <uni-easyinput v-model="customFormData.productName" placeholder="请输入产品名称" />
  7. </uni-forms-item>
  8. <uni-forms-item label="地区" required name="area" >
  9. <uni-easyinput v-model="customFormData.area" placeholder="请输入地区" />
  10. </uni-forms-item>
  11. <uni-forms-item label="价格" required name="price" >
  12. <uni-easyinput v-model="customFormData.price" placeholder="请输入价格" />
  13. </uni-forms-item>
  14. <uni-forms-item label="手机号" required name="phone" >
  15. <uni-easyinput v-model="customFormData.phone" placeholder="请输入手机号" />
  16. </uni-forms-item>
  17. <uni-forms-item label="地址" required name="address">
  18. <uni-easyinput v-model="customFormData.address" placeholder="请输入地址" />
  19. </uni-forms-item>
  20. <uni-forms-item label="类型" required name="type" >
  21. <uni-data-picker placeholder="请选择分类" popup-title="请选择所属分类" :localdata="TreeData" v-model="customFormData.type"/>
  22. </uni-forms-item>
  23. <uni-forms-item label="图片" required name="url" label-width="85">
  24. <upload :imgArr="imageList" :fileSize="1" :limit="3" @updateImg="updateImg"/>
  25. </uni-forms-item>
  26. </uni-forms>
  27. <button type="primary" @click="submit('customForm')">提交</button>
  28. </view>
  29. </template>
  30. <script>
  31. import {listConfig,addQuotations} from "@/api/quotations/quotations";
  32. import upload from '../../components/upload/index.vue'
  33. export default {
  34. components: {
  35. upload
  36. },
  37. data() {
  38. return {
  39. // 基础表单数据
  40. baseFormData: {
  41. productName: '',
  42. area:'',
  43. price:'',
  44. phone:'',
  45. address:'',
  46. type:'',
  47. url:[]
  48. },
  49. //树数据
  50. TreeData:[],
  51. // 校验规则
  52. rules: {
  53. productName: {
  54. rules: [{
  55. required: true,
  56. errorMessage: '产品名称不能为空'
  57. }]
  58. },
  59. area: {
  60. rules: [{
  61. required: true,
  62. errorMessage: '地区不能为空'
  63. }]
  64. },
  65. phone: {
  66. rules: [
  67. {
  68. required: true,
  69. errorMessage: '手机号码不能为空'
  70. },
  71. {
  72. pattern:'^1[3456789]\\d{9}$',
  73. errorMessage: '手机号不合法'
  74. }
  75. ]
  76. },
  77. address: {
  78. rules: [{
  79. required: true,
  80. errorMessage: '地址不能为空'
  81. }]
  82. },
  83. type: {
  84. rules: [{
  85. required: true,
  86. errorMessage: '分类不能为空'
  87. }]
  88. },
  89. imgUrlList: {
  90. rules: [{
  91. required: true,
  92. errorMessage: '图片不能为空'
  93. }]
  94. },
  95. price: {
  96. rules: [{
  97. required: true,
  98. errorMessage: '年龄不能为空'
  99. }, {
  100. format: 'number',
  101. errorMessage: '年龄只能输入数字'
  102. }]
  103. }
  104. },
  105. imageList: [],
  106. customFormData:{}
  107. }
  108. },
  109. onLoad() {},
  110. onReady() {
  111. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  112. this.$refs.customForm.setRules(this.rules)
  113. },
  114. created() {
  115. this.getType();
  116. },
  117. methods: {
  118. getType(){
  119. listConfig().then(response => {
  120. this.TreeData = this.formatOptions(response.data.quotations);
  121. });
  122. },
  123. //转换为下拉选列表
  124. formatOptions(data) {
  125. return data.map(item => ({
  126. value: item.id,
  127. text: item.productName,
  128. children: item.children ? this.formatOptions(item.children) : null,
  129. }));
  130. },
  131. submit(ref) {
  132. this.$refs[ref].validate().then(res => {
  133. if (res) {
  134. this.customFormData.url = this.imageList.join(',')
  135. addQuotations(this.customFormData).then(response => {
  136. this.$modal.msgSuccess('提交成功');
  137. this.customFormData = {...this.baseFormData};
  138. this.imageList = [];
  139. uni.navigateTo({
  140. url: '../quotations/quotations'
  141. });
  142. }).catch(err => {
  143. console.log('err', err);
  144. })
  145. }
  146. })
  147. },
  148. updateImg(imgList) {
  149. this.imageList = imgList;
  150. }
  151. }
  152. }
  153. </script>
  154. <style lang="scss">
  155. .example {
  156. padding: 15px;
  157. background-color: #fff;
  158. }
  159. .uni-forms .uni-forms-item:last-child{
  160. padding-left: 4%;
  161. }
  162. .segmented-control {
  163. margin-bottom: 15px;
  164. }
  165. .button-group {
  166. margin-top: 15px;
  167. display: flex;
  168. justify-content: space-around;
  169. }
  170. .form-item {
  171. display: flex;
  172. align-items: center;
  173. }
  174. .button {
  175. display: flex;
  176. align-items: center;
  177. height: 35px;
  178. margin-left: 10px;
  179. }
  180. </style>