x-form.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <view>
  3. <u--form labelPosition="top" labelWidth="auto" :model="model" :rules="rules" ref="uForm">
  4. <view v-for="(item,index) in list" :key="index">
  5. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  6. v-if="item.type == 'input' && !item.visible">
  7. <view class="card_form_item">
  8. <u--input v-model="model[`${item.field}`]" :disabled="item.disabled ? true : false"
  9. :placeholder="item.placeholder"></u--input>
  10. </view>
  11. </u-form-item>
  12. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  13. v-else-if="item.type == 'radio' && !item.visible">
  14. <view class="card_form_item" style="margin-top: 10rpx;">
  15. <u-radio-group v-model="model[`${item.field}`]" placement="row">
  16. <u-radio shape="circle" :customStyle="{marginRight: '20px'}"
  17. v-for="(item1, index2) in item.options" :key="index2" :label="item1.label"
  18. :name="item1.value"></u-radio>
  19. </u-radio-group>
  20. </view>
  21. </u-form-item>
  22. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  23. v-else-if="item.type == 'cascader' && !item.visible">
  24. <view class="card_form_item">
  25. <u--input :placeholder="item.placeholder" suffixIcon="arrow-down"
  26. v-model="model[`${item.field}`]" @focus="change"></u--input>
  27. </view>
  28. </u-form-item>
  29. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  30. v-else-if="item.type == 'upload' && !item.visible">
  31. <view class="card_form_item">
  32. <u-upload :fileList="fileList1" name="1" multiple :maxCount="10"
  33. @afterRead="afterRead($event, item.field)"
  34. @delete="deletePic($event, item.field)"></u-upload>
  35. </view>
  36. </u-form-item>
  37. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  38. v-else-if="item.type == 'textarea' && !item.visible">
  39. <view class="card_form_item">
  40. <u--textarea v-model="model[`${item.field}`]" autoHeight
  41. :placeholder="item.placeholder"></u--textarea>
  42. </view>
  43. </u-form-item>
  44. <u-form-item :required="item.required ? true : false" :label="item.label" :prop="item.field"
  45. v-else-if="item.type == 'line' && !item.visible">
  46. <view class="card_form_item">
  47. <u-line></u-line>
  48. </view>
  49. </u-form-item>
  50. </view>
  51. </u--form>
  52. <u-picker :show="show" ref="uPicker" :columns="addressColumns" :defaultIndex="areaDetault" keyName="label"
  53. @confirm="confirm($event, 'area')" @change="changeHandler" @cancel="cancel"></u-picker>
  54. <view style="width: 100%;height: 170rpx;"></view>
  55. <view class="check_btn">
  56. <view class="btn_submit">
  57. <u-button type="primary" text="提交" @click="clientele"></u-button>
  58. </view>
  59. </view>
  60. </view>
  61. </template>
  62. <script>
  63. const ENV = require('@/.env.js')
  64. import {
  65. urbanArea
  66. } from '@/static/js/districtCode.js'
  67. export default {
  68. name: 'x-form',
  69. props: {
  70. list: {
  71. type: Array,
  72. default () {
  73. return []
  74. }
  75. },
  76. model: {
  77. type: Object,
  78. default () {
  79. return {}
  80. }
  81. },
  82. rules: {
  83. type: Object,
  84. default () {
  85. return {}
  86. }
  87. },
  88. userType: {
  89. type: Number,
  90. default () {
  91. return null
  92. }
  93. },
  94. },
  95. data() {
  96. return {
  97. show: false,
  98. areaData: urbanArea(), //原始数据
  99. addressColumns: [], //数据数组
  100. areaDetault: [], //默认选中数据
  101. fileList1: [],
  102. }
  103. },
  104. mounted() {
  105. // this.getAreaList()
  106. },
  107. methods: {
  108. change() {
  109. // console.log(this.columns, 23)
  110. this.show = true
  111. this.addressColumns = [
  112. this.areaData,
  113. this.areaData[0].children,
  114. ];
  115. this.$nextTick(() => {
  116. if (this.model.hr) {
  117. this.setDefault()
  118. }
  119. })
  120. },
  121. confirm(value, type) {
  122. let arr = value.value
  123. let arr1 = []
  124. let arr2 = []
  125. arr.forEach(item => {
  126. arr1.push(item.value)
  127. arr2.push(item.label)
  128. })
  129. this.model.hr = arr1.join('/')
  130. this.model.region = arr2.join('/')
  131. this.show = false
  132. this.$emit('confirm', value)
  133. },
  134. cancel() {
  135. this.show = false
  136. },
  137. // 补全客户资料
  138. clientele() {
  139. this.$refs.uForm.validate().then(res => {
  140. this.$emit('clientele')
  141. }).catch(errors => {
  142. // uni.$u.toast('校验失败')
  143. })
  144. },
  145. // 新增图片
  146. async afterRead(event, type) {
  147. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  148. let lists = [].concat(event.file)
  149. let fileListLen = this[`fileList${event.name}`].length
  150. lists.map((item) => {
  151. this[`fileList${event.name}`].push({
  152. ...item,
  153. status: 'uploading',
  154. message: '上传中'
  155. })
  156. })
  157. for (let i = 0; i < lists.length; i++) {
  158. const result = await this.uploadFilePromise(lists[i].url)
  159. let item = this[`fileList${event.name}`][fileListLen]
  160. this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
  161. status: 'success',
  162. message: '',
  163. url: this.$baseUrl + result
  164. }))
  165. fileListLen++
  166. }
  167. var arr = []
  168. this.fileList1.forEach(item1 => {
  169. let img = item1.url.split(this.$baseUrl)
  170. arr.push(img[1])
  171. })
  172. this.model.addressImg = arr.join()
  173. this.model[type] = this.model.addressImg
  174. this.$refs.uForm.validateField('addressImg')
  175. },
  176. // 删除图片
  177. deletePic(event, type) {
  178. this[`fileList${event.name}`].splice(event.index, 1)
  179. var arr = []
  180. this.fileList1.forEach(item1 => {
  181. arr.push(item1.url)
  182. })
  183. this.model.addressImg = arr.join()
  184. this.model[type] = this.model.addressImg
  185. },
  186. uploadFilePromise(url) {
  187. return new Promise((resolve, reject) => {
  188. let a = uni.uploadFile({
  189. url: ENV.APP_DEV_URL + '/api/upload', // 仅为示例,非真实的接口地址
  190. filePath: url,
  191. name: 'file',
  192. // formData: {
  193. // user: 'test'
  194. // },
  195. header: {
  196. 'Authorization': 'Bearer ' + uni.getStorageSync('access_token'),
  197. },
  198. success: (res) => {
  199. let state = JSON.parse(res.data)
  200. setTimeout(() => {
  201. if (state.code == 200) {
  202. resolve(state.data)
  203. }
  204. }, 1000)
  205. }
  206. });
  207. })
  208. },
  209. // 户籍所在地数据初始化
  210. getAreaList() {
  211. //省的数组
  212. const provinceData = this.areaData.map(e => {
  213. return e.label
  214. })
  215. //市的数组
  216. const cityData = this.areaData.map(e => {
  217. const arr = []
  218. arr.push(
  219. e.children.map(c => {
  220. return c.label
  221. })
  222. )
  223. return arr
  224. })
  225. const arr = [provinceData, cityData[0][0]] //组装
  226. this.addressColumns = arr
  227. },
  228. // 根据回显数据设置默认选中项
  229. setDefault() {
  230. // 户籍所在地
  231. let temp = []
  232. temp = this.model.hr.split('/')
  233. let index, index1 = 0
  234. let arr, arr1 = []
  235. //市区对应每列的第几个
  236. this.areaData.forEach((item, i) => {
  237. if (item.value == temp[0]) {
  238. index = i
  239. item.children.forEach((val, ind) => {
  240. if (val.value == temp[1]) {
  241. index1 = ind
  242. }
  243. })
  244. }
  245. })
  246. //匹配省市区对应每列的第几个
  247. arr = this.areaData.map(e => {
  248. return e
  249. })
  250. arr1 = this.areaData[index].children.map(e => {
  251. return e
  252. })
  253. this.addressColumns = [arr, arr1]
  254. this.areaDetault = [index, index1]
  255. },
  256. // 选中时执行
  257. changeHandler(e) {
  258. const {
  259. columnIndex,
  260. index,
  261. indexs,
  262. picker = this.$refs.uPicker
  263. } = e
  264. if (columnIndex === 0) {
  265. const children1 = this.areaData[index].children.map(e1 => {
  266. return e1
  267. })
  268. picker.setColumnValues(1, children1)
  269. if (columnIndex === 1) {
  270. const children3 = this.areaData[indexs[0]].children[indexs[1]].children.map(e2 => {
  271. return e2
  272. })
  273. picker.setColumnValues(2, children3)
  274. }
  275. }
  276. },
  277. }
  278. }
  279. </script>
  280. <style lang="scss">
  281. .card_form_item {
  282. width: 100%;
  283. }
  284. .check_btn {
  285. position: fixed;
  286. display: flex;
  287. align-items: center;
  288. justify-content: center;
  289. left: 0;
  290. right: 0;
  291. bottom: 0;
  292. z-index: 10;
  293. background-color: #fff;
  294. border-top: 1rpx solid #f7f7f7;
  295. padding-bottom: constant(safe-area-inset-bottom); //兼容 IOS<11.2
  296. padding-bottom: env(safe-area-inset-bottom); //兼容 IOS>11.2
  297. }
  298. .btn_submit {
  299. margin-top: 10rpx;
  300. padding-bottom: 20rpx;
  301. width: calc(100% - 80rpx);
  302. }
  303. </style>