x-form.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <!-- 表单 -->
  3. <view style="margin: 0rpx 20rpx 0rpx 30rpx;">
  4. <u-form :model="model" :rules="rules" labelPosition="left" ref="uForm" labelWidth="70">
  5. <view v-for="(item,index) in list" :key="index">
  6. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  7. v-if="item.type == 'input' && customerName(item)">
  8. <view class="card_form_item">
  9. <u-input v-model="model[`${item.field}`]" :disabled="item.disabled ? true : false"
  10. :placeholder="item.placeholder"></u-input>
  11. </view>
  12. </u-form-item>
  13. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  14. v-else-if="item.type == 'select'">
  15. <view class="card_form_item">
  16. <u-input :placeholder="item.placeholder" suffixIcon="arrow-down"
  17. v-model="model[`${item.field}`]" @focus="change(item)"></u-input>
  18. </view>
  19. </u-form-item>
  20. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  21. v-else-if="item.type == 'selectil'">
  22. <view class="card_form_item_selectil frameil" @click="change(item)">
  23. <view class="title_input_selectil" :class="model[`${item.field}`] ? '' : 'hsColor'">
  24. {{model[`${item.field}`] || item.placeholder}}
  25. </view>
  26. <u-icon name="arrow-down" size="18px"></u-icon>
  27. </view>
  28. </u-form-item>
  29. <!-- 保温箱选择 -->
  30. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  31. v-else-if="item.type == 'selectkey' && getCooler()">
  32. <view class="card_form_item_selectil frameil" @click="incubatorChange(item)">
  33. <view class="title_input_selectil" :class="model[`${item.field}`] ? '' : 'hsColor'">
  34. {{model[`${item.field + 'name'}`] || item.placeholder}} {{model[`${item.field}`]}}
  35. </view>
  36. <u-icon name="arrow-down" size="18px"></u-icon>
  37. </view>
  38. </u-form-item>
  39. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  40. v-else-if="item.type == 'textarea'">
  41. <view class="card_form_item">
  42. <u-textarea v-model="model[`${item.field}`]" autoHeight
  43. :placeholder="item.placeholder"></u-textarea>
  44. </view>
  45. </u-form-item>
  46. </view>
  47. </u-form>
  48. <u-popup :show="incubatorShow" closeable @close="close" @open="open">
  49. <view class="card_incubator">
  50. <view class="head_place">选择保温箱</view>
  51. <view class="card_search">
  52. <u-search placeholder="请输入保温箱名称" v-model="keyword" :showAction="false"
  53. @change="searchChange"></u-search>
  54. </view>
  55. <view style="display: flex;flex-direction: column;" v-if="incubatorData.length > 0">
  56. <view class="card_title_item" :class="item.name == model.coolerBoxIdname ? 'color_blue' : ''"
  57. v-for="(item,index) in incubatorData" :key="index" @click="incubatorClick(item)">
  58. {{item.name}}
  59. </view>
  60. </view>
  61. <view style="margin-bottom: 20px;" v-else>
  62. <u-empty mode="list" text="暂无保温箱" marginTop="50"></u-empty>
  63. </view>
  64. </view>
  65. </u-popup>
  66. <u-picker :defaultIndex="findIndex(model[fieldType], columns[0])" :show="show" :columns="columns"
  67. keyName="label" @confirm="confirm($event, fieldType)" @cancel="cancel"></u-picker>
  68. </view>
  69. </template>
  70. <script>
  71. export default {
  72. name: 'x-form',
  73. props: {
  74. list: {
  75. type: Array,
  76. default () {
  77. return []
  78. }
  79. },
  80. model: {
  81. type: Object,
  82. default () {
  83. return {}
  84. }
  85. },
  86. rules: {
  87. type: Object,
  88. default () {
  89. return {}
  90. }
  91. },
  92. },
  93. data() {
  94. return {
  95. show: false,
  96. columns: [],
  97. fieldType: '',
  98. incubatorShow: false,
  99. incubatorData: [],
  100. keyword: '',
  101. incuType: '',
  102. }
  103. },
  104. // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
  105. onReady() {
  106. this.$refs.uForm.setRules(this.rules);
  107. },
  108. methods: {
  109. customerName(value) {
  110. var userInfo = this.$cache.getCache('userInfo')
  111. if (userInfo.userType == 'customer') {
  112. if (value.field != 'customerName') {
  113. if (value.type == 'input') {
  114. return true
  115. }
  116. }
  117. } else {
  118. if (value.type == 'input') {
  119. return true
  120. }
  121. }
  122. },
  123. change(e) {
  124. this.columns = []
  125. this.fieldType = e.field
  126. this.show = true
  127. this.$nextTick(() => {
  128. this.columns.push(e.options)
  129. })
  130. },
  131. // 保温箱选择
  132. incubatorChange(event) {
  133. this.incuType = event.field
  134. // console.log(event, 3254)
  135. this.incubatorShow = true
  136. this.$api.get('/api/cooler-box', {
  137. page: 1,
  138. pageSize: 999,
  139. isBind: true,
  140. name: this.keyword,
  141. }).then(res => {
  142. if (res.code == 200) {
  143. this.incubatorData = res.data.list
  144. }
  145. })
  146. },
  147. // 选择保温箱
  148. incubatorClick(event) {
  149. this.$refs.uForm.validateField(this.incuType);
  150. this.model[this.incuType] = event.id
  151. this.$emit('incubatorClick', event)
  152. this.incubatorShow = false
  153. },
  154. // 搜索
  155. searchChange() {
  156. this.incubatorData = []
  157. this.incubatorChange()
  158. },
  159. // 选择确定
  160. confirm(value, type) {
  161. this.$refs.uForm.validateField(type);
  162. this.show = false
  163. this.model[type] = value.value[0].label
  164. },
  165. getCooler() {
  166. if (this.model.deliveryCondition == "保温箱") {
  167. return true
  168. }
  169. },
  170. // 点击确定
  171. cancel() {
  172. this.show = false
  173. },
  174. findIndex(code, list) {
  175. if (!code || !list) {
  176. return [0]
  177. }
  178. return [list.findIndex((item) => (item.label === code || item.label === code))]
  179. },
  180. close() {
  181. this.incubatorShow = false
  182. },
  183. open() {
  184. },
  185. //子组件校验,传递到父组件
  186. async validateForm() {
  187. let flag = null
  188. await this.$refs.uForm.validate(valid => {}).then(res => {
  189. flag = true
  190. }).catch(err => {
  191. flag = false
  192. })
  193. return flag
  194. }
  195. },
  196. }
  197. </script>
  198. <style lang="scss">
  199. .card_form_item {
  200. width: 100%;
  201. }
  202. .card_form_item_selectil {
  203. width: 100%;
  204. display: flex;
  205. flex-direction: row;
  206. align-items: center;
  207. justify-content: space-between;
  208. padding: 6px 9px;
  209. height: 52rpx;
  210. }
  211. .title_input_selectil {
  212. font-size: 30rpx;
  213. // width: 100%;
  214. }
  215. .hsColor {
  216. color: rgb(192, 196, 204);
  217. }
  218. // 伪元素1rpx边框
  219. .frameil {
  220. position: relative; //重要
  221. }
  222. .frameil::after {
  223. position: absolute;
  224. content: '';
  225. border: 2rpx solid #dadbde;
  226. width: 200%;
  227. height: 200%;
  228. top: 0;
  229. left: 0;
  230. transform: scale(0.5);
  231. transform-origin: 0 0;
  232. pointer-events: none;
  233. border-radius: 20rpx;
  234. /* 使伪元素不会阻止鼠标事件 */
  235. }
  236. .card_incubator {
  237. min-height: 600rpx;
  238. max-height: 900rpx;
  239. }
  240. .head_place {
  241. display: flex;
  242. justify-content: center;
  243. align-items: center;
  244. padding: 20rpx;
  245. }
  246. .card_search {
  247. margin: 20rpx;
  248. }
  249. .card_title_item {
  250. position: relative;
  251. padding: 30rpx;
  252. // border-bottom: 1rpx solid #dadbde;
  253. }
  254. .color_blue {
  255. color: #2979ff;
  256. }
  257. .card_title_item:before {
  258. content: " ";
  259. position: absolute;
  260. left: 0;
  261. bottom: 0;
  262. width: 100%;
  263. height: 1px;
  264. border-top: 1px solid #e7e6e4;
  265. -webkit-transform-origin: 0 0;
  266. transform-origin: 0 0;
  267. -webkit-transform: scaleY(0.5);
  268. transform: scaleY(0.5);
  269. }
  270. </style>