x-form.vue 7.3 KB

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