form.js 6.8 KB

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