addCooperative.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <uni-section title="提交前请仔细核对信息,提交后不可修改!" type="line">
  3. <view class="example">
  4. <!-- 自定义表单校验 -->
  5. <uni-forms ref="customForm" :rules="rules" :modelValue="customFormData" label-align="center">
  6. <uni-forms-item label="名 称" required name="name" label-width="125" label-align="center" >
  7. <uni-easyinput v-model="customFormData.name" placeholder="请输入供销社名称" />
  8. </uni-forms-item>
  9. <uni-forms-item label="地 址" required name="address" label-width="95" label-align="center" >
  10. <uni-easyinput v-model="customFormData.address" placeholder="请输入供销社地址" />
  11. </uni-forms-item>
  12. <uni-forms-item label="电 话" required name="chargePhone" label-width="95" label-align="center" >
  13. <uni-easyinput v-model="customFormData.chargePhone" placeholder="请输入负责人电话" />
  14. </uni-forms-item>
  15. <uni-forms-item label="负责人" required name="chargePeople" label-width="95" label-align="center" >
  16. <uni-easyinput v-model="customFormData.chargePeople" placeholder="请输入负责人姓名" />
  17. </uni-forms-item>
  18. <uni-forms-item label="图 片" required name="url" label-width="95">
  19. <upload :imgArr="imageList" :fileSize="1" :limit="3" @updateImg="updateImg"/>
  20. </uni-forms-item>
  21. </uni-forms>
  22. <button type="primary" @click="submit('customForm')">提交</button>
  23. </view>
  24. </uni-section>
  25. </template>
  26. <script>
  27. import { addCooperative } from "@/api/cooperative/cooperative";
  28. import upload from '../../components/upload/index.vue'
  29. export default {
  30. components: {
  31. upload
  32. },
  33. data() {
  34. return {
  35. // 基础表单数据
  36. baseFormData: {
  37. name: '',
  38. address:'',
  39. chargePeople:'',
  40. chargePhone:'',
  41. imgUrl:'',
  42. },
  43. // 校验规则
  44. rules: {
  45. name: {
  46. rules: [{
  47. required: true,
  48. errorMessage: '供销社名称不能为空'
  49. }]
  50. },
  51. address: {
  52. rules: [{
  53. required: true,
  54. errorMessage: '供销社地址不能为空'
  55. }]
  56. },
  57. chargePeople: {
  58. rules: [{
  59. required: true,
  60. errorMessage: '负责人姓名不能为空'
  61. }]
  62. },
  63. chargePhone: {
  64. rules: [
  65. {
  66. required: true,
  67. errorMessage: '负责人电话不能为空'
  68. },
  69. {
  70. pattern:'^1[3456789]\\d{9}$',
  71. errorMessage: '负责人电话不合法'
  72. }
  73. ]
  74. },
  75. imgUrlList: {
  76. rules: [{
  77. required: true,
  78. errorMessage: '图片不能为空'
  79. }]
  80. },
  81. },
  82. imageList: [],
  83. customFormData:{}
  84. }
  85. },
  86. onLoad() {},
  87. onReady() {
  88. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  89. this.$refs.customForm.setRules(this.rules)
  90. },
  91. created() {
  92. },
  93. methods: {
  94. //转换为下拉选列表
  95. formatOptions(data) {
  96. return data.map(item => ({
  97. value: item.id,
  98. text: item.productName,
  99. children: item.children ? this.formatOptions(item.children) : null,
  100. }));
  101. },
  102. submit(ref) {
  103. this.$refs[ref].validate().then(res => {
  104. if (res) {
  105. this.customFormData.imgUrl = this.imageList.join(',')
  106. addCooperative(this.customFormData).then(response => {
  107. this.$modal.msgSuccess('提交成功');
  108. this.customFormData = {...this.baseFormData};
  109. uni.navigateTo({
  110. url: '../cooperative/cooperative'
  111. });
  112. }).catch(err => {
  113. console.log('err', err);
  114. })
  115. }
  116. })
  117. },
  118. updateImg(imgList) {
  119. this.imageList = imgList;
  120. }
  121. }
  122. }
  123. </script>
  124. <style lang="scss">
  125. .example {
  126. padding: 15px;
  127. background-color: #fff;
  128. }
  129. .segmented-control {
  130. margin-bottom: 15px;
  131. }
  132. .button-group {
  133. margin-top: 15px;
  134. display: flex;
  135. justify-content: space-around;
  136. }
  137. .form-item {
  138. display: flex;
  139. align-items: center;
  140. }
  141. .button {
  142. display: flex;
  143. align-items: center;
  144. height: 35px;
  145. margin-left: 10px;
  146. }
  147. </style>