form.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // components/form/form.js
  2. import api from '../../api/api'
  3. import util from '../../../utils/util'
  4. const app = getApp()
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. options: {
  10. multipleSlots: true // 复数插槽: 是
  11. },
  12. properties: {
  13. formLayoutData:{
  14. value:{},
  15. type:Object
  16. }
  17. },
  18. /**
  19. * 组件的初始数据
  20. */
  21. data: {
  22. pickerVisible:false,
  23. dateVisible:false,
  24. cascaderVisible:false,
  25. disabledStatus:false,
  26. pickerIndex:0,
  27. timePickerValue:'',
  28. actPicker:{},
  29. options:[]
  30. },
  31. /**
  32. * 组件的方法列表
  33. */
  34. lifetimes:{
  35. created(){
  36. },
  37. attached () {
  38. this.getarealist()
  39. getApp().globalData.formData = this
  40. }
  41. },
  42. methods: {
  43. onBlur (detail) {
  44. let data = detail.currentTarget.dataset.item
  45. let index = detail.currentTarget.dataset.index
  46. this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value
  47. if (data.role.formatter) {
  48. let regStatus = data.role.formatter(detail.detail.value)
  49. this.setErrorMsg(regStatus,index)
  50. }
  51. },
  52. onClear (detail) {
  53. let index = detail.currentTarget.dataset.index
  54. this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value
  55. this.setData({
  56. formLayoutData:this.data.formLayoutData
  57. })
  58. },
  59. onPicker (detail) {
  60. let data = detail.currentTarget.dataset.item
  61. this.setData({
  62. pickerIndex:detail.currentTarget.dataset.index,
  63. pickerVisible:true,
  64. actPicker:{
  65. label:data.label,
  66. data:data.data
  67. }
  68. })
  69. console.log(this.data.formLayoutData.formInfo)
  70. },
  71. onPickerConfirm (detail) {
  72. this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.value[0]
  73. this.data.actPicker.data.forEach(e=>{
  74. if (e.value === detail.detail.value[0]) {
  75. this.data.formLayoutData.formInfo.filter(v=>{
  76. if (v.id === e.hide) {
  77. v.force = false
  78. v.errorMsg = null,
  79. v.inputValue = 0
  80. }
  81. })
  82. }
  83. })
  84. this.setData({
  85. formLayoutData:this.data.formLayoutData
  86. })
  87. },
  88. showDatePicker (detail) {
  89. let data = detail.currentTarget.dataset.item
  90. this.setData({
  91. pickerIndex:detail.currentTarget.dataset.index,
  92. dateVisible:true,
  93. timePickerValue:data.inputValue
  94. })
  95. },
  96. onTimePickerConfirm (detail) {
  97. this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.value
  98. this.setData({
  99. formLayoutData:this.data.formLayoutData
  100. })
  101. },
  102. showCascader (detail) {
  103. let data = detail.currentTarget.dataset.item
  104. this.setData({
  105. pickerIndex:detail.currentTarget.dataset.index,
  106. cascaderVisible:true,
  107. })
  108. },
  109. // onChange (detail) {
  110. // let data = detail.currentTarget.dataset.item
  111. // let index = detail.currentTarget.dataset.index
  112. // this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value
  113. // if (data.role.formatter) {
  114. // let regStatus = data.role.formatter(detail.detail.value)
  115. // this.setErrorMsg(regStatus,index)
  116. // }
  117. // },
  118. onChange (detail) {
  119. this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.selectedOptions.map(e=>{
  120. return e.value
  121. }).join(',')
  122. this.setData({
  123. formLayoutData:this.data.formLayoutData
  124. })
  125. },
  126. validate (fn) {
  127. let arr = this.data.formLayoutData.formInfo.map((e,index)=>{
  128. // console.log(e)
  129. if (!e.force) return e
  130. console.log(e)
  131. if (e.label === '关联订单') {
  132. console.log(e.force && e.inputValue !== '')
  133. }
  134. if (e.force && e.inputValue !== '') {
  135. if (e.role.formatter) {
  136. let regStatus = e.role.formatter(e.inputValue)
  137. this.setErrorMsg(regStatus,index)
  138. } else {
  139. this.setErrorMsg('',index)
  140. }
  141. } else {
  142. console.log(e.label)
  143. this.setErrorMsg(e.label + '不能为空',index)
  144. }
  145. return e
  146. })
  147. let isValidate = !arr.some(item=>item.errorMsg && item.errorMsg !== '')
  148. fn(isValidate)
  149. },
  150. setErrorMsg (msg,index) {
  151. this.data.formLayoutData.formInfo[index].errorMsg = msg
  152. this.setData({
  153. formLayoutData:this.data.formLayoutData
  154. })
  155. },
  156. async getarealist () {
  157. const res = await api.arealist()
  158. this.setData({
  159. options:util.createMenu(res.data)
  160. })
  161. },
  162. setObjectData (data) {
  163. let that = this, obj = {}
  164. that.setData({
  165. disabledStatus:false
  166. })
  167. that.compData = that.selectComponent("#upload")
  168. that.compData.toSetFileData().then(e=>{
  169. that.setData({
  170. disabledStatus:false
  171. })
  172. data.forEach(e=>{
  173. if (e.type === 'upload') {
  174. e.inputValue = that.compData.data.attachmentids
  175. }
  176. obj[e.id] = e.inputValue
  177. })
  178. if (obj.provinceArr) {
  179. let arr = obj.provinceArr.split(",")
  180. obj.province = arr[0]
  181. obj.city = arr[1]
  182. obj.county = arr[2]
  183. }
  184. that.triggerEvent("click", obj);
  185. })
  186. },
  187. formSubmit () {
  188. var that = this
  189. console.log(this.data.formLayoutData.formInfo)
  190. that.validate((valid) => {
  191. console.log(valid)
  192. if (valid) {
  193. that.setObjectData(that.data.formLayoutData.formInfo)
  194. } else {
  195. return false;
  196. }
  197. });
  198. }
  199. }
  200. })