cascader.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import Api from "../../model/api";
  2. import {getDataSet, getEventParam} from "../../utils/utils";
  3. import {studentTypes} from "../../model/enum";
  4. Component({
  5. properties: {
  6. perType: String,
  7. parentIds: String,
  8. },
  9. observers: {
  10. parentIds: async function (parentIds) {
  11. if (!parentIds) {
  12. this.setData({
  13. area: ''
  14. })
  15. }
  16. await this.getTabData({id: ""}, -1);
  17. },
  18. },
  19. data: {
  20. show: false,
  21. active: 0,
  22. loading: false,
  23. tabs: [],
  24. dict: {
  25. 0: "省级",
  26. 1: "市级",
  27. 2: "区县级",
  28. 3: "街道(镇)级",
  29. 4: "社区(村)级",
  30. },
  31. area: ''
  32. },
  33. methods: {
  34. async restoreData(index) {
  35. if (!this.data.parentIds) {
  36. return;
  37. }
  38. let areaIdArray = this.data.parentIds.split(",");
  39. if (areaIdArray && areaIdArray.length > index) {
  40. let id = areaIdArray[index];
  41. const cell = this.findCell(id, index);
  42. let event = {currentTarget: {dataset: {}}};
  43. event.currentTarget.dataset.cell = cell;
  44. event.currentTarget.dataset.index = index;
  45. await this.onClick(event, true);
  46. }
  47. this.setData({
  48. area: this.data.tabs.map(item => item.title).filter(item=>item).join("/")
  49. })
  50. },
  51. findCell(id, index, key = 'id') {
  52. if (this.data.tabs) {
  53. return this.data.tabs[index].data.find(item => item[key] == id)
  54. }
  55. },
  56. onChange(e) {
  57. },
  58. async handleClick(event) {
  59. const {cell, index} = getDataSet(event);
  60. this.data.tabs[index].title = cell.name;
  61. this.data.tabs[index].code = cell.code;
  62. this.setData({
  63. tabs: this.data.tabs,
  64. });
  65. //加载新一级别的数据 级联组件 正常加载这个即可
  66. // await this.getTabData(cell, index);
  67. //以下属于该app变种规则扩展
  68. if (this.data.perType == studentTypes.COMMUNITY_WORKER) {
  69. if (this.data.tabs[0].title == "吉林省") {
  70. if (index == 3) { // 村一级单独请求
  71. this.setData({
  72. loading: true
  73. })
  74. this.setData({
  75. tabs: this.data.tabs.slice(0, index + 1),
  76. }, () => {
  77. this.selectComponent("#tabs")?.resize()
  78. })
  79. let res = await Api.getUnitList({
  80. parentId: cell.id
  81. })
  82. this.setData({
  83. loading: false
  84. })
  85. res.data = res.data.map(item => {
  86. return {
  87. name: item.unitname,
  88. code: item.unitcode,
  89. id: item.unitcode,
  90. }
  91. })
  92. this.changeTab(res.data, index);
  93. return;
  94. }
  95. } else {
  96. if (index == 1) {
  97. return;
  98. }
  99. }
  100. }
  101. await this.getTabData(cell, index);
  102. },
  103. async onClick(event, isCode) {
  104. if (!isCode && this.data.parentIds) {
  105. //让 父id变化不走观察者
  106. this.data.parentIds = "";
  107. }
  108. await this.handleClick(event);
  109. },
  110. click() {
  111. let code = '';
  112. console.log("传出去的code1", code)
  113. if (this.data.perType == studentTypes.COMMUNITY_WORKER) {
  114. if (this.data.tabs[0].title == "吉林省") {
  115. if (this.data.tabs.length >= 5) {
  116. code = this.data.tabs[4].code;
  117. }
  118. } else { //非吉林省社区工作者 只需要选择省市
  119. if (this.data.tabs.length >= 2) {
  120. code = this.data.tabs[1].code;
  121. }
  122. }
  123. } else {
  124. code = this.data.tabs[this.data.tabs.length - 1].code || this.data.tabs[this.data.tabs.length - 2].code;
  125. }
  126. this.setData({
  127. show: false,
  128. area: this.data.tabs.map(item => item.title).filter(item=>item).join("/")
  129. })
  130. console.log("传出去的code", code)
  131. this.triggerEvent("change", {
  132. 'formData.code': code
  133. });
  134. },
  135. showArea() {
  136. this.setData({
  137. show: true
  138. })
  139. },
  140. close() {
  141. this.setData({show: false})
  142. },
  143. changeTab(data, index) {
  144. let oldTabs = this.data.tabs.slice(0, index + 1);
  145. let indexObj = {};
  146. if (data && data.length > 0) {
  147. oldTabs = [
  148. ...oldTabs,
  149. {data: data.reverse()}
  150. ];
  151. indexObj = {active: index + 1}
  152. this.setData({
  153. tabs: oldTabs,
  154. ...indexObj
  155. }, () => {
  156. this.selectComponent("#tabs")?.resize()
  157. this.restoreData(indexObj.active);
  158. })
  159. } else {
  160. this.setData({
  161. tabs: oldTabs,
  162. ...indexObj
  163. }, () => {
  164. this.selectComponent("#tabs")?.resize()
  165. })
  166. }
  167. },
  168. async getTabData(cell, index) {
  169. //抽取出来可扩展
  170. //获取多级数据的实现方法,这里是网络请求,根据业务也可以本地获取全部数据,分级在这个方法提供
  171. this.setData({
  172. loading: true
  173. })
  174. this.setData({
  175. tabs: this.data.tabs.slice(0, index + 1),
  176. }, () => {
  177. this.selectComponent("#tabs")?.resize()
  178. })
  179. const res = await Api.getRegionList({parentId: cell.id})
  180. this.setData({
  181. loading: false
  182. })
  183. this.changeTab(res.data, index);
  184. },
  185. },
  186. async attached() {
  187. await this.getTabData({id: ""}, -1);
  188. },
  189. });