stores.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import _ from 'lodash'
  2. import {lookupScope, lookupFn} from "../lib/lib";
  3. import {serverInvokeUrlTransform, apiConvert} from "../lib/config";
  4. import {calcObjectFlat} from '../lib/systemLib'
  5. /**
  6. * 构建一个 grid 支持的 dataSource
  7. */
  8. export function gridInvokeBuild(scope, grid, config, dataSource, reloadParams = {}, isExcelExport = false, excelExportCallBack = null) {
  9. const me = grid
  10. const params = calcObjectFlat(scope.viewModel.data, dataSource.params)
  11. let storeOption = {}
  12. if (dataSource.method === 'invoke') {
  13. // 默认支持 gridInvoke
  14. storeOption = {
  15. remoteSort: config.remoteSort,
  16. remoteFilter: config.remoteFilter,
  17. autoLoad: true,
  18. proxy: {
  19. type: 'jsonAjax',
  20. $owner: me,
  21. url: serverInvokeUrlTransform(dataSource.url),
  22. extraParams: _.defaultsDeep({}, reloadParams, params),
  23. reader: {
  24. type: 'json',
  25. rootProperty: 'data',
  26. totalProperty: 'pagination.total',
  27. successProperty: 'success',
  28. messageProperty: 'msg',
  29. transform: function (data) {
  30. if (grid.dataTransform) {
  31. if (typeof grid.dataTransform === 'string') {
  32. grid.dataTransform = lookupFn(lookupScope(grid), grid.dataTransform)
  33. }
  34. return grid.dataTransform.call(scope, grid, data)
  35. }
  36. return data
  37. }
  38. }
  39. },
  40. listeners: {
  41. load: function (store, records, successful, operation) {
  42. const rep = operation.getResponse().responseJson
  43. me.exportExcelTotal = rep.pagination?.total || rep.data?.length || 0
  44. if (isExcelExport) {
  45. if (typeof excelExportCallBack === 'function') {
  46. excelExportCallBack(rep);
  47. } else if (excelExportCallBack) {
  48. console.error("导出回调方法错误!")
  49. }
  50. me.fireEvent('excelDataLoadComplete', me, successful, records);
  51. } else {
  52. me.fireEvent('dataLoadComplete', me, successful, records);
  53. }
  54. }
  55. }
  56. }
  57. } else if (apiConvert) {
  58. // 外部扩展的 apiConvert
  59. storeOption = apiConvert.gridInvokeBuild(scope, grid, config, dataSource, params, reloadParams)
  60. } else {
  61. throw new TypeError("不支持的 API 请求方式")
  62. }
  63. if (isExcelExport) {
  64. const excelStore = new Ext.data.Store(storeOption);
  65. excelStore.load()
  66. } else {
  67. me.setStore(new Ext.data.Store(storeOption))
  68. }
  69. }
  70. export default function () {
  71. Ext.define('Yvan.JsonAjaxProxy', {
  72. extend: 'Ext.data.proxy.Ajax',
  73. alias: 'proxy.jsonAjax',
  74. actionMethods: {
  75. create: "POST",
  76. read: "POST",
  77. update: "POST",
  78. destroy: "POST"
  79. },
  80. buildRequest: function (operation) {
  81. // 参考源码 ext-all-debug.js:71468 method:buildRequest
  82. const me = this
  83. const {$owner} = me // 在 grid.initComponent 中赋值 $owner
  84. const scope = lookupScope($owner)
  85. const gridParam = me.getParams(operation)
  86. const customParam = {}
  87. // 提取 srot 元素
  88. if (gridParam.sort) {
  89. const sort = JSON.parse(gridParam.sort)
  90. // 字符串 [{"property":"BRANCHID","direction":"ASC"}]
  91. // 转换为对象 [{colId: "BRANCHID", sort: "asc"}]
  92. customParam.sortModel = []
  93. _.forEach(sort, s => {
  94. customParam.sortModel.push({
  95. colId: s.property,
  96. sort: _.toLower(s.direction)
  97. })
  98. })
  99. delete gridParam.sort
  100. }
  101. // 提取筛选元素
  102. if (gridParam.filter) {
  103. const filter = JSON.parse(gridParam.filter)
  104. // "[{"operator":"like","value":"1","property":"id"}]"
  105. // 转换为 filterModel: {BRANCHID: {filterType: "text", type: "contains", filter: "12"}}
  106. customParam.filterModel = {}
  107. _.forEach(filter, s => {
  108. const newFilterItem = {}
  109. if (s.operator === 'like') {
  110. newFilterItem.filterType = 'text';
  111. newFilterItem.type = 'contains';
  112. newFilterItem.filter = s.value;
  113. } else {
  114. // 无法识别的筛选类型
  115. debugger
  116. }
  117. customParam.filterModel[s.property] = newFilterItem
  118. })
  119. delete gridParam.filter
  120. }
  121. // 被 grid.constructor 作为方法存在
  122. const extraParams = _.cloneDeep(me.getExtraParams())
  123. const params = _.defaultsDeep(gridParam, extraParams)
  124. // var request = this.superclass.buildRequest.apply(this, arguments);
  125. let request = new Ext.data.Request({
  126. params: {},
  127. action: operation.getAction(),
  128. records: operation.getRecords(),
  129. url: me.buildUrl(),
  130. jsonData: {
  131. args: [
  132. params
  133. ],
  134. ...customParam
  135. },
  136. proxy: me
  137. });
  138. operation.setRequest(request);
  139. return request;
  140. },
  141. afterRequest: function (req, res) {
  142. // Extend.afterExtRequest(req, res)
  143. }
  144. });
  145. Ext.define('Ext.ux.data.MyReader', {
  146. extend: 'Ext.data.reader.Json',
  147. xtype: 'yvgridreader',
  148. useSimpleAccessors: false,
  149. // 重写解析 data 的方法
  150. // extractData: function (data) {
  151. // // callParent 要换写成 this.superclass.XXX.call(this, ...)
  152. // // return me.callParent([data]);
  153. //
  154. // if (this.metaData && Ext.isArray(this.metaData.fields)) {
  155. // // data : [][] 换写成数组
  156. // data = _.map(data, row => {
  157. // const newRow = {}
  158. // for (var i = 0; i < this.metaData.fields.length; i++) {
  159. // newRow[this.metaData.fields[i]] = row[i]
  160. // }
  161. // return newRow
  162. // })
  163. // }
  164. //
  165. // const rr = this.superclass.extractData.call(this, data);
  166. // return rr;
  167. // }
  168. });
  169. }