tool-form.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import request from '@/components/thrid/em-element-ui/src/tools/request';
  2. import {
  3. isTypeof,
  4. utilsTrim,
  5. formatDate,
  6. axiosReqParams,
  7. responseKeyToValue,
  8. mergeRequestParams,
  9. LOCAL_STORAGE_CURR
  10. } from '@/components/thrid/em-element-ui/src/tools/utils';
  11. //form表单提交数据解释
  12. export function submitQueryData(_this, paramType = true) {
  13. let res = {},
  14. rowObj = _this.rowData, // 当前操作数据
  15. btnType = rowObj._btn && rowObj._btn.type ? rowObj._btn.type : '', // 操作按钮
  16. oldRow = _this.rowData._oldData || {}, // 操作前数据
  17. currEvent = _this.queryURL.hasOwnProperty(btnType) ? _this.queryURL[btnType] || {} : {}, // 操作事件的对象
  18. isInFormData = currEvent.hasOwnProperty('indata') ? currEvent.indata : true, // 是否是提交所有表单数据,默认true
  19. isparams = _this.options.hasOwnProperty('isparams') ? _this.options.isparams : false; // disabled行输入框是否提交参数
  20. // 用户自带参数
  21. if (paramType && rowObj._params && isTypeof(rowObj._params) === 'object') {
  22. for (let [pk, pv] of Object.entries(rowObj._params)) {
  23. res[pk] = pv;
  24. }
  25. }
  26. _this.pageColumn.forEach(item => {
  27. let postValue = '',
  28. field = item.field,
  29. isCompares = true,
  30. inputType = _this.inputType ? _this.inputType : 'show',
  31. fieldKey = item[inputType] && item[inputType].field ? item[inputType].field : item.field,
  32. subFieldKey = item[inputType] && item[inputType].subField ? item[inputType].subField : '',
  33. noShow = item[inputType] && item[inputType].noShow ? item[inputType].noShow : 0,
  34. is_all_form_data = isInFormData || item.hasOwnProperty(inputType),
  35. is_field = !((btnType == 'add' && (noShow & 1) == 1) || (btnType == 'edit' && (noShow & 2) == 2)),
  36. is_disabled = isparams === false ? true : ((isparams & 1) == 1 ? true : (btnType == 'add' ? true : (item[inputType] && item[inputType].isDisabled ? false : true))),
  37. is_auto_push = isparams === false ? true : ((isparams & 2) == 2 ? true : (item.hasOwnProperty('isAutoPush') ? item.isAutoPush : true)),
  38. is_submit = isTypeof(item.ispush) == 'undefined' ? true : item.ispush;
  39. //is_submit代表是否提交改字段, is_field控制列表字段在不同操作事件中显示情况
  40. if (is_submit && is_field && is_auto_push && is_disabled && is_all_form_data) {
  41. let currValue = rowObj[field];
  42. if (isTypeof(currValue) == 'object') {
  43. postValue = (item[inputType] && item[inputType].dataType === 'object') ? currValue : JSON.stringify(currValue);
  44. } else if (isTypeof(currValue) == 'array') {
  45. // 多字段参数
  46. if (item[inputType] && item[inputType].mulField && isTypeof(item[inputType].mulField) === 'object') {
  47. // 多选下拉列表对象参数
  48. if (item[inputType].type == 'select' && item[inputType].multiple && currValue.length > 0) {
  49. let tmpDataArray = [];
  50. currValue.forEach(selectItem => {
  51. let tmpDataObject = {},
  52. tmpFieldArray = isTypeof(selectItem) == 'string' ? selectItem.split(item[inputType].sign ? item[inputType].sign : '#0') : selectItem;
  53. for (let [kk, vv] of Object.entries(item[inputType].mulField)) {
  54. let fieldType = isTypeof(tmpFieldArray[vv]),
  55. tmpFieldValue = tmpFieldArray[vv] ? (fieldType === 'object' ? JSON.stringify(tmpFieldArray[vv]) : (fieldType === 'array' ? JSON.stringify(tmpFieldArray[vv]) : (fieldType === 'undefined' ? '' : tmpFieldArray[vv]))) : '';
  56. tmpDataObject[kk] = tmpFieldValue;
  57. }
  58. tmpDataArray.push(tmpDataObject);
  59. })
  60. postValue = tmpDataArray;
  61. } else {
  62. // 多字段单个字符串传参
  63. for (let [kk, vv] of Object.entries(item[inputType].mulField)) {
  64. let fieldType = isTypeof(currValue[vv]),
  65. tmpFieldValue = currValue[vv] ? (fieldType === 'object' ? JSON.stringify(currValue[vv]) : (fieldType === 'array' ? JSON.stringify(currValue[vv]) : (fieldType === 'undefined' ? '' : currValue[vv]))) : '';
  66. formInputDataCache(item, inputType, kk, tmpFieldValue);
  67. formDataParams(res, kk, tmpFieldValue, item, inputType, isCompares, subFieldKey, rowObj);
  68. }
  69. return false;
  70. }
  71. } else if (item[inputType] && item[inputType].type == 'file') {
  72. if (item[inputType].iType == 'string') {
  73. let tmpFileParams = []
  74. currValue.forEach(fileParams => {
  75. tmpFileParams.push(fileParams[(item[inputType].paramField ? item[inputType].paramField : 'url')])
  76. })
  77. postValue = item[inputType].hasOwnProperty('sign') ? tmpFileParams.join(item[inputType].sign) : tmpFileParams.join(',')
  78. } else {
  79. postValue = currValue
  80. }
  81. } else if (item[inputType] && (item[inputType].type == 'checkbox-group' || item[inputType].type == 'input-range')) {
  82. if (item[inputType].iType == 'string') {
  83. postValue = item[inputType].hasOwnProperty('sign') ? currValue.join(item[inputType].sign) : currValue.join(',')
  84. } else {
  85. postValue = currValue
  86. }
  87. } else if (item[inputType] && (item[inputType].type == 'cascader' || item[inputType].type == 'select')) {
  88. if (item[inputType] && item[inputType].iType == 'string') {
  89. if (item[inputType].hasOwnProperty('sign')) {
  90. postValue = item[inputType].sign ? currValue.join(item[inputType].sign) : currValue.join('');
  91. } else {
  92. postValue = currValue.join();
  93. }
  94. } else {
  95. postValue = item[inputType].dataType === 'array' ? currValue : JSON.stringify(currValue);
  96. }
  97. } else {
  98. postValue = item[inputType].dataType === 'array' ? currValue : JSON.stringify(currValue);
  99. }
  100. } else if (isTypeof(currValue) == 'boolean') {
  101. postValue = currValue;
  102. } else if (typeof (currValue) == 'undefined') {
  103. postValue = '';
  104. } else {
  105. if (item[inputType] && item[inputType].mulField && isTypeof(item[inputType].mulField) === 'object') {
  106. for (let [kk, vv] of Object.entries(item[inputType].mulField)) {
  107. let tmpFieldArray = isTypeof(currValue) == 'string' ? currValue.split(item[inputType].sign ? item[inputType].sign : '#0') : currValue,
  108. tmpFieldValue = tmpFieldArray[vv] ? tmpFieldArray[vv] : '';
  109. formInputDataCache(item, inputType, kk, tmpFieldValue)
  110. formDataParams(res, kk, tmpFieldValue, item, inputType, isCompares, subFieldKey, rowObj);
  111. }
  112. return false;
  113. } else if (item[inputType] && item[inputType].valueField) {
  114. let mapValue = rowObj[item[inputType].valueField]
  115. postValue = mapValue
  116. if (item[inputType].props) {
  117. let tmpMapValue = item[inputType].props && item[inputType].props.hasOwnProperty('index') ? (mapValue && mapValue[item[inputType].props.index]) : mapValue;
  118. postValue = item[inputType].props.key && tmpMapValue && tmpMapValue.hasOwnProperty(item[inputType].props.key) ? tmpMapValue[item[inputType].props.key] : ''
  119. }
  120. } else {
  121. postValue = currValue;
  122. }
  123. }
  124. if (item[inputType] && item[inputType].isCompare && postValue == oldRow[field]) {
  125. isCompares = false
  126. }
  127. formInputDataCache(item, inputType, fieldKey, postValue);
  128. formDataParams(res, fieldKey, postValue, item, inputType, isCompares, subFieldKey, rowObj);
  129. // 提交字段对应多个字段
  130. if (item[inputType] && item[inputType].fieldList && isTypeof(item[inputType].fieldList) == 'array') {
  131. let tmpMergeValue = [];
  132. item[inputType].fieldList.forEach(currField => {
  133. res[currField] ? tmpMergeValue.push(res[currField]) : tmpMergeValue.push(rowObj[currField].toString())
  134. })
  135. res[fieldKey] = tmpMergeValue.join(item[inputType].sign ? item[inputType].sign : '')
  136. formInputDataCache(item, inputType, fieldKey, res[fieldKey]);
  137. }
  138. }
  139. });
  140. return res;
  141. }
  142. // 缓存编辑的字段
  143. export function formInputDataCache(item, inputType, field, value) {
  144. value = item[inputType] && (!item[inputType].hasOwnProperty('trim') || (item[inputType].hasOwnProperty('trim') && item[inputType].trim === true))
  145. ? utilsTrim(value)
  146. : value;
  147. if (item[inputType] && item[inputType].isCache) {
  148. let tmpObj = {},
  149. tmpLocalData = [],
  150. key = LOCAL_STORAGE_CURR + (item[inputType].cacheField ? item[inputType].cacheField : field);
  151. if (localStorage.getItem(key)) {
  152. tmpLocalData = JSON.parse(localStorage.getItem(key));
  153. tmpLocalData = isTypeof(tmpLocalData) == 'array' ? tmpLocalData : [];
  154. }
  155. tmpLocalData.unshift({ value });
  156. //数组去重
  157. tmpLocalData = tmpLocalData.reduce((cur, next) => {
  158. tmpObj[next.value] ? "" : tmpObj[next.value] = true && cur.push(next);
  159. return cur;
  160. }, []);
  161. localStorage.setItem(key, JSON.stringify(tmpLocalData));
  162. }
  163. }
  164. export function formDataParams(data, key, value, item, inputType, isCompares, subKey = '', objRow = {}) {
  165. if (item[inputType] && item[inputType].dataType) {
  166. let dataType = isTypeof(item[inputType].dataType) === 'string' ? item[inputType].dataType : (item[inputType].dataType.hasOwnProperty(key) ? item[inputType].dataType[key] : '')
  167. // 根据不同dataType转变value值
  168. switch (dataType) {
  169. case 'number':
  170. value = Number(value);
  171. break;
  172. case 'boolean':
  173. value = value ? true : false;
  174. break;
  175. case 'date':
  176. value = formatDate(value, 'yyyy-MM-dd');
  177. break;
  178. case 'datetime':
  179. value = formatDate(value, 'yyyy-MM-dd hh:mm:ss');
  180. break;
  181. case 'object':
  182. value = value === '' ? null : value;
  183. break;
  184. }
  185. }
  186. value = item[inputType] && (!item[inputType].hasOwnProperty('trim') || (item[inputType].hasOwnProperty('trim') && item[inputType].trim === true))
  187. ? utilsTrim(value)
  188. : value;
  189. // 封装参数格式
  190. if (item[inputType] && item[inputType].parent) {
  191. let parent = item[inputType].parent
  192. if (isTypeof(parent) === 'array') {
  193. const parentList = [...item[inputType].parent];
  194. parent = parentList.pop()
  195. parentList.forEach(dataItem => {
  196. if (!data.hasOwnProperty(dataItem)) {
  197. data[dataItem] = {};
  198. }
  199. data = data[dataItem];
  200. })
  201. }
  202. if (!data.hasOwnProperty(parent)) {
  203. data[parent] = {};
  204. }
  205. item[inputType].isNode && (data[key] = value)
  206. isCompares && (data[parent][key] = value);
  207. if (subKey) {
  208. data[parent][subKey] = objRow[subKey];
  209. }
  210. if (item[inputType].isNode) {
  211. data[key] = value;
  212. subKey && (data[subKey] = objRow[subKey]);
  213. }
  214. } else {
  215. isCompares && (data[key] = value);
  216. if (subKey) {
  217. data[subKey] = objRow[subKey];
  218. }
  219. }
  220. return data;
  221. }
  222. ///提交edit form表单
  223. export function submitEditFormInfo(_this, formName) {
  224. _this.$refs[formName].validate((valid) => {
  225. if (valid) {
  226. let queryData = submitQueryData(_this);
  227. request(axiosReqParams(_this.rowData, mergeRequestParams(queryData, _this.publicParams), _this.rowData._axios, _this.options)).then((response) => {
  228. let result = responseKeyToValue(response, _this, _this, _this.rowData._btn.type);
  229. if (result !== false) {
  230. _this.$emit('reload');
  231. _this.$emit('clear', 'edit');
  232. }
  233. }).catch((err) => {
  234. });
  235. }
  236. });
  237. }
  238. ///dialog弹出按钮事件处理
  239. export function otherEvent(_this, item, formName) {
  240. if (_this.queryURL.hasOwnProperty(item.type)) {
  241. _this.$refs[formName].validate((valid) => {
  242. if (valid) {
  243. let queryData = submitQueryData(_this, false),
  244. data = _this.rowData._params && _this.rowData._params.data ? (_this.rowData._params.data || {}) : {},
  245. queryTmpData = Object.assign({}, data, queryData);
  246. request(axiosReqParams(_this.rowData, mergeRequestParams(queryTmpData, _this.publicParams), _this.rowData._axios, _this.options)).then((response) => {
  247. let result = responseKeyToValue(response, _this, _this, _this.rowData._btn.type);
  248. if (result !== false) {
  249. _this.$emit('reload');
  250. _this.$emit('clear', 'edit');
  251. }
  252. }).catch((err) => {
  253. });
  254. }
  255. });
  256. } else {
  257. _this.$emit('onListEvent', item, _this.rowData);
  258. }
  259. }
  260. // dialog弹出按钮事件处理
  261. export function otherDialogEvent(_this, item) {
  262. if (_this.queryURL.hasOwnProperty(item.type)) {
  263. submitRemoveEvent(_this)
  264. } else {
  265. _this.$emit('onListEvent', item, _this.rowData);
  266. }
  267. }
  268. //提交确认按钮,做rules验证
  269. export function submitRemoveEvent(_this) {
  270. let rules = _this.rowData._rules;
  271. if (rules) {
  272. if (rules.isRules) {
  273. request(axiosReqParams(rules, mergeRequestParams(rules.params, _this.publicParams), rules._axios, _this.options)).then((response) => {
  274. if (response.data && response.data.total > 0) {
  275. _this.$message({ type: 'error', message: rules.message });
  276. } else {
  277. submitRemoveFormInfo(_this);
  278. }
  279. });
  280. } else {
  281. submitRemoveFormInfo(_this);
  282. }
  283. } else {
  284. submitRemoveFormInfo(_this);
  285. }
  286. }
  287. ///提交删除操作
  288. export function submitRemoveFormInfo(_this) {
  289. let item = Object.assign({}, _this.rowData),
  290. data = _this.rowData._params.data;
  291. item._url = _this.rowData._url ? _this.rowData._url : _this.rowData.url;
  292. item._method = _this.rowData._params.method;
  293. request(axiosReqParams(item, mergeRequestParams(data, _this.publicParams), _this.rowData._axios, _this.options)).then((response) => {
  294. let result = responseKeyToValue(response, _this, _this, _this.rowData._btntype);
  295. _this.$emit('reload');
  296. _this.subClearBtn();
  297. }).catch((err) => {
  298. });
  299. }